score:0
in the template (html), the id for the canvas has to be different for each chart
score:0
i am going to give you diferrents approaches the first one it is the most simple the anothers you need a litle more knowledge...i hope it's help
1.- You can genera a single component just for render the chartjs graphic for example call it chart-dynamic with several inputs id for grabs the unique id you need for render several charts, and dataChart for all fully object to render , asuming that your tile. component looks like this
Important!! you dataChart must be thinking like a array of object and each object it is basicly a chart you will render into your template (follow the oficial documentacion of chartJs)
<div *ngIf="tile.type === 'tileImg'">
<div class="card custom-card"
routerLinkActive="glowing">
<img class="card-img-top rounded" src="{{ tile.imgPath }}" alt="Tile image" />
<div class="card-body">
<p class=" card-text text-center">{{ tile.name }}</p>
</div>
</div>
</div>
<div *ngIf="tile.type === 'tileChart'">
<div class="card custom-card"
routerLinkActive="glowing">
<!-- NEW CODE -->
<ng-container *ngIf="dataChart?.length > 0" >
<div *ngFor="let chart of dataChart; let i=index">
<app-chart-dynamic [id]="SomeUniqueID" [dataChart]="chart" [type]="chart.type"></app-chart-dynamic>
</div>
</ng-container>
<!-- Finish here -->
<div class="card-body">
<p class=" card-text text-center">{{ tile.name }}</p>
</div>
</div>
</div>
IN YOUR tile.component.ts Generate data method as array of objects, move generateChart function into the new component
import { Component, OnInit, Input } from '@angular/core';
import { Chart } from 'chart.js';
import { Tile } from 'src/app/tile-container/tile/tile.model';
//import { TileChart } from 'src/app/tile-container/tile/tile-chart.model';
@Component({
selector: 'app-tile',
templateUrl: './tile.component.html',
styleUrls: ['./tile.component.css']
})
export class TileComponent implements OnInit {
@Input() tile: Tile;
//tileChart: TileChart;
chart = [];
public dataChart: [];
constructor() { }
ngOnInit() {
//console.log(this.tile);
//console.log(this.tile.getType());
//console.log(this.tile.getChartType() + " " + this.tile.getChartData() + " " + this.tile.getType().localeCompare('tileChart'));
//console.log(this.tile.getType() == 'tileChart');
this.getCharts();
}
public getCharts() {
// call data from you service or data mock
this.dataChart = {....response};
}
}
now assuming you has created your new component should looks like this (you have already import charJs and another stuff)
import { Component, OnInit, Input, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { Chart } from 'chart.js';
@Component({
selector: 'app-chart-dynamic',
templateUrl: './chart-dynamic.component.html',
styleUrls: ['./chart-dynamic.component.css']
})
export class ChartDynamic implements OnInit, AfterViewInit {
@Input() datasChart: any;
@Input() id: string;
@Input() type?: string;
public idChart: any;
@ViewChild('chart') chart: ElementRef;
public chartObject: any;
constructor() { }
ngOnInit() {
}
generateChart(id: string ,chartType?: string, chartData: any) {
this.idChart = this.id;
this.chart = new Chart(`${this.idChart}`, this.datasChart );
}
ngAfterViewInit() {
this.drawGraphics();
}
}
app-chart-dynamic html file
<div class="some-class-style" >
<canvas [id]="id" #chart> {{ chart }}</canvas>
</div>
it should work if you add into your modules and etc
the another approach is combine viewChild and viewChildren with factory resolver it is more complex but more power full you should check firts based on angular documentation
Source: stackoverflow.com
Related Query
- Chart.js creating multiple Charts in Angular in same Component
- how to not repeat code while creating multiple charts in chart js
- angular 5 reset chart js bar in same component
- Building Multiple Charts Using Chart JS in an Angular Application
- Loading multiple doughnut chart from chart.JS in a single component of angular 6
- Chart JS show multiple data points for the same label
- Chart.js - Multiple Doughnut Charts on same Canvas
- how to create multiple chart on one component vue
- Multiple charts on same page not working - ng2-charts
- Create multiple dynamic stacked chart using chart.js in Angular 10?
- Chart.js - Creating multiple charts on one page
- ERROR TypeError: "this.canvas is undefined" | Problem with creating a chart with angular and chart.js
- How can I load multiple Chartjs charts with different data on the same page?
- Chart.js -> line chart -> multiple points with the same X
- Multiple Charts of the Same Type in ChartJS2 ReactJS - Buggy Tooltips
- creating a dynamic number of charts using chart.js using angular
- Chart.js 3.5: linear gradient doesn't apply properly when multiple bars in the same chart
- Chart.js + Angular 5 - Destroy() multiple dynamic charts created through *ngFor loop
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- show multiple responsive chart in the same page using chart.js
- multiple charts js in page but with same tooltips (about last chart)
- Chart js update multiple charts
- Trying to get multiple chart.js charts to load on the same page
- ng2 charts bar chart need spacing between 2 bars in series Angular
- Angular + Ng2 Charts: Chart not displaying as Child Component
- Want to multiple charts on same page with different data
- generate multiple charts via loop for chart js
- multiple horizontal bar charts to display in same row
- How do i have a single Chart.vue component instead of different chart components and then display charts from it by changing id of the API
- How to make multiple stacked charts to share the same yAxis?
More Query from same tag
- How can I show percentages when hovering over my chart?
- How to add datetime formate for public function as key(stdClass) in controller/laravel
- Chart JS no animation line chart
- How to unregister chartjs-plugin-labels globally?
- How to hide row in chartJS when filtering?
- Draw Line Chart Using Chart.js
- How to show dynamic dates with month name of current month and previous month in momentjs?
- ChartJS how to sync animations when page loads
- How can i iterate or get all object out of array with object key in Chart.js
- Show nested objects in Chart.js
- chart resizing in ng2-google-charts
- (Typescript) How do you set pointRadius, pointHoverRadius, ect in ChartJs 3.7.0 & react-chart-js-2 4.0.1
- How to Increase the label font size and decrease the size of my pie using chart.js?
- Vertical Punchcard with chartjs
- Datasets in Chart.js stacked barchart appear overlaid instead of stacked
- Delay animation of chart.js
- Usage of ChartJS v3 with TypeScript and Webpack
- Referencing locally declared variable into an array for data for chartjs
- angular chartjs not showing data properly
- What is the correct format for time data in chartjs?
- Chartjs - Donut Chart label for small values not visible
- Overlay loading indicator before data and charts are loaded
- Using chart.js with laravel passing data from controller to view
- Dynamically set data on Angular-chart.js
- Can't get event from chartjs-annotation-plugin on mouseenter
- chart.js label color change based on value
- ChartJS incorrect plot when plotting multiple line charts in one graph
- Want to save chart.js charts to image using django cron job
- Change line point to triangle in Chart.js
- Remove percentage on DoughtChart in ng2 chart.js