日期:2014-05-19  浏览次数:20793 次

用jfreechart作柱状图柱子的颜色问题
请问怎么用jfreechart做柱状图时不同柱子的颜色怎么设置,要每个柱子颜色不同,不是用renderer.setSeriesPaint(0, new Color.RED)设置每个类型的柱子的,谢谢

------解决方案--------------------
请参考代码,自己看看吧
Java code
import java.awt.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.*;

public class BarChartDemo3 extends ApplicationFrame {
  static class CustomRenderer extends BarRenderer {
    private Paint colors[];

    public Paint getItemPaint(int i, int j) {
      return colors[j % colors.length];
    }

    public CustomRenderer(Paint apaint[]) {
      colors = apaint;
    }
  }

  public BarChartDemo3(String s) {
    super(s);
    CategoryDataset categorydataset = createDataset();
    JFreeChart jfreechart = createChart(categorydataset);
    ChartPanel chartpanel = new ChartPanel(jfreechart);
    chartpanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(chartpanel);
  }

  private static CategoryDataset createDataset() {
    double ad[][] = { { 4D, 3D, -2D, 3D, 6D } };
    return DatasetUtilities.createCategoryDataset("Series ", "Category ", ad);
  }

  private static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart ){
                wit