我一直发现,当我想用 Highcharts 和颜色做非常具体的事情时,您需要使用 color 属性预先设置您的数据。因此,假设您已经以 Highcharts 能够理解的方式构建了数据:
// Data formatted so it can be dropped directly into
// a Highcharts pie-chart
let top_data = [
{
name: "Internet Explorer",
y: 7.23,
drilldown: "Internet Explorer"
},{
name: "Chrome",
y: 62.74,
drilldown: "Chrome"
},{
name: "Firefox",
y: 10.57,
drilldown: "Firefox"
},{
name: "Safari",
y: 5.58,
drilldown: "Safari"
},{
name: "Edge",
y: 4.02,
drilldown: "Edge"
},{
name: "Opera",
y: 1.92,
drilldown: "Opera"
},{
name: "Other",
y: 7.62,
drilldown: null
}];
现在您需要确保最大的类别获得特定的颜色,因此我们将继续对数据进行排序:
// Sort in descending order
// (this makes sure the 0th entry is the largest)
top_data.sort(function(a,b) {return b.y-a.y});
现在,您可以将color 属性添加到您的每个块中:
// Color for largest slice
let top_color = 'blue';
// Color array for the rest of the colors
let colors = Highcharts.getOptions().colors;
top_data.forEach((dat,d) => {
if (d==0) {
// Assign first (largest) entry to dedicated color
dat['color'] = top_color;
} else {
// Assign slice to next color in list
dat['color'] = colors[(d-1)%colors.length];
}
});
现在您将其作为数据对象传递给图表配置的系列参数。
为了为向下钻取切片创建唯一的颜色列表,您可以在图表的colors 参数中设置颜色数组。这些颜色将传播到向下钻取饼图切片(不会覆盖您为顶级饼图切片指定的任何唯一颜色)。最终图表将具有以下形式:
// Create the chart
Highcharts.chart('container', {
chart: {
type: 'pie'
},
colors: ['blue', 'green', 'red'], // Or any other unique list of colors
/* All your other config parameters ... */
series: [
{
name: "Browsers",
colorByPoint: true,
data: top_data
}
],
/* And the rest, like your drilldown data */
});