JFreeChart柱状图单组柱子的不同颜色显示

来源:互联网 发布:生死狙击改枪软件 编辑:程序博客网 时间:2024/04/28 07:55

JFreeChart柱状图中单组柱子用不同颜色来显示的实现方法是自定义一个Renderer来继承BarRenderer类,然后重载getItemPaint(int i,int j)方法。

实现效果如下:

实现代码如下:

[html] view plaincopy
  1. public class CustomRenderer extends org.jfree.chart.renderer.category.BarRenderer {  
  2.   
  3.     /**  
  4.      *   
  5.      */  
  6.     private static final long serialVersionUID = 784630226449158436L;  
  7.     private Paint[] colors;  
  8.     //初始化柱子颜色  
  9.     private String[] colorValues = { "#AFD8F8", "#F6BD0F", "#8BBA00", "#FF8E46", "#008E8E", "#D64646" };  
  10.   
  11.     public CustomRenderer() {  
  12.         colors = new Paint[colorValues.length];  
  13.         for (int i = 0; i < colorValues.length; i++) {  
  14.             colors[i] = Color.decode(colorValues[i]);  
  15.         }  
  16.     }  
  17.   
  18.     //每根柱子以初始化的颜色不断轮循  
  19.     public Paint getItemPaint(int i, int j) {  
  20.         return colors[j % colors.length];  
  21.     }  
  22. }  

[html] view plaincopy
  1. public class CreateJFreeChartBarColor {  
  2.   
  3.     /**  
  4.      * 创建JFreeChart Bar Chart(柱状图)  
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // 步骤1:创建CategoryDataset对象(准备数据)  
  8.         CategoryDataset dataset = createDataset();  
  9.         // 步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置  
  10.         JFreeChart freeChart = createChart(dataset);  
  11.         // 步骤3:将JFreeChart对象输出到文件,Servlet输出流等  
  12.         saveAsFile(freeChart, "E:\\bar.png", 500, 400);  
  13.     }  
  14.   
  15.     // 保存为文件  
  16.     public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {  
  17.         FileOutputStream out = null;  
  18.         try {  
  19.             File outFile = new File(outputPath);  
  20.             if (!outFile.getParentFile().exists()) {  
  21.                 outFile.getParentFile().mkdirs();  
  22.             }  
  23.             out = new FileOutputStream(outputPath);  
  24.             // 保存为PNG文件  
  25.             ChartUtilities.writeChartAsPNG(out, chart, weight, height);  
  26.             out.flush();  
  27.         } catch (FileNotFoundException e) {  
  28.             e.printStackTrace();  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         } finally {  
  32.             if (out != null) {  
  33.                 try {  
  34.                     out.close();  
  35.                 } catch (IOException e) {  
  36.                     // do nothing  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     // 根据CategoryDataset生成JFreeChart对象  
  43.     public static JFreeChart createChart(CategoryDataset categoryDataset) {  
  44.         JFreeChart jfreechart = ChartFactory.createBarChart("学生统计图", // 标题  
  45.                 "学生姓名", // categoryAxisLabel (category轴,横轴,X轴的标签)  
  46.                 "年龄", // valueAxisLabel(value轴,纵轴,Y轴的标签)  
  47.                 categoryDataset, // dataset  
  48.                 PlotOrientation.VERTICAL, false, // legend  
  49.                 false, // tooltips  
  50.                 false); // URLs  
  51.   
  52.         Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);  
  53.   
  54.         jfreechart.setTextAntiAlias(false);  
  55.         jfreechart.setBackgroundPaint(Color.white);  
  56.   
  57.         CategoryPlot plot = jfreechart.getCategoryPlot();// 获得图表区域对象  
  58.   
  59.         // 设置横虚线可见  
  60.         plot.setRangeGridlinesVisible(true);  
  61.         // 虚线色彩  
  62.         plot.setRangeGridlinePaint(Color.gray);  
  63.         // 数据轴精度  
  64.         NumberAxis vn = (NumberAxis) plot.getRangeAxis();  
  65.         // vn.setAutoRangeIncludesZero(true);  
  66.         DecimalFormat df = new DecimalFormat("#0.0");  
  67.         vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式  
  68.   
  69.         // x轴设置  
  70.         CategoryAxis domainAxis = plot.getDomainAxis();  
  71.         domainAxis.setLabelFont(labelFont);// 轴标题  
  72.         domainAxis.setTickLabelFont(labelFont);// 轴数值  
  73.         // Lable(Math.PI/3.0)度倾斜  
  74.         // domainAxis.setCategoryLabelPositions(CategoryLabelPositions  
  75.         // .createUpRotationLabelPositions(Math.PI / 3.0));  
  76.         domainAxis.setMaximumCategoryLabelWidthRatio(6.00f);// 横轴上的 Lable  
  77.         // 是否完整显示  
  78.   
  79.         // 设置距离图片左端距离  
  80.         domainAxis.setLowerMargin(0.1);  
  81.         // 设置距离图片右端距离  
  82.         domainAxis.setUpperMargin(0.1);  
  83.         // 设置 columnKey 是否间隔显示  
  84.         // domainAxis.setSkipCategoryLabelsToFit(true);  
  85.         plot.setDomainAxis(domainAxis);  
  86.         // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)  
  87.         plot.setBackgroundPaint(new Color(255, 255, 204));  
  88.   
  89.         // y轴设置  
  90.         ValueAxis rangeAxis = plot.getRangeAxis();  
  91.         rangeAxis.setLabelFont(labelFont);  
  92.         rangeAxis.setTickLabelFont(labelFont);  
  93.         // 设置最高的一个 Item 与图片顶端的距离  
  94.         rangeAxis.setUpperMargin(0.15);  
  95.         // 设置最低的一个 Item 与图片底端的距离  
  96.         rangeAxis.setLowerMargin(0.15);  
  97.         plot.setRangeAxis(rangeAxis);  
  98.   
  99.         // 解决中文乱码问题(关键)  
  100.         TextTitle textTitle = jfreechart.getTitle();  
  101.         textTitle.setFont(new Font("黑体", Font.PLAIN, 20));  
  102.         domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
  103.         domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  104.         vn.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
  105.         vn.setLabelFont(new Font("黑体", Font.PLAIN, 12));  
  106.         // jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
  107.   
  108.         // 使用自定义的渲染器  
  109.                 CustomRenderer renderer = new CustomRenderer();  
  110.         // 设置柱子宽度  
  111.         renderer.setMaximumBarWidth(0.2);  
  112.         // 设置柱子高度  
  113.         renderer.setMinimumBarLength(0.2);  
  114.         // 设置柱子边框颜色  
  115.         renderer.setBaseOutlinePaint(Color.BLACK);  
  116.         // 设置柱子边框可见  
  117.         renderer.setDrawBarOutline(true);  
  118.         // 设置每个地区所包含的平行柱的之间距离  
  119.         renderer.setItemMargin(0.5);  
  120.         jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);  
  121.         // 显示每个柱的数值,并修改该数值的字体属性  
  122.         renderer.setIncludeBaseInRange(true);  
  123.         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  124.         renderer.setBaseItemLabelsVisible(true);  
  125.         plot.setRenderer(renderer);  
  126.         // 设置柱的透明度  
  127.         plot.setForegroundAlpha(1.0f);  
  128.   
  129.         // 背景色 透明度  
  130.         plot.setBackgroundAlpha(0.5f);  
  131.   
  132.         return jfreechart;  
  133.     }  
  134.   
  135.     // 创建CategoryDataset对象  
  136.     public static CategoryDataset createDataset() {  
  137.         double[][] data = new double[][] { { 25, 24, 40, 12, 33, 33 } };  
  138.         String[] rowKeys = { "" };  
  139.         String[] columnKeys = { "张三", "李四", "王五", "马六", "陈七", "赵八" };  
  140.         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);  
  141.         return dataset;  
  142.     }  
  143.   
  144. }  
0 0
原创粉丝点击