I\'m making a GUI that display result of background calculations. But before that, I wanted to test changing the dataset. Here is my code:
DefaultXYDataset
Your snippet is incorrectly synchronized; you should update your dataset
from the process()
method of a SwingWorker
, as shown here. Instead of a DateAxis
, use a NumberAxis
, as shown in ChartFactory.createXYLineChart().
Addendum: This variation on the example cited plots the worker's progress on a line chart. Note that createXYLineChart()
uses NumberAxis
for both domain and range.
private XYSeriesCollection collection = new XYSeriesCollection();
private XYSeries series = new XYSeries("Result");
...
collection.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"Newton's Method", "X", "Y", collection,
PlotOrientation.VERTICAL, false, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setRange(1.4, 1.51);
plot.getDomainAxis().setStandardTickUnits(
NumberAxis.createIntegerTickUnits());
XYLineAndShapeRenderer renderer =
(XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
this.add(new ChartPanel(chart), BorderLayout.CENTER);
...
private int n;
@Override
protected void process(List<Double> chunks) {
for (double d : chunks) {
label.setText(df.format(d));
series.add(++n, d);
}
}