Jqplot pie / donut chart seriesColors数组不重复使用/重复

时间:2012-12-21 05:24:38

标签: colors jqplot reusability series donut-chart

我正在使用jqplot绘制饼图和圆环图。 我正在使用'seriesColors'为切片http://www.jqplot.com/docs/files/jqplot-core-js.html#jqPlot.seriesColors

提供自定义颜色

seriesColors:[“0571B0”,“#5E3C99”,“#008837”]

如果数据(要传递的数组值)只有三个值,那么它会正确显示颜色。 但是如果有超过3个值,它只显示黑色切片。 它不会从头开始重复/重复使用颜色(如文档中所述)。

这是:

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'}
                });

但是如果我在数组中添加第4个值,则不会重复使用颜色。 即如果我将上面的数组修改为

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

然后第4个切片('d')以黑色显示。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

找到了解决此问题的方法。 希望这可以帮助那些面临类似问题的人。

这是代码。

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