angular cli - ng2-charts Bitcoin live chart -
i'm newbie , i'm trying render live bitcoin chart using ng2-charts library, fetched data somehow did not know how visualize data chart due structure of data this:
"bpi": { "2017-08-11": 3679.6074, "2017-08-12": 3917.6487, "2017-08-13": 4111.1963}
this api: https://api.coindesk.com/v1/bpi/historical/close.json
this model chart want make it: https://www.coindesk.com/price/
here codes:
historical-bpi.service.ts:
import { injectable } '@angular/core'; import { http } '@angular/http'; import 'rxjs/add/operator/map'; @injectable() export class historicalbpiservice { private jsonbaseurl: string = 'https://api.coindesk.com/v1/bpi/'; constructor(private http:http) { } getbpidata(url: string){ return this.http.get(this.jsonbaseurl+url) .map(res => res.json()); } } market-data.component.ts:
import { component, oninit } '@angular/core'; import { historicalbpiservice } '../../services/historical-bpi.service'; @component({ selector: 'app-market-data', templateurl: './market-data.component.html', styleurls: ['./market-data.component.scss'] }) export class marketdatacomponent implements oninit { private dataurl: string = 'historical/close.json'; constructor(private historicalbpiservice:historicalbpiservice){} // linechart public linechartdata:array<any> = [ {data:[]} ]; public linechartlabels:array<any> = []; public linechartoptions:any = { responsive: true }; public linechartcolors:array<any> = [ { // grey backgroundcolor: 'rgba(148,159,177,0.2)', bordercolor: 'rgba(148,159,177,1)', pointbackgroundcolor: 'rgba(148,159,177,1)', pointbordercolor: '#fff', pointhoverbackgroundcolor: '#fff', pointhoverbordercolor: 'rgba(148,159,177,0.8)' }, { // dark grey backgroundcolor: 'rgba(77,83,96,0.2)', bordercolor: 'rgba(77,83,96,1)', pointbackgroundcolor: 'rgba(77,83,96,1)', pointbordercolor: '#fff', pointhoverbackgroundcolor: '#fff', pointhoverbordercolor: 'rgba(77,83,96,1)' }, { // grey backgroundcolor: 'rgba(148,159,177,0.2)', bordercolor: 'rgba(148,159,177,1)', pointbackgroundcolor: 'rgba(148,159,177,1)', pointbordercolor: '#fff', pointhoverbackgroundcolor: '#fff', pointhoverbordercolor: 'rgba(148,159,177,0.8)' } ]; public linechartlegend:boolean = false; public linecharttype:string = 'line'; // events public chartclicked(e:any):void { console.log(e); } public charthovered(e:any):void { console.log(e); } ngoninit(){ this.historicalbpiservice.getbpidata(this.dataurl) .subscribe( res => { this.linechartdata[0].data.push(res.bpi); this.linechartlabels.push(res.bpi); this.linechartdata = [...this.linechartdata]; this.linechartlabels = [...this.linechartlabels]; console.log(this.linechartdata); } ) } } the template:
<div class="container"> <div style="display: block;"> <canvas basechart [datasets]="linechartdata" [labels]="linechartlabels" [options]="linechartoptions" [colors]="linechartcolors" [legend]="linechartlegend" [charttype]="linecharttype" (charthover)="charthovered($event)" (chartclick)="chartclicked($event)"></canvas> </div> </div> thank in advance.
edit: got data chart, somehow not visualized still.
this how changed code in component.ts file (the rest same):
ngoninit(){ this.historicalbpiservice.getbpidata(this.dataurl) .subscribe( res => { this.linechartdata.push(object.values(res.bpi)); this.linechartlabels.push(object.keys(res.bpi)); this.linechartdata = [...this.linechartdata]; this.linechartlabels = [...this.linechartlabels]; console.log(this.linechartdata,this.linechartlabels); } ) }
you should populate data , labels array :
... ngoninit() { this.historicalbpiservice.getbpidata(this.dataurl) .subscribe( res => { this.linechartdata[0].data.push(...object.values(res.bpi)); this.linechartlabels.push(...object.keys(res.bpi)); //console.log(this.linechartdata,this.linechartlabels); } ) } ... 
Comments
Post a Comment