score:6
In your
canvas ... add a #yourId
(example : canvas *ngFor="let chart of charts; let i = index" id="canvas{{i}} #yourId")
Then you can use @ViewChildren('yourId') myCharts: any; (you can't use myCharts in ngOnInit, only in ngAfterViewInit and after) which will give you your array of charts.
I won't provide much more detail but you can use what's inside your myCharts (use a console.log(myCharts) to see in detail what's in there), you can use this to change data and so on.
Hope this helps.
score:3
Here is one of the possible implementations of the previously proposed solution. I created an array called "charts", which will contain as many elements as charts I want to create. Each element of this array has an identifier and the key where we will place the chart later (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'), {
// ...
}
})
}
Source: stackoverflow.com
Related Query
- Create multiple dynamic charts
- Create multiple dynamic stacked chart using chart.js in Angular 10?
- How to create dynamic charts with Django and Chart.js?
- Chart.js + Angular 5 - Destroy() multiple dynamic charts created through *ngFor loop
- How to create dynamic D3 tickFormat in dc charts
- Dynamic multiple charts in chart.js with dynamic data inside them
- Create data structure in c# for multiple series charts
- how to use chart.js with knockout.js to create dynamic charts
- How do I create multiple charts at the same page with angular?
- how to not repeat code while creating multiple charts in chart js
- Page with multiple chart.js charts to pdf
- ChartJS - Donut charts with multiple rings
- Chart.js - displaying multiple line charts using multiple labels
- Chart.js - Multiple Doughnut Charts on same Canvas
- how to create multiple chart on one component vue
- Multiple charts in one page with chart.js
- Chart.js Multiple charts with one common legend
- Multiple charts on same page not working - ng2-charts
- How to use chart.js script to draw multiple pie Charts
- How to align multiple pie charts side by side?
- Display multiple chart.js charts using for loop in Django template
- Multiple Chart.js Charts in Partial Views Overwriting Each Other
- Chart.js - Creating multiple charts on one page
- How to add multiple background color in line charts
- ChartJS, merge legends for multiple charts
- How do I correctly include Moment.js and Chart.js with RequireJS when I want to create charts with time scales?
- Multiple charts not working chart.js
- Angular 6 multiple dynamic Charts.js not showing
- Impossible to render multiple charts with chart.js
- How can I load multiple Chartjs charts with different data on the same page?
More Query from same tag
- How to pass variable (list) to JavaScript in Django?
- Update chart axis labels after hover - chart.js
- why my multiple chart.js does not work properly?
- How to add background color for doughnut mid using chart,js
- how to use chart.js in angular 7
- ChartJS passing data dynamically, then passing data into variables
- ChartJS bar not showing up for simple data points
- ChartJs not showing top label
- ChartJS donut chart clipping the edges
- Javascript 2D array getting each element
- How to pass a nested function to an HTML button and Dropdown menu
- Chart.js can't push dataset to charts made dynamically
- Destroying chart.js is not working when chart created inside function - chart.destroy() is not a function
- Can't display proper time on chart.js timeline
- Chart.js 3.6.2, Angular 10: Logarithmic Line chart Y-Axis problems. How to set and keep Y-Axis properties, even when data changes?
- How to add left/right padding in Chart.js bar chart?
- Chart.js, how to reduce space between labels and bars (horizontal bar chart)
- Lost column of bar chart in Chart.js
- Chart js multiple pie chart on one page
- vue.js vue-chartjs chart not updating when chartData replaced, updated etc
- Chart Js clickable bar
- Animated Word Web in Javascript
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- chartjs output data in month week and day
- How to detect click on chart js 3.7.1 axis label?
- Chart.js - Timeline
- Chart.js bar chart bars and labels do not align
- Chartjs with Node.js : Unexpected token <
- Dynamic line chart with chart.js and PHP
- ChartJS: Custom tooltip always displaying