【发布时间】:2021-02-18 07:33:12
【问题描述】:
我有两个数据集要格式化成两个柱形图。
(2) [{…}, {…}]
0:
historyDate: "2021-02-10T10:00:000Z"
documentStatusHistory:
CANCELLED: 6
COMPLETED: 52
IN_PROGRESS: 1
OPEN: 1
PHASE_OUT: 1
1:
historyDate: "2021-02-17T10:00:000Z"
documentStatusHistory:
CANCELLED: 6
COMPLETED: 52
IN_PROGRESS: 1
OPEN: 1
PHASE_OUT: 1
图表在ngAfterContentInit中配置
ngAfterContentInit() {
const chartOptions: Options = this.createDefaultColumnOptions();
chartOptions.title.text = this.chartTitle;
this.resultChart = Highcharts.chart(this.resultChartTarget.nativeElement, chartOptions);
this.resultChart.addSeries(this.buildColumnSeries(this.uuidService.getNewUuid(), this.chartData));
}
列数据在这里创建
private buildColumnSeries(id: string, chartData: any[]) {
const options: SeriesOptions = {
id,
type: 'column',
data: [],
} as SeriesOptions;
chartData.forEach((item) => {
const date = new Date(item.historyDate);
const newDate = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
for (const status in item.documentStatusHistory) {
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: status.replace('_', ' '),
data: [[newDate, item.documentStatusHistory[status]]],
color: DisplayUtil.getStatusColor(status)
});
}
});
return options;
}
这里是图表选项
private createDefaultColumnOptions(): Options {
return {
chart: {
zoomType: 'xy',
marginLeft: 70,
marginRight: 70
},
title: {
useHTML: true
},
legend: {
verticalAlign: 'top',
labelFormat: '<b>{name}</b>',
enabled: true
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
week: '%e. %b'
},
title: {
text: 'Date'
}
},
yAxis: [
{ // Primary yAxis
title: {
text: 'Total ' + this.chartTitle
}
}
],
tooltip: {
shared: true
},
plotOptions: {
column: {
stacking: 'normal'
},
series: {
cursor: 'pointer'
}
},
credits: {
enabled: false
}
} as Options;
}
}
最后我得到以下图表
我现在不完全清楚为什么我得到了两次传说。 每一列的结构都正确。 有没有人指出这里出了什么问题?
【问题讨论】:
-
嗨@Pascal,问题可能是由于在
buildColumnSeries和ngAfterContentInit方法中使用了两次addSeries造成的。 -
我注意到这种方法是错误的。我在给定的数组上迭代错误。
-
如果您在
buildColumnSeries中向我提供chartData参数的结果,我可以为您提供帮助。
标签: typescript highcharts angular-highcharts