我已经使用angular-highcharts在一个图中创建了两个饼图,现在我想在单击第一个图时对第二个饼图执行相同的操作,反之亦然。当我单击第一个饼图的任何部分时,它也会被选中并切片。与此同时,我想对第二个饼图执行相同的操作。 这样,我正在创建两个饼图,感谢您的帮助,将不胜感激。
const pChart = new Chart({
chart: {
type: 'pie',
styledMode: true
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
title: {
text: ''
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
formatter: function () {
return Math.round(this.percentage * 100) / 100 + ' %';
},
distance: -30,
color: '#000'
},
colors: ['#f7a35c', '#90ed7d'],
size: 180,
borderColor: 'none',
shadow: true
},
series: {
point: {
events: {
legendItemClick: function () {
return false; // <== returning false will cancel the default action
}
}
},
cursor: 'pointer',
events: {
click: (event) => {
console.log('event', event);
for (let j = 0; j < this.qecData.length; j++) {
this.select[j] = this.qecData[j].reasonForDebit === event.point.name;
}
}
}
}
},
series: [
{
name: 'Frequency',
data: freqData,
center: ['20%', '60%'],
showInLegend: true,
startAngle: 270,
title: {
align: 'center',
text: 'Frequency Graph',
verticalAlign: 'top',
y: -50,
style: {
color: '#869ca7'
}
}
},
{
name: 'Amount',
data: amtData,
center: ['80%', '60%'],
showInLegend: false,
startAngle: 180,
title: {
align: 'center',
text: 'Amount Graph',
verticalAlign: 'top',
y: -50,
style: {
color: '#869ca7'
}
}
}
]
});
答案 0 :(得分:0)
当您有权访问单击的点时,可以使用series.pie.point.events.click
回调来实现。在那里,您将找到该点和图表索引的索引,该索引使您可以在第二张图表中具有相同索引的点上调用select
方法。检查下面发布的代码和演示。它使用了我建议您使用的 highcharts-angle官方包装(可以在此处下载:https://github.com/highcharts/highcharts-angular)。
app.module.ts:
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { HighchartsChartModule } from "highcharts-angular";
import { ChartComponent } from "./chart.component";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent, ChartComponent],
imports: [BrowserModule, HighchartsChartModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
chart.component.html:
<div class="boxChart__container">
<div>
<highcharts-chart
id="container"
[Highcharts]="Highcharts"
[constructorType]="chartConstructor"
[options]="chartOptions"
[callbackFunction]="chartCallback"
[(update)]="updateFromInput"
[oneToOne]="true"
style="width: 100%; height: 400px; display: block;"
>
</highcharts-chart>
<highcharts-chart
id="container"
[Highcharts]="Highcharts"
[constructorType]="chartConstructor"
[options]="chartOptions"
[callbackFunction]="chartCallback"
[(update)]="updateFromInput"
[oneToOne]="true"
style="width: 100%; height: 400px; display: block;"
>
</highcharts-chart>
</div>
</div>
chart.component.ts:
import { Component, OnInit } from "@angular/core";
import * as Highcharts from "highcharts";
import * as HighchartsMore from "highcharts/highcharts-more";
import * as HighchartsExporting from "highcharts/modules/exporting";
HighchartsMore(Highcharts);
HighchartsExporting(Highcharts);
@Component({
selector: "app-chart",
templateUrl: "./chart.component.html"
})
export class ChartComponent implements OnInit {
title = "app";
chart;
charts;
updateFromInput = false;
Highcharts = Highcharts;
chartConstructor = "chart";
chartCallback;
chartOptions = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: "pie"
},
title: {
text: "Browser market shares in January, 2018"
},
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:
(Highcharts.theme && Highcharts.theme.contrastTextColor) ||
"black"
}
}
}
},
series: [
{
name: "Brands",
colorByPoint: true,
point: {
events: {
click: function() {
this.series.chart.chartComponent.showCharts(this);
}
}
},
data: [
{
name: "Chrome",
y: 61.41,
sliced: true,
selected: true
},
{
name: "Internet Explorer",
y: 11.84
},
{
name: "Firefox",
y: 10.85
},
{
name: "Edge",
y: 4.67
},
{
name: "Safari",
y: 4.18
},
{
name: "Sogou Explorer",
y: 1.64
},
{
name: "Opera",
y: 1.6
},
{
name: "QQ",
y: 1.2
},
{
name: "Other",
y: 2.61
}
]
}
]
};
constructor() {
const self = this;
self.chartCallback = chart => {
self.chart = chart;
self.chart.chartComponent = self;
self.charts = Highcharts.charts;
};
}
showCharts(point) {
const chart = point.series.chart,
chartIndex = chart.index,
pointIndex = point.index,
secondChartIndex = chartIndex === 0 ? 1 : 0,
secondChart = this.charts[secondChartIndex],
secondChartPoint = secondChart.series[0].points[pointIndex];
secondChartPoint.select();
}
ngOnInit() {}
}
演示:
https://codesandbox.io/s/nw8yq0n9ol
API参考:
https://api.highcharts.com/highcharts/series.pie.point.events.click
https://api.highcharts.com/class-reference/Highcharts.Point#select