1

I can't find it in Google so I ask here. How I can set color for bar dependent of value PrimeFaces BarChart. For example if I have value less than 50% bar is red. Maybe someone have similar problem and can send me a solution

DanteVoronoi
  • 1,133
  • 1
  • 13
  • 20
  • You can also set colors through [script](http://www.jqplot.com/examples/multipleBarColors.php). Just add `model.setExtender("chartExtender");`. And create js function `function chartExtender() {seriesColors:[]// multiple color value from bean method}` – Hiren Nov 29 '16 at 10:21

1 Answers1

0

I used primefaces some time ago and from what i remember, I had a situation similar to yours.

What you can do is, set the color dynamically, for example in the bean you can have your object BarChartModel which has the property setSeriesColors(String seriesColors). Don't know what 'value' is in your example but anything works, and it's similar to what I am explaining.

In the bean you would have the getBarColor(value) function in which you decide, depending on the value what color string to return. This way you can implement any logic you want and how many colors you want for example: under 25% - red, 25-75% - yellow, over 75% - green.

public String getBarColor(int value){
    String color="#006400" //default color

    if (value < 25) {
        color="#8B0000";
    } else if (value >= 25 && value <= 75) {
        color="#228B22";
    } else {
        color="#FFD700";
    }

    return color;
}

Hope it helps. I think you can adapt this to your situation.

Raul Cuth
  • 2,269
  • 1
  • 12
  • 17
  • PrimeFaces charts consist of canvas elements, you cannot style them using the style attribute. – Jasper de Vries Nov 28 '16 at 12:14
  • See http://stackoverflow.com/questions/23846494/customize-primefaces-chart – Jasper de Vries Nov 28 '16 at 12:17
  • @JasperdeVries so this is impossible to change this color? – DanteVoronoi Nov 28 '16 at 15:03
  • At least not using this answer. You need to create an `extender` to customize your graph. I myself never worked with it. I prefer to use Google Charts. – Jasper de Vries Nov 28 '16 at 15:19
  • @JasperdeVries I know about `extender` but I can't find example or something similar with my problem. – DanteVoronoi Nov 28 '16 at 15:35
  • I got some time to try and think about this problem some more and i found that in the bean, on your BarChartModel object, you can actually do barChart.setSeriesColors(String seriesColors). and here you could integrate the function of getBarColor somehow, according to your needs. At least i hope so. Hope this better suits your situation. – Raul Cuth Nov 28 '16 at 16:27