score:1
Accepted answer
when i tried to replicate your code, i got the same issue that legends had a strike through them... the reason was the format of the piechartdata:any object and the data [2193,3635,8417,0]
which was being fed into it!!
you had defined piechartdata
as an array with single object with a data property which consisted of an array.
piechartdata:any = [ { data: [] } ];
while, the values were being set as a number array [2193,3635,8417,0], which means:
piechartdata:any = [];
to replicate the delay caused by your http call...i used settimeout and got the form working as intended...
relevant ts:
import { component } from '@angular/core';
import { charttype, chartoptions } from 'chart.js';
import { label } from 'ng2-charts';
import * as plugindatalabels from 'chartjs-plugin-datalabels';
@component({
selector: 'my-app',
templateurl: './app.component.html',
styleurls: ['./app.component.css']
})
export class appcomponent {
name = 'angular';
somedata: any[] = [];
public piechartoptions: chartoptions = {
responsive: true,
legend: {
position: 'top',
},
plugins: {
datalabels: {
formatter: (value, ctx) => {
const label = ctx.chart.data.labels[ctx.dataindex];
return label;
},
},
}
};
public piechartlabels: label[] = ['pending', 'success', 'fail', 'created'];
public piecharttype: charttype = 'pie';
public piechartlegend = true;
public piechartplugins = [plugindatalabels];
public piechartcolors = [
{
backgroundcolor: ['rgba(30, 169, 224, 0.8)',
'rgba(255,165,0,0.9)',
'rgba(139, 136, 136, 0.9)',
'rgba(255, 161, 181, 0.9)',
]
},
];
//public piechartdata: number[] = [2193,3635,8417,0];
public piechartdata: number[] = [];
constructor() { }
ngoninit() {
settimeout(() => {
this.somedata = [300, 500, 100];
this.piechartdata = this.somedata;
}, 5000);
}
}
relevant html:
<hello name="{{ name }}"></hello>
<div *ngif='this.somedata.length <=0'>
please wait while the latest data is fetched
</div>
<div *ngif='this.somedata.length >0'>
we got data: {{this.somedata.length}}
<div class="chart">
<canvas basechart
[data]="piechartdata"
[labels]="piechartlabels"
[charttype]="piecharttype"
[options]="piechartoptions"
[plugins]="piechartplugins"
[colors]="piechartcolors"
[legend]="piechartlegend">
</canvas>
</div>
</div>
working stackblitz available here
Source: stackoverflow.com
Related Query
- Angular / ng2-charts: Fetching json data in chart object showing: Cannot read property 'length' of undefined
- Uncaught TypeError: Cannot read property 'draw' of undefined : Angular chart js
- Charts js and laravel: Render chart after passing in json data
- Charts in Angular js with JSON Object
- ng2 charts bar chart need spacing between 2 bars in series Angular
- Chart.js - Multiple JSON data object array [{x: Date, y: count}....] representing each dataset of the chart
- angular 4 line chart data is not showing data from http get
- chart cannot read multiple datasets from the back-end (single data is fine)
- Angular Charts - not able to read data
- How to pass a chart.js chart data object in json from a controller in asp.net mvc
- How to set the data of chart in angular which is taken from the backend nodejs api that is store in mongodb and showing on the html page
- chart js showing some weird data at top of charts
- How to display Json object in chart js consisting of lists created using Django rest framework in angular application
- Angular 8 and ng2 charts - Updating labels and data
- Bar Chart cannot read data and label in laravel
- Angular chart showing series but no data
- Chart js pie chart not showing dynamic data using JSON
- Chartjs Bar Chart showing old data when hovering
- Chart Js Cannot read property 'length' of undefined
- Series Details Not Showing in Angular Chart with Charts.js
- TypeError: Cannot read property 'legend' of undefined | Angular + ng2-charts
- Charts js only showing 2 data points on sparkline
- Parsing JSON data into Chart.js bar chart
- How to update data Chart in async way in Angular Chart.js?
- Chart.js load new data from saved JSON object
- Chart not displaying from JSON data
- chartjs - multi axis line chart - cannot read property 'min' of undefined
- Angular chart how to show the legend data value by default along with legend name
- Chart JS not showing On hover with small data
- chart annotations not showing in Angular 2
More Query from same tag
- Chart.js Stacked Group Bar model after updating version Chart.js 2.9.4 -> Chart.js 3.5.0
- How to draw a horizontal line over bar columns in Chart.js that's started from X axis?
- Remove zero values from tooltip
- how to access class variables inside chart.js custom tooltip interface
- laravel-chartjs not displaying anything
- How to fetch all data from API files and assign their values to chart?
- Rotating a chart.js pie chart with two datapoints to align both sectors on horizontal axis
- how to see labels without losing middle text in doughnut chart
- Chart.js manipulate each bar in chart with different labels
- How can I arrange according to month using chart Js?
- Why itemSort is not triggered when I hover a dataset point in Chart.js?
- Chart.js dataset controller 'null' when chart drawn
- Angular js pie chart styling
- ChartJS - Show percentage on hover (AngularJS)
- Displaying data from simple html form to display in chartjs
- Changing x axis labels in Chart.js line chart
- chartjs: set default min for the line chart
- how to change display and styles of the labels in react-chartjs-2
- How to draw a hover effect behind a bar chart?
- Chart JS - Line chart with days in x-axis
- ng2-Chart: can we show the tooltip data of pie chart on load?
- Rest api / json error while using it with react chartjs
- label too long in yAxis with chartJs
- Price history with chart js
- How to resize chartjs in bootstrap columns for different browsers
- Make Line Chart Dynamic From JSON File Using Chartjs
- Assigning data structures to chartjs properties, NOT individually
- Calling data from search bar to chart.js graph in Angular
- Using ChartJS to create a multiple grouped bar chart - see picture below
- Non numeric for x-axis in scatter chart in CHART.js