score:1
You can use ViewChild
to reference html element and use it inside you component. I have also modified few things in your code to toggle charts.
To summarize:
- Use
ViewChild
to access html element in your component - Updated app component to toggle charts as opposed to just adding data
- Updated label to accept click event to toggle checkbox
Take a look at this stackblitz.
app.component.html
<label>
<input type="checkbox" value=1
(change)="chooseEntity($event.target.checked, 1, entityData[0])">
Microsoft
</label>
<label>
<input type="checkbox" (change)="chooseEntity($event.target.checked, 2, entityData[1])">
IBM
</label>
<div *ngIf="chartData.length !== 0">
<app-limus-utilisation-chart *ngFor="let entity of chartData" [chartdata]="entity"></app-limus-utilisation-chart>
</div>
chart.component.html
<div #chartReport>
<canvas #canvas></canvas>
</div>
chart.component.ts
import {
Component,
ElementRef,
Input,
OnInit,
ViewChild,
ViewEncapsulation
} from "@angular/core";
import { Chart } from "chart.js";
import { OnChanges } from "@angular/core";
@Component({
selector: "app-limus-utilisation-chart",
templateUrl: "./chart.component.html",
encapsulation: ViewEncapsulation.None
})
export class LimusUtilisationChartComponent implements OnInit, OnChanges {
myChart: Chart;
@Input() chartdata: any;
@ViewChild("canvas") stackchartcanvas: ElementRef;
constructor() {}
ngOnChanges() {
this.getStackedChart();
}
ngOnInit(): void {
this.getStackedChart();
}
getStackedChart() {
Chart.Tooltip.positioners.custom = function(elements, position) {
//debugger;
return {
x: position.x,
y:
elements[0]._view.base - (elements[0].height() + elements[1].height())
};
};
const canvas: any = this.stackchartcanvas.nativeElement;
const ctx = canvas.getContext("2d");
var data = {
labels: this.chartdata.buyernames,
datasets: [
{
label: "Utilised Limit",
data: this.chartdata.utilisedlimitData,
backgroundColor: "#22aa99"
},
{
label: "Available Limit",
data: this.chartdata.availablelimit,
backgroundColor: "#994499"
}
]
};
setTimeout(() => {
this.myChart = new Chart(ctx, {
type: "bar",
data: data,
options: {
tooltips: {
mode: "index",
intersect: true,
position: "custom",
yAlign: "bottom"
},
scales: {
xAxes: [
{
stacked: true
}
],
yAxes: [
{
stacked: false,
display: false
}
]
}
}
});
});
}
}
score:4
The problem is this line here:
const canvas: any = document.getElementById('canvas1');
You have multiple elements with that ID on the page (Because you did *ngFor), so it always attaches itself to the first element on the page.
Instead of using getElementByID, you should use Angular's built-in @ViewChild
.
Like this:
chart.component.html:
<canvas #stackchartcanvas></canvas>
chart.component.ts:
@ViewChild("stackchartcanvas") myCanvas: ElementRef<HTMLCanvasElement>;
....
....
....
getStackedChart() {
const canvas = this.myCanvas.nativeElement;
}
Stackblitz: https://stackblitz.com/edit/angular-chart-js-kny4en?file=src%2Fapp%2Fchart.component.ts
(Also, in your original code, this.chartData.push()
ran EVERY time a checkbox was clicked, even if the checkbox was false, but that's a different, unrelated problem, which has also been fixed.)
Source: stackoverflow.com
Related Query
- Create multiple dynamic stacked chart using chart.js in Angular 10?
- How to create stacked bar chart using react-chartjs-2?
- Multiple stacked bar chart using ChartJs
- Using two JSON objects to make Chart.JS Bar Chart Dynamic in Typescript / Angular
- How to create a stacked donut chart in Angular 5
- Using ChartJS to create a multiple grouped bar chart - see picture below
- Create multiple pie chart simultaneously using chart.js
- Building Multiple Charts Using Chart JS in an Angular Application
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- how to create line chart using chart.js in angular 2+
- How to create an animate-on-update Stacked Bar chart using Chart.js(or Chartist)?
- create a multi line chart using Chart.js
- Create multiple dynamic charts
- How to create single value Doughnut or Pie chart using Chart.js?
- how to create multiple chart on one component vue
- Extending Existing Chart Types angular chart js using chart js 2.0
- X-axis multiple colored label for bar chart using chart.js
- Show labels on each sector to polar chart using angular js chart
- How to create a gantt chart using Chart.js and populate dates?
- Create Chart using Reactjs Chartjs and axios
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- Stacked bar chart with rounded corner of bar using Chart.js
- How to create a stacked graph using ChartJS
- Displaying line chart for multiple months using chart.js
- Angular 6 multiple dynamic Charts.js not showing
- How do I create stacked chart with Chart.js version 2.0.0-beta
- angular 5 chart.js Failed to create chart
- How to draw multiple color bar in a bar chart using chart.js
- Create stacked bar chart with a single dataset per stack
- How to make dynamic chart using Vue component with chart-js
More Query from same tag
- Cycle through finite set of colours for indefinite set of data in Javascript
- Stacked bar chart starting from 0 - ChartJS React
- Getting rid of Chart.js canvas subpixels
- text inside the bar - chart.js
- passing json data to chartjs
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- ChartDataLabels is globally active for some reason
- Can't trigger beforeDraw with Vue-Chartjs (or any other plugins)
- Chartjs backgroundColor for line chart does not appear in Vue app
- Chartjs v2 on retina display has label size issue (text is too large)
- Dynamically Creating Json Array Javascript
- How to put labels at 4 points of the day?
- Query result into array in Laravel 6
- How to sort color segments by value within the 100% stacked bars rather than by value across all the stacked bars in Highcharts or ChartJs
- Issue regards data in charts JS
- core.js:4197 ERROR TypeError: chart_js__WEBPACK_IMPORTED_MODULE_2__ is not a constructor
- Chartjs is graphing my values at positions 0,1,2 rather than their values along the x axis
- Plot a Line chart using Chart.js with CSV Data
- My charts doesn't work after inserting X-axis
- Vertical Punchcard with chartjs
- Chart.js have the y-axis step from 20 to 1 (instead of 1 to 20)
- Chart.js different y axis depending on graph from select options
- Bug : Overlay loading automatically (bootstrap) (laravel 5.2)
- Horizontal xAxis labels in Chart.js
- Chartjs Bar Chart add background color to value labels
- Providing server side data for Chart.js charts
- Chart.js line chart set background color
- ChartJS: datalabels: show percentage value in Pie piece
- Chart.js coming up as blank
- ChartJS does not render correctly