java – 在XYJfree图表中自定义条形颜色
发布时间:2020-12-15 08:43:36 所属栏目:Java 来源:网络整理
导读:如何绘制不同颜色的不同条形图,我尝试使用渲染器,这里是我的示例代码: public IntervalXYDataset createDataset() throws InterruptedException { parseFile(); final XYSeries series = new XYSeries("Analysis"); int i=0; while(parsedArray[i]!=0) { se
如何绘制不同颜色的不同条形图,我尝试使用渲染器,这里是我的示例代码:
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],请你指导我.非常感谢你. 解决方法
你想要做的是以下内容:
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:误解了OPs问题,评论中的新解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |