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

实现效果如下:

实现代码如下:

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

JFreeChart柱状图单组柱子的不同颜色显示的更多相关文章

  1. jfreeChart柱状图各属性详细设置

    一. 下载与环境配置 此最新版本为 1.0.13 解压jfreechart-1.0.13.zip 将lib目录下的jfreechart-1.0.13.jar .jcommon-1.0.16.jar 复 ...

  2. echarts柱状图每个柱子显示不同颜色,并且能够实现点击每种颜色影藏对应柱子的功能

    ---------------------------------------------------------代码区---------------------------------------- ...

  3. highcharts 柱状图在柱子顶部显示y轴数据

    var plotOptions={ column:{ //borderColor: "#CCCC66",//边框 shadow: true, //阴影 dataLabels:{ / ...

  4. ECharts在柱状图的柱子上方显示数量的方法

    在setOption()方法中的series配置中加上itemStyle配置 如下: series: [{ name: '人数', type: 'bar', data: [], //x轴对应列的值 i ...

  5. highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度

    highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小  大] | ...

  6. ChartControl 折线图 柱状图

    添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...

  7. Echarts堆积柱状图排序问题

    Echarts堆积柱状图排序是按照堆积柱状图的柱子高度进行从大到小(或者从小到大)进行排序,方便查阅各坐标情况.以下是我自己研发的方法,有不对的地方敬请谅解,随时欢迎指教. 排序后效果如下图: (1) ...

  8. Echart、Excel、highcharts、jfreechart对比

      Echart Excel highcharts jfreechart 柱状图 √ √ √ √ 条形图 √ √ √ √ 折线图 √ √ √ √ 面积图 √ √ √ √ 散点图 √ √ √ √ 气泡图 ...

  9. echarts细节的修改(2):矩形数图,柱状图,折线图,雷达图等

    1.矩形数图的配置,是直接拿饼图的配置 然后将type换成treemap. 修改类型 option.series.type = 'treemap'; 关闭面包屑导航 option.series.bre ...

随机推荐

  1. php 图像裁剪(自定义裁剪图片大小)

    <?php /** * 图像裁剪 * @param $title string 原图路径 * @param $content string 需要裁剪的宽 * @param $encode str ...

  2. node-webkit无边框窗口用纯JS实现拖动改变大小

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     & ...

  3. c#实战开发:以太坊钱包对接私链 (二)

    上一篇讲了 以太坊私链搭建 首先下载Ethereum Wallet 钱包 可以直接百度 下载如果直接打开它会默认连接公链 所以我们要通过命令打开 "F:\Program Files\Ethe ...

  4. Mapreduce的api编程

    KEYIN:输入的KEY是maptask所读取到的一行文本的起始偏移量,longVALUEIN:输入的VALUE的类型,输入的VALUE是maptask所读取到的一行文本内容,StringKEYOUT ...

  5. ios --键盘监听JYKeyBoardListener

    没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡 .h // // JYKeyBoardListener.h // // Created by JianF.Sun on 17/9/2 ...

  6. elasticsearch6.7 05. Document APIs(9)Bulk API

    8.Bulk API 可以把多个index或delete操作放在单个bulk API中执行.这样可以极大地提高索引速度. /_bulkAPI使用如下的JSON结构: action_and_meta_d ...

  7. maven配置之:<distributionManagement>snapshot快照库和release发布库

    在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次.我们知道,maven的依赖管理是基于版本管理的, ...

  8. java_查找里程

    题目内容: 下图为国内主要城市之间的公路里程: 你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程. 注意:任何两个城市之间的里程都已经给出,不需要计算经第三地中转. ...

  9. MinGW编译Mongo-CXX-Driver

    8. mongo-cxx-driver pacman -S mingw-w64-x86_64-cyrus-sasl pacman -S mingw-w64-x86_64-extra-cmake-mod ...

  10. 微信小程序-06-详解介绍.js 逻辑层文件-注册页面

    上一篇介绍的是 app.js 逻辑层文件中注册程序,对应的每个分页面都会有的 js 文件中 page() 函数注册页面 微信小程序-06-详解介绍.js 逻辑层文件-注册页面 宝典官方文档: http ...