【发布时间】:2011-11-17 04:22:15
【问题描述】:
如何用不同的颜色绘制不同的条,我尝试使用渲染器,这是我的示例代码:
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);
}
我可以设置我在程序中评论的宽度,但是我现在想为不同的条设置颜色,例如我想在图表中获取条并为数组 [0] 绘制红色和[3] 为蓝色,单元格 [17] 为橙色,请您指导我。非常感谢。
【问题讨论】:
标签: java charts jfreechart bar-chart