【问题标题】:Highcharts - How to format yAxis label to have single digit?Highcharts - 如何将 yAxis 标签格式化为单个数字?
【发布时间】:2020-05-13 21:54:31
【问题描述】:

我正在使用 Highcharts 显示来自 API 的数据。我想将我的 yAxis 标记为“数千”,并将数字显示为一位数。例如,我现在的图表标签是 2k, 4k, 6k, 8k, 10k 我想调整格式以仅显示 2,4,6,8,10

我目前正在使用formatter:function () { } 来返回我的2k,4k,6k...,因为它以前显示2000,4000,6000... 我在确定如何正确格式化我的数字以便以单个数字显示时遇到问题。

我的预期结果是左侧显示的 yAxis 标签显示 2,4,6,8,10

这是我的图表的jsfiddle

这也是我的代码:

Highcharts.chart('container', {
    chart: {
        type: 'line'
    },
    title: {
        text: 'Monthly Average Temperature'
    },
    subtitle: {
        text: 'Source: WorldClimate.com'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    yAxis: {
    labels: {
                formatter: function () {
                    return this.axis.defaultLabelFormatter.call(this);
                }
            },
        title: {
            text: 'Thousands'
        }
    },
    plotOptions: {
        line: {
            dataLabels: {
                enabled: false
            },
            enableMouseTracking: false
        }
    },
    series: [{
        name: 'Tokyo',
        data: [3625, 4320, 4055, 4852, 5120, 6251, 7852, 8000, 9102, ]
    }, {
        name: 'London',
        data: [2210, 3152, 3852, 4102, 4300, 4657, 6521, 7022, 7982]
    }]
});

【问题讨论】:

    标签: javascript highcharts


    【解决方案1】:

    您可以使用 label.formatter 回调和如下所示的逻辑来显示想要的格式。

    演示:https://jsfiddle.net/BlackLabel/c0e2jwa6/

      yAxis: {
        labels: {
          formatter() {
            return this.value / 1000
          }
        },
        title: {
          text: 'Thousands'
        }
      },
    

    https://api.highcharts.com/highcharts/yAxis.labels.formatter

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多