JfreeChart实现柱状图的每根柱子颜色不同
2012-04-23 17:06
316 查看
前段时间项目中遇到过这种情况,在此通过两种方式实现了每根柱子颜色不同。
第一种方式:参照了/article/8346404.html提供的方法。这里就不在献丑了,但是这种方式在使用的时候会遇到一种情况,就是柱状图的label不太好控制,和柱子对不上。如图一。
第二种方式:修改 org.jfree.data.general.DatasetUtilities中的createCategoryDataset方法,如图二。修改如下
@SuppressWarnings("rawtypes")
public static CategoryDataset createCategoryDataset(Comparable[] columnKeys, double[][] data) {
if (columnKeys == null) {
throw new IllegalArgumentException("Null 'columnKeys' argument.");
}
if (ArrayUtilities.hasDuplicateItems(columnKeys)) {
throw new IllegalArgumentException(
"Duplicate items in 'columnKeys'.");
}
int columnCount = 0;
for (int r = 0; r < data.length; ++r) {
columnCount = Math.max(columnCount, data[r].length);
}
if (columnKeys.length != columnCount) {
throw new IllegalArgumentException(
"The number of column keys does not match the number of columns in the data array.");
}
DefaultCategoryDataset result = new DefaultCategoryDataset();
for (int c = 0; c < data[0].length; ++c) {
Comparable columnKey = columnKeys[c];
result.addValue(new Double(data[0][c]), columnKey, columnKey);
}
return result;
}
图一:
图二:
第一种方式:参照了/article/8346404.html提供的方法。这里就不在献丑了,但是这种方式在使用的时候会遇到一种情况,就是柱状图的label不太好控制,和柱子对不上。如图一。
第二种方式:修改 org.jfree.data.general.DatasetUtilities中的createCategoryDataset方法,如图二。修改如下
@SuppressWarnings("rawtypes")
public static CategoryDataset createCategoryDataset(Comparable[] columnKeys, double[][] data) {
if (columnKeys == null) {
throw new IllegalArgumentException("Null 'columnKeys' argument.");
}
if (ArrayUtilities.hasDuplicateItems(columnKeys)) {
throw new IllegalArgumentException(
"Duplicate items in 'columnKeys'.");
}
int columnCount = 0;
for (int r = 0; r < data.length; ++r) {
columnCount = Math.max(columnCount, data[r].length);
}
if (columnKeys.length != columnCount) {
throw new IllegalArgumentException(
"The number of column keys does not match the number of columns in the data array.");
}
DefaultCategoryDataset result = new DefaultCategoryDataset();
for (int c = 0; c < data[0].length; ++c) {
Comparable columnKey = columnKeys[c];
result.addValue(new Double(data[0][c]), columnKey, columnKey);
}
return result;
}
图一:
图二:
相关文章推荐
- JFreeChart柱状图单组柱子的不同颜色显示
- JFreeChart柱状图单组柱子的不同颜色显示
- JFreeChart柱状图单组柱子的不同颜色显示
- echarts柱状图每个柱子显示不同颜色,并且能够实现点击每种颜色影藏对应柱子的功能
- jfreechart 实现单组柱子不同颜色
- JFreeChart柱状图单组柱子的不同颜色显示
- Highcharts做柱状图怎样样每个柱子都是不同的颜色显示
- Highcharts做柱状图怎样样每个柱子都是不同的颜色显示
- 介绍一篇“让echarts柱状图的每个柱子显示不同的颜色”的好博客
- c#怎么实现chart柱状图的不同颜色
- (3)Jfreechart柱状图自定义柱子颜色以及柱子的方向
- Echarts柱状图的每个柱子显示不同颜色问题
- JFreeChart柱状图的颜色设置与宽度分配
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- devexpress实现单元格根据条件显示不同的样式(颜色、字体、对齐方式,大小等)
- Textview增加图片并实现点击与文字设置不同大小颜色
- echarts如何给柱形图的每个柱子设置不同颜色
- js slideToggle 的另一种实现方式,根据不同的状态添加选中颜色
- Flex中实现对一个text渲染不同的字体颜色示例
- 使用JFreeChart实现基于web的3D柱状图