Search code examples
primefacesjqplot

Different colors for one series bar in p:chart


I have trouble making this work.

I have one HorizontalBarChartModel with only one series. I tried some solutions found here but none worked for me.

This is the part of method that creates my chart on bean:

ChartSeries estadosSeries = new ChartSeries();
    for (EstadoIniciativaEnum estado : distinctEstados) {
        int value = getIniciativaService().countIniciativasByAnoAndEstadoAndEstrategica(ano, getOrganismoAtual(), estado, estrategica).intValue();
        estadosSeries.set(estado.getDescricao(), value);

        if (value > maxValue)
            maxValue = value;
    }

    if (distinctEstados.isEmpty()) {
        estadosSeries.set(getMessage("sgii.home.chart.empty"), 0);
    }

    horizontalBarModel.addSeries(estadosSeries);

    horizontalBarModel.setTitle(SGIIJsfUtil.getMessage(title));
    horizontalBarModel.setSeriesColors(getColorsByEstados(distinctEstados));
    horizontalBarModel.setExtender("chartExtender");

And this is my page:

 <p:chart id="idPieIniciativaEstado" type="bar" model="#{chartBean.horizontalBarModel}"
             style="width:400px;height:300px">
        <p:ajax event="itemSelect" listener="#{chartBean.goToPesquisarFromTotal}"/>
        <h:outputScript>
            function chartExtender() {
                // this = chart widget instance
                // this.cfg = options
                this.cfg.seriesDefaults.rendererOptions.varyBarColor = true;
            }
        </h:outputScript>
    </p:chart>

And as you can see, I continue having the same trouble: chart_pic

How can I change the colors having only one series on chart?


Solution

  • I found what ive had doing wrong. I had one attribute:

     chart.isStacked(true);
    

    marked as true on the chart object from bean. When i remove this, the solution of extender works.

    Thanks for your help and sorry for make you losing time with an dumb question.