Nikhilesh
Nikhilesh

Reputation: 35

How to set Y axis range of of BarGraph created using ChartFactory.createBarChart

I have created a bar graph using jfree.chart.ChartFactory using below code. I need to set the range of Y axis as 0 - 100. How can I set the max value.

import java.io.File;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarGraph
{
    public static void main( String[ ] args )throws Exception 
    {
       final DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
       dataset.addValue( 10 , "FIAT" , "Speed");
       dataset.addValue( 30 , "AUDI", "Speed");
       dataset.addValue( 20 , "FORD", "Speed");
       dataset.addValue( 50 , "BMW", "Speed");
       JFreeChart barChart = ChartFactory.createBarChart3D(
           "CAR USAGE STATISTICS", 
           "Category", "Score", 
           dataset,PlotOrientation.VERTICAL, 
           true, true, false);
       int width = 380; /* Width of the image 480*/
       int height = 280; /* Height of the image 360*/ 
       File BarChart = new File( "../ELec/WebContent/img/BarChart.jpeg" ); 
       ChartUtilities.saveChartAsJPEG( BarChart , barChart , width , height   );
      }
  }

This is the output I got. I need set the Y axis max value as 100:

image

Upvotes: 3

Views: 2715

Answers (1)

trashgod
trashgod

Reputation: 205775

Invoke setRange() on the plot's range axis to set a particular range and turn off auto-ranging:

CategoryPlot plot = (CategoryPlot) barChart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0, 100);

image

Upvotes: 1

Related Questions