1

I am using TimeSeries jfreechart to show network performance.I want to show total time passed in seconds but it showing only seconds from 0 to 59 and then reset seconds to 0 again.I have to show data for last 120 seconds.

Here is the code : This function is used to create chart:

private JFreeChart createChart(XYDataset xydataset) {

    result = ChartFactory.createTimeSeriesChart("admin0", "", "MBytes/S", xydataset, true, true, true);
    TextTitle objTitle = new TextTitle("admin0", new Font("Verdana", Font.BOLD, 12));
    result.setTitle(objTitle);

        final XYPlot plot = result.getXYPlot();
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinesVisible(true);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinePaint(Color.GRAY);
        plot.setDomainGridlinePaint(Color.GRAY);


       DateAxis xaxis = (DateAxis)plot.getDomainAxis();
        xaxis.setAutoRange(true); ////set true to move graph with time.
        xaxis.setFixedAutoRange(120000.0);
        xaxis.setTickUnit(new DateTickUnit(DateTickUnit.SECOND, 15, new SimpleDateFormat("ss")));


        NumberAxis range = (NumberAxis) plot.getRangeAxis();///y-Axis
        range.setRange(0.0, 1.0);
        range.setTickUnit(new NumberTickUnit(0.2));

        XYItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesPaint(1, Color.GREEN);

        return result;

}

And here is the code for creating dataset:

  private XYDataset createAdmin0DatasetTest() {

     TimeSeriesCollection dataset = new TimeSeriesCollection();

    try
    {

     if(performanceData != null)
     {
         long speed = 0;
         double recieveRate = 0;
         double sendRate = 0;
         long timeinMilli = 0;
         long devider = 4294967296l;
         long snapTime = 0;

         Vector admin0Vec = (Vector)this.performanceData.get("admin0");
            if(admin0Vec != null && admin0Vec.size() > 0)
            {
                Vector innerVec = (Vector)admin0Vec.get(0);             
                recieveRate = Long.parseLong(innerVec.get(2).toString());
                sendRate = Long.parseLong(innerVec.get(1).toString());

                timeinMilli = Long.parseLong(innerVec.get(0).toString());

                }catch(Exception ex)
                {
                    System.out.println("Exception in adding same values");
                }


                for(int i  = 1 ; i < admin0Vec.size() ; i++)
                {
                    innerVec = (Vector)admin0Vec.get(i);                
                    recieveRate = Long.parseLong(innerVec.get(2).toString());
                    sendRate = Long.parseLong(innerVec.get(1).toString());
                    timeinMilli = Long.parseLong(innerVec.get(0).toString());
                    try
                    {
                        this.adminRecieve.addOrUpdate(new Second(new Date(timeinMilli)), recieveRate);
                        this.adminSend.addOrUpdate(new Second(new Date(timeinMilli)), sendRate);

                    }catch(Exception ex)
                    {
                        System.out.println("Exception in adding same values");
                        //ex.printStackTrace();
                    }


                }
                dataset = new TimeSeriesCollection(this.adminRecieve);
                dataset.addSeries(adminSend);

            }
     }
     }catch(Exception ex)
     {
         ex.printStackTrace();
     }

  return dataset;
}

Please help me out

1 Answers1

1

Hint:

You are using DateAxis for your domain axis and render it as seconds, so surely it will only display the seconds-part of the data without computing any totals. Moreover, it does not have to start at zero and will only display 120 seconds worth of data.

What you want is not a time series, i.e. numbers vs. time, but a data series of numbers vs. numbers (elapsed seconds). So construct it in that way and use NumberAxis for the domain.

Note: The above is for really showing the total elapsed time, e.g. for data between seconds 480 and 600 the labels will be for example 480, 500, 520, 540, 560, 580, 600 (i.e. total, as asked in the title, since some moment). If the question is to have static labels, e.g. -120, -100, -80, -60, -40, -20, 0, with moving data then setting ticks and labels on the axis needs to be done differently.

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
  • How to use NumberAxis with timeSeries graph ? Because I need a moving graph showing network performance just like we have Desktop performance graph with seconds on x-axis. If I try to use NumberAxis as x-axis,graph stop moving forward. Can you please explain me with any running example ? – user1553769 Jan 10 '14 at 10:13
  • AFAIK the "moving" feature does not depend on the axis to be DateAxis and data being time series. The "moving" functionality is provided by limiting the data range, via `setFixedAutoRange`, and after that adding data that is beyond the original range. You can do exactly the same for the `NumberAxis`: `xaxis.setAutoRange(true); xaxis.setFixedAutoRange(120000.0);` where the data here should be the difference in milliseconds to the first point in the data set, or milliseconds since the epoc – Oleg Sklyar Jan 10 '14 at 10:21
  • So you need to change `dataset = new TimeSeriesCollection(this.adminRecieve);` to not beign a `TimeSeriesCollection` as you do not need the concept of time here (in sense of years, months, ..., seconds, milliseconds) and need just a numerical representation of a moment in time or the difference from this moment to an earlier moment (start): just get the millisecond out of date/time and populate a numeric dataset (sorry I do not remember the JavaDoc of jfreechart to tell you objects of what class exactly you need to construct) – Oleg Sklyar Jan 10 '14 at 10:25
  • Thank you for ur quick response , Now i am using dataset = new XYSeriesCollection() and this.adminRecieve = new XYSeries() and creating TimeSeriesChart with it but when I try to get NumberAxis xaxis = (NumberAxis)plot.getDomianAxis() its giving me exception as "java.lang.ClassCastException: org.jfree.chart.axis.DateAxis cannot be cast to org.jfree.chart.axis.NumberAxis" – user1553769 Jan 10 '14 at 10:51
  • Because of this `ChartFactory.createTimeSeriesChart(...`? – Oleg Sklyar Jan 10 '14 at 10:57
  • Than you so much I got your point.That is working fine now. Now how can I make static my x-axis from 0 to 120 and keep the graph moving? – user1553769 Jan 10 '14 at 11:42
  • I have not done this for years and do not remember, but [the following](http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=30322) will help I hope – Oleg Sklyar Jan 10 '14 at 11:47