如何设置使用ChartFactory.createBarChart创建的BarGraph的Y轴范围

时间:2016-04-29 16:21:24

标签: java jfreechart

我使用下面的代码使用jfree.chart.ChartFactory创建了一个条形图。我需要将Y轴的范围设置为0 - 100.如何设置最大值。

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   );
      }
  }

这是我得到的输出。我需要将Y轴最大值设置为100:

image

1 个答案:

答案 0 :(得分:1)

在图表的范围轴上调用setRange()以设置特定范围并关闭自动范围:

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

image