JFreeChart柱状图的完整实例

JFreeChart柱状图的完整实例
2010-12-15 17:50

 

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class AliHorizonBarChartDemo extends ApplicationFrame{
 
 public AliHorizonBarChartDemo(String s){
  //这里是继承自ApplicationFrame
  super(s);
  //创建Dataset
  CategoryDataset categoryDataset=createDataset();
  //创建jfreechart
  JFreeChart jfreechart=createChart(categoryDataset);
  //创建容器
  ChartPanel chartPanel=createPanel();
  //设置容器的初始大小(Dimension是用来设置尺寸的)
  chartPanel.setPreferredSize(new Dimension(400,200));//尤其注意的是panel的高度会严重影响柱体的宽度
  //下面这句是关键,控制是否显示,也就是将整个组件放入到组件中去
  /**
   * 对JFrame添加组件有两种方式:
          1)用getContentPane()方法获得JFrame的内容面板,再对其加入组件:frame.getContentPane().add(childComponent)
           2)建立一个Jpanel或JDesktopPane之类的中间容器,把组件添加到容器中,用setContentPane()方法把该容器置为JFrame的内容

                   面板:
               JpanelcontentPane=newJpanel();//把其它组件添加到Jpanel中;
               frame.setContentPane(contentPane); //把contentPane对象设置成为frame的内容面板
   */
     setContentPane(chartPanel);
 }
 public CategoryDataset createDataset(){
  System.out.println("========createDataSet========");
  //创建Dataset
  DefaultCategoryDataset defaultDataset=new DefaultCategoryDataset();
  //创建分类
  defaultDataset.addValue(100d, "S1","Apple" );
  defaultDataset.addValue(101d, "S1", "banana");
  defaultDataset.addValue(150d, "S1", "grape");
  defaultDataset.addValue(12d,"S1","pear");
  
  return defaultDataset;
  
 }
 public JFreeChart createChart(CategoryDataset dataset){
  System.out.println("===========createJfreechart========");
  //创建jfreechart
        JFreeChart jfreechart=ChartFactory.createBarChart("Ali's fruits","type","number",dataset,PlotOrientation.HORIZONTAL,false,true,false); 
        //设置jfreechart的背景
        System.out.println("jfreechart="+jfreechart);
        jfreechart.setBackgroundPaint(Color.yellow);
       
        //创建plot
        CategoryPlot categoryPlot=(CategoryPlot)jfreechart.getPlot();
        System.out.println("categoryPlot="+categoryPlot);
        //设置场景的背景色
        categoryPlot.setBackgroundPaint(Color.gray);
        //还可以设置场景的横向网格
        categoryPlot.setDomainGridlinePaint(Color.red);
        //设置场景横向网格的可见性,如果不设置的话则不可见
        categoryPlot.setDomainGridlinesVisible(true);
        //设置场景的竖向网格
        categoryPlot.setRangeGridlinePaint(Color.green);
        //设置最长的说明标签的显示比例
        System.out.println(categoryPlot.getDomainAxis());
        categoryPlot.getDomainAxis().setMaximumCategoryLabelWidthRatio(0.8F);
       
        //设置轴
        NumberAxis numberAxis=(NumberAxis)categoryPlot.getRangeAxis();
        //设置轴的单位
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
       
        //添加额外的效果(表示器)
        BarRenderer barrender=(BarRenderer)categoryPlot.getRenderer();
        //设置是否显示外框
        barrender.setDrawBarOutline(true);
        //显示每个柱子的具体值
        //下面这两句缺一不可
        barrender.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        barrender.setSeriesItemLabelsVisible(0, Boolean.TRUE);
        //添加渐变色
        GradientPaint gradientPaint=new GradientPaint(0.0f,0.0f,new Color(255,255,255),0.0f,0.0f,new Color(170,170,255));
        //给柱体上色
        barrender.setSeriesPaint(0,gradientPaint);
       return jfreechart;
 }
 public  ChartPanel createPanel(){
  JFreeChart jfreechart = createChart(createDataset());
  return new ChartPanel(jfreechart);
  
 }
 public static void main(String[] args){
  AliHorizonBarChartDemo demo=new AliHorizonBarChartDemo("ALI's bar chart");
  demo.pack();
  RefineryUtilities.centerFrameOnScreen(demo);
  demo.setVisible(true);
  demo.setDefaultCloseOperation(1);
  
 }
}

 

你可能感兴趣的:(apple,UI,jfreechart)