JFreeChart柱状图单组柱子的不同颜色显示
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", 500, 400); } // 保存为文件 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(255, 255, 204)); // 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[][] { { 25, 24, 40, 12, 33, 33 } }; String[] rowKeys = { "" }; String[] columnKeys = { "张三", "李四", "王五", "马六", "陈七", "赵八" }; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); return dataset; } } |
JFreeChart柱状图单组柱子的不同颜色显示的更多相关文章
- jfreeChart柱状图各属性详细设置
一. 下载与环境配置 此最新版本为 1.0.13 解压jfreechart-1.0.13.zip 将lib目录下的jfreechart-1.0.13.jar .jcommon-1.0.16.jar 复 ...
- echarts柱状图每个柱子显示不同颜色,并且能够实现点击每种颜色影藏对应柱子的功能
---------------------------------------------------------代码区---------------------------------------- ...
- highcharts 柱状图在柱子顶部显示y轴数据
var plotOptions={ column:{ //borderColor: "#CCCC66",//边框 shadow: true, //阴影 dataLabels:{ / ...
- ECharts在柱状图的柱子上方显示数量的方法
在setOption()方法中的series配置中加上itemStyle配置 如下: series: [{ name: '人数', type: 'bar', data: [], //x轴对应列的值 i ...
- highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小 大] | ...
- ChartControl 折线图 柱状图
添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...
- Echarts堆积柱状图排序问题
Echarts堆积柱状图排序是按照堆积柱状图的柱子高度进行从大到小(或者从小到大)进行排序,方便查阅各坐标情况.以下是我自己研发的方法,有不对的地方敬请谅解,随时欢迎指教. 排序后效果如下图: (1) ...
- Echart、Excel、highcharts、jfreechart对比
Echart Excel highcharts jfreechart 柱状图 √ √ √ √ 条形图 √ √ √ √ 折线图 √ √ √ √ 面积图 √ √ √ √ 散点图 √ √ √ √ 气泡图 ...
- echarts细节的修改(2):矩形数图,柱状图,折线图,雷达图等
1.矩形数图的配置,是直接拿饼图的配置 然后将type换成treemap. 修改类型 option.series.type = 'treemap'; 关闭面包屑导航 option.series.bre ...
随机推荐
- Sphinx coreseek 3.2
功能 中文的拆词索引 MySQL中like模糊查询. >1>5>%可以用索引 配置 用编辑器打开 path 配置 改绝对路径 https://blog.csdn.net/u013 ...
- [PHP] 算法-复制复杂链表的PHP实现
复杂链表的复制: 1.在旧链表中每个结点的后面复制出一个结点,隔代 2.把旧链表的随机指向部分,复制到新添加的结点上 3.把新结点从旧链表中拆分出来成新链表 1. linklist=head whil ...
- Moco服务器jar包实现简易的API搭建
永远不要停止前进的脚步,就像你不会忘记以前那些窘迫的连一无所有都称不上的裸露的记忆一样.追求永远的打怪升级,武装自己.双手的努力让曾经那些不堪的记忆在时间的长河中渐渐风化隐匿,但请不要忘记它留下的那一 ...
- JavaSE Map集合
Map集合 在Map集合中保存的数据为一组数据,其中:一个数据为key,另外一个数据为value.而key和value具备对应的关系,在集合中它们属于一组(一对)数据.而每个key只能对应唯一的一个v ...
- 【nginx】详细配置说明
———————————————————————相关文章———————————————————————————— Centos之安装Nginx及注意事项 [Linux]nginx常用命令 ——————— ...
- 【Java并发编程】11、volatile的使用及其原理
一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...
- 服务器端事件发送SSE
背景 近期有这么一个需求: 手机端需要展示一个比较大的pdf 基于手机端网络/流量/体验等考虑,希望不通过pdf下载然后展示 而是把pdf转成一张张的图片,然后再在手机上展示. 分析 pdf转图片,肯 ...
- How to distinguish between strings in heap or literals?
Question: I have a use case where I can get pointers of strings allocated either in memory or litera ...
- What are the differences between a pointer variable and a reference variable in C++?
Question: I know references are syntactic sugar, so code is easier to read and write. But what are t ...
- Fundebug是这样备份数据的
摘要: 数据还是要备份的,万一删库了呢? 本文代码仓库: fundebug-mongodb-backup 引言 今年8月,腾讯云竟然把客户前沿数据的数据弄没了,Fundebug在第一时间进行了一些简单 ...