在我的代码中我有停止和启动按钮我也有组合框,我想在JFree图表中设置数据当我按速度或操纵杆时我怎么能这样做所以图表从开始开始也是正确的我在哪里我在图表中设置数据是因为尚未测试除Math.random以外的实际数据?
public DTest(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset = new
DynamicTimeSeriesCollection(1, 200, new Second());
dataset.setTimeBase(new Second());
dataset.addSeries(new float[]{0.0f}, 0, "S1");
System.out.println("Series count = " + dataset.getSeriesCount());
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
final JPanel content = new JPanel(new FlowLayout());
final JButton btn = new JButton("Stop");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_timer.stop();
}
});
final JButton btn1 = new JButton("Run");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_timer.start();
}
});
JComboBox comb = new JComboBox();
comb.addItem("Select");
comb.addItem("Speed");
comb.addItem("Joy Stick");
content.add(chartPanel);//panel for chart
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(btn);
btnPanel.add(btn1);
btnPanel.add(comb);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(content, BorderLayout.NORTH);
pane.add(btnPanel, BorderLayout.CENTER);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//setContentPane(content);
comb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox jComb = (JComboBox) e.getSource();
if (jComb.getSelectedItem().equals("Speed")) {
System.out.println("Speed is Pressed");
try {
_timer = new javax.swing.Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here I want to set speed data for chart
double factor = 0.90 + 0.2 * Math.random();
lastValue = lastValue * (float) factor;
float float_array[] = new float[1];
float_array[0] = lastValue;
System.out.println(
"lastValue contain the following "
+ lastValue);
dataset.advanceTime();
dataset.appendData(float_array);
}
});
_timer.setRepeats(true);
_timer.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (jComb.getSelectedItem().equals("Joy Stick")) {
System.out.println("Joy Stick is Pressed");
try {
_timer = new javax.swing.Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// here I want to set joy stick data for chart
double factor = 0.90 + 0.2 * Math.random();
lastValue = lastValue * (float) factor;
float float_array[] = new float[1];
float_array[0] = lastValue;
System.out.println("lastValue is " + lastValue);
dataset.advanceTime();
dataset.appendData(float_array);
}
});
_timer.setRepeats(true);
_timer.start();
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
}
}
});
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
"Dynamic Graph", "Time", "Value", dataset, true, true,
false);
final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
//axis.setFixedAutoRange(60.0); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(-100.0, 100.0);
return result;
}