"I am using Angular 6. I have a donut chart on dashboard its data doesn't get load on first time , when i go to some other page and come back donut chart displays. On refresh also data disappears. I am getting api data with help of resolver. Graph and other components gets loaded but not this chart. It works completely fine when given static data."
import { Component, OnInit } from '@angular/core';
import * as Chartist from 'chartist';
import { ChartType, ChartEvent } from "ng-chartist/dist/chartist.component";
import { ActivatedRoute } from '@angular/router';
var obtained: any
export interface Chart {
type: ChartType;
data: Chartist.IChartistData;
options?: any;
responsiveOptions?: any;
events?: ChartEvent;
}
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
total: any
obtained: any
public dataApi: any;
constructor(private route:ActivatedRoute) {
}
ngOnInit() {
this.dataApi = this.route.snapshot.data['dashboard'];
if(this.dataApi.status_code==1)
{
obtained = this.dataApi.data1.obtained
}
}
dash: any ={
"series": [
{
"name": "progress",
"className": "ct-grey",
"value": 50-obtained
},
{
"name": "done",
"className": "ct-allow",
"value": obtained
}
]
}
DonutChart: Chart = {
type: 'Pie',
data: this.dash,
options: {
donut: true,
startAngle: 0,
labelInterpolationFnc: function (value) {
return obtained;
}
},
events: {
draw(data: any): void {
if (data.type === 'label') {
if (data.index === 0) {
data.element.attr({
dx: data.element.root().width() / 2,
dy: data.element.root().height() / 2
});
} else {
data.element.remove();
}
}
}
}
};
}
I just got chartist work on angular 6. Im using js to fix this instead of ts.
Install these:
then add css and js in angular.json
in app.component
in app.html
goto https://gionkunz.github.io/chartist-js/examples.html and copy paste any example chart there to your app. it should work without any problem =)