本文介绍了如何更改JFreeChart的饼图颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how to customize the colors of JFreeChart graphic.lets see my java code :

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}" />

推荐答案

您可以像这样更改单件的颜色:

You can change the color of single pieces like this:

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具有固定的颜色,其余的将自动着色.

So with your code, all the pies are colored automatically, after my code changes, the J-1 and J+1 have a fixed color, the rest gets automatically colored.

这篇关于如何更改JFreeChart的饼图颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:58