Reputation: 1503
in my diploma i have some time expensive calculation in SwingWorker
class. When SwingWorker
call done
method. All result should put on chart. About result - I do not know how many values in legend will be. I did it all but chart not refreshed. Not show series at all. I try through dataset.addSeries(someSeries)
and chart.getXYPlot().setDataset(someDataset)
. In examples project like DynamicDataDemo1.java and etc. they just add new point to series and not refresh/repaint graphics. Why graphics not refreshed? I have information what add
method of XYSeries
send SeriesChangeEvent
to all registered listeners and automatically refresh chart. But i don't know how many XYSeries
will be. Can not understand how i can refresh chart through setDataset
method of XYPlot.
public class OutputChart extends JPanel {
private JFreeChart chart;
private XYSeriesCollection dataset;
private ChartPanel chartPanel;
public OutputChart() {
dataset = createDataset();
chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 400));
add(chartPanel);
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"Результаты вычислений алгоритма",// chart title
"N", // x axis label
"V", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setShapesFilled(true);
// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
/**
* Create data set for chart
* @return empty data set
*/
private XYSeriesCollection createDataset(){
return new XYSeriesCollection();
}
public void setDataset(XYSeries series){ //try through addSeries and setDataset of XYPlot.
dataset.addSeries(series);
}
public void setDataset(XYDataset dataset){ //try through addSeries and setDataset of XYPlot.
chart.getXYPlot().setDataset(dataset);
}
}
Upvotes: 1
Views: 6054
Reputation: 205785
This example shows adding (or removing) a series to (or from) a DefaultXYDataset
; no special effort is required.
An instance of XYSeriesCollection
, a related XYDataset
, should work similarly, as long as the application correctly uses the event dispatch thread. As suggested in this example and the SwingWorker
API, either process()
or done()
should meet that obligation.
By default, XYPlot
registers itself as a DatasetChangeListener
. Verify that this has not been disabled.
An sscce may shed some light on the problem.
Upvotes: 1