0

我正在使用 jqplot 绘制饼图和圆环图。我正在使用“seriesColors”为切片提供自定义颜色http://www.jqplot.com/docs/files/jqplot-core-js.html#jqPlot.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') 以黑色显示。

我该如何解决?

4

1 回答 1

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
                }
});
于 2012-12-21T07:28:22.757 回答