2016-05-07 11 views
0

図のグラフには、一部の製品の売上のsumが表示されています。DynamicReportsのチャートを作成して年間売上を比較する

私が今したいのは、別のグラフを作成することですが、異なる年に同じ売上と価値を比較することです。このリンクで見つけたように:http://www.dynamicreports.org/examples/groupchartreport2しかし、単一のグラフで、これまで私は達成することができませんでした。

ありがとうございました。私が使用している

inserir a descrição da imagem aqui

テストクラス:

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 @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 
/*!40103 SET @[email protected]@TIME_ZONE */; 
/*!40103 SET TIME_ZONE='+00:00' */; 
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 
/*!40111 SET @[email protected]@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 [email protected]_TIME_ZONE */; 

/*!40101 SET [email protected]_SQL_MODE */; 
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; 
/*!40014 SET [email protected]_UNIQUE_CHECKS */; 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
/*!40111 SET [email protected]_SQL_NOTES */; 

ポンポン:私は解決

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>mavenproject1</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <dependencies> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sourceforge.dynamicreports</groupId> 
      <artifactId>dynamicreports-core</artifactId> 
      <version>3.1.3</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-collections</groupId> 
      <artifactId>commons-collections</artifactId> 
      <version>3.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sf.squirrel-sql.thirdparty-non-maven</groupId> 
      <artifactId>napkinlaf</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>net.xp-forge.maven.plugins</groupId> 
      <artifactId>xp-maven-plugin</artifactId> 
      <version>3.2.5</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sf.jasperreports</groupId> 
      <artifactId>jasperreports-fonts</artifactId> 
      <version>4.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.poi</groupId> 
      <artifactId>poi</artifactId> 
      <version>3.7</version> 
     </dependency> 
     <dependency> 
      <groupId>org.pushingpixels</groupId> 
      <artifactId>trident</artifactId> 
      <version>1.3</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.eclipse.swt.gtk.linux</groupId> 
        <artifactId>x86</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>jfree</groupId> 
      <artifactId>jfreechart</artifactId> 
      <version>1.0.13</version> 
     </dependency> 

     <dependency> 
      <groupId>tomcat</groupId> 
      <artifactId>jasper-compiler</artifactId> 
      <version>5.5.23</version> 
     </dependency> 
     <dependency> 
      <groupId>xml-apis</groupId> 
      <artifactId>xml-apis</artifactId> 
      <version>1.0.b2</version> 
      <type>jar</type> 
     </dependency> 
     <dependency> 
      <groupId>net.sf.jasperreports</groupId> 
      <artifactId>jasperreports</artifactId> 
      <version>5.5.0</version> 
      <type>jar</type> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jdesktop</groupId> 
      <artifactId>beansbinding</artifactId> 
      <version>1.2.1</version> 
      <type>jar</type> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.1-api</artifactId> 
      <version>1.0.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.1.Final</version> 
      <type>jar</type> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.25</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-jpamodelgen</artifactId> 
      <version>4.3.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>com.github.jai-imageio</groupId> 
      <artifactId>jai-imageio-core</artifactId> 
      <version>1.2.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.swinglabs</groupId> 
      <artifactId>swingx</artifactId> 
      <version>0.9</version> 
     </dependency> 
    </dependencies> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <maven.compiler.source>1.7</maven.compiler.source> 
     <maven.compiler.target>1.7</maven.compiler.target> 
    </properties> 
</project> 

答えて

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の2番目の部分を欠いていました。しかし、私はこのチャート、コードをやったやり方について意見を述べたいと思います。

関連する問題