创建多个动态图表
Create multiple dynamic charts
我正在开发 Web 应用程序 - MEAN 堆栈。我正在尝试使用 ChartJS 圆环图,但我需要它是完全动态的——首先,图表的数量是动态的(每个图表代表其他东西)所以有时它是 3,有时是 20。第二,我希望 ti 能够访问每个图表以实时更改数据。有可能吗?我试图创建一个数组来保存每个图表数据,并使用 *ngFor 为每个图表创建一个 canvas 元素,但它没有用。
我的chartjs.component.ts:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Chart } from 'chart.js';
import { pieceLabel } from 'chart.piecelabel.js';
import {ElementRef} from '@angular/core';
@Component({
selector: 'app-chartjs',
templateUrl: './chartjs.component.html',
styleUrls: ['./chartjs.component.css']
})
export class ChartjsComponent implements OnInit {
constructor( private _dataService : DataService, private elementRef: ElementRef) {
}
jsons: any;
NumberOfSystems: Number;
charts = [];
ngOnInit() {
this._dataService.getData().subscribe(data => {
this.jsons = data
this.NumberOfSystems = this.jsons.data[0][1].systems.length
this.createChartsData()
});
}
createChartsData()
{
var array=[];
for(var i =0; i<this.NumberOfSystems;i++)
{
var pie ={
type: 'doughnut',
data: {
labels: ["Disks", "Mgmt", "Hardware", "FC", "Vols&Pols"],
datasets: [
{
backgroundColor:["#008000","#008000","#008000","#008000","#008000"],
data: [20,20,20,20,20]
}
]
},
options: {
title: {
display: false
},
animations: true,
tooltips: {
enabled: true
},
legend: {
display: true
}
}
};
array.push(pie);
}
this.createCharts(array);
}
createCharts(pieData){
for(var j = 0; j<this.NumberOfSystems;j++)
{
let htmlRef = this.elementRef.nativeElement.select(`#canvas`+j);
console.log(htmlRef);
var tempChart = new Chart(htmlRef,pieData[j]);
this.charts.push(tempChart);
}
}
}
这是 chartjs.component.html:
<div>
<canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}}">{{charts}}</canvas>
</div>
在此状态下,ElementRef 为空。
在你的
canvas ... 添加#yourId
(示例:canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}} #yourId")
然后就可以使用@ViewChildren('yourId') myCharts: any; (您不能在 ngOnInit 中使用 myCharts,只能在 ngAfterViewInit 及之后使用)这将为您提供图表数组。
我不会提供更多细节,但您可以使用 myCharts 中的内容(使用 console.log(myCharts) 详细查看其中的内容),您可以使用它来更改数据等上。
希望对您有所帮助。
这是先前提出的解决方案的一种可能实现方式。
我创建了一个名为 "charts" 的数组,它将包含与我要创建的图表一样多的元素。该数组的每个元素都有一个标识符和我们稍后将放置图表的键 (AfterViewInit)。
HTML:
<div>
<canvas *ngFor="let chart of charts; let i = index" id="mychart{{i}}" #mycharts>{{chart.chart}}</canvas>
</div>
.TS:
import {Component, OnInit, Input, AfterViewInit, ViewChildren} from '@angular/core';
import { Chart } from 'chart.js';
// ...
@ViewChildren('mycharts') allMyCanvas: any; // Observe #mycharts elements
charts: any; // Array to store all my charts
constructor() {
this.charts = [
{
"id": "1", // Just an identifier
"chart": [] // The chart itself is going to be saved here
},
{
"id": "2",
"chart": []
},
{
"id": "3",
"chart": []
}
]
}
ngAfterViewInit() {
let canvasCharts = this.allMyCanvas._results; // Get array with all canvas
canvasCharts.map((myCanvas, i) => { // For each canvas, save the chart on the charts array
this.charts[i].chart = new Chart(myCanvas.nativeElement.getContext('2d'), {
// ...
}
})
}
我正在开发 Web 应用程序 - MEAN 堆栈。我正在尝试使用 ChartJS 圆环图,但我需要它是完全动态的——首先,图表的数量是动态的(每个图表代表其他东西)所以有时它是 3,有时是 20。第二,我希望 ti 能够访问每个图表以实时更改数据。有可能吗?我试图创建一个数组来保存每个图表数据,并使用 *ngFor 为每个图表创建一个 canvas 元素,但它没有用。
我的chartjs.component.ts:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Chart } from 'chart.js';
import { pieceLabel } from 'chart.piecelabel.js';
import {ElementRef} from '@angular/core';
@Component({
selector: 'app-chartjs',
templateUrl: './chartjs.component.html',
styleUrls: ['./chartjs.component.css']
})
export class ChartjsComponent implements OnInit {
constructor( private _dataService : DataService, private elementRef: ElementRef) {
}
jsons: any;
NumberOfSystems: Number;
charts = [];
ngOnInit() {
this._dataService.getData().subscribe(data => {
this.jsons = data
this.NumberOfSystems = this.jsons.data[0][1].systems.length
this.createChartsData()
});
}
createChartsData()
{
var array=[];
for(var i =0; i<this.NumberOfSystems;i++)
{
var pie ={
type: 'doughnut',
data: {
labels: ["Disks", "Mgmt", "Hardware", "FC", "Vols&Pols"],
datasets: [
{
backgroundColor:["#008000","#008000","#008000","#008000","#008000"],
data: [20,20,20,20,20]
}
]
},
options: {
title: {
display: false
},
animations: true,
tooltips: {
enabled: true
},
legend: {
display: true
}
}
};
array.push(pie);
}
this.createCharts(array);
}
createCharts(pieData){
for(var j = 0; j<this.NumberOfSystems;j++)
{
let htmlRef = this.elementRef.nativeElement.select(`#canvas`+j);
console.log(htmlRef);
var tempChart = new Chart(htmlRef,pieData[j]);
this.charts.push(tempChart);
}
}
}
这是 chartjs.component.html:
<div>
<canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}}">{{charts}}</canvas>
</div>
在此状态下,ElementRef 为空。
在你的
canvas ... 添加#yourId
(示例:canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}} #yourId")
然后就可以使用@ViewChildren('yourId') myCharts: any; (您不能在 ngOnInit 中使用 myCharts,只能在 ngAfterViewInit 及之后使用)这将为您提供图表数组。
我不会提供更多细节,但您可以使用 myCharts 中的内容(使用 console.log(myCharts) 详细查看其中的内容),您可以使用它来更改数据等上。
希望对您有所帮助。
这是先前提出的解决方案的一种可能实现方式。 我创建了一个名为 "charts" 的数组,它将包含与我要创建的图表一样多的元素。该数组的每个元素都有一个标识符和我们稍后将放置图表的键 (AfterViewInit)。
HTML:
<div>
<canvas *ngFor="let chart of charts; let i = index" id="mychart{{i}}" #mycharts>{{chart.chart}}</canvas>
</div>
.TS:
import {Component, OnInit, Input, AfterViewInit, ViewChildren} from '@angular/core';
import { Chart } from 'chart.js';
// ...
@ViewChildren('mycharts') allMyCanvas: any; // Observe #mycharts elements
charts: any; // Array to store all my charts
constructor() {
this.charts = [
{
"id": "1", // Just an identifier
"chart": [] // The chart itself is going to be saved here
},
{
"id": "2",
"chart": []
},
{
"id": "3",
"chart": []
}
]
}
ngAfterViewInit() {
let canvasCharts = this.allMyCanvas._results; // Get array with all canvas
canvasCharts.map((myCanvas, i) => { // For each canvas, save the chart on the charts array
this.charts[i].chart = new Chart(myCanvas.nativeElement.getContext('2d'), {
// ...
}
})
}