0
votes

I am using Highcharts to display data from an API. I would like to label my yAxis as "Thousands" and have the numbers shown in a single digit. For example my graph right now labels are 2k, 4k, 6k, 8k, 10k I would like to adjust to formatting to only display 2,4,6,8,10

I am currently using formatter:function () { } to return my the 2k,4k,6k... as it previously displayed 2000,4000,6000... I am having issues figuring how to to properly format my numbers in order to display with a single digit.

My expected outcome is for the yAxis labels displayed on the left side to show 2,4,6,8,10

Here is a jsfiddle of my graph.

Also here is my code:

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]
    }]
});
1

1 Answers

0
votes

You can use the label.formatter callback with the logic shown below to display wanted format.

Demo: https://jsfiddle.net/BlackLabel/c0e2jwa6/

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

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