Angular 5和chart.js ts错误

时间:2018-04-30 05:50:50

标签: angular typescript chart.js

在Angular 5中我安装了chartjs及其类型:

纱线添加chart.js --save

纱线添加@ types / chartjs --dev

我有这样的图表一切看起来不错但在终端我有以下错误..我怎么能解决这个问题,谢谢:

我的图表此代码位于ngAfterinit:

import * as Chart from 'chart.js';


new Chart(<HTMLCanvasElement>document.getElementById("user-rev-chart"), {
  type: 'horizontalBar',
  data: {
    labels: ["5 star", "4 start", "3 start", "2 star", "1 star"],
    datasets: [
      {
        fill: true,
        label: false,
        backgroundColor: ["#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3"],
        data: [10, 2, 5, 4, 5]
      }
    ]
  },
  options: {
    maintainAspectRatio: false,
    responsive: false,
    legend: {display: false},
    title: {
      display: false,
      text: ''
    },
    scales:
      {
        yAxes: [{
          // barPercentage: 0.4,
          barThickness: 20,
          barPercentage: .5,
          categoryPercentage: .2,
          isFixedWidth: true,
          //Number - Pixel width of the bar
          barWidth: 20,
          gridLines: {
            display: false
          },
          ticks: {
            min: 0,
            stepSize: 1,
            fixedStepSize: 1,
          }
        }],
        xAxes: [{
          display: false,
          gridLines: {
            display: false
          },
          ticks: {
            min: 0,
            stepSize: 1,
            fixedStepSize: 1,
          }
        }],
      }
  }
});

ERROR:

 ERROR in src/app/components/home/detail/detail.component.ts(253,77): error TS2345: Argument of type '{ type: string; data: { labels: string[]; datasets: { fill: true; label: boolean; backgroundColor...' is not assignable to parameter of type 'ChartConfiguration'.
  Types of property 'data' are incompatible.
    Type '{ labels: string[]; datasets: { fill: true; label: boolean; backgroundColor: string[]; data: numb...' is not assignable to type 'ChartData'.
      Types of property 'datasets' are incompatible.
        Type '{ fill: true; label: boolean; backgroundColor: string[]; data: number[]; }[]' is not assignable to type 'ChartDataSets[]'.
          Type '{ fill: true; label: boolean; backgroundColor: string[]; data: number[]; }' is not assignable to type 'ChartDataSets'.
            Types of property 'label' are incompatible.
              Type 'boolean' is not assignable to type 'string'.

1 个答案:

答案 0 :(得分:1)

尝试angular2-chartjs这对于chartjs非常有用的角度库

npm install angular2-chartjs chart.js --save
npm install @types/chart.js --save

在app.module.ts中导入并声明 ChartModule

...
import { ChartModule } from 'angular2-chartjs';

@NgModule({
  imports:      [...,  ChartModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

AppComponent.ts 中声明图表图表,类型,数据,选项属性并在ngOnInit方法中指定值

...
@ViewChild(ChartComponent) chart: ChartComponent;
type;
data;
options;
...

ngOnInit() {

    this.type = 'horizontalBar';
    this.data = {
        labels: ["5 star", "4 start", "3 start", "2 star", "1 star"],
        datasets: [
            {
                fill: true,
                label: false,
                backgroundColor: ["#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3"],
                data: [10, 2, 5, 4, 5]
            }
        ]
    }
    this.options = {
        maintainAspectRatio: false,
        responsive: false,
        legend: { display: false },
        title: {
            display: false,
            text: ''
        },
        scales:
            {
                yAxes: [{
                    // barPercentage: 0.4,
                    barThickness: 20,
                    barPercentage: .5,
                    categoryPercentage: .2,
                    isFixedWidth: true,
                    //Number - Pixel width of the bar
                    barWidth: 20,
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        min: 0,
                        stepSize: 1,
                        fixedStepSize: 1,
                    }
                }],
                xAxes: [{
                    display: false,
                    gridLines: {
                        display: false
                    },
                    ticks: {
                        min: 0,
                        stepSize: 1,
                        fixedStepSize: 1,
                    }
                }],
            }
    }
}

AppComponent.html 模板中包含图表标记

...
<chart #chart [type]="type" [data]="data" [options]="options" style="height: 100%"></chart>

预览水平条实施的链接https://stackblitz.com/edit/angular-geq984