JfreeChart实现柱状图的每根柱子颜色不同

          前段时间项目中遇到过这种情况,在此通过两种方式实现了每根柱子颜色不同。

           第一种方式:参照了http://blog.csdn.net/peterwanghao/article/details/7055967提供的方法。这里就不在献丑了,但是这种方式在使用的时候会遇到一种情况,就是柱状图的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;
    }


图一:                                                                                                   


图二:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值