Highcharts TypeScript,y轴标签

har*_*ang 7 highcharts typescript

有关设置y轴标签的方法,请参阅讨论Y轴Highcharts文本标签.

我在我的TypeScript定义中使用了https://github.com/borisyankov/DefinitelyTyped/blob/master/highcharts/highcharts.d.ts,但我找不到定义y轴格式化程序的方法.

以前有人试过吗?

- 更新 -

我的origianl JavaScript代码是

var yearChartOptions = {
    yAxis: {
        plotLines: [{
            label: {
                formatter: function () {
                    return '$' + Highcharts.numberFormat(this.value/1000, 0) +'k ';
                }
            },
        }]
    },
};

// Create the chart
var yearChart = new Highcharts.Chart(yearChartOptions);
Run Code Online (Sandbox Code Playgroud)

在代码中我有一个this.value,它是每个bar(我用条形图)的值.在TypeScript中(我还没有更改为删除数组).

    var yearChartOptions: HighchartsOptions = {};
    yearChartOptions.chart = {};
    yearChartOptions.yAxis = [];
    yearChartOptions.yAxis[0] = {};
    yearChartOptions.yAxis[0].plotLines = {};
    yearChartOptions.yAxis[0].plotLines.label = {};
    yearChartOptions.yAxis[0].plotLines.label.style.formatter(() => "$" + Highcharts.numberFormat(this.value / 1000, 0) + "k ");

    // Create the chart
    var yearChart = new Highcharts.Chart(yearChartOptions);
Run Code Online (Sandbox Code Playgroud)

输出是

/*
Compile Error. 
See error list for details
 D:/MyProject/Scripts/test.ts(36,56): The property 'formatter' does not exist on value of type 'HighchartsCSSObject'
D:/MyProject/Scripts/test.ts(36,107): The property 'value' does not exist on value of type 'Dashboard'
*/
Run Code Online (Sandbox Code Playgroud)

它不会编译.

Fen*_*ton 2

更新-Definitely Typed Highcharts 定义现在已得到我的修复,因此您可以下载最新版本,希望它能解决您的问题。

Highcharts 的类型定义建议您需要将 amd 选项数组传递给 yAxis。这会在 TypeScript 中编译。

var chart = new Highcharts.Chart({
    yAxis: [{
        labels: {
            formatter: function (): string {
                return 'My Label';
            }
        }
    }]
});
Run Code Online (Sandbox Code Playgroud)

但是,我不确定这是否与文档相符,文档建议您将 yAxis 选项作为 object 传递,而不是作为许多这些对象的数组传递。从只有一个 yAxis 的角度来看,这也是有意义的。

为了实现我认为有效的版本(即不是数组):

/// <reference path="highchart.d.ts" />

var yearChartOptions: HighchartsOptions = {
    yAxis: {
        plotLines: {
            label: {
                formatter: function (): string {
                    return '$' + Highcharts.numberFormat(this.value / 1000, 0) + 'k ';
                }
            },
        }
    }
};


// Create the chart
var yearChart = new Highcharts.Chart(yearChartOptions);
Run Code Online (Sandbox Code Playgroud)

您可以在此处调整您的 highchart.d.ts 文件...

interface HighchartsOptions {
    chart?: HighchartsChartOptions;
    // others...
    yAxis?: HighchartsAxisOptions; // <-- remove the [] from this line
}
Run Code Online (Sandbox Code Playgroud)

我已向 Definely Typed 提交了拉取请求来解决此问题。

基于您的代码的完整工作示例: