如何设置使用ChartFactory.createBarChart创建的BarGraph的Y轴范围 [英] How to set Y axis range of of BarGraph created using ChartFactory.createBarChart

查看:149
本文介绍了如何设置使用ChartFactory.createBarChart创建的BarGraph的Y轴范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

调用

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

这篇关于如何设置使用ChartFactory.createBarChart创建的BarGraph的Y轴范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆