首页 > 解决方案 > 带有 Angular 组件的 Chart.js - 类型“字符串”不可分配给类型“ChartTitleOptions | 不明确的'

问题描述

我正在尝试使用 Chart.js 在我的网站上实现一个聊天组件,但出现错误

“类型 'string' 不可分配给类型 'ChartTitleOptions | undefined'”

,版本有问题吗?

import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { Chart } from 'chart.js'

@Component({
  selector: 'app-line-chart',
  templateUrl: './line-chart.component.html',
  styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent implements OnInit {
  @Input() stock!: string;
  LineChart:any = []
  constructor() { }

  ngOnInit() {
    this.LineChart = new Chart('linechart', {
      type: 'line',
      data: {
        labels: ['jan', 'fev'],
        datasets: [{
          label: 'Number xxxxxx',
          data: [1, 2],
          fill: false,
          lineTension: 0.3,
          borderColor: 'red',
          borderWidth: 1
        }]
      },
      options: {
        title: 'Line Chart',
        display: true
      },
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      }
    });
  }
}

标签: angularchartschart.js

解决方案


title 属性不接受选项的字符串。相反,它需要一个对象,您可以在其中配置有关标题的内容,包括字符串。所以你的配置看起来像这样:

options: {
  title:{
    display: true,
    text: 'TitleText to display'
  }
}

文档:https ://www.chartjs.org/docs/2.9.4/configuration/title.html


推荐阅读