Sunday, 7 June 2015

JFreeChart Adding subtitle to the chart

You can add subtitle to the chart using addSubtitle method of class JFreeChart.
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.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

public class Main {
 public static void main(String args[]) {

  String title = "Line chart demo";

  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(500, "Revenue", "2009");
  dataset.addValue(550, "Revenue", "2010");
  dataset.addValue(498, "Revenue", "2011");
  dataset.addValue(325, "Revenue", "2012");
  dataset.addValue(670, "Revenue", "2013");
  dataset.addValue(710, "Revenue", "2014");

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

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

 }
}


Output

You can configure subtitle (by changing font size, position etc.,). Following application configure sub title font, position, alignment.
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.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

public class Main {
 public static void main(String args[]) {

  String title = "Line chart demo";

  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(500, "Revenue", "2009");
  dataset.addValue(550, "Revenue", "2010");
  dataset.addValue(498, "Revenue", "2011");
  dataset.addValue(325, "Revenue", "2012");
  dataset.addValue(670, "Revenue", "2013");
  dataset.addValue(710, "Revenue", "2014");

  /* create chart */
  JFreeChart chart = ChartFactory.createLineChart(title, "Year",
    "Revenue in M$", dataset, PlotOrientation.VERTICAL, false,
    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);

  chart.addSubtitle(subTitle);

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

 }
}


Output


Another example with two organizations (A, B) data

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.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

public class Main {
 public static void main(String args[]) {

  String title = "Line chart demo";

  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.addValue(500, "Company A Revenue", "2009");
  dataset.addValue(550, "Company A Revenue", "2010");
  dataset.addValue(450, "Company A Revenue", "2011");
  dataset.addValue(650, "Company A Revenue", "2012");
  dataset.addValue(550, "Company A Revenue", "2013");
  dataset.addValue(700, "Company A Revenue", "2014");
  
  dataset.addValue(200, "Company B Revenue", "2009");
  dataset.addValue(450, "Company B Revenue", "2010");
  dataset.addValue(398, "Company B Revenue", "2011");
  dataset.addValue(425, "Company B Revenue", "2012");
  dataset.addValue(770, "Company B Revenue", "2013");
  dataset.addValue(810, "Company B Revenue", "2014");

  /* create chart */
  JFreeChart chart = ChartFactory.createLineChart(title, "Year",
    "Revenue in M$", dataset, PlotOrientation.VERTICAL, false,
    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);

  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