パンクル:ここ
チャーティスト チャートのレンダリングに使用している子 comp chartist.component があります
私の親コンポーネントapp.tsでは、chartist.component を2 回呼び出して、折れ線グラフと棒グラフをレンダリングしています。
親カンプには、折れ線グラフと棒グラフを更新するボタンがあり、データ変数内のデータを変更するだけで、チャートをレンダリングするために子カンプに渡されます。
私の問題は、更新ボタンをクリックしたときにチャートが 1 回だけ更新されることです。データ オブジェクトが変更されるたびにグラフを更新したいと考えています。
app.ts (親コンポーネント):
import {Component, NgModule, OnInit, AfterViewInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ChartistChart} from './chartist.component'
@Component({
selector: 'my-app',
templateUrl: './src/app.html',,
})
export class App implements OnInit, AfterViewInit {
name:string;
data: any;
isFirst:true;
dataIndex:number;
allData:any;
constructor() {
this.name = 'Angular2'
this.dataIndex=0;
this.allData=[{chart data1},{chart data 2}];
}
ngOnInit() {
this.getAllCharts();
}
ngAfterViewInit() {}
public getAllCharts(){
this.data= this.allData[this.dataIndex];
return this.allData[this.dataIndex];
}
public updateLineChart(){
this.isFirst = !this.isFirst;
this.dataIndex = this.isFirst?0:1;
this.data.simpleLineData = this.allData[this.dataIndex].simpleLineData;
this.data.simpleLineOptions = this.allData[this.dataIndex].simpleLineOptions;
}
public updateBarChart(){
this.isFirst = !this.isFirst;
this.dataIndex = this.isFirst?0:1;
this.data.simpleBarData = this.allData[this.dataIndex].simpleBarData;
this.data.simpleBarOptions = this.allData[this.dataIndex].simpleBarOptions;
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, ChartistChart ],
bootstrap: [ App ]
})
export class AppModule {}
app.html:
<div style="width:100%; height:350px">
<button (click)="updateLineChart()">Update Line chart</button> DataIndex: {{dataIndex}}
<chartist-chart *ngIf="data" chartistChartClass="ct-chart" chartistChartType="Line" [chartistChartData]="data['simpleLineData']"
[chartistChartOptions]="data['simpleLineOptions']">
</chartist-chart>
</div>
<br/> <br/>
<div style="width:100%; height:350px">
<button (click)="updateBarChart()">Update Bar chart</button> DataIndex: {{dataIndex}}
<chartist-chart *ngIf="data" chartistChartClass="ct-chart" chartistChartType="Bar" [chartistChartData]="data['simpleBarData']"
[chartistChartOptions]="data['simpleBarOptions']">
</chartist-chart>
</div>
chartist.component.ts (子):
import {
Component, ViewChild, Input, Output, ElementRef, EventEmitter, ChangeDetectorRef
} from '@angular/core';
@Component({
selector: 'chartist-chart',
templateUrl: './src/chartistChart.html',
providers: [],
})
export class ChartistChart {
@Input() chartistChartType: string;
@Input() chartistChartData: Object;
@Input() chartistChartOptions: Object;
@Input() chartistChartResponsive: Object;
@Input() chartistChartClass: string;
@Output() onChartReady = new EventEmitter<any>();
@ViewChild('chartistChart') public _selector: ElementRef;
private chart;
constructor(private cdr: ChangeDetectorRef){}
ngAfterViewInit() {
console.log('chart init', this.chartistChartData)
let _self = this;
_self.chart = new Chartist[_self.chartistChartType](_self._selector.nativeElement,
_self.chartistChartData,
_self.chartistChartOptions);
_self.onChartReady.emit(_self.chart);
this.cdr.detectChanges();
}
ngOnChanges(changes) {
if (this.chart) {
console.log('chartistChartData changed: ',this.chartistChartData);
(<any>this.chart).update(this.chartistChartData, this.chartistChartOptions);
}
}
ngOnDestroy(): void {
if (this.chart) {
this.chart.detach();
}
}
}
chartistChart.html:
<div #chartistChart class="{{chartistChartClass || ''}}"></div>
this.getAllCharts()
ボタンをクリックするたびに手動で呼び出すだけでthis.data
、新しいデータ シリーズに再割り当てされます。
public updateLineChart() {
this.dataIndex = this.dataIndex ? 0 : 1;
this.getAllCharts();
}
public updateBarChart() {
this.dataIndex = this.dataIndex ? 0 : 1;
this.getAllCharts();
}
冗長isFirst
フラグを取り除くこともできdataIndex
ます。これで十分です。そして、あなたは設定する必要はありませんthis.data.simpleBarData
し、this.data.simpleLineData
それはすでに、テンプレート内の小道具として渡されたとして、あまりにも手動。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加