如何确定JFreeChart XY图的域范围? [英] How to fixate the domain range of a JFreeChart XY diagram?

查看:93
本文介绍了如何确定JFreeChart XY图的域范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFreeChart图表,它显示来自传感器的测量值.该图应显示这些值如何随时间变化.

I have a JFreeChart chart, which displays measurements from a sensor. The diagram should show how those values change over time.

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

I create the diagram using following code:

    // 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());

此实现存在一个问题-域轴不断变化,因此,信号的表示也发生变化.因此,很难从视觉上分辨出信号是否发生了显着变化.

There is a problem with this implementation - the domain axis constantly changes, hence, the representation of the signals changes as well. Therefore, it is hard to tell visually whether the signal has changed significantly.

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

At the start of a measurement session the domain ranges from 0 to approx. 550.

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

After some time I get more data and now the maximum value is 35000.

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

If we wait longer, it changes again (actually, with every received measurement a litlle).

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

How can I change the code such that the diagram shows last X measurements (i. e. the size of the domain axis remains constants) and kind of slides over the time?

更新1(2013年1月6日18:06 MSK):我更改了将新数据项添加到的代码

Update 1 (01.06.2013 18:06 MSK): I changed the code for adding a new data item to

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

其中voltage是新的传感器值.

设置图表的代码也已更改:

The code for setting up the chart has also changed:

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;
}

除了我有时会收到以下异常之外,这工作正常:

This works fine except that I sometimes get following exception:

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)

有什么想法要解决吗?

推荐答案

如何执行在事件分配线程中添加数据?

How can I execute adding the data in the event dispatch thread?

此处所示,SwingWorker非常适合此操作.在工作程序的doInBackground()方法中轮询或侦听您的数据源,并在process()中更新图表的模型,该模型在事件分发线程上执行.

As shown here, SwingWorker is ideal for this. Poll or listen to your data source in the worker's doInBackground() method and update the chart's model in process(), which executes on the event dispatch thread.

这篇关于如何确定JFreeChart XY图的域范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆