Home Chartgraphs 3D Bar Chart

 
 

Share on Google+Share on Google+

3D Bar Chart

Advertisement
In this section we are providing you an example to create a 3D Bar Chart.

3D Bar Chart

     

In this section we are providing you an example to create a 3D Bar Chart.

Description of Program 

For creating a 3D Bar chart we use the object of DefaultCategoryDataset. After creating the object of DefaultCategoryDataset we add some data in this object by invoking setValue() method. In this example you learn some more interactive features of JFreeChart like set the background color of chart and adjust the color the title etc.

Description of Code

For defining a dataset for a Bar chart we have to create an object of DefaultCategoryDataset type :
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

After creating the instance of dataset then we have to add the data in the data set by invoking the method setValue(). In this example we show more than one set of bars for the same chart. This can done by the following modification :
  dataset.setValue(6, "Science", "Rahul");
  dataset.setValue(8, "Maths", "Rahul");
  dataset.setValue(5, "Science", "Deepak");
  dataset.setValue(3, "Maths", "Deepak");

First argument specifies the total marks obtained by the student and the second argument specify what will appear in the legend to the meaning of the bar.

  JFreeChart chart = ChartFactory.createBarChart3D("Comparison between Students", "Student", "Marks", dataset, PlotOrientation.VERTICAL, true, true, false);
After added the data in dataset we create the 3D Bar chart by invoking the createBarChart3D() method. This method is a static method of ChartFactory class and its returns the object of JFreeChart type.This method syntax is:

  Public static JFreeChart createBarChart3D(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);

  chart.setBackgroundPaint(Color.yellow);
Above method is used to set the color of chart background

  chart.getTitle().setPaint(Color.blue); 
Above method is used to set the color of chart title

  CategoryPlot p = chart.getCategoryPlot(); 
Above method is used to get the object of the Plot for Bar Chart

  p.setRangeGridlinePaint(Color.red);
Above method is used to set the color of plot Gridlines

  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
After this we create the object of ChartFrame. It used to display a chart.

Here is code of Program :

import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
import java.awt.*;

public class BarExample2{
public static void main(String arg[]){
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(6"Science""Rahul");
  dataset.setValue(8"Maths""Rahul");
  dataset.setValue(5"Science""Deepak");
  dataset.setValue(3"Maths""Deepak");
  dataset.setValue(6"Science""Vinod");
  dataset.setValue(9"Maths""Vinod");
  dataset.setValue(2"Science""Chandan");
  dataset.setValue(4"Maths""Chandan");
  JFreeChart chart = ChartFactory.createBarChart3D
  (
"Comparison between Students","Students""Marks"
  dataset, PlotOrientation.VERTICAL, true,true, 
false);
  chart.setBackgroundPaint(Color.yellow);
  chart.getTitle().setPaint(Color.blue)
  CategoryPlot p = chart.getCategoryPlot()
  p.setRangeGridlinePaint(Color.red)
  ChartFrame frame1=new ChartFrame("3D Bar Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(300,300);
  }
}

Output of the Program :

Download this Example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Discuss: 3D Bar Chart   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
wks
February 28, 2012
java

when i execute example i get error
heronote
April 1, 2012
Google Search 3D Graph

sin(5.5x)*cos(5*y)+x*x+1 5000-140*(x*x+y*y)+(x*x+y*y)^2 sin(x^2+y^2)/(abs(x*y)+1) from -2.5 to 2.5 tanh(y(y^4+5x^4-10(x^2)(y^2))/(x^2+y^2)^4) (x^2+((3 y)/2-(x^2+abs(x)-6)/(x^2+abs(x)+2))^2)-36 Free OpenGL eBook: http://www.heronote.com/files/OpenGL.htm Free OpenGL ES eBook: http://www.heronote.com/files/OpenGL.ES.htm Free WebGL eBook: http://www.heronote.com/files/WebGL.htm
sonam
May 7, 2012
how to run this code??

i hav copied this whole code in a jsp file and trying to run with tomcat but its not working.....pls send your suggestion at sonamstyle@gmail.com
Samih
June 27, 2012
Ask

hello guys how are you plz someone help i dont have the library jfreechart.org how can i download or add it NETbeans 7.1 and thanx
DMCA.com