首页 文章

Kendo,TypeScript定义错误:

提问于
浏览
1

我正在使用下面显示的语法(取自http://demos.telerik.com/kendo-ui/bar-charts/index)在TypeScript文件中创建一个Kendo图表:

function createChart() {
            $("#chart").kendoChart({
                title: {
                    text: "Site Visitors Stats \n /thousands/"
                },
                legend: {
                    visible: false
                },
                seriesDefaults: {
                    type: "bar"
                },
                series: [{
                    name: "Total Visits",
                    data: [56000, 63000, 74000, 91000, 117000, 138000]
                }, {
                    name: "Unique visitors",
                    data: [52000, 34000, 23000, 48000, 67000, 83000]
                }],
                valueAxis: {
                    max: 140000,
                    line: {
                        visible: false
                    },
                    minorGridLines: {
                        visible: true
                    },
                    labels: {
                        rotation: "auto"
                    }
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                    majorGridLines: {
                        visible: false
                    }
                },
                tooltip: {
                    visible: true,
                    template: "#= series.name #: #= value #"
                }
            });
        }

当我这样做时,我得到以下错误,这似乎表明ChartOptions预计是一个数组:

“错误70类型的参数'{title:{text:string;}; legend:{visible:boolean;}; seriesDefaults:{type:string;}; ser ...'不能分配给'ChartOptions类型的参数' . 属性类别'categoryAxis'是不兼容的 . 类型'{[x:number]:undefined; categories:string []; majorGridLines:{visible:boolean;};}'不能分配给'ChartCategoryAxisItem []'类型 . 类型'{[x:number]中缺少属性'length':undefined; categories:string []; majorGridLines:{visible:boolean;};}' . “

如果我修改createChart函数,如下所示,错误消失,但图表不呈现:

function createChart() {
            $("#chart").kendoChart([{ //Notice opening array bracket
                title: {
                    text: "Site Visitors Stats \n /thousands/"
                },
                legend: {
                    visible: false
                },
                seriesDefaults: {
                    type: "bar"
                },
                series: [{
                    name: "Total Visits",
                    data: [56000, 63000, 74000, 91000, 117000, 138000]
                }, {
                    name: "Unique visitors",
                    data: [52000, 34000, 23000, 48000, 67000, 83000]
                }],
                valueAxis: {
                    max: 140000,
                    line: {
                        visible: false
                    },
                    minorGridLines: {
                        visible: true
                    },
                    labels: {
                        rotation: "auto"
                    }
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                    majorGridLines: {
                        visible: false
                    }
                },
                tooltip: {
                    visible: true,
                    template: "#= series.name #: #= value #"
                }
            }]); // And closing array
        }

还有其他人遇到过这个问题吗?在我看来是定义文件中的错误?

2 回答

  • 1

    我能够通过将valueAxis和categoryAxis值定义为数组来解决此问题,如下所示:

    function createChart() {
                $("#chart").kendoChart({
                    title: {
                        text: "Site Visitors Stats \n /thousands/"
                    },
                    legend: {
                        visible: false
                    },
                    seriesDefaults: {
                        type: "bar"
                    },
                    series: [{
                        name: "Total Visits",
                        data: [56000, 63000, 74000, 91000, 117000, 138000]
                    }, {
                        name: "Unique visitors",
                        data: [52000, 34000, 23000, 48000, 67000, 83000]
                    }],
                    valueAxis: [{
                        max: 140000,
                        line: {
                            visible: false
                        },
                        minorGridLines: {
                            visible: true
                        },
                        labels: {
                            rotation: "auto"
                        }
                    }],
                    categoryAxis: [{
                        categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                        majorGridLines: {
                            visible: false
                        }
                    }],
                    tooltip: {
                        visible: true,
                        template: "#= series.name #: #= value #"
                    }
                });
            }
    
  • 0

    我已经为这种类型定义创建了一个补丁,它允许您使用的确切代码 .

    它太大而无法放置在Stack Overflow上,所以补丁就在这里:https://github.com/Steve-Fenton/DefinitelyTyped/blob/patch-1/kendo-ui/kendo-ui.d.ts

    如果您的代码与此定义一起工作,我可以向绝对类型项目发送拉取请求以使其更新(因此它将作为NuGet包提供等) .

相关问题