开发者

dynamic generation of jfree chart -pie chart

is their a way that we can populate pie chart dynamically.... that based on the values got from database...can any one guide me 开发者_如何学Go?


The value from db....may not be fixed.....so.....the parameters...will be varying........how to do?

I am getting data from db....using ResultSet

You can try something like this

    DefaultPieDataset pieDataSet = new DefaultPieDataset();

// select subject,value from datatable;
// rs will be your ResultSet

    while (rs.next()) {
        String sub = rs.getString(1);
        int val = rs.getInt(2);
        pieDataSet.setValue(sub, new Integer(val));
    }

If the value is not stored in integer format in the database then parse it using

    int val = Integer.parseInt(rs.getString(2));


To dynamically update your dataset for any JFreeChart object (sorry if I don't use a Pie chart for an example, but you can figure it out):

Make sure your data from DB is populated into a DataSet object where it is part of a class field.

Create a class helper method that returns a JFreeChart which contains:

  1. a ChartFactory method which creates a JFreeChart object
  2. a plot object
  3. any objects that allow chart and plot rendering, etc.

Pass this DataSet object to your ChartFactory method that relates to the type of chart you want

Call your class helper method from an event or something that allows the chart to be updated where it passes JFreeChart object to a ChartPanel object...

chartPanel.setChart(createChart(axisX, axisY));

If you want the chart to be updated in intervals, use a Timer-based object to update the code.

For an example of such helper method can be found in my other post:

Jfree chart change Y axis data


There are several jFreeChart samples on their site here:

http://www.jfree.org/jfreechart/samples.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜