JDBCCategoryDataset - 执行多个查询

时间:2012-01-12 13:36:50

标签: java jfreechart

我想使用JDBCCategoryDataset类在JFrame中创建三个图表。

如何提供三个单独的查询?我在数据集对象中给出了三个不同的查询,但结果显示了三个相似的图表。另外,它执行所有三个的最后一个查询。我已经看到了使用DefaultPieDataset给定默认(静态)值的方式,但我想从数据库中动态检索数据。

我知道我可以创建几个JDBCCategoryDataset个对象吗?还有更好的方法吗?

package barchart;

import Extra.OpenFile;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.ui.RefineryUtilities;

public class Chart {
        public static void main(String[] args) throws IOException {

           try {
           JDBCCategoryDataset dataset = new JDBCCategoryDataset(
            "jdbc:mysql://localhost:3306/jobfinder",
            "com.mysql.jdbc.Driver","giannis", "giannis");

           JFrame frame = new JFrame("Charts");
           frame.setLayout(new GridLayout(2,1));

           String query = "";
           query = OpenFile.getContent("query"); //Gets the query from a file
                                                // cause it's big.It's a custom class.
           dataset.executeQuery(query);
           JFreeChart chart =
           ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
           dataset, PlotOrientation.VERTICAL, true, true, false);

           chart.setBackgroundPaint(Color.white);
           CategoryPlot plot = (CategoryPlot) chart.getPlot();
           plot.setBackgroundPaint(Color.lightGray);
           plot.setRangeGridlinePaint(Color.white);

           BarRenderer renderer = (BarRenderer) plot.getRenderer();
           renderer.setSeriesPaint(1, Color.CYAN);
           renderer.setSeriesPaint(0, Color.DARK_GRAY);
           renderer.setDrawBarOutline(false);
           renderer.setItemMargin(0.0);

           ChartPanel chartPanel = new ChartPanel(chart, false);
           chartPanel.setPreferredSize(new Dimension(700, 270));

           frame.add(chartPanel);


//               query = "SELECT occuDscr Jobs,COUNT(pstOccuId) Quantity FROM occupation_field " +
//                       "INNER JOIN job_post ON occuId = pstOccuId GROUP BY Jobs";
//               dataset.executeQuery(query);
//               JFreeChart chart1 =
//               ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
//               dataset, PlotOrientation.VERTICAL, true, true, false);
//
//               ChartPanel chartPanel1 = new ChartPanel(chart1, false);
//               chartPanel.setPreferredSize(new Dimension(500/2, 270/2));
//
//               frame.add(chartPanel1);
//
//               query = "SELECT occuDscr Jobs,COUNT(usrOccuId) Quantity FROM occupation_field " +
//                       "INNER JOIN users ON occuId = usrOccuId GROUP BY Jobs";
//               dataset.executeQuery(query);
//               JFreeChart chart2 =
//               ChartFactory.createBarChart3D("Job Statistics", "Posts/Replys", "Quantity",
//               dataset, PlotOrientation.VERTICAL, true, true, false);
//
//               ChartPanel chartPanel2 = new ChartPanel(chart2, false);
//               chartPanel.setPreferredSize(new Dimension(500/2, 270/2));
//
//              frame.add(chartPanel2);

           frame.setVisible(true);
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.pack();

           RefineryUtilities.centerFrameOnScreen(frame);

       } catch (FileNotFoundException fe) {
             System.out.println("Error Occurred " + fe.getMessage());
             System.exit(0);
       } catch (IOException ie) {
             System.out.println("Error Occurred " + ie.getMessage());
             System.exit(0);
       } catch (ClassNotFoundException ce) {
             System.out.println("Error Occurred " + ce.getMessage());
             System.exit(0);
      }  catch (SQLException se) {
             System.out.println("Error Occurred " + se.getMessage());
             System.exit(0);
      }
   }
}

1 个答案:

答案 0 :(得分:1)

从概念上讲,最简单的方法是为每个面板提供自己的查询特定JDBCCategoryDataset。在此相关example中,每个ThermometerDemo面板都有自己的DefaultValueDataset

或者,定义一个具有单个JDBCCategoryDataset的类,该类检索组成三个CategoryDataset实例所需的所有数据,并公开静态工厂方法,每个方法返回每个饼图所需的单个数据集。 / p>

附录:后一种方法假设有一些有用的启发式方法来区分完整结果集的子集。例如,这是连续子集的部分实现:

/** @see https://stackoverflow.com/questions/8835974 */
public class MyCategoryDataset implements CategoryDataset{

    private JDBCCategoryDataset set;
    private List subList;

    private MyCategoryDataset(JDBCCategoryDataset set, int first, int last) {
        this.set = set;
        subList = set.getRowKeys().subList(first, first);
    }

    public static CategoryDataset createSubset(
            JDBCCategoryDataset set, int first, int last) {
        return new MyCategoryDataset(set, first, first);
    }

    @Override
    public Comparable getRowKey(int row) {
        return (Comparable) subList.get(row);
    }

    @Override
    public int getRowIndex(Comparable key) {
        return subList.indexOf(key);
    }

    @Override
    public List getRowKeys() {
        return subList;
    }

    // TODO
    //@Override
    //public Comparable getColumnKey(int column) {}
    //
    //@Override
    //public int getColumnIndex(Comparable key) {}
    //
    //@Override
    //public List getColumnKeys() {}
    //
    //@Override
    //public Number getValue(Comparable rowKey, Comparable columnKey) {}
    //
    //@Override
    //public int getRowCount() {}
    //
    //@Override
    //public int getColumnCount() {}
    //
    //@Override
    //public Number getValue(int row, int column) {}
    //
    //@Override
    //public void addChangeListener(DatasetChangeListener listener) {}
    //
    //@Override
    //public void removeChangeListener(DatasetChangeListener listener) {}
    //
    //@Override
    //public DatasetGroup getGroup() {}
    //
    //@Override
    //public void setGroup(DatasetGroup group) {}
}