Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义XYJfree图表中的条形图颜色_Java_Charts_Jfreechart_Bar Chart - Fatal编程技术网

Java 自定义XYJfree图表中的条形图颜色

Java 自定义XYJfree图表中的条形图颜色,java,charts,jfreechart,bar-chart,Java,Charts,Jfreechart,Bar Chart,如何用不同的颜色绘制不同的条,我尝试使用渲染器,以下是我的示例代码: public IntervalXYDataset createDataset() throws InterruptedException { parseFile(); final XYSeries series = new XYSeries("Analysis"); int i=0; while(parsedArray[i]!=0) { series.

如何用不同的颜色绘制不同的条,我尝试使用渲染器,以下是我的示例代码:

    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));
}
List myBarColors=。。。。。
XYPlot=(XYPlot)chart.getPlot();
XYItemRenderer=plot.getRenderer();
对于(int i=0;i<40;i++){
setSeriesPaint(i,myBarColors.get(i));
}

编辑2:被误解的OPs问题,注释中的新解决方案。

最灵活的方法是在自定义中覆盖
AbstractRenderer
的方法,如
XylineandShaperEnder
所示。我找到了答案
创建两个系列,然后添加所需的条数,并为每个系列设置颜色。使用setSeriesPaint

这是一种简单的方法;但是
Color.yellow
是第四种默认颜色,因此它在三个以上的系列中失败。您好,我尝试过这样做,它对值“0”起作用,就像在setSeriesPaint(0,Color.yellow)中一样;但是我有大约40条-这循环了40次:-(series.add(xaxisArray[I],yaxisArray[I]))你能帮我修复这个错误吗。我想为每个条设置颜色。非常感谢。@helloMaga我不知道你到底想做什么,但我更新了我的答案以显示循环中的设置颜色。你好,Jordan,请看我上面的问题,我已经声明了我的问题。@helloMaga好的,我又看了一眼原始帖子。我认为您对数据集使用了错误的类。我面前没有任何东西可以测试这一点,但请尝试使用CatagoricalDataset或XYBarDataset。如果您使用这些方法,那么setSeriesPoint()方法应该适用于您尝试执行的操作。