Ionic 5 中的 Chart.js

fio*_*877 1 ionic-framework

我一直在尝试通过查看本教程( https://ichi.pro/es/ionic-5-charts-graphs-usando-la-biblioteca-chartjs-102145521332905 )在Ionic 5中使用Chart.js,但我明白了错误: 在此输入图像描述

除了文件名之外,代码几乎相同。不太明白为什么会发生这种情况,欢迎任何帮助。

import { Component, AfterViewInit, ViewChild , ElementRef } from '@angular/core';
import {Chart} from "chart.js";

@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page implements AfterViewInit{
  @ViewChild('lineCanvas') private lineCanvas: ElementRef;

  lineChart: any;

  constructor() { }

  ngAfterViewInit() {
    this.lineChartMethod();
  }

  lineChartMethod() {
    this.lineChart = new Chart(this.lineCanvas.nativeElement, {
      type: 'line',
      data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'],
        datasets: [
          {
            label: 'Sell per week',
            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: [65, 59, 80, 81, 56, 55, 40, 10, 5, 50, 10, 15],
            spanGaps: false,
          }
        ]
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

这是 HTML

<ion-content [fullscreen]="true">
   <div class="ion-padding">
    <ion-card>
      <ion-card-header>
        Line Chart
      </ion-card-header>
      <ion-card-content>
        <canvas #lineCanvas style="position: relative; height:20vh; width:40vw"></canvas>
      </ion-card-content>
    </ion-card>
  </div>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

小智 7

Chart.js 需要在加载数据之前注册控制器,但无论底层机制如何,这里都是来自 Chart.js 文档的在打字稿中初始化 Chart 的正确方法:

还提供简短的注册格式来快速注册所有内容。

从'chart.js'导入{图表,可注册};Chart.register(...registerables);

最后,有一条单独的路径可以在一行中为您执行上述操作:

从“chart.js/auto”导入图表;

来源: https: //www.chartjs.org/docs/master/getting-started/integration.html