日期:2014-05-17  浏览次数:20856 次

JFreechart生成柱状图,怎么更改柱子的颜色
我用JFreechart生成了张柱状图(JFreeChart chart = ChartFactory.createBarChart3D(......)),如下所示,



默认颜色是红色,怎么将红色更改,变成别的颜色,或者自己设定的颜色。


谢谢,大侠!!!


------解决方案--------------------
抄了段代码
Java code
     chart.setBackgroundPaint(Color.lightGray);

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setNoDataMessage("NO DATA!");

        final CategoryItemRenderer renderer = new CustomRenderer(
            new Paint[] {Color.red, Color.blue, Color.green,
                Color.yellow, Color.orange, Color.cyan,
                Color.magenta, Color.blue}
        );
//        renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
        renderer.setItemLabelsVisible(true);
        final ItemLabelPosition p = new ItemLabelPosition(
            ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 45.0
        );
        renderer.setPositiveItemLabelPosition(p);
        plot.setRenderer(renderer);

------解决方案--------------------
很清晰啦
import java.io.*;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import java.awt.Color;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import java.awt.Font;
import org.jfree.chart.axis.AxisLocation;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.Dimension;
/**
* Title: Java开发手册
*
* Description: 适合中高级使用者
*
* Copyright: Copyright (c) 2007
*
* Company: lingbrother
*
* @author lingbrother
* @version 1.0
*/
public class BarChartDemo {
public BarChartDemo() {
super();
}

public static void main(String[] args) throws IOException{
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D(
"招生信息总览", // 图表标题
"应报与实报对照", // 目录轴的显示标签
"人数", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
true // 是否生成URL链接
);
CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象
//设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
org.jfree.chart.axis.CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%
domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10
domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)
domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%
//domainAxis.setMaximumCategoryLabelLines(1);
//domainAxis.setMaximumCategoryLabelWidthRatio(0);

//设定柱子的属性
org.jfree.chart.axis.ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)

//设置图表的颜色
org.jfree.chart.renderer.category.BarRenderer3D renderer;
renderer = new org.jfree.chart.renderer.category.BarRenderer3D();
renderer.setBaseOutlinePain