如何固定JFreeChart XY图的域范围?

时间:2013-06-01 13:16:24

标签: java jfreechart

我有一张JFreeChart图表,显示传感器的测量结果。该图应显示这些值如何随时间变化。

我使用以下代码创建图表:

    // create the chart...
    final JFreeChart chart = ChartFactory.createXYLineChart(
        "Pulse sensor data",      // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.white);


    // get a reference to the plot for further customisation...
    final XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

    renderer.setDrawSeriesLineAsPath(true);

    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, false);
    plot.setRenderer(renderer);

    // change the auto tick unit selection to integer units only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

此实现存在问题 - 域轴不断变化,因此信号的表示也会发生变化。因此,很难直观地判断信号是否发生了显着变化。

在测量会话开始时,域的范围从0到大约。 550。

enter image description here

一段时间后,我获得了更多数据,现在最大值为35000。

enter image description here

如果我们等待更长时间,它会再次发生变化(实际上,每次接收到的测量值都会变小)。

enter image description here

如何更改代码,使图表显示最后X次测量(即域轴的大小保持不变)以及幻灯片的种类?

更新1(01.06.2013 18:06 MSK):我更改了添加新数据项的代码

sensorSeries.add(new Millisecond(new Date()), voltage);

其中voltage是新的传感器值。

设置图表的代码也发生了变化:

dataset = new TimeSeriesCollection();

sensorSeries = new TimeSeries("Pulse sensor data");
sensorSeries.setMaximumItemAge(12500);

dataset.addSeries(sensorSeries);

final JFreeChart chart = createChart(dataset);

[...]

private JFreeChart createChart(final XYDataset dataset) {        
    // create the chart...              
    final JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Pulse sensor data",      // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );

    final XYPlot plot = chart.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);        

    final ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setAutoRange(true);

    return chart;
}

除非我有时会遇到以下异常,否则这样可以正常工作:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1201, Size: 1201
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at org.jfree.data.time.TimeSeries.getRawDataItem(TimeSeries.java:422)
    at org.jfree.data.time.TimeSeries.getTimePeriod(TimeSeries.java:454)
    at org.jfree.data.time.TimeSeriesCollection.getXValue(TimeSeriesCollection.java:428)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:987)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:913)
    at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3828)
    at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3389)
    at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1237)
    at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1672)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)

任何想法如何解决?

1 个答案:

答案 0 :(得分:1)

  

如何在事件派发线程中执行添加数据?

如图所示hereSwingWorker是理想的选择。在工作人员的doInBackground()方法中轮询或收听您的数据源,并在process()中更新图表的模型,该模型在事件派发线程上执行。

image