JFreeChart Dynamic Data Demo Chart Example

Chapter: JFreeChart Last Updated: 08-07-2016 09:17:05 UTC

Program:

            /* ............... START ............... */
                
import java.awt.BorderLayout;
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.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class JavaJFreeChartDynmicDataDemo extends ApplicationFrame implements ActionListener {

	/** The time series data. */
	private TimeSeries series;
	private double lastValue = 100.0;

	public JavaJFreeChartDynmicDataDemo(final String title) {

		super(title);
		this.series = new TimeSeries("Random Data", Millisecond.class);
		final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
		final JFreeChart chart = createChart(dataset);

		final ChartPanel chartPanel = new ChartPanel(chart);
		final JButton button = new JButton("Add New Data Item");
		button.setActionCommand("ADD_DATA");
		button.addActionListener(this);

		final JPanel content = new JPanel(new BorderLayout());
		content.add(chartPanel);
		content.add(button, BorderLayout.SOUTH);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(content);

	}

	private JFreeChart createChart(final XYDataset dataset) {
		final JFreeChart result = ChartFactory.
				createTimeSeriesChart("Dynamic Data Demo", "Time", "Value", dataset,
				true, true, false);
		final XYPlot plot = result.getXYPlot();
		ValueAxis axis = plot.getDomainAxis();
		axis.setAutoRange(true);
		axis.setFixedAutoRange(60000.0); // 60 seconds
		axis = plot.getRangeAxis();
		axis.setRange(0.0, 200.0);
		return result;
	}

	public void actionPerformed(final ActionEvent e) {
		if (e.getActionCommand().equals("ADD_DATA")) {
			final double factor = 0.90 + 0.2 * Math.random();
			this.lastValue = this.lastValue * factor;
			final Millisecond now = new Millisecond();
			System.out.println("Now = " + now.toString());
			this.series.add(new Millisecond(), this.lastValue);
		}
	}

	public static void main(final String[] args) {

		final JavaJFreeChartDynmicDataDemo demo = new 
				JavaJFreeChartDynmicDataDemo("Dynamic Data Demo");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}
}
                /* ............... END ............... */
        

Output

Now = Fri Jul 08 14:34:55 IST 2016
Now = Fri Jul 08 14:35:06 IST 2016
Now = Fri Jul 08 14:35:07 IST 2016
Now = Fri Jul 08 14:35:07 IST 2016
Now = Fri Jul 08 14:35:07 IST 2016
Now = Fri Jul 08 14:35:08 IST 2016
Now = Fri Jul 08 14:35:08 IST 2016
Now = Fri Jul 08 14:35:08 IST 2016
Now = Fri Jul 08 14:35:08 IST 2016
Now = Fri Jul 08 14:35:09 IST 2016
Now = Fri Jul 08 14:35:09 IST 2016
Now = Fri Jul 08 14:35:09 IST 2016
JFreeChart Dynamic Data Demo Chart Example

Tags

JFreeChart Dynamic Data Demo, Java, JFreeChart

Similar Programs Chapter Last Updated
JFreeChart Scatter Chart JFreeChart 20-10-2017
JFreeChart XY Step Chart JFreeChart 10-06-2017
JFreeChart Wafer Map Chart JFreeChart 10-06-2017
JFreeChart XY Step Area Chart JFreeChart 10-06-2017
JFreeChart BarChart Color Customise JFreeChart 14-10-2016
JFreeChart Bar Chart 3D Example JFreeChart 05-08-2016
JFreeChart Bar Chart 3D Horizontal JFreeChart 05-08-2016
JFreeChart Area Chart Example JFreeChart 05-08-2016
JFreeChart Bubble Chart Example JFreeChart 03-08-2016
JFreeChart XY Chart Example JFreeChart 03-08-2016
JFreeChart Background Paint Example JFreeChart 07-07-2016
JFreeChart Get Plot Example JFreeChart 07-07-2016
JFreeChart Background Color Example JFreeChart 06-07-2016
JFreeChart Subtitles Example JFreeChart 06-07-2016
JFreeChart Chart Title Example JFreeChart 06-07-2016
JFreeChart Chart Border Example JFreeChart 05-07-2016
JFreeChart Time Series Chart Example JFreeChart 23-06-2016
JFreeChart Pie Chart 3D Example JFreeChart 08-06-2016
JFreeChart Line Chart Example JFreeChart 04-06-2016
JFreeChart Bar Chart Example JFreeChart 22-09-2018
JFreeChart Pie Chart Example JFreeChart 04-06-2016

1