Pritesh
Pritesh

Reputation: 337

Want to update jfreechat xylinechart continuously

I have xyline chart created using jfreechart inside netbeans swing.

I have two files one is for getting values for x and y coordinates continuously and other is for display of them now I am creating the chart from constructor of the display file and it displays the chart using hard coded values but when I pass my incoming the chart is not updating accordingly.

Please find below code for display file :

public Display_Unit_Mod() {

JPanel jPanel1 = createChartPanel();

}

public JPanel createChartPanel() {
    String chartTitle = "";
    String xAxisLabel = "x_value";
    String yAxisLabel = "y_value";
    XYDataset dataset = createDataset();

    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, 
            xAxisLabel, yAxisLabel, dataset);


    customizeChart(chart);


    return new ChartPanel(chart);
}

public XYDataset createDataset() {
    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series1 = new XYSeries("");


    System.out.println(String.valueOf(test.x_value));
      System.out.println(String.valueOf(test.y_value));

            series1.add(test.x_value,test.y_value); ->test is the object of other file and using it I am taking values from there
            //series1.add(7.0,8.0);
            //series1.add(3.0,4.0); -> displays values and prints chart
            //series1.add(1.0,2.0);


    dataset.addSeries(series1);

    return dataset;
}

private void customizeChart(JFreeChart chart) {
    XYPlot plot = chart.getXYPlot();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

    // sets paint color for each series
    renderer.setSeriesPaint(0, Color.RED);

    // sets thickness for series (using strokes)
    renderer.setSeriesStroke(0, new BasicStroke(4.0f));
    renderer.setSeriesStroke(1, new BasicStroke(3.0f));
    renderer.setSeriesStroke(2, new BasicStroke(2.0f));

    // sets paint color for plot outlines
    plot.setOutlinePaint(Color.BLUE);
    plot.setOutlineStroke(new BasicStroke(2.0f));

    // sets renderer for lines
    plot.setRenderer(renderer);

    // sets plot background
    plot.setBackgroundPaint(Color.DARK_GRAY);

    // sets paint color for the grid lines
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.BLACK);

    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.BLACK);

    }

I can see the values coming by system.out.println but not getting reflected on UI.

Please help me updating the same.

Upvotes: 2

Views: 434

Answers (1)

trashgod
trashgod

Reputation: 205785

Retrieve your data values in the background using a SwingWorker, as shown here. Update the chart's dataset in your implementation of process(), as shown here. This listening chart will update itself in response. A related example using SwingTimer is seen here.

Upvotes: 4

Related Questions