chart.js - 提供的参数与调用目标的任何签名都不匹配(angular2)

时间:2016-08-02 08:04:06

标签: angular chart.js typescript-typings

我在chart.js version 2.2.1应用中使用angular 2

当调用Chart的构造函数时,会显示错误:
Supplied parameters do not match any signature of call targetnpm start命令不会编译应用程序。

var myChart = new Chart(ctx, {
    type: this.chartType,
    data: chartData,
    options: chartOptions
});

使用上述方法,图表在浏览器中绘制 (但我必须:1)评论创建图表的代码,2)运行npm start, 3)取消注释代码)

我重新安装了TypeScript Definitions (d.ts) for chartjs

chart.d.ts中是Chart的签名:

interface Chart {
    Line(data: LinearChartData, options?: LineChartOptions): LinearInstance;
    Bar(data: LinearChartData, options?: BarChartOptions): LinearInstance;
    Radar(data: LinearChartData, options?: RadarChartOptions): LinearInstance;

    PolarArea(data: CircularChartData[], options?: PolarAreaChartOptions): CircularInstance;
    Pie(data: CircularChartData[], options?: PieChartOptions): CircularInstance;
    Doughnut(data: CircularChartData[], options?: PieChartOptions): CircularInstance;
}

declare var Chart: {
    new (context: CanvasRenderingContext2D): Chart;
    defaults: {
        global: ChartSettings;
    }
};

根据那个Typescript定义,我必须以这种方式创建图表:

var myChart = new Chart(ctx).Line(chartData, chartOptions);

通过这种方式调用Chart实例不包含Line(/*...*/)函数。

我该如何解决问题?

我已按照此处的建议安装了chart.js的输入法: https://www.nuget.org/packages/chartjs.TypeScript.DefinitelyTyped/

1 个答案:

答案 0 :(得分:0)

要解决问题中列出的问题:

问:
当调用Chart的构造函数时,会显示错误:

  • Supplied parameters do not match any signature of call target.

  • 并且npm start命令无法编译应用程序。

用于设置(angular2-rc4,chart.js-2.2.1),
在实例化Chart的组件中声明全局变量 图表:declare var Chart: any;

整个代码如下:

import { Component, Input, Output, 
         EventEmitter, ElementRef, 
         Inject, AfterViewInit} from '@angular/core';


declare var Chart: any; //THIS LINE resolves the problem listed in question!!! 


@Component({
    selector: 'my-chart',  
    template: `<canvas height="{{chartHeight}}"></canvas>`,
    styles: [`:host { display: block; }`]
})

export class LinearChartDirective implements AfterViewInit {
    @Input() chartData: Array<any>;
    @Input() chartXAxisLabels: Array<any>;

    @Input() showLegend: boolean;
    @Input() legendLabel: string;
    @Input() chartHeight: number;

    chart: any;

    constructor( @Inject(ElementRef) private element: ElementRef) {    }

    ngAfterViewInit() {        
        let context = this.element.nativeElement.children[0].getContext('2d');
        this.createChart(ctx);
    }

    createChart(ctx: CanvasRenderingContext2D) {    
        if(!this.chartData)
            return;

        let data = {
            labels: this.chartXAxisLabels,
            datasets: [{
                label: this.legendLabel,
                data: this.chartData,

                backgroundColor: ['rgba(54, 162, 235, 0.2)'],
                borderColor: ['rgba(54, 162, 235, 1)'],
                borderWidth: 1,
                lineTension: 0, // set to 0 means - No bezier

                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: 2,
                pointHitRadius: 10
            }]
        };

        let chartOptions = {           
            legend: { display: this.showLegend !== undefined ? this.showLegend : false },

            responsive: true,
            maintainAspectRatio: true,
            scales: { yAxes: [{ ticks: { beginAtZero: true } }] }
        };

        //next line is no more complaining about "Supplied parameters..."
        this.chart = new Chart(ctx, { type: 'line', data: data, options: chartOptions });
    }  
}