我正在Angular 2中处理高级图表,但问题是,一次只能显示一个图表。
我现在该怎么办?
两者都在ngOninit内部
我需要同时显示图表,并且我已经尝试了所有方法,但是没有用
谁能建议我该怎么做
第一个图表开始是饼图
//Pie chart starts
Highcharts.chart(this.container.nativeElement, {
// Created pie chart using Highchart
chart: {
type: 'pie',
options3d: {
enabled: true,
alpha: 45
}
},
title: {
text: 'Contents using Pie chart'
},
subtitle: {
text: '3D donut in Highcharts'
},
plotOptions: {
pie: {
innerSize: 100,
depth: 45
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of
total<br/>'
},
//GET ALL
series: [{
name: 'TOTAL chart',
data: [
{
name: 'Payment',
y:15,
drilldown: 'payment'
}
]
}],
//Drill Downs
drilldown: {
series: [
{
name: 'Payment versions',
id: 'payment',
data: [
['Payment A', 55.03],
['Payment B', 15.83]
]
}
]
}
})
//Pie chart ends
现在下一个图表开始,这是一个垂直图表
//Vertical bar chart starts
Highcharts.chart(this.container.nativeElement, {
// Created pie chart using Highchart
chart: {
type: 'column'
},
xAxis: {
type: 'category'
},
series: [{
name: 'TOTAL chart',
data: [
{
name: 'Planned',
y: 30,
drilldown: 'planned'
}
]
}],
//Drill Downs
drilldown: {
series: [
{
name: 'Planned Drill Down',
id: 'planned',
data: [
['plan A', 55.03],
['plan B', 15.83]
]
}
]
}
})
//Vertical bar chart ends
现在,您要将两个图表都添加到同一容器中。因此,第二个图表将覆盖第一个图表的内容。
您需要两个单独的容器。
<div #pieChartContainer></div>
<div #barChartContainer></div>
然后在您的组件类中:
import { Component, ViewChild } from "@angular/core";
import * as Highcharts from "highcharts";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
@ViewChild("pieChartContainer", { static: false }) pieChartContainer;
@ViewChild("barChartContainer", { static: false }) barChartContainer;
ngOnInit() {}
ngAfterViewInit() {
//Pie chart starts
Highcharts.chart(this.pieChartContainer.nativeElement, {
// Created pie chart using Highchart
chart: {
type: "pie",
options3d: {
enabled: true,
alpha: 45
}
},
title: {
text: "Contents using Pie chart"
},
subtitle: {
text: "3D donut in Highcharts"
},
plotOptions: {
pie: {
innerSize: 100,
depth: 45
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat:
'<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
//GET ALL
series: [
{
name: "TOTAL chart",
data: [
{
name: "Payment",
y: 15,
drilldown: "payment"
}
]
}
],
//Drill Downs
drilldown: {
series: [
{
name: "Payment versions",
id: "payment",
data: [["Payment A", 55.03], ["Payment B", 15.83]]
}
]
}
});
//Pie chart ends
//Vertical bar chart starts
Highcharts.chart(this.barChartContainer.nativeElement, {
// Created pie chart using Highchart
chart: {
type: "column"
},
xAxis: {
type: "category"
},
series: [
{
name: "TOTAL chart",
data: [
{
name: "Planned",
y: 30,
drilldown: "planned"
}
]
}
],
//Drill Downs
drilldown: {
series: [
{
name: "Planned Drill Down",
id: "planned",
data: [["plan A", 55.03], ["plan B", 15.83]]
}
]
}
});
//Vertical bar chart ends
}
}
这里是Working Demo供您参考。