Random errors when changing series using JFreeChart

后端 未结 1 705
执念已碎
执念已碎 2020-11-22 02:15

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          


        
相关标签:
1条回答
  • 2020-11-22 02:34

    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.

    enter image description here

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题