【问题标题】:Javascript Highcharts: series datalabel formatter function to loop data not formatting all data form the arrayJavascript Highcharts:用于循环数据的系列数据标签格式化程序函数不格式化数组中的所有数据
【发布时间】:2015-06-23 23:06:16
【问题描述】:

我遇到了格式化程序方法没有从数组中获取所有数据的问题。例如:

   var test = [1234, 4567, 1564, 7899];

   chartData.plotOptions.series.datalabels.formatter = function () {
       for (var i = 0; i < test.length; i++) {
           // formatNumber() is a dummy method in this example
           return formatNumber(test[i]);
       }
   };

   After the loop, the datalabel only shows the first index data on each segment of bar chart. 

结果只返回第一个索引。我不确定为什么它没有遍历整个数组。有人可以帮帮我吗?

【问题讨论】:

    标签: javascript arrays loops foreach highcharts


    【解决方案1】:

    for 循环的第一次迭代之后,您的代码将return,这就是为什么您只能看到第一个索引。

    也就是说,formatter 是一个应用于每个数据点的回调,因此您不需要循环测试值。您应该尝试以下方法:

    chartData.plotOptions.series.datalabels.formatter = function() {
        return formatNumber(this.y);
    }
    

    请参阅formatter documentation 了解可用数据。

    【讨论】:

    • 做到了!谢谢!
    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多