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

╄→尐↘猪︶ㄣ 提交于 2019-12-07 14:56:18

问题


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:


回答1:


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



来源:https://stackoverflow.com/questions/36943211/how-to-set-y-axis-range-of-of-bargraph-created-using-chartfactory-createbarchart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!