我有一个 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。
一段时间后,我获得了更多数据,现在最大值为 35000。
如果我们等待更长的时间,它会再次发生变化(实际上,每次收到的测量值都是一点点)。
如何更改代码以使图表显示最后的 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)
任何想法如何解决它?