I have a question to the little runnable application at the bottom,
So if a button is pressed, than a RegularTimePeriode and a Value is added to the serie.
If I press the button for instance 1 million times than than 1 million periods and values are added but only a trickle of the data is shown.
Is there any possibility to remove data when it isn't shown, so in the way: one data is added at the right hand of the serie and one data is removed at the left hand.
Thanks a lot,
Poller
Code: Select all
package javaapplication28;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
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.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
public class DynamicDataDemo2 extends ApplicationFrame {
static class DemoPanel extends JPanel implements ActionListener
{
private TimeSeries series1;
private TimeSeries series2;
private double lastValue1 = 100.0;
private double lastValue2 = 500.0;
public DemoPanel() {
super(new BorderLayout());
this.series1 = new TimeSeries("Random 1", Millisecond.class);
this.series2 = new TimeSeries("Random 2", Millisecond.class);
TimeSeriesCollection dataset1 = new TimeSeriesCollection(
this.series1);
TimeSeriesCollection dataset2 = new TimeSeriesCollection(
this.series2);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Dynamic Data Demo 2", "Time", "Value", dataset1,
true, true, false);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(4, 4, 4, 4));
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(10000.0); // 10 seconds
plot.setDataset(1, dataset2);
NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
rangeAxis2.setAutoRangeIncludesZero(false);
plot.setRenderer(1, new DefaultXYItemRenderer());
plot.setRangeAxis(1, rangeAxis2);
plot.mapDatasetToRangeAxis(1, 1);
ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
JButton button1 = new JButton("Add To Series 1");
button1.setActionCommand("ADD_DATA_1");
button1.addActionListener(this);
JButton button2 = new JButton("Add To Series 2");
button2.setActionCommand("ADD_DATA_2");
button2.addActionListener(this);
JButton button3 = new JButton("Add To Both");
button3.setActionCommand("ADD_BOTH");
button3.addActionListener(this);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBackground(Color.white);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
add(buttonPanel, BorderLayout.SOUTH);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
}
public void actionPerformed(ActionEvent e) {
boolean add1 = false;
boolean add2 = false;
if (e.getActionCommand().equals("ADD_DATA_1")) {
add1 = true;
}
else if (e.getActionCommand().equals("ADD_DATA_2")) {
add2 = true;
}
else if (e.getActionCommand().equals("ADD_BOTH")) {
add1 = true;
add2 = true;
}
if (add1) {
double factor = 0.90 + 0.2 * Math.random();
this.lastValue1 = this.lastValue1 * factor;
this.series1.add(new Millisecond(), this.lastValue1);
System.out.println("Amount of the Items in series1: " + this.series1.getItemCount());
}
if (add2) {
double factor = 0.90 + 0.2 * Math.random();
this.lastValue2 = this.lastValue2 * factor;
this.series2.add(new Millisecond(), this.lastValue2);
}
}
}
public DynamicDataDemo2(String title) {
super(title);
setContentPane(new DemoPanel());
}
public static JPanel createDemoPanel() {
return new DynamicDataDemo2.DemoPanel();
}
public static void main(String[] args) {
DynamicDataDemo2 demo = new DynamicDataDemo2(
"JFreeChart: DynamicDataDemo2.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}