我在 Highcharts (angular-highcharts) 中得到了一个令人困惑的图例
I'm getting a confusing legend in Highcharts (angular-highcharts)
我有两个数据集,我想将其格式化为两个柱形图。
(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;
}
}
最后我得到如下图表
我现在不太清楚为什么我得到了两次图例。
每列的结构都正确。
有人知道这里出了什么问题吗?
目前解决方案如下
private buildColumnSeries(id: string, chartData: any[]) {
const options: SeriesOptions = {
id,
type: 'column',
data: [],
} as SeriesOptions;
console.log(chartData);
const OPEN = [];
const COMPLETED = [];
const IN_PROGRESS = [];
const PHASE_OUT = [];
const CANCELLED = [];
chartData.forEach((item) => {
OPEN.push(item.documentStatusHistory.OPEN);
COMPLETED.push(item.documentStatusHistory.COMPLETED);
IN_PROGRESS.push(item.documentStatusHistory.IN_PROGRESS);
PHASE_OUT.push(item.documentStatusHistory.PHASE_OUT);
CANCELLED.push(item.documentStatusHistory.CANCELLED);
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'OPEN',
data: OPEN,
color: DisplayUtil.getStatusColor('OPEN')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'COMPLETED',
data: COMPLETED,
color: DisplayUtil.getStatusColor('COMPLETED')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'IN_PROGRESS',
data: IN_PROGRESS,
color: DisplayUtil.getStatusColor('IN_PROGRESS')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'PHASE_OUT',
data: PHASE_OUT,
color: DisplayUtil.getStatusColor('PHASE_OUT')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'CANCELLED',
data: CANCELLED,
color: DisplayUtil.getStatusColor('CANCELLED')
});
return options;
}
但我不想让状态保持硬编码而是动态的,因为我不知道状态名称是否可以更改。
所以过了一会儿,这里是动态凯斯的解决方案。
private buildColumnSeries(id: string, chartData: any[]) {
const options: SeriesOptions = {
id,
type: 'column',
data: [],
} as SeriesOptions;
const data = [];
const map = new Map();
chartData.forEach((item) => {
const keys = Object.keys(item.documentStatusHistory);
keys.forEach((key) => {
let found = false;
data.find(element => {
if (element.key === key) {
found = true;
}
});
if (found) {
const array = map.get(key);
array.push(item.documentStatusHistory[key]);
map.set(key, array);
} else {
data.push({key, series: [1]});
map.set(key, [item.documentStatusHistory[key]]);
}
});
});
map.forEach((value: [], key: string) => {
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: key,
data: value,
color: DisplayUtil.getStatusColor(key)
});
});
return options;
}
我有两个数据集,我想将其格式化为两个柱形图。
(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;
}
}
最后我得到如下图表
我现在不太清楚为什么我得到了两次图例。 每列的结构都正确。 有人知道这里出了什么问题吗?
目前解决方案如下
private buildColumnSeries(id: string, chartData: any[]) {
const options: SeriesOptions = {
id,
type: 'column',
data: [],
} as SeriesOptions;
console.log(chartData);
const OPEN = [];
const COMPLETED = [];
const IN_PROGRESS = [];
const PHASE_OUT = [];
const CANCELLED = [];
chartData.forEach((item) => {
OPEN.push(item.documentStatusHistory.OPEN);
COMPLETED.push(item.documentStatusHistory.COMPLETED);
IN_PROGRESS.push(item.documentStatusHistory.IN_PROGRESS);
PHASE_OUT.push(item.documentStatusHistory.PHASE_OUT);
CANCELLED.push(item.documentStatusHistory.CANCELLED);
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'OPEN',
data: OPEN,
color: DisplayUtil.getStatusColor('OPEN')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'COMPLETED',
data: COMPLETED,
color: DisplayUtil.getStatusColor('COMPLETED')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'IN_PROGRESS',
data: IN_PROGRESS,
color: DisplayUtil.getStatusColor('IN_PROGRESS')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'PHASE_OUT',
data: PHASE_OUT,
color: DisplayUtil.getStatusColor('PHASE_OUT')
});
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: 'CANCELLED',
data: CANCELLED,
color: DisplayUtil.getStatusColor('CANCELLED')
});
return options;
}
但我不想让状态保持硬编码而是动态的,因为我不知道状态名称是否可以更改。
所以过了一会儿,这里是动态凯斯的解决方案。
private buildColumnSeries(id: string, chartData: any[]) {
const options: SeriesOptions = {
id,
type: 'column',
data: [],
} as SeriesOptions;
const data = [];
const map = new Map();
chartData.forEach((item) => {
const keys = Object.keys(item.documentStatusHistory);
keys.forEach((key) => {
let found = false;
data.find(element => {
if (element.key === key) {
found = true;
}
});
if (found) {
const array = map.get(key);
array.push(item.documentStatusHistory[key]);
map.set(key, array);
} else {
data.push({key, series: [1]});
map.set(key, [item.documentStatusHistory[key]]);
}
});
});
map.forEach((value: [], key: string) => {
this.resultChart.addSeries({
type: 'column',
yAxis: 0,
name: key,
data: value,
color: DisplayUtil.getStatusColor(key)
});
});
return options;
}