简体   繁体   中英

Using a Hibernate datasource with JFreeCharts

I have a Java Web Application which allows a user to select a bunch of fields (with filters, aggregations etc.) which will then be used to create charts/graphs.

The database behind the application is a standard PostGreSQL 9.04 database but we are using the Hibernate ORM to access data.

The code I have right now creates an HQL Query (Hibernate Query Language) based on the users selections from the web interface. What I really need is some way to use that query to create a dataset that JFreeCharts can use to build the chart.

Are there any examples of JFreeChart and Hibernate integration? Having researched it myself I can't really find much other than this post on the Hibernate forum which says it can be done but giving no detail on how:

https://forum.hibernate.org/viewtopic.php?f=6&t=997556

To summarise: I have an HQL Query, I want to use that to create a data source which JFreeCharts can use to create Pie/Bar/Stacked Bar/Line charts.

Any help would be much appreciated.

不存在现有的实现,你可能会在看到现有的数据模型变化的一个启发org.jfree.data.jdbc

I tried something. Here's the code for my entity class:

package com.gxet4n.jfreechart;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="mobile_tbl")
public class Mobile_tbl {

   @Id
   private int id;

   @Column(name="mobile_brand")
   private String mobile_brand;

   @Column(name="unit_sale")
   private int unit_sale;

   // getters and setters

}

Then here's the code for my main class :

package com.gxet4n.jfreechart;

import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class PieChart_HQL {

    public static void main( String[ ] args )throws Exception {

    DefaultPieDataset dataset = new DefaultPieDataset( );
    DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
    try {

        SessionFactory sessionFactory = new Configuration().configure().addAnnotatedClass(Mobile_tbl.class).buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Query<?> query = session.createQuery("FROM Mobile_tbl");
        List<Mobile_tbl> mobiles = (List<Mobile_tbl>)query.list();

        session.close();


        for (Mobile_tbl m : mobiles) {
            dataset.setValue(m.getMobile_brand(), m.getUnit_sale());
        }

        for (Mobile_tbl m : mobiles) {
            dataset2.setValue(m.getUnit_sale(),m.getMobile_brand(),"");
        }

    } catch (Exception e) {
         System.out.println(e.getMessage()); 
    }

      JFreeChart chart = ChartFactory.createPieChart(
                 "Mobile Sales",   // chart title           
                 dataset,          // data           
                 true,             // include legend          
                 true,           
                 false );
      JFreeChart barChart = ChartFactory.createBarChart(
                 "Mobiles Sales",           
                 "Mobile Brand",            
                 "Unit Sale",            
                 dataset2,          
                 PlotOrientation.VERTICAL,           
                 true, true, false);

              int width = 560;    /* Width of the image */
              int height = 370;   /* Height of the image */ 
              File pieChart = new File( "Pie_Chart_HQL.png" );
              ChartUtils.saveChartAsPNG(pieChart, chart, width, height);
              File barchart = new File( "Bar_Chart_HQL.png" );
              ChartUtils.saveChartAsPNG(barchart, barChart, width, height);
    }
}

Finally there's the results : HQL查询中的条形图 HQL查询中的饼图

I used hibernate core 5.2.12.Final and jfreechart 1.5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM