Search code examples
javascriptdevexpresschart.jspie-chartchartjs-2.6.0

js devexpress PieChart Series color runtime


I am working on js.devexpress Pie Chart. I'd like to change the series color at runtime.

I am fetching data from webservice and as per my logic i have to change color red or green as per value. if value is less then 0 [Zero] then Pie Color is red else green.

My code is

 <div id="canvas-holder" >       
    <div id="pie" ></div>
</div>

 var pie =   $("#pie").dxPieChart({
        size: {width: 500},
        //palette: "bright",
        dataSource: [{country: "On",area: 0}],
        series: [{argumentField: "country", valueField: "area"}],
        title: "Area of Countries"             
    }).dxPieChart("instance");

    setInterval(function () {
        debugger;
        pie.option("Color", "red");

        pie.element().css({ Color: 'red' })

        var chartOptions = pie.option();
        pie.series[0].Color = "red";
        pie.option(chartOptions);

    }, 1000);
</script>

`


Solution

  • Quick solution I found : visit devexpress support forum for More Details

    Sample Code Download link

    Code is

    <div id="canvas-holder">
        <div id="pie"></div>
    </div>
    
    <script>
        var ds = [{ country: "On", area: 0 }];
    
        var pie = $("#pie").dxPieChart({
            size: { width: 500 },
            //palette: "bright",
            dataSource: ds,
            series: [{ argumentField: "country", valueField: "area" }],
            title: "Area of Countries",
            customizePoint: function (pointInfo) {
                if (pointInfo.value <= 0) {
                    return {
                        color: "red"
                    }
                }
                return {
                    color: "green"
                }
            }
        }).dxPieChart("instance");
    
        var isVal = 0;
    
        setInterval(function () {           
    
            if (isVal == 0)
                isVal = 1;
            else
                isVal = 0;
    
            ds[0].area = 1;
    
            pie.option("dataSource", ds);
    
        }, 1000);
    
    </script>