Results 1 to 4 of 4
  1. #1
    smh123 is offline Member
    Join Date
    Apr 2017
    Posts
    1
    Rep Power
    0

    Default Creating a live graph

    I have multiple sensor data coming into java which is then presented in a graph.

    I am having trouble getting the graph to be live (I want it to update automatically every second). Below is the code I am using to create the graph how can I make this a live graph?

    Java Code:
    import java.awt.Dimension;
    import java.util.ArrayList;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    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.*;
    
    
    public class graphPlot extends JFrame{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static ArrayList<Double> data1;
    private static ArrayList<Double> data2;
    private static ArrayList<Double> data3;
    private static ArrayList<Double> data4;
    private static ArrayList<Double> data5;
    private static ArrayList<Double> data6;
    
    public graphPlot(String s,ArrayList<Double> data1,ArrayList<Double> data2,ArrayList<Double> data3,ArrayList<Double> data4,ArrayList<Double> data5,ArrayList<Double> data6)
    {   
        super(s);       
        this.tempcelcius = data1;
        this.humidity = data2;
        this.barometric = data3;
        this.raindetect = data4;
        this.moisturedetect = data5;
        this.vibrationdetect = data6;
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }
    
    private static XYDataset createDataset()
    {
        //create dataset here
        XYSeriesCollection xyseriescollection = new XYSeriesCollection();
        XYSeries xyseries = new XYSeries("data1");
        XYSeries xyseries1 = new XYSeries("data2");
        XYSeries xyseries2 = new XYSeries("data3");
        XYSeries xyseries3 = new XYSeries("data4");
        XYSeries xyseries4 = new XYSeries("data5");
        XYSeries xyseries5 = new XYSeries("data6");
    
    
        int i=1;
        for(Double data1:data1){
            //System.out.println(i+" "+data1);
            xyseries.add(i,data1);
            ++i;
        }
        i=1;
        for(Double data1:data2){
            //System.out.println(i+" "+data1);
            xyseries1.add(i,data1);
            ++i;
        }
        i=2;
        for(Double data1:data3){
            //System.out.println(i+" "+data1);
            xyseries2.add(i,data1);
            ++i;
        }
        i=3;
        for(Double data1:data4){
            //System.out.println(i+" "+data1);
            xyseries3.add(i,data1);
            ++i;
    
        }
        i=4;
        for(Double data1:data5){
            //System.out.println(i+" "+data1);
            xyseries4.add(i,data1);
            ++i;
    
        }
        i=5;
        for(Double data1:data6){
            //System.out.println(i+" "+data1);
            xyseries5.add(i,data1);
            ++i;
    
        }   
        xyseriescollection.addSeries(xyseries);
        xyseriescollection.addSeries(xyseries1);
        xyseriescollection.addSeries(xyseries2);
        xyseriescollection.addSeries(xyseries3);
        xyseriescollection.addSeries(xyseries4);
        xyseriescollection.addSeries(xyseries5);
    
        return xyseriescollection;
    
    
    }
    
    private static JFreeChart createChart(XYDataset xydataset)
    {
        JFreeChart jfreechart = ChartFactory.createXYLineChart("Sensor Plots", "X", "Y", xydataset, PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyplot = (XYPlot)jfreechart.getPlot();
        xyplot.setDomainPannable(true);
        xyplot.setRangePannable(true);
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
        xylineandshaperenderer.setBaseShapesVisible(true);
        xylineandshaperenderer.setBaseShapesFilled(true);
        NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        return jfreechart;
    }
    
    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(createDataset());
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setMouseWheelEnabled(true);
        return chartpanel;
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Creating a live graph

    Note: I do not know anything about the 3rd party package you are using so the following is a guess:
    Does the charting package you are using have methods that can be called to update what is being displayed?
    Use a Timer to call the update method(s) every time period.
    Last edited by Norm; 04-11-2017 at 05:15 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Creating a live graph

    When you get new data in you can add it to the relevant Series.
    That will inform the Collection, which will then inform the Plot, which will finally inform the Chart that there has been a data change and a need to redraw.

    So all you have to do is add data to the series.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

Similar Threads

  1. Creating a graph which receives values from input sensors
    By jackwong0099 in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2013, 04:19 PM
  2. Replies: 16
    Last Post: 03-21-2012, 09:41 AM
  3. creating a graph from a text file
    By chinolee in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 08:36 AM
  4. Live changes
    By abhiN in forum Advanced Java
    Replies: 0
    Last Post: 03-19-2008, 03:32 PM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 04:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •