Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highcharts tooltip format millions billions

Tags:

highcharts

I want to display couple of points on a highcharts line chart which are big numbers.

e.g. 100,000, 10,000,000, 1,000,000,000

When I display these, the y axis automatically formats the number into 100 k, 10 M, 1,000 M etc but the tooltip still shows the actual big number.

Is it possible to show 1,000,000,000 as 1 B or 1000 M in the tooltip itself.

Example - http://jsfiddle.net/ynCKW/1/

I am trying to play with the numberFormat function but I dont think its the right function.

Highcharts.numberFormat(this.y,0)

Do I have to write a custom function which would do this formatting in the tooltip?

like image 471
Sumedh Avatar asked Apr 28 '13 06:04

Sumedh


People also ask

Is it possible to use tooltipformatter for all charts?

So setting the format once for all charts is much more convenient then setting it for every single chart. Also the formatted date in the header is missing now. In our case tooltipFormatter applies format only for y property, I found couple ways how to add format not only for y,

Where can I find the Highcharts API?

These pages outline the chart configuration options, and the methods and properties of Highcharts objects. Feel free to search this API through the search bar or the navigation tree in the sidebar. Options for the tooltip that appears when the user hovers over a series or point.

How extensible is Highcharts?

There are loads of extensibility points, if you don't like the default behaviour or need to change it slightly to fit your requirements, then it's more than likely Highcharts has you covered. Highcharts provides some very sane defaults, however the more you use it and want to push its boundaries the more you'll have to do yourself.

How to calculate symbols in a Highcharts chart?

You can use Highcharts modified function which calculates symbols. To do that, you need to use labels.formatter.


2 Answers

You can use the same logic as implemented in Highcharts core:

    tooltip: {
        formatter: function () {
            var ret = '',
                multi,
                axis = this.series.yAxis,
                numericSymbols = ['k', 'M', 'G', 'T', 'P', 'E'],
                i = numericSymbols.length;
            while (i-- && ret === '') {
                multi = Math.pow(1000, i + 1);
                if (axis.tickInterval >= multi && numericSymbols[i] !== null) {
                    ret = Highcharts.numberFormat(this.y / multi, -1) + numericSymbols[i];
                }
            }
            return ret;
        }
    },

And jsFiddle: http://jsfiddle.net/ynCKW/2/

EDIT for Highcharts v6:

We can call build-in method, which should be easier to maintain: http://jsfiddle.net/BlackLabel/ynCKW/104/

    tooltip: {
        valueSuffix: '',
        formatter: function () {
            var axis = this.series.yAxis;

            return axis.defaultLabelFormatter.call({
                axis: axis,
                value: this.y
            });
        }
    },
like image 66
Paweł Fus Avatar answered Oct 15 '22 23:10

Paweł Fus


Piggybacking off @Pawel Fus, a slight tweak allows you to have negative currency values as well but with the negative outside the $ (i.e. -$100K versus -$-100k).

function () {
    var isNegative = this.value < 0 ? '-' : '';
    var absValue = Math.abs(this.value);
    return isNegative + '$' + this.axis.defaultLabelFormatter.call({
        axis: this.axis,
        value: absValue
    });
}

Here is a jsFiddle: http://jsfiddle.net/4yuo9mww/1/

like image 37
Danton Noriega Avatar answered Oct 16 '22 00:10

Danton Noriega