Sunday 7 June 2015

JFreeChart Time Series charts

Time series charts are like line charts, only advantage is, we can put dates as values on domain access.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class Main {
 public static void main(String args[]) {
  TimeSeries s1 = new TimeSeries("Company A");
  s1.add(new Month(1, 2015), 2581.8);
  s1.add(new Month(2, 2015), 581.8);
  s1.add(new Month(3, 2015), 467.3);
  s1.add(new Month(4, 2015), 1033.8);
  s1.add(new Month(5, 2015), 167.6);
  s1.add(new Month(6, 2015), 1448.8);
  s1.add(new Month(7, 2015), 148.3);
  s1.add(new Month(8, 2015), 153.9);
  s1.add(new Month(9, 2015), 1420.7);
  s1.add(new Month(10, 2015), 1230.2);
  s1.add(new Month(11, 2015), 1031.8);
  s1.add(new Month(12, 2015), 1939.6);

  TimeSeries s2 = new TimeSeries("Company B");
  s2.add(new Month(1, 2015), 5081.8);
  s2.add(new Month(2, 2015), 629.6);
  s2.add(new Month(3, 2015), 1203.2);
  s2.add(new Month(4, 2015), 987.2);
  s2.add(new Month(5, 2015), 1204.1);
  s2.add(new Month(6, 2015), 1123.6);
  s2.add(new Month(7, 2015), 119.2);
  s2.add(new Month(8, 2015), 116.5);
  s2.add(new Month(9, 2015), 1150);
  s2.add(new Month(10, 2015), 1010.5);
  s2.add(new Month(11, 2015), 106.1);
  s2.add(new Month(12, 2015), 1980.3);

  TimeSeriesCollection dataset = new TimeSeriesCollection();
  dataset.addSeries(s1);
  dataset.addSeries(s2);

  JFreeChart chart = ChartFactory.createTimeSeriesChart(
    "time chart example", "Year", "Revenue in M$", dataset, true,
    true, false);

  /* create and display chart on frame */
  ChartFrame frame = new ChartFrame("JFreeChart Demo", chart);
  frame.setVisible(true);
  frame.pack();

 }
}


Output



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment