I am using jqplot to draw pie-chart and donut-charts. And I am using the 'seriesColors' to give customised colors to the slices http://www.jqplot.com/docs/files/jqplot-core-js.html#jqPlot.seriesColors

seriesColors : [ "0571B0", "#5E3C99", "#008837"]

If the data(array-values to be passed) has only three values, then it does display the colors properly. But if there are more than 3 values, it just displays that slice in black color. It doesn't repeat/reuse the colors from the beginning (as said in the documentation).

Here it is:

var s2 = [['a', 8], ['b', 12], ['c', 6]];
var plot1 = $.jqplot('div_1', [s2], {
                title: 'Chart 1',

                seriesDefaults:{
                  renderer:$.jqplot.DonutRenderer ,
                  rendererOptions:{
                        startAngle: -90,
                        innerDiameter: 100,
                        showDataLabels: true,
                        dataLabels:'percent'
                     }
                    },
                    seriesColors: ["#0571B0", "#5E3C99", "#008837"],
                    highlighter: {
                        show: true
                    },
                    legend: { show:true, rendererOptions: {numberRows: 1}, location: 's', placement: 'outsideGrid'}
                });

But if I add a 4th value in the array, the colors are not reused. i.e if I modify the above array to

var s2 = [['a', 8], ['b', 12], ['c', 6], ['d', 9]];

Then the 4th slice ('d') is displayed in black color.

How do I fix this?

有帮助吗?

解决方案

Found a fix to this. Hope this helps out others who are facing a similar issue.

Here's the code.

var dataValues = [['a', 8], ['b', 12], ['c', 6], ['d', 9], ['e', 14]];

//Define the seriesColors array..
var seriesColors = ["#0571B0", "#5E3C99", "#008837"];

var seriesColorsLength = seriesColors.length;
var donutChartSeriesColors = new Array();

//Prepare a new array which would be passe to the chart..
//This will handle even if there are more value than the seriesColors array..
for(var i = 0; i < dataValues.length; i++) {
donutChartSeriesColors[i] = seriesColors[(seriesColorsLength-1) % i];
}

var plot1 = $.jqplot('div_1', [dataValues ], {
            title: 'Chart 1',

            seriesDefaults:{
              renderer:$.jqplot.DonutRenderer ,
              rendererOptions:{
                    startAngle: -90,
                    innerDiameter: 100,
                    showDataLabels: true,
                    dataLabels:'percent'
                 }
                },
                seriesColors: donutChartSeries,
                highlighter: {
                    show: true
                }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top