Search code examples
javajfreechart

JFreeChart relative scaling


I want to create a Linegraph with JFreechart. My Data is like :

Number1,Number2,Timestamp

So I want two Lines Where the Y-Axis is Number1 and Number2 and the X-Axis it the Timestamp. I save the Chart as a png Image and the X-Axis looks just to long I think it is because of the timestamp beeing long apart. I Searched google and Stackoverflow but answers like this dont work for me. How can i Fix this?

Here is my code:

         TimeSeries ts = new TimeSeries("Systole");

        ts.add(new Minute(new Date(1356999660000L)), 141);
        ts.add(new Minute(new Date(1356999840000L)), 129);
        ts.add(new Minute(new Date(1433074800000L)), 146);
        ts.add(new Minute(new Date(1433075700000L)), 136);

        TimeSeries diast = new TimeSeries("Diastole");

        diast.add(new Minute(new Date(1356999660000L)), 95);
        diast.add(new Minute(new Date(1356999840000L)), 86);
        diast.add(new Minute(new Date(1433074800000L)), 98);
        diast.add(new Minute(new Date(1433075700000L)), 91);
        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(systole);
        dataset.addSeries(diastole);
        XYDataset data = dataset;
  final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(ts);
        dataset.addSeries(diast);
        XYDataset data = dataset;
        //String title, String timeAxisLabel, String valueAxisLabel, XYDataset dataset

        JFreeChart chart = ChartFactory.createTimeSeriesChart("Blutdruck", "", "", data);      
        SaveImage(chart.createBufferedImage(500,500),"Test1.png");

enter image description here


Solution

  • The "vertical lines" effect happens because you are plotting a series that fluctuates on very different scales (minutes vs. years). Obviously, the variations at the scale of a minute are completely invisible when plotting them over a period of two years. It is like trying to draw the streets of a city on the map of a country.

    There is no out-of-the-box solution for that problem, but you could find inspiration from the typical share price plots (from Google or Yahoo), where they let the user choose the scale they want to use.

    Variation over five days: enter image description here

    Variation over three months: enter image description here

    Note that the variation over three months is smoother than the variation over five days. The reason is that they do not try to plot the value at every minute. Instead, they "subsample" their data.

    In other words, they take the average over one day and plot only the values for each day.