当我添加更多数据时,我在更新我的 jfreechart 时遇到问题 - 我试图每 5 秒更新一次折线图并让它在点之间画线。我面临的问题是添加键 - 您无法添加具有相同值的键,所以我不确定如何使用新数据更新图表?
请有人建议我可能会出错的地方
public static void main (String[] args){
String val= args[0];
LineChart chart = new LineChart("Name " + val);
while(true){
Main main = new Main();
XMLData data = getMyData()
chart.addToDataSet(data);
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
LineChart.incremementxAxisTimePlot(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
package com.chart.graph;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import com.graph.logfiledata.XMLData;
/**
* A simple demonstration application showing how to create a line chart using data from an
* {@link XYDataset}.
*
*/
public class LineChart extends ApplicationFrame {
private static int xAxisTimePlot = 5;
private XYSeriesCollection dataset = new XYSeriesCollection();
public LineChart(final String title) {
super(title);
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
public static void incremementxAxisTimePlot(boolean update){
if(update){
xAxisTimePlot+=5;
}
}
XYSeries series2 = new XYSeries("1");
XYSeries series3 = new XYSeries("2");
XYSeries series4 = new XYSeries("3");
XYSeries series5 = new XYSeries("4");
XYSeries series6 = new XYSeries("5");
XYSeries series7 = new XYSeries("6");
XYSeries series8 = new XYSeries("7");
XYSeries series9 = new XYSeries("8");
XYSeries series10 = new XYSeries("9");
public void addToDataSet(XMLData data){
series2.add(xAxisTimePlot, data.getOne());
series3.add(xAxisTimePlot, data.getTwo());
series4.add(xAxisTimePlot, data.getThree());
...
//dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
dataset.addSeries(series4);
dataset.addSeries(series5);
dataset.addSeries(series6);
dataset.addSeries(series7);
dataset.addSeries(series8);
dataset.addSeries(series9);
dataset.addSeries(series10);
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart("Line Chart","X","Y", dataset, PlotOrientation.VERTICAL,true,true, false);
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.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
}