Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Create a dual axis chart using JFreeChart

This Example shows you how to create a dual axis chart using JFreeChart. Code given below creates a simple dual axis chart for a match.

Ads

Create a dual axis chart using JFreeChart

     

This Example shows you how to create a dual axis chart using JFreeChart. Code given below creates a simple dual axis chart for a match.
In the code given below we have extended class ApplicationFrame to create a frame and also pass a string value to the constructor of ApplicationFrame class by using super keyword that will be name of the created frame.

Methods used in this example are described below:

pack(): This method invokes the layout manager.

centerFrameOnScreen(): This method is used for the position of the frame in the middle of the screen.

setVisible(): This method is used for display frame on the screen.

addValue(): This method is used to add values in DefaultCategoryDataset class object. DualAxisChart.jsp

import java.awt.Color;

import java.awt.Font;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class DualAxisChart extends ApplicationFrame {

  final String team1 = "1st Team";
  final String team2 = "2nd Team";

  public DualAxisChart(String titel) {
  super(titel);

  final JFreeChart chart = createChart();
  final ChartPanel chartPanel = new ChartPanel(chart);
  chartPanel.setPreferredSize(
   new 
java.awt.Dimension(600450));
  setContentPane(chartPanel);
  }

  public double[][] run() {
  double[][] run = new double[][]{
  {106247281294},
  {26381649210}
  };
  return run;
  }

  private CategoryDataset createRunDataset1() {
  final DefaultCategoryDataset dataset = 
 
new DefaultCategoryDataset();

  double[] run = run()[0];

  for (int i = 0; i < run.length; i++) {
  dataset.addValue(run[i], team1+
   " Run"
"" (i + 1));
  }
  return dataset;
  }

  private CategoryDataset createRunDataset2() {
  final DefaultCategoryDataset dataset = 
  
new DefaultCategoryDataset();

  double[] run = run()[1];

  for (int i = 0; i < run.length; i++) {
  dataset.addValue(run[i], team2+
  
" Run""" (i + 1));
  }
  return dataset;
  }

  private CategoryDataset createRunRateDataset1() {
  final DefaultCategoryDataset dataset 
   = 
new DefaultCategoryDataset();

  double[] run = run()[0];
  float num = 0;

  for (int i = 0; i < run.length; i++) {
  num += run[i];
  dataset.addValue(num / (i + 1)
  team1+
" Runrate""" (i + 1));
  }
  return dataset;
  }

  private CategoryDataset createRunRateDataset2() {
  final DefaultCategoryDataset dataset =
   new DefaultCategoryDataset();

  double[] run = run()[1];
  float num = 0;

  for (int i = 0; i < run.length; i++) {
  num += run[i];
  dataset.addValue(num / (i + 1)
  team2+
" Runrate""" (i + 1));
  }
  return dataset;
  }

  private JFreeChart createChart() {

  final CategoryDataset dataset1 = createRunDataset1();
  final NumberAxis rangeAxis1 = new NumberAxis("Run");
  rangeAxis1.setStandardTickUnits(
  NumberAxis.createIntegerTickUnits
());
  final BarRenderer renderer1 = new BarRenderer();
  renderer1.setSeriesPaint(0, Color.red);
  renderer1.setBaseToolTipGenerator(
  
new StandardCategoryToolTipGenerator());
  final CategoryPlot subplot1 = 
  new 
CategoryPlot(dataset1, null, 
  rangeAxis1, renderer1
);
  subplot1.setDomainGridlinesVisible(true);

  final CategoryDataset runrateDataset1 
  = createRunRateDataset1
();
  final ValueAxis axis2 = new NumberAxis("Run Rate");
  subplot1.setRangeAxis(1, axis2);
  subplot1.setDataset(1, runrateDataset1);
  subplot1.mapDatasetToRangeAxis(11);
  final CategoryItemRenderer runrateRenderer1 
   = 
new LineAndShapeRenderer();
  runrateRenderer1.setSeriesPaint(0, Color.red);

  subplot1.setForegroundAlpha(0.7f);
  subplot1.setRenderer(0, renderer1);
  subplot1.setRenderer(1, runrateRenderer1);


  final CategoryDataset dataset2 = createRunDataset2();
  final NumberAxis rangeAxis2 = new NumberAxis("Run");
  rangeAxis2.setStandardTickUnits(
   NumberAxis.createIntegerTickUnits
());
  final BarRenderer renderer2 = new BarRenderer();
  renderer2.setSeriesPaint(0, Color.blue);
  renderer2.setBaseToolTipGenerator(
  new 
StandardCategoryToolTipGenerator());
  final CategoryPlot subplot2 = 
 
new CategoryPlot(dataset2, null, 
  rangeAxis2, renderer2
);
  subplot2.setDomainGridlinesVisible(true);


  final CategoryDataset runrateDataset2 = 
  createRunRateDataset2
();
  final ValueAxis axis3 = new NumberAxis("Run Rate");
  subplot2.setRangeAxis(1, axis3);
  subplot2.setDataset(1, runrateDataset2);
  subplot2.mapDatasetToRangeAxis(11);
  final CategoryItemRenderer runrateRenderer2 = 
  
new LineAndShapeRenderer();
  runrateRenderer2.setSeriesPaint(0, Color.blue);

  subplot2.setForegroundAlpha(0.7f);
  subplot2.setRenderer(0, renderer2);
  subplot2.setRenderer(1, runrateRenderer2);

  final CategoryAxis domainAxis = new CategoryAxis("Over");
  final CombinedDomainCategoryPlot plot = 
 
new CombinedDomainCategoryPlot(domainAxis);

  plot.add(subplot1, 1);
  plot.add(subplot2, 1);


  final JFreeChart chart = new JFreeChart(
  "Score Bord"new Font("SansSerif", Font.BOLD, 12),
  plot, true);
  return chart;
  }

  public static void main(final String[] args) {

  final String title = "Score Bord";
  final DualAxisChart chart = new DualAxisChart(title);
  chart.pack();
  RefineryUtilities.centerFrameOnScreen(chart);
  chart.setVisible(true);
  }
}

Output:



Download code

Advertisements

Ads

Online Training Classes

Share on Google+Share on Google+

Create a dual axis chart using JFreeChart

Posted on: September 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Create a dual axis chart using JFreeChart   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Africa
November 30, 2011
Horizontal axis

Please, how can I modify the "names" for the horizontal axis, I mean, I want show words like "track 1", "track 2", "track 3", "track 4", and so on... not names like 1, 2, 3, 4, 5, ....., 10. Thank you

Ads

 

Ads