在DynamicReports上创建图表以按年比较销售额

时间:2016-05-07 05:17:45

标签: java mysql jasper-reports dynamic-reports

在图片的图表中,我显示了某些产品销售的double*

我现在想要的是创建另一个图表,但要比较不同年份的相同销售额和价值。就像在这个链接中找到的那样:http://www.dynamicreports.org/examples/groupchartreport2,但在一个图表中,到目前为止,我无法完成。

感谢您的帮助。

inserir a descrição da imagem aqui

测试班我使用:

sum

脚本数据库:

import java.awt.*;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;
import net.sf.dynamicreports.report.builder.chart.BarChartBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.builder.style.Styles;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.PageOrientation;
import net.sf.dynamicreports.report.constant.PageType;
import net.sf.dynamicreports.report.exception.DRException;

class ButtonFrame extends JFrame {

    JButton bChange;
    Connection con = new SQLConnection().getConnection();

    ButtonFrame(String title) {
        super(title);
        setLayout(new FlowLayout());
        JasperReportBuilder report = DynamicReports.report();//a new report
        String query =  "select SUM(value_sale) as sum,COUNT(id_sale) as count,YEAR(date_sale) as year, YEAR(date_sale) as y,id,pro.name "
            + "from sales s "
            + "inner join product pro on pro.id=s.prduct_id "
            + "group by name";

        TextColumnBuilder<String> product = col.column("Product", "name", type.stringType());
        TextColumnBuilder<BigDecimal> sum = col.column("Sum", "sum", type.bigDecimalType()).setPattern("R$ #,###,###.00");
        TextColumnBuilder<Integer> count = col.column("Count", "count", type.integerType());
        TextColumnBuilder<String> year = col.column("Year", "year", type.stringType());

        BarChartBuilder chart1 = cht.barChart()
                .seriesColors((new Color(49, 79, 79)), (new Color(0, 255, 255)), (new Color(178, 255, 102)), (new Color(0, 250, 154)), (new Color(0, 100, 0)), (new Color(233, 150, 22)))
                //.setUseSeriesAsCategory(true)
                .setCategory(year)
                .setShowValues(Boolean.TRUE)
                .series(cht.serie(count).setSeries(product));

        try {
            report();
            report.setPageFormat(PageType.A4, PageOrientation.PORTRAIT);
            StyleBuilder bold = Styles.style().bold();
            StyleBuilder centeredBold = Styles.style(bold)
                    .setHorizontalAlignment(HorizontalAlignment.CENTER);
            StyleBuilder columnStyle = Styles.style(centeredBold);
              report.setColumnTitleStyle(columnStyle);
               report.setColumnStyle(Styles.style().setHorizontalAlignment(HorizontalAlignment.CENTER));
            report.columns(year,count, sum)
                    .summary(chart1)
                    .setDataSource(query, con)
                    .show();
        } catch (DRException ex) {
            Logger.getLogger(NovoJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public class TestingChart {

    public static void main(String[] args) {
        ButtonFrame frm = new ButtonFrame("Testing Chart");
       // frm.setSize(150, 75);
      //  frm.setVisible(true);
    }
}

波姆:

CREATE DATABASE  IF NOT EXISTS `products` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `products`;
-- MySQL dump 10.13  Distrib 5.5.49, for debian-linux-gnu (x86_64)
--
-- Host: 127.0.0.1    Database: products
-- ------------------------------------------------------
-- Server version   5.5.49-0+deb8u1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `product`
--

DROP TABLE IF EXISTS `product`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `product`
--

LOCK TABLES `product` WRITE;
/*!40000 ALTER TABLE `product` DISABLE KEYS */;
INSERT INTO `product` VALUES (1,'food'),(2,'iron'),(3,'computer'),(4,'bags');
/*!40000 ALTER TABLE `product` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `sales`
--

DROP TABLE IF EXISTS `sales`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `sales` (
  `id_sale` int(11) NOT NULL,
  `date_sale` date DEFAULT NULL,
  `prduct_id` int(11) NOT NULL,
  `value_sale` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_sale`),
  KEY `fk_sales_prducts_idx` (`prduct_id`),
  CONSTRAINT `fk_sales_prducts` FOREIGN KEY (`prduct_id`) REFERENCES `product` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `sales`
--

LOCK TABLES `sales` WRITE;
/*!40000 ALTER TABLE `sales` DISABLE KEYS */;
INSERT INTO `sales` VALUES (1,'2014-12-12',1,'125'),(3,'2003-07-09',1,'45'),(4,'2004-12-12',1,'23.55'),(5,'2014-10-10',2,'99.99'),(6,'2014-01-01',4,'56'),(7,'2003-10-10',3,'25'),(8,'2014-02-02',2,'36.55');
/*!40000 ALTER TABLE `sales` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

1 个答案:

答案 0 :(得分:0)

我解决了,问题出在我的query,这是正确的:

String query = "select SUM(value_sale) as sum,COUNT(id_sale) as count,YEAR(date_sale) as year, YEAR(date_sale) as y,id,pro.name "
                + "from sales s "
                + "inner join product pro on pro.id=s.prduct_id "
                + "group by year,name";

这部分更具体:group by year,name。我错过了group by的第二部分。然而,我想对我做这个图表的方式,代码有一些看法。