score:0
@ViewChild(BaseChartDirective)
public chart: BaseChartDirective;
...
this.chart.chart.update();
use update()
method of chartjs
score:0
Here is Solution, after the update of data, simple barChartData = null and barChartData = "new data".
<div style="display: block" *ngIf="barChartData">
<canvas baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)">
</canvas>
</div>
score:0
Thats a bug in the component. You follow this path ng2-charts/charts/charts.js and change for yourself. Reference(https://github.com/valor-software/ng2-charts/issues/614)
BaseChartDirective.prototype.ngOnChanges = function (changes) {
if (this.initFlag) {
// Check if the changes are in the data or datasets
if (changes.hasOwnProperty('data') || changes.hasOwnProperty('datasets')) {
if (changes['data']) {
this.updateChartData(changes['data'].currentValue);
}
else {
this.updateChartData(changes['datasets'].currentValue);
}
// add label change detection every time
if (changes['labels']) {
if (this.chart && this.chart.data && this.chart.data.labels) {
this.chart.data.labels = changes['labels'].currentValue;
}
}
this.chart.update();
}
else {
// otherwise rebuild the chart
this.refresh();
}
}};
score:0
Try this
public lineChartData:Array<any> = [];
setTimeout(() => {
this.lineChartData= [
{data: [], label: ''}
{data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A'},
{data: [28, 48, 40, 19, 86, 27, 90], label: 'Series B'},
{data: [18, 48, 77, 9, 100, 27, 40], label: 'Series C'},
];
}, 0)
score:4
You can bind the chart directive via ViewChild like so:
...
export class HomeComponent {
@ViewChild(BaseChartDirective)
public chart: BaseChartDirective; // Now you can reference your chart via `this.chart`
void updateChart() {
this.chart.chart.update(); // This re-renders the canvas element.
}
Simply call updateChart
every time your dataset has changed to keep your chart up to date!
score:7
Please follow this and it will work!
1) Import these ones...
import { Component, OnInit, ViewChild } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts';
2) Into the export class (in your ts file) add this...
@ViewChild(BaseChartDirective) chart: BaseChartDirective;
then, first you should initialize the object that contains the data and label info. 1, 1, 1 is an example, you can use 0 if want...
public barChartData: any[] = [{
data: [1,1,1,1,1,1,1],
label: 'this is an example'
}];
3) in your ngOnInit method add a setTimeout function, or use the call to the backend by http;
setTimeout(() => {
this.chart.chart.data.datasets[0].data = [55, 355, 35, 50, 50, 60, 10]
this.chart.chart.update()
}, 2000);
4) in your html file, make sure to add baseChart to the canvas tag
<div class="chart-wrapper">
<canvas baseChart class="chart"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
[colors]="chartColors"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)">
</canvas>
</div>
5) just to explore a little more, in the ngOnInit method, you can perform a console.log of (this.chart.chart) and you will find more information about the object...
Hope it helps!
Source: stackoverflow.com
Related Query
- How to update data Chart in async way in Angular Chart.js?
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- ng2 charts bar chart need spacing between 2 bars in series Angular
- How to change label color of ng2 chart in angular
- how to not repeat code while creating multiple charts in chart js
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to zoom charts in chart.js using angular 7
- How to display the labels in doughnut chart using ng2 charts?
- Ng2-charts / chart.js - how to refresh/update chart - angular 4
- Update charts in chartjs and angular
- How to set single color on each bar in angular chart js
- Angular chart how to show the legend data value by default along with legend name
- ng2 chart multiple chart update
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- How to add title inside doughnut chart in Angular Chart?
- Angular Charts.js: Can't refresh chart with data
- how can i modify scale labels in angular chart js?
- How to update a chart using VueJS and ChartJS
- chart.js pie chart - how to update dataset with smooth transition
- How to rotate the Label text in doughnut chart slice vertically in chart.js canvas, Angular 12?
- How to create overlapping bar charts in angular js?
- Update Chart js chart dynamically in angular 2 from data getting from nodejs service
- How to send data to chart js using angular
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- How to change the color of the bar in barchart if the data is negative - Angular Charts
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- Merge 2 charts into 1 chart using update button (chart.js)
- How to show Nested charts Radar chart within Doughnut Chart?
- How to create a stacked donut chart in Angular 5
- Chart js update multiple charts
More Query from same tag
- Reverse tick order
- Adding object data to data structure of chart.js chart does not show any data
- How to add a second set of labels to a Chart.js doughnut chart?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- implement chartjs zoom with buttons
- react-chartjs-2 chart.js: How to make strokewidth of dougnut chart thin?
- How can i reformat dates
- to increase space between x axis and first horizontal bar in chart js
- ChartJS: unwanted top/bottom padding when legend is aligned right
- How to update data from ajax call (chart.js)
- How to custom index label on each bar chart using chartjs?
- how can I make selected data is highlighted on chart js?
- Is there a way to give a pie chart in chart.js a different color outer border than the border color between segments?
- Why is Chart.JS linking my start and end nodes in the line chart?
- Chart Js Change Label orientation on x-Axis for Line Charts
- ChartJS: Percentage labels
- Dollar sign on y-axis with ChartJS
- Use multiple datasets with Javascript Chart
- Chart.js - How to offset bars from ZeroLine
- Chart JS Fill Between two lines
- Chart.js with dual axis on bar and line graph
- Creating charts (charts.js) values coming from SQLite database using AngularJS
- Chart JS: Bar Chart to have min Length as value range is too big
- ChartJS stacked bar chart not displaying as stacked
- Unable to parse color in line chart (angular-chart.js)
- chartjs how to change decimal to integers X-axis
- Chart.js: Line displayed thinner than defined
- How to control visibility of canvas image via button outside the canvas(ChartJs)?
- Is it possible to add a custom font to chart js?
- Chart js always show labels on a doughnut chart