欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jfreechar 柱状图 博客分类: java语言 jfreechart柱状图 

程序员文章站 2024-03-20 08:58:28
...

版本: jfreechar.1.0.13



jfreechar 柱状图
            
    
    博客分类: java语言 jfreechart柱状图 





package demo;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
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.CategoryItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.PaintList;

public class BarChartDemo8 extends ApplicationFrame
{
  public BarChartDemo8(String paramString)
  {
    super(paramString);
    CategoryDataset localCategoryDataset = createDataset();
    JFreeChart localJFreeChart = createChart(localCategoryDataset);
    ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
    localChartPanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(localChartPanel);
  }

  private static CategoryDataset createDataset()
  {
    String str1 = "First";
    String str2 = "Second";
    String str3 = "Third";
    String str4 = "北京";
    String str5 = "天津";
    String str6 = "上海";
    String str7 = "广州";
    String str8 = "深圳";
    
    
    
    
    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
    localDefaultCategoryDataset.addValue(1.0D, str1, str4);
    localDefaultCategoryDataset.addValue(4.0D, str1, str5);
    localDefaultCategoryDataset.addValue(3.0D, str1, str6);
    localDefaultCategoryDataset.addValue(5.0D, str1, str7);
    localDefaultCategoryDataset.addValue(5.0D, str1, str8);
    
    
  
    
    /*localDefaultCategoryDataset.addValue(5.0D, str2, str4);
    localDefaultCategoryDataset.addValue(7.0D, str2, str5);
    localDefaultCategoryDataset.addValue(6.0D, str2, str6);
    localDefaultCategoryDataset.addValue(8.0D, str2, str7);
    localDefaultCategoryDataset.addValue(4.0D, str2, str8);
    
    localDefaultCategoryDataset.addValue(4.0D, str3, str4);
    localDefaultCategoryDataset.addValue(3.0D, str3, str5);
    localDefaultCategoryDataset.addValue(2.0D, str3, str6);
    localDefaultCategoryDataset.addValue(3.0D, str3, str7);
    localDefaultCategoryDataset.addValue(6.0D, str3, str8);
    */
    
    return localDefaultCategoryDataset;
  }

  private static JFreeChart createChart(CategoryDataset paramCategoryDataset)
  {
      
      Font font = new Font("微软雅黑", Font.PLAIN, 12);  
      
      Font fontTitle = new Font("微软雅黑", Font.BOLD, 14);  
      
      
    /**
     * Creates a bar chart. The chart object returned by this method uses a 
     * CategoryPlot instance as the plot, with a CategoryAxis for the domain axis, 
     * a NumberAxis as the range axis, and a BarRenderer as the renderer.

        Parameters:
                    title : the chart title (null permitted).
                    categoryAxisLabel : the label for the category axis (null permitted).
                    valueAxisLabel : the label for the value axis (null permitted).
                    dataset : the dataset for the chart (null permitted).
                    orientation : the plot orientation (horizontal or vertical) (null not permitted).
                    legend : a flag specifying whether or not a legend is required.
                    tooltips : configure chart to generate tool tips?
                    urls : configure chart to generate URLs?
        Returns:
                    A bar chart.  
     */
    JFreeChart localJFreeChart = 
        ChartFactory.createBarChart("注册会员统计", 
                                    "( 月份 )", 
                                    "数量", 
                                    paramCategoryDataset, 
                                    PlotOrientation.VERTICAL,
                                    false, 
                                    true, 
                                    false);
    
    
    //localJFreeChart.setBackgroundImage();//设置背景图片
    localJFreeChart.setTitle(new TextTitle(localJFreeChart.getTitle().getText(), fontTitle));//设置标题字体样式
    
    
    /**
     * Returns the plot for the chart. 
     * The plot is a class responsible for coordinating the visual representation of the data, 
     * including the axes (if any).
     =============================================================================================*/
    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
    
    
    
    
    /**
     * Returns the range axis for the plot. 
     * If the range axis for this plot is null, then the method will return the parent plot's 
     * range axis (if there is a parent plot).
     =============================================================================================*/
    NumberAxis localNumberAxis = (NumberAxis)localCategoryPlot.getRangeAxis();
    
    /**Sets the source for obtaining standard tick units for the axis,
     * and sends an AxisChangeEvent to all registered listeners. */
    localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    localNumberAxis.setUpperMargin(0.15D);
    localNumberAxis.setLabelFont(font);
    
    
    
    
    
    /**
     * Returns a reference to the renderer for the plot.
     * =============================================================================================*/     
    CategoryItemRenderer localCategoryItemRenderer = localCategoryPlot.getRenderer();    
    localCategoryItemRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    
    /**Sets a flag that controls the visibility of the item labels for a series.
     */
    localCategoryItemRenderer.setSeriesItemLabelsVisible(0, Boolean.TRUE); //设置柱体顶部显示数字
    localCategoryItemRenderer.setSeriesPaint(0, new Color(0,128,128));     //设置柱体颜色
    
    
    
    
    
    
    /**
     * Returns the domain axis for the plot. 
     * If the domain axis for this plot is null, then the method will return the parent 
     * plot's domain axis (if there is a parent plot).
     =============================================================================================*/
    
    
    
    CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis();    
    localCategoryAxis.setLabelFont(font); //设置X轴标志字体
    //localCategoryAxis.setAxisLinePaint();
    //localCategoryAxis.setAxisLineStroke(stroke);
    // localCategoryAxis.setCategoryLabelPositionOffset(0);//设置类别标签距离X轴的高度
    localCategoryAxis.setUpperMargin(0.10);   //设置右边距
    localCategoryAxis.setCategoryMargin(0.55);//设置每个柱体的宽度(0.70 表示: 百分比)
    localCategoryAxis.setTickLabelFont(font);//设置X轴上的分类字体
    localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);//设置X轴上的分类倾斜的角度
    
 
    
    return localJFreeChart;
  }

  public static JPanel createDemoPanel()
  {
    JFreeChart localJFreeChart = createChart(createDataset());
    return new ChartPanel(localJFreeChart);
  }

  public static void main(String[] paramArrayOfString)
  {
    BarChartDemo8 localBarChartDemo8 = new BarChartDemo8("JFreeChart: BarChartDemo8.java");
    localBarChartDemo8.pack();
    RefineryUtilities.centerFrameOnScreen(localBarChartDemo8);
    localBarChartDemo8.setVisible(true);
  }
}

















  • jfreechar 柱状图
            
    
    博客分类: java语言 jfreechart柱状图 
  • 大小: 13.3 KB