简体   繁体   English

Jfreechart中的动态饼图-如何设置颜色

[英]Dynamic Pie chart in Jfreechart - how to set colours

I am trying to plot a dynamic pie chart using JFreeChart. 我正在尝试使用JFreeChart绘制动态饼图。 I do not know how many sections will be present in advance. 我不知道事先会出现多少节。 I receive data every second which updates the graph. 我每秒都会收到更新图表的数据。

How can I set colour for each section of the chart ? 如何为图表的每个部分设置颜色? I do not wish to have the default colours. 我不希望使用默认颜色。

I have tried setting the DrawingSupplier. 我试过设置DrawingSupplier。 Doesn't work. 不起作用 Can anybody help ? 有人可以帮忙吗?

The Plot of the chart for pie charts can be cast to a PiePlot which lets you set the paint color, outline color, shadow color ect.. of each piece of the pie. Plot图表饼图可以转换为PiePlot它可以让你设置的油漆颜色,轮廓颜色,阴影颜色等..各分一杯羹的。 Heres a little sample of setting the colors for each piece of the pie. 以下是为每块馅饼设置颜色的一些示例。 If the number of pieces is greater than the amount of colors defined in our array then it will reuse the colors. 如果块数大于数组中定义的颜色数量,则它将重新使用颜色。

The setSectionPaint method takes any type of java.awt.Paint so you can also pass in gradient paints among other things. setSectionPaint方法采用任何类型的java.awt.Paint因此您还可以传递渐变颜料。

PieDataset dataset = ...
JFreeChart chart = ChartFactory.createPieChart("My Chart", dataset, false, true, false);

// all possible colors for each piece of the pie
Color[] colors = new Color[] { new Color(232, 124, 35),
        new Color(51, 109, 178), new Color(182, 52, 49),
        new Color(103, 131, 45), new Color(108, 77, 146),
        new Color(46, 154, 183), new Color(151, 64, 64) };

PiePlot plot = (PiePlot) chart.getPlot();
// set each sections inside paint
int i = 0;
for (Object key : dataset.getKeys()) {
    plot.setSectionPaint((Comparable) key, colors[i % colors.length]);
    i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM