How to implement zingchart in angular2

I have an existing project in which I want to implement zingcharts.

I tried 3 different tutorials mainly from " https://blog.zingchart.com/2016/07/19/zingchart-and-angular-2-charts-back-at-it-again/ ".

However, I cannot get this to work in my project. So I decided that first I would try to implement it in the easiest way, and then try better. This is what I did, but he is not buying yet.

Being new to angular2, I'm not quite sure if this will work.

I went to the ZingChart website and tried to implement this basic example -> https://www.zingchart.com/docs/getting-started/your-first-chart/

So, I created 2 chart.ts and chart.component.html files and implemented

"<script src="https://cdn.zingchart.com/zingchart.min.js"></script>"

in 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>

-

. ? -, . Angular2 .

+3
1

angular2 (@2.2.3) ZingChart :

< > -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');
  }
}

ZingChartModel - :

< > -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;
  }
}

+5

Source: https://habr.com/ru/post/1666794/


All Articles