JFreeChart Custom Color Chart?

I use JFreeChartsin java to create a histogram. My question is quite simple ... how can I choose my own color for all bars in the histogram? I am not sure if this setting will be done in GradientPaint. An example of my code that defines the color of a bar is:

   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 for custom colors or not. Basically, I don’t know if there is a GradientPaintright way or not. If so, can anyone let me know how I can edit this code to make it a special color, not blue?

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

  • shade: 142
  • Sat: 109
  • Lum: 126
  • Red: 79
  • Green: 129
  • Blue: 189

?

+4
3

, 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);

, - . , .

google PDF- jfreechart. . , JavaFX, , jfreechart - . . javafx ;)

+7
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);

, . , (, ), 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;
    }
}
+4

Custom colors on a chart using JfreeChart

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

create a new class name CustomRenderer extends BarRenderer3Dor selectBarRenderer

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;
   }
}
+1
source

Source: https://habr.com/ru/post/1547019/


All Articles