我正在做一个有角度的项目,在这里我有一个按钮,希望它隐藏高图上除特定序列以外的所有序列。这可能很简单,但是我只是从事了一段时间的Web开发,而我在某些基础知识上却有点不足。我不是真正的网络开发人员,但同意承担一些工作,而不是让公司雇用知道自己在做什么的人。无论如何,合适的开发人员会为我设置项目,并向我展示如何做我想做的事情,但他目前正在休假,所以我变得有些困惑。
因此,我正在使用图表显示一些数据。我有一个highcharts文件夹,其中包含一个称为highcharts-component.ts的可重用组件,如下所示:
import { Component, ElementRef, Input, OnDestroy, OnInit } from "@angular/core";
import { chart } from "highcharts";
import * as Highcharts from "highcharts";
import drilldown from "highcharts/modules/drilldown.src.js";
drilldown(Highcharts);
declare var require: any;
require("highcharts/highcharts-more")(Highcharts);
require("highcharts/modules/solid-gauge")(Highcharts);
require("highcharts/modules/heatmap")(Highcharts);
require("highcharts/modules/treemap")(Highcharts);
require("highcharts/modules/funnel")(Highcharts);
@Component({
selector: "app-chart",
styleUrls: ["./styles.scss"],
templateUrl: "./template.html"
})
export class ChartComponent implements OnDestroy, OnInit {
@Input()
set options(value: Highcharts.Options) {
this._options = value;
this.chart = chart(this.elementRef.nativeElement, this._options);
}
private chart: Highcharts.ChartObject;
private _options: Highcharts.Options;
constructor(private elementRef: ElementRef) {}
ngOnDestroy() {
this.chart = null;
}
ngOnInit() {
this.chart = chart(this.elementRef.nativeElement, this._options);
}
public showSingleSeries(seriesNumber) {
for (let x = 0; x < this.chart.series.length; x++) {
if (x == seriesNumber) {
this.chart.series[seriesNumber].show();
} else {
this.chart.series[seriesNumber].hide();
}
}
}
}
然后在我的名为card-fancy-example.ts的组件文件中,我有类似的内容:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, OnInit, ViewChild, ElementRef, VERSION } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
import * as Highcharts from "highcharts";
/**
* @title Card with multiple sections
*/
@Component({
selector: 'card-fancy-example',
templateUrl: 'card-fancy-example.html',
styleUrls: ['card-fancy-example.css'],
})
export class CardFancyExample {
myChart: Highcharts.Options = {
// Created pie chart using Highchart
chart: {
type: 'pie'
},
title: {
text: 'Contents using Pie chart'
},
subtitle: {
text: '3D donut in Highcharts'
},
plotOptions: {
pie: {
innerSize: 100,
depth: 45
}
}
};
constructor() {
}
ngOnInit() {
this.loadChart();
}
loadChart()
{
this.myChart = {
...this.myChart,
series: [{
name: 'Operating Systems',
data: [
{
name: 'Windows',
y: 88.19,
drilldown: 'windows-versions'
},
['MacOSX', 9.22],
['Linux', 1.58],
['Others', 1.01]
]
}],
drilldown: {
series: [{
name: 'Windows versions',
id: 'windows-versions',
data: [
['Win 7', 55.03],
['Win XP', 15.83],
['Win Vista', 3.59],
['Win 8', 7.56],
['Win 8.1', 6.18]
]
}]
}
}
}
showSingleSeries(seriesNumber)
{
// Where I want to call the showSingleSeries method in highcharts-component.ts
}
}
我的html文件如下:
<button (click)="showSingleSeries(0)">Remove All Series But Windows</button>
<button (click)="showSingleSeries(1)">Remove All Series But Linux</button>
<app-chart [options]="myChart"></app-chart>
该图表可以正确加载,但是现在当我单击特定按钮时,我希望只能显示“ Windows”或“ MacOSX”系列。我在highcharts-component.ts中有一个方法可以做到这一点,名为showSingleSeries,它看起来像这样:
public showSingleSeries(seriesNumber) {
for (let x = 0; x < this.chart.series.length; x++) {
if (x == seriesNumber) {
this.chart.series[x].show();
} else {
this.chart.series[x].hide();
}
}
}
问题是,我对angular的了解还不足以知道如何访问showSingleSeries方法?我该怎么做呢?这是我在stackblitz
中所拥有的谢谢。
编辑:我在代码中编辑了两个错误,并更改了图表类型。我已经用解决方案更新了stackblitz。谢谢@Lys
答案 0 :(得分:2)
编辑
您希望父级调用childs方法。您应该使用view child。
html中的
<app-chart [options]="myChart" #charts></app-chart>
@ViewChild('charts') charts;
然后将其命名为this.charts.showSingleSeries(1);