Sunday 7 June 2015

JFreeChart XYLineChart


import java.awt.Color;
import java.awt.Font;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

public class Main {
 public static void main(String args[]) {
  XYSeries organization1 = new XYSeries("Organization-1");
  organization1.add(2005, 300);
  organization1.add(2006, 400);
  organization1.add(2007, 500);
  organization1.add(2008, 600);
  organization1.add(2009, 400);
  organization1.add(2010, 900);

  XYSeries organization2 = new XYSeries("Organization-2");
  organization2.add(2005, 400);
  organization2.add(2006, 500);
  organization2.add(2007, 600);
  organization2.add(2008, 700);
  organization2.add(2009, 500);
  organization2.add(2010, 1000);

  XYSeries organization3 = new XYSeries("Organization-3");
  organization3.add(2005, 200);
  organization3.add(2006, 300);
  organization3.add(2007, 400);
  organization3.add(2008, 500);
  organization3.add(2009, 300);
  organization3.add(2010, 800);

  XYSeriesCollection dataset = new XYSeriesCollection();
  dataset.addSeries(organization1);
  dataset.addSeries(organization2);
  dataset.addSeries(organization3);

  /* create chart */
  JFreeChart chart = ChartFactory.createXYLineChart("Revenue Comparison",
    "Year", "Revenue in M$", dataset, PlotOrientation.VERTICAL,
    true, true, false);

  TextTitle subTitle = new TextTitle("Company revenue details");
  subTitle.setFont(new Font("SansSerif", Font.ITALIC, 25));
  subTitle.setPosition(RectangleEdge.BOTTOM);
  subTitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);

  XYPlot plot = (XYPlot) chart.getPlot();
  plot.setBackgroundPaint(Color.LIGHT_GRAY);
  plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
  plot.setDomainGridlinePaint(Color.white);
  plot.setRangeGridlinePaint(Color.white);

  chart.addSubtitle(subTitle);

  /* 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