【问题标题】:how to change pie chart colors of JFreeChart?如何更改 JFreeChart 的饼图颜色?
【发布时间】:2012-10-12 03:21:28
【问题描述】:

如何自定义 JFreeChart 图形的颜色。 让我们看看我的java代码:

private StreamedContent chartImage ;

public void init(){
    JFreeChart jfreechart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
    File chartFile = new File("dynamichart");
    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
    chartImage = new DefaultStreamedContent(new FileInputStream( chartFile), "image/png");
}

public PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
          dataset.setValue("J-2", 10);
          dataset.setValue("J-1", 15);
          dataset.setValue("J", 50);
          dataset.setValue("J+1", 20);
          dataset.setValue("J+2", 15);
    return dataset;
}

html页面:

<p:graphicImage id="MyImage" value="#{beanCreateImage.chartImage}" />

【问题讨论】:

  • 你想改变每个饼的颜色还是什么?
  • 您想要究竟实现什么?如果你不说你想要什么,人们几乎不可能给出正确的答案。 (要馅饼不一定能得到苹果派,馅饼很多。);-)
  • 我想改变每个馅饼的颜色。谢谢
  • @berber5 那我的回答应该能帮到你。

标签: jfreechart


【解决方案1】:

你可以像这样改变单件的颜色:

JFreeChart chart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);
plot.setSectionPaint("J-1", new Color(120, 0, 120));
// or do this, if you are using an older version of JFreeChart:
//plot.setSectionPaint(1, Color.black);
//plot.setSectionPaint(3, new Color(120, 0, 120));

因此,使用您的代码,所有饼图都会自动着色,在我的代码更改后,J-1J+1 具有固定颜色,其余部分会自动着色。

【讨论】:

  • 如何查看new Color(227, 26, 28)的结果,
  • 如果plot.setSectionPaint(String id, Color col) 不存在,您使用的是相当旧的 JFreeChart 版本。是的,然后您可以简单地使用索引访问各个部分(在较新版本的 JFreeChart 中已弃用)。
  • 查看'new Color(227, 26, 28)'的结果是什么意思?你的意思是像Color Picker
  • 我发现 new Color(int x, int y, int z) 的意思是:x : red, y: green and z: blue。
  • 嗯,好的,你可以在documentation 中查找这些内容。如果您使用的是 Eclipse 或 netbeans 等较新的 IDE,您可以将鼠标悬停在代码上(在本例中为 Color),您将看到更多信息。
【解决方案2】:

要为图表设置颜色,您可以实现DrawingSupplier 接口,在这种情况下我使用了DefaultDrawingSupplier

public class ChartDrawingSupplier extends DefaultDrawingSupplier  {

    public Paint[] paintSequence;
    public int paintIndex;
    public int fillPaintIndex;

    {
        paintSequence =  new Paint[] {
                new Color(227, 26, 28),
                new Color(000,102, 204),
                new Color(102,051,153),
                new Color(102,51,0),
                new Color(156,136,48),
                new Color(153,204,102),
                new Color(153,51,51),
                new Color(102,51,0),
                new Color(204,153,51),
                new Color(0,51,0),
        };
    }

    @Override
    public Paint getNextPaint() {
        Paint result
        = paintSequence[paintIndex % paintSequence.length];
        paintIndex++;
        return result;
    }


    @Override
    public Paint getNextFillPaint() {
        Paint result
        = paintSequence[fillPaintIndex % paintSequence.length];
        fillPaintIndex++;
        return result;
    }   
}

然后将此代码包含在您的 `init()' 方法中

JFreeChart jfreechart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
Plot plot = jfreechart.getPlot();
plot.setDrawingSupplier(new ChartDrawingSupplier());
...

【讨论】:

  • 如何定义Color(227, 26, 28)的颜色?
  • @berber5 new Color(227, 26, 28) 只是一种通过设置红色、绿色和蓝色值来定义颜色的方法。你应该选择一组适合你的应用的颜色,你可以找到一些例子herehere
  • +1 有趣的是,我不知道“DrawingSupplier”类。虽然在他的情况下,我更喜欢我的方式。
  • 您可能需要考虑使用 DefaultDrawingSupplier 构造函数并调用 super(),因为您可以提供该 Paint 序列,而无需自己重写任何方法。
【解决方案3】:

您可以在从数据集中获取数据的同时根据标签自定义颜色:

// Add custom colors
        PiePlot plot = (PiePlot) chart.getPlot();

        for (int i = 0; i < dataset.getItemCount(); i++) {
            if(dataset.getKey(i).equals("J+1")){ 
                plot.setSectionPaint(i, Color.black);
            }
        }

您也可以使用 switch-case 语句或您喜欢的语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 2017-03-25
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多