2

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Pritesh
  • 337
  • 1
  • 10
  • do an updateUI() on the chart? Maybe your X or Y ranges do not capture the data? – Oliver Watkins Aug 11 '14 at 07:24
  • I have tried updateUI() and repaint() when I get the values of x and y but no luck yet. – Pritesh Aug 11 '14 at 08:27
  • if you comment out the cutomizeChart code, what do you see? – Oliver Watkins Aug 11 '14 at 08:28
  • It only makes difference in look and feel no luck with displaying the points. – Pritesh Aug 11 '14 at 08:51
  • maybe leave that method out of your question then, because it has no affect on the answer – Oliver Watkins Aug 11 '14 at 09:05
  • so if you hard code it like : series1.add(7.0,8.0); it works, but if you get the 7.0 and 8.0 from your file it doesn't work? – Oliver Watkins Aug 11 '14 at 09:07
  • yeah exactly and I suspect that issue happens because here the UI appears with the values provided to it but when we provide it again it do not take from there and so I am not able to update the values once they are there. – Pritesh Aug 11 '14 at 09:10
  • I think maybe your test.x_value is a String value. Not a number. Could I be correct? – Oliver Watkins Aug 11 '14 at 09:11
  • I suspect the same but I have printed the values and that showed me the numbers like 50.0,55.0 and 45.0 etc so I feel that can't be the issue. – Pritesh Aug 11 '14 at 09:14
  • try series1.add(new Double(test.x_value).getValue(),new Double(test.y_value).getValue(); – Oliver Watkins Aug 11 '14 at 09:16
  • thanks for the update. but no luck with that we have to find out a way to place this somehow in loop but my main concern is as this one is created inside constructor how can I call it again and can update UI? – Pritesh Aug 11 '14 at 10:05
  • @OliverWatkins: `updateUI()` would be inappropriate in this context; the view, a `ChartPanel` containing the `chart`, listens for updates to its model, `dataset`. – trashgod Aug 11 '14 at 15:48

1 Answers1

4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045