使用JFreeChart时出现奇怪的日历错误

时间:2015-10-24 09:28:20

标签: java plot jfreechart timeserieschart

我通过使用TimeSeries代替DynamicTimeSeries“解决”了这个问题,因为我意识到我不需要后者,因为我的唯一目的是手动更新图表。另请参阅here以获取进一步参考。

我正在尝试从列表中创建线条图,同时每当我向图表添加值时也能够自动更新图表。我使用 DynamicTimeSeriesCollection 。 列表的元素是特定秒的当前价格。因此,列表的长度是我想要绘制的时间间隔(以秒为单位)的长度。创建图表works fine

我正在使用

创建图表
Chart bitcoinChart = new Chart("bitcoin", prices);

我的图表课程在这里:

import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.awt.*;


public class Chart extends ApplicationFrame {

    private String title;
    private DynamicTimeSeriesCollection dataset;

    public Chart(final String title, DoubleArrayList doubleArrayList) {
        super(title);
        this.title = title;

        Second second = new Second(30, 41, 12, 12, 8, 2015);
        dataset = new DynamicTimeSeriesCollection(1, doubleArrayList.size(), second);
        dataset.setTimeBase(second);
        dataset.addSeries(doubleArrayListToFloatArray(doubleArrayList), 0, title);

        JFreeChart chart = createChart(dataset);
        this.add(new ChartPanel(chart), BorderLayout.CENTER);
        this.pack();
        RefineryUtilities.centerFrameOnScreen(this);
        this.setVisible(true);
    }



    public void update(Double data) {
            float[] newData = new float[1];
            newData[0] = data.floatValue();
            dataset.advanceTime();
            dataset.appendData(newData);

    }


    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
                title, "time", "price", dataset, true, true, false);

        final XYPlot plot = result.getXYPlot();
        ValueAxis domain = plot.getDomainAxis();
        domain.setAutoRange(true);
        ValueAxis range = plot.getRangeAxis();
        range.setAutoRange(true);
        return result;
    }


    public static float[] doubleArrayListToFloatArray(DoubleArrayList doubleArrayList) {
        float[] floatArray = new float[doubleArrayList.size()];
        for(int j=0; j<doubleArrayList.size(); j++) floatArray[j] = doubleArrayList.get(j).floatValue();
        return floatArray;
    }
}

每当我尝试使用

添加新值时,似乎都会发生奇怪的错误
Double test = 260d;
bitcoinchart.update(test);

。错误是:

java.lang.ArrayIndexOutOfBoundsException: -530
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2397)
at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2818)
at java.util.Calendar.updateTime(Calendar.java:3353)
at java.util.Calendar.getTimeInMillis(Calendar.java:1782)
at org.jfree.data.time.Second.getFirstMillisecond(Second.java:317)
at org.jfree.data.time.DynamicTimeSeriesCollection.getX(DynamicTimeSeriesCollection.java:906)
at org.jfree.data.time.DynamicTimeSeriesCollection.getX(DynamicTimeSeriesCollection.java:731)
at org.jfree.data.xy.AbstractXYDataset.getXValue(AbstractXYDataset.java:77)
at org.jfree.data.general.DatasetUtilities.iterateToFindDomainBounds(DatasetUtilities.java:1426)
at org.jfree.data.general.DatasetUtilities.findDomainBounds(DatasetUtilities.java:694)
at org.jfree.chart.renderer.xy.AbstractXYItemRenderer.findDomainBounds(AbstractXYItemRenderer.java:720)
at org.jfree.chart.renderer.xy.AbstractXYItemRenderer.findDomainBounds(AbstractXYItemRenderer.java:692)
at org.jfree.chart.plot.XYPlot.getDataRange(XYPlot.java:4462)
at org.jfree.chart.axis.DateAxis.autoAdjustRange(DateAxis.java:1293)
at org.jfree.chart.axis.DateAxis.configure(DateAxis.java:755)
at org.jfree.chart.plot.XYPlot.configureDomainAxes(XYPlot.java:974)
at org.jfree.chart.plot.XYPlot.datasetChanged(XYPlot.java:4539)
at org.jfree.data.general.AbstractDataset.notifyListeners(AbstractDataset.java:232)
at org.jfree.data.general.AbstractDataset.fireDatasetChanged(AbstractDataset.java:215)
at org.jfree.data.general.AbstractSeriesDataset.seriesChanged(AbstractSeriesDataset.java:114)
at org.jfree.data.time.DynamicTimeSeriesCollection.fireSeriesChanged(DynamicTimeSeriesCollection.java:844)
at org.jfree.data.time.DynamicTimeSeriesCollection.advanceTime(DynamicTimeSeriesCollection.java:595)
at com.wordnik.client.Chart.update(Chart.java:43)
at com.wordnik.client.AlgoTest.main(AlgoTest.java:60)
at com.wordnik.client.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

此错误是由 dataset.advanceTime() 行引起的。显然,每当我试图再迈一步时,日历就会抱怨,但我无法弄明白为什么。

0 个答案:

没有答案