【发布时间】:2019-03-02 13:05:49
【问题描述】:
我想根据 JSON 文件的内容不断定义饼图中每个切片的颜色。因此,如果 JSON 文件是 op = [{name: "Idle", y: 3},{name: "Overload", y: 7}] 但有时它也不断变化,则格式数据始终包含 string(name : Busy/Overload/Idle) 和 int(y)。
我尝试了不同的方法,其中一种:
ngOnInit() {
this.indicatorservice.getIndicator().subscribe((res)=>{
this.indicator = res;
let op = this.indicator.map(e=>{
let key = Object.keys(e)[0]
return { name: key, y: +e[key] }
})
op.forEach(element => {
if (element.name == 'Busy') {
this.colorVal = '#ffff00';
}
if (element.name == 'Idle') {
this.colorVal = '#008000';
}
if (element.name == 'Overload') {
this.colorVal = '#ff0000';
}
});
setTimeout( ()=>{
this.Highcharts.chart({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie',
renderTo:'container',
},
title: {
text: 'Todays Shift Indicator'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: 'white'
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: op,
color: this.colorVal
}]
})},300);
})
}
动态采样数据:
op : [{name: "Idle", y: 3},{name: "Overload", y: 7},{name: "Busy", y: 2}]
or sometimes...
op : [{name: "Idle", y: 3},{name: "Overload", y: 7}]
etc.
但不幸的是,它不起作用。
任何帮助将不胜感激。谢谢
【问题讨论】:
-
嗨@baimWonk,您能否提供一个数据结构示例(
res变量)? -
我会更新我的问题先生@ppotaczek
标签: javascript angular highcharts pie-chart