score:1
i think that the right way to update the chart, specially the line chart with multiple lines is to import ViewChild and ElementRef in your component, then you must import the BaseChartDirective to create an instance of your chart, then you can update your data and labels of the chart.
so first you have to give the class = "chart" for the chart in the html
<div class="chart">
<canvas baseChart [datasets]="barChartData" [labels]="barChartLabel" [options]="barChartOptions">
</canvas>
</div>
if you notice i am using the datasets attribute to bind the data not the data attribute; now for the component you have to respect the structure of the data you're giving:
private datasets_lines: { label: string, data: Array<any> }[]
now the full code should be something like this:
import { Component, Input, OnInit, NgZone, OnChanges, ViewChild, ElementRef } from '@angular/core';
import { BaseChartDirective } from 'ng2-charts/ng2-charts';
@Component({
selector: 'app-bar-chart-demo',
templateUrl: './bar-chart-demo.component.html',
styleUrls: ['./bar-chart-demo.component.css'],
inputs:['chartLabel', 'chartData']
})
export class BarChartDemoComponent{
@ViewChild(BaseChartDirective) chart: BaseChartDirective;
public barChartOptions :any = {
scaleShowVerticalLines:false,
responsive:true
};
//Labels
public barChartLabel :string[];
//Data
public barChartData: { label: string, data: Array<any> }[]=[];
ngOnChanges(){
// do your changes here
setTimeout(() => {
this.barChartData=this.chartData;
this.barChartLabel=this.chartLabel;
//console.log(this.barChartData);
if (this.chart && this.chart.chart && this.chart.chart.config) {
this.chart.chart.config.data.labels = this.barChartLabel;
this.chart.chart.config.data.datasets = this.barChartData;
this.chart.chart.config.data.options = this.barChartOptions;
this.chart.chart.update();
}
});
}
ngOnInit(){
this.barChartLabel=['23 mei', '24 mei', '25 mei', '26 mei', '27 mei', '28 mei', '29 mei'];
this.barChartData=[
{ data: [15, 29, 24, 21, 26, 15, 10], label: 'Opening/Time' },
{ data: [11, 20, 27, 26, 22, 17, 11], label: 'Closing/Time' }
];
}
}
}
Note: you have to check that the number of the data equals the number of the labels
you can check my other exemple on this link ng2-charts update labels and data
Source: stackoverflow.com
Related Query
- ng-charts not updating labels when chart data is updated at same time
- vue.js vue-chartjs chart not updating when chartData replaced, updated etc
- colors are not updated when updating chart
- When updating Chart.js chart the data is updated but canvas is emptied
- Hide Y-axis labels when data is not displayed in Chart.js
- Stacked line chart not displaying correctly when Xaxis is time
- ChartJS shows date, but not time, when displaying labels in time axis
- vue-chart.js / vuex: Chart not updating when vuex state changes
- Chart.js animations when updating chart data
- AngularJS Charts not updating data correctly
- Vuejs - Chart.js wrapper vue3-chart-v2 not displaying labels when data are passed as object
- ChartJS not showing data for time series bar chart
- Angular6 and ng2-charts does not display any charts when i fill data from webservice
- Angular-Charts bar chart does not update when I change the data, series, labels
- Dynamically updating time data in chart.js freezes the chart
- Two data sets with different time instances on the same ChartJs chart
- (Lifecycle problem?) React Chart js-2 not updating data from componentDidMount
- Chart.js not showing data when pass dynamic labels
- data in charts of charts.js is changing when I click in the line chart
- Chart is not populating when getting the data from AJAX call
- Firefox is not responding when loading 800+ data sets in Bar Chart with Charts.Js
- Chart.js returns a console error and does not display the chart when using variables as data input
- The real time chart sometimes does not display when I switch the target
- Charts with previous data appear when hovering the cursor over the chart in Chart.js
- Background color does not work when trying to create my data before using scatter chart with chart.js
- Chart Data Not Updating for Both Graphs?
- Position of the x-axis labels is not in sync with the line chart data points
- Angular 8 and ng2 charts - Updating labels and data
- Chart.js time object labels not updating correctly
- Chart JS: Donut/Doughnut Chart: Tooltip to be shown always for all the data. All tooltip is not shown when multiple data are with 0 data
More Query from same tag
- Load ChartJS via VueJS - ChartJS empty and 0px dimension
- TypeError React-Chartjs After Upgrading to Chart.js v3
- Horizontal 'limit' line in Chart.js
- Y Axes not displaying properly on Chartjs
- Chart.js label and point getting cutoff on the right
- chart js showing some weird data at top of charts
- Chart.js time scale graph - xAxis labelling
- How to chart missing data in chartjs
- Plotting multiple lines (line chart) in Apache Zeppelin using AngularJS
- Bind Angular FormGroup to Chartjs data
- Chart JS no animation line chart
- Chart.js do not display in Boostrap tabpanel
- how to add a title to my ng2-charts bar chart
- Can't resolve 'chart.js/auto'
- How to export a chart.js chart to excel?
- I want to hide the label in a tooltip because it shows undefined
- Chartjs / ng2-charts line on hover does not work
- Enable Stepped lines in Chart.Js
- e.labels.map is not a function when creating a chart with chart.js
- Using chart js version 3, how to add custom data to external tooltip?
- Problem displaying two graphs on a single chart using javascript and chart.js
- Chart.js and right side free space
- Display a single tooltip with Chart.JS?
- How to Create Chart.JS Bar Graph with Min, Max & Average
- Angular chart from Chart.js
- Change the Center color of doughnut chart in Chart.Js
- How to get the actual chart width and height in chart.js
- Char.js Interpolation linear
- How to plot multiple value for one X-axis chart.js
- Chart js legend are being cut off if the bar height is equal to port height - chart js