【问题标题】:Highcharts TypeScript, y-axis labelHighcharts TypeScript,y轴标签
【发布时间】:2013-02-20 17:17:19
【问题描述】:

y轴标签的设置方法请参考讨论Highcharts text labels for y-axis。

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

有人试过吗?

-- 更新--

我的原始 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);

在代码中,我有一个 this.value,它是每个条形图(我使用条形图)的值。在 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);

输出是

/*
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'
*/

而且它不会编译。

【问题讨论】:

  • 我不认为这是重复的。发帖人正在尝试使用该答案中的代码,但 TypeScript 编译器拒绝了它。
  • @hardywang 更新后的定义解决了您的问题吗?

标签: highcharts typescript


【解决方案1】:

更新 - Definitely Typed Highcharts definition 现在有我的修复,所以你可以下载最新版本,希望它能解决你的问题。

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

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

但是,我不确定这是否与 the documentation, which suggests you pass the yAxis options as an 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);

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

interface HighchartsOptions {
    chart?: HighchartsChartOptions;
    // others...
    yAxis?: HighchartsAxisOptions; // <-- remove the [] from this line
}

我已经提交了一个拉取请求到肯定类型来解决这个问题。

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

【讨论】:

  • 仍然有编译错误,因为未找到yearChartOptions.yAxis.plotLines.label.style.formatter(() =&gt; "$" + Highcharts.numberFormat(this.value / 1000, 0) + "k "); 格式化程序并且无法识别this 关键字。示例stackoverflow.com/questions/4987457/… 和文档api.highcharts.com/highcharts#yAxis.labels.formatter
  • 我已经更新了To achieve what I believe is the valid version评论下的例子。
  • 谢谢,我试过你的语法来实例化对象中的对象,它可以工作。我不确定我的方法有什么问题,实例化对象,然后设置对象的属性,但它不起作用。
  • 使用你的解决方案还有一个问题,请查看stackoverflow.com/questions/14992232/…我打开了另一个disssion。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
相关资源
最近更新 更多