如何用不同的颜色绘制不同的条形,我尝试使用渲染器,这是我的示例代码:
public IntervalXYDataset createDataset() throws InterruptedException {
parseFile();
final XYSeries series = new XYSeries("Analysis");
int i=0;
while(parsedArray[i]!=0)
{
series.add(xaxisArray[i], yaxisArray[i]);
i++;
}
final XYSeriesCollection dataset = new XYSeriesCollection(series);
dataset.setIntervalWidth(0.15);//set width here
return dataset;
}
这就是我绘制图表的方式:
public className (final String title) throws InterruptedException {
super(title);
IntervalXYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRenderer().setSeriesPaint( 0, Color.black);//0 works and paints all 40 bars in black, 1 and above fails.
// plot.getRenderer().setSeriesPaint( 1, Color.green);// this fails
chartPanel.setPreferredSize(new java.awt.Dimension(2000,1000));//(width,height) of display
setContentPane(chartPanel);
}
我可以按照我在程序中评论的那样设置宽度,但是我现在想要设置不同条形的颜色,例如我想抓住图表中的条形并为 array[0] 绘制红色,为 [ 绘制蓝色3] 和橙色细胞[17],你能指导我吗?非常感谢。
您想要执行的操作如下:
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRenderer().setSeriesPaint(1, Color.yellow);
将 1 替换为您要更改其颜色的条形的索引(从零开始)。
编辑以回复评论:
List<Color> myBarColors = .....
XYPlot plot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = plot.getRenderer();
for (int i = 0; i < 40; i++) {
renderer.setSeriesPaint(i, myBarColors.get(i));
}
编辑2:误解OP问题,评论中的新解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)