如何将Chart.js与Angular 7与数据库中的数据集成

问题描述 投票:0回答:1

Chart.js包含来自Chartjs link的数据

code screen images

angular typescript chart.js line ngx-charts
1个回答
0
投票

嘿,我发现这篇文章非常有用:http://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/

它帮助我理解了ChartJS。

只是以示例中提到的格式格式化API数据,这样做会很好。

in .HTML file  ``` <canvas #lineCanvas></canvas> ```

in .TS file  ```
import { Component, ViewChild, OnInit } from '@angular/core';
import { NavController, MenuController, NavParams, ToastController } from 'ionic-angular';
import { Chart } from 'chart.js';
import { DashboardProvider } from '../../providers/dashboard/dashboard';
import { tokenKey } from '@angular/core/src/view';

@Component({
    selector: 'page-dashboard',
    templateUrl: 'dashboard.html',
    providers: [DashboardProvider]
})

export class DashboardPage implements OnInit {
    @ViewChild('lineCanvas') lineCanvas;
    lineChart: any;
    GetAthleteScoreByTest : any;

    constructor(public navCtrl: NavController,
        private dashApis: DashboardProvider) {
    }

    ngOnInit() {
      this.dashApis.GetAthleteScoreByTest(ACCESS_TOKEN)
                .subscribe(
                    (response) => {
                        this.GetAthleteScoreByTest = response["data"];
                        let labels = ['0'];
                        let data = [0];
                        let i = 1;
                        this.GetAthleteScoreByTest.forEach(element => {
                            data.push(element["AthleteScore"]);
                            labels.push(i.toString());
                            ++i;
                        });
                        this.lineChart = new Chart(this.lineCanvas.nativeElement, {

                            type: 'line',
                            data: {
                                labels: labels,
                                datasets: [
                                    {
                                        label: "Score Comparision",
                                        fill: false,
                                        lineTension: 0.1,
                                        backgroundColor: "rgba(75,192,192,0.4)",
                                        borderColor: "rgba(75,192,192,1)",
                                        borderCapStyle: 'butt',
                                        borderDash: [],
                                        borderDashOffset: 0.0,
                                        borderJoinStyle: 'miter',
                                        pointBorderColor: "rgba(75,192,192,1)",
                                        pointBackgroundColor: "#fff",
                                        pointBorderWidth: 1,
                                        pointHoverRadius: 5,
                                        pointHoverBackgroundColor: "rgba(75,192,192,1)",
                                        pointHoverBorderColor: "rgba(220,220,220,1)",
                                        pointHoverBorderWidth: 2,
                                        pointRadius: 1,
                                        pointHitRadius: 10,
                                        data: data,
                                        spanGaps: false,
                                    }
                                ]
                            }

                        });
                    },
                    (error) => {

                    }
                );
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.