javascript - Jak implementovat ZingChart do Angular2
Mám stávající projekt, do kterého chci implementovat zingcharts.
Vyzkoušel jsem 3 různé návody, hlavně z blogu "https://blog.zingchart.com/2016/07/19/zingchart-and-angular-2-charts-back-at-it-again/".
Nicméně to v mém projektu nemohu rozběhnout. Takže jsem se rozhodl, že to nejdříve zkusím implementovat co nejjednodušším způsobem a později vyzkouším lepší způsoby. Toto jsem udělal, ale zatím se to nezobrazuje.
Jelikož jsem v Angular2 nováček, nejsem si úplně jistý, jestli to bude fungovat.
Šel jsem na webovou stránku ZingChart a zkusil implementovat tento základní příklad ->https://www.zingchart.com/docs/getting-started/your-first-chart/
Tak jsem vytvořil 2 soubory chart.ts a chart.component.html a implementoval
"<script src="https://cdn.zingchart.com/zingchart.min.js"></script>"
v souboru index.html
//chart.ts
import { Component } from '@angular/core';
@Component({
selector: 'rt-chart',
templateUrl: './chart.component.html'
})
export class Chart
{
}
//chart.component.html
<!--Chart Placement[2]-->
<div id="chartDiv"></div>
<script>
var chartData = {
type: "bar", // Specify your chart type here.
title: {
text: "My First Chart" // Adds a title to your chart
},
legend: {}, // Creates an interactive legend
series: [ // Insert your series data here.
{ values: [35, 42, 67, 89]},
{ values: [28, 40, 39, 36]}
]
};
zingchart.render({ // Render Method[3]
id: "chartDiv",
data: chartData,
height: 400,
width: 600
});
</script>
Zavolal jsem to ve své již fungující webové stránce jako
Nezobrazuje se. Co dělám špatně? Něco mi uniká? Angular2 je pro mě docela nové.
Děkuji
Odpověď č. 1
S nejnovější verzí Angular2 (@2.2.3) můžete využít speciální direktivu ZingChart takto:
zing-chart.directive.ts
declare var zingchart: any;
@Directive({
selector : 'zing-chart'
})
export class ZingChartDirective implements AfterViewInit, OnDestroy {
@Input()
chart : ZingChartModel;
@HostBinding('id')
get something() {
return this.chart.id;
}
constructor(private zone: NgZone) {}
ngAfterViewInit() {
this.zone.runOutsideAngular(() => {
zingchart.render({
id : this.chart.id,
data : this.chart.data,
width : this.chart.width,
height: this.chart.height
});
});
}
ngOnDestroy() {
zingchart.exec(this.chart.id, 'destroy');
}
}
kde ZingChartModel je jen modelem vašeho grafu:
zing-chart.model.ts
export class ZingChartModel {
id: String;
data: Object;
height: any;
width: any;
constructor(config: Object) {
this.id = config['id'];
this.data = config['data'];
this.height = config['height'] || 300;
this.width = config['width'] || 600;
}
}
Zde je dokončený Příklad Plunker
