Help with making real-time chart update

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
zozoheir1
Posts: 3
Joined: Tue Sep 20, 2016 7:34 pm
antibot: No, of course not.

Help with making real-time chart update

Post by zozoheir1 » Tue Sep 20, 2016 8:15 pm

Hello everyone,

I have been stuck for a couple of days now and couldn't find anything online that could help specifically with what I am trying to do, even though I believe it should be very doable.
I am getting external data from a database and this data will be pulled in the getExternalData() method.
What I am trying to do is have a Time Series XY Line chart that will, every second, pull the data and add it to my " series" as such:
series.add( "current time in format hour:seconds" , Pulled Data );

and have the chart update and trace on the screen real time

The problems are:

- I have never used threading and am not sure how/where to include that in this code
- I am not sure if this is doable with the code below or if I have to rearrange everything

Code: Select all


import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;

import org.jfree.data.time.Second;
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 CreatePnLChart extends ApplicationFrame {

    public CreatePnLChart(final String title) {
        super(title);
        final XYDataset dataset = createDataset();
        try{
            Thread.sleep(1000);
        }
        catch(InterruptedException e){
        }
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);

    }

    private JFreeChart createChart(final XYDataset dataset) {

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "Sample Chart",
                "Time",
                "PnL",
                dataset,
                true,
                true,
                false
        );

        chart.setBackgroundPaint(Color.white);

        final XYPlot plot = chart.getXYPlot();

        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("hh:mm:ss"));
        return chart;
    }

    private XYDataset createDataset() {
        Calendar cal = Calendar.getInstance();
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        final TimeSeries series = new TimeSeries("Series 1", Second.class);

        series.add(new Second(cal.getTime()), 1); // Where I would like to add the pulled data every second

        dataset.addSeries(series);
        return dataset;
    }

    public static void main(final String[] args) {
        final CreatePnLChart demo = new CreatePnLChart("Time Series Demo 12");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);

        demo.setVisible(true);

    }

    private double getExternalData(){
        double PnL = 0;
        return PnL;
    }

}

Thank you guys!

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Help with making real-time chart update

Post by John Matthews » Tue Sep 20, 2016 10:14 pm

Because database latency is variable, perform your query in the background of a SwingWorker, publish() interim results and update the chart's dataset in your implementation of process(). The listening chart will update itself in response. A simple example is shown here.

zozoheir1
Posts: 3
Joined: Tue Sep 20, 2016 7:34 pm
antibot: No, of course not.

Re: Help with making real-time chart update

Post by zozoheir1 » Wed Sep 21, 2016 2:51 am

Thanks for your answer :) Unfortunately it sounds like I am not familiar at all with Swings...

Is there any good basic tutorial on Swings and how they relate to pulling this data from the database while displaying the chart please? I think I need to take some steps back before solving my issue

thanks

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Help with making real-time chart update

Post by John Matthews » Wed Sep 21, 2016 2:51 pm

I frequently rely on the relevant Oracle tutorials: JDBC™ Database Access and Creating a GUI With JFC/Swing

zozoheir1
Posts: 3
Joined: Tue Sep 20, 2016 7:34 pm
antibot: No, of course not.

Re: Help with making real-time chart update

Post by zozoheir1 » Thu Sep 22, 2016 2:39 am

Awesome thanks John I m starting to read the tutorials !

Locked