Java JFreeChart 条形图自定义颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24555380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:58:03  来源:igfitidea点击:

JFreeChart Bar chart custom color?

javaswingchartsjfreechartbar-chart

提问by user3794422

I am using JFreeChartsin java to create a bar chart. My question is fairly simple... how can I choose a custom color for all of the bars in a bar chart? I'm not sure if this customization would be done in a GradientPaint. An example of my code that determines bar color is:

JFreeCharts在 java 中使用来创建条形图。我的问题很简单……如何为条形图中的所有条形选择自定义颜色?我不确定此自定义是否会在GradientPaint. 确定条形颜色的代码示例是:

   final GradientPaint gp0 = new GradientPaint(
                    0.0f, 0.0f, Color.blue, 
                    0.0f, 0.0f, Color.blue
                );

I'm not sure if this is the right way to go for custom colors or not. Basically, I don't know if GradientPaintis the right way to go or not. If it is, could someone let me know how I could edit this code to make it a custom color rather than blue?

我不确定这是否是自定义颜色的正确方法。基本上,我不知道GradientPaint走的路是否正确。如果是,有人可以让我知道如何编辑此代码以使其成为自定义颜色而不是蓝色吗?

I'm not sure if this helps, but say the information for the custom color was

我不确定这是否有帮助,但说自定义颜色的信息是

  • hue: 142
  • Sat: 109
  • Lum:126
  • Red: 79
  • Green: 129
  • Blue: 189
  • 色相:142
  • 周六:109
  • 亮度:126
  • 红色:79
  • 绿色:129
  • 蓝色:189

With this is there a way to customize the color of the chart?

有了这个,有没有办法自定义图表的颜色?

采纳答案by Tomas Bisciak

It was a while since i coded with jfreechart.Bud if i remember corectly this was code that i wrote to change bar paint ;).

自从我用 jfreechart.Bud 编码以来已经有一段时间了,如果我记得正确的话,这是我为更改条形图而编写的代码;)。

    CategoryPlot cplot = (CategoryPlot)chart.getPlot();
    cplot.setBackgroundPaint(SystemColor.inactiveCaption);//change background color

    //set  bar chart color

    ((BarRenderer)cplot.getRenderer()).setBarPainter(new StandardBarPainter());

    BarRenderer r = (BarRenderer)chart.getCategoryPlot().getRenderer();
    r.setSeriesPaint(0, Color.blue);

Im looking at the code for my first application ever written.Im not sure if it will work now.

我正在查看我编写的第一个应用程序的代码。我不确定它现在是否可以工作。

For future i recommend to google out or purchase PDF guide to jfreechart.You find all the references and samples there.Bud if you can ,skip to JavaFX i strongly recommend it ,working with jfreechart is pain.To be honest.Implementing charts in javafx is easy and looks way better ;)

对于未来,我建议谷歌搜索或购买 jfreechart 的 PDF 指南。你可以在那里找到所有参考资料和示例。如果可以的话,跳到 JavaFX 我强烈推荐它,使用 jfreechart 是痛苦的。老实说。在 javafx 中实现图表很简单,看起来更好;)

回答by bradykey

CategoryPlot plot = chart.getCategoryPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();

// set the color (r,g,b) or (r,g,b,a)
Color color = new Color(79, 129, 189);
renderer.setSeriesPaint(0, color);

This will set all bars to that specific color. If you would like the colors to change for each row (say, for a stacked bar chart), you can call dataset.getRowCount(), with dataset being of type CategoryDataset, to return to you the number of rows involved for each column of the bar chart. Then, you could index the series in the renderer.setSeriesPaint()call based on the index of the row.

这会将所有条设置为该特定颜色。如果您希望改变每一行的颜色(例如,对于堆积条形图),您可以调用dataset.getRowCount(),数据集的类型为CategoryDataset,以返回条形图每列涉及的行数。然后,您可以renderer.setSeriesPaint()根据行的索引对调用中的系列进行索引。

for (int i = 0; i < dataset.getRowCount(); i++){
    switch (i) {
    case 0:
        // red
        color = new Color(255, 0, 0);
        break;
    case 1:
        // blue
        color = new Color(0, 0, 255);
        break;
    default:
        // green
        color = new Color(0, 255, 0);
        break;
    }
}

回答by Chetan Bhagat

Custom Colors in Bar Chart using JfreeChart

使用JfreeChart在条形图中自定义颜色

CategoryItemRenderer barColor = new CustomRenderer(new Paint[]{});
plot.setRenderer(barColor);

create a new class name is CustomRenderer extends BarRenderer3Dor you choose BarRenderer

创建一个新的类名是CustomRenderer extends BarRenderer3D或者你选择BarRenderer

class CustomRenderer extends BarRenderer3D {

    private Paint[] colors;
    public CustomRenderer(final Paint[] colors) {
        this.colors = colors;
    }

    public Paint getItemPaint(final int row, final int column) {
        if(column==0)
            return Color.blue;
        else if(column==1)
            return Color.CYAN;
        else  
            return Color.RED;
   }
}

回答by Osanda Deshan

I think easiest way is using getRenderer().setSeriesPaint(index, color)method.

我认为最简单的方法是使用getRenderer().setSeriesPaint(index, color)方法。

So as an example you can try the below code for a bar chart which has 3 bars grouped.

因此,作为示例,您可以尝试使用以下代码来创建一个包含 3 个条形图的条形图。

JFreeChart barChart = ChartFactory.createBarChart(
                "Bar Chart Titke",
                "Category", "Score",
                dataset,PlotOrientation.HORIZONTAL,
                true, true, false);

        CategoryPlot plot = barChart.getCategoryPlot();
        plot.getRenderer().setSeriesPaint(0, new Color(128, 0, 0));
        plot.getRenderer().setSeriesPaint(1, new Color(0, 0, 255));
        plot.getRenderer().setSeriesPaint(2, new Color(0, 230, 255));