Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding thousands separator for custom formatted highcharts tooltip

Tags:

highcharts

I am using highcharts to in my app and want to add tooltip with thousand separator like I did it in data labels. I used custom formatting for tooltip, so what should I change to achieve this

tooltip options in highcharts

tooltip: {
                formatter: function () {
                    return (this.x + ':' + this.y);
                },
                shared: true,
                useHTML: true
            },

JS FIDDLE

like image 388
S. M. Shahinul Islam Avatar asked Oct 08 '15 09:10

S. M. Shahinul Islam


2 Answers

Use Highcharts.numberFormat() in point.x or point.y

example:

tooltip: {
  enabled: true,
  split: true,
  shared: true,
  formatter: function () {
    // The first returned item is the header, subsequent items are the points
    return [Highcharts.dateFormat("%Y-%m-%d %H:%M:%S", this.x)].concat(
      this.points
        ? this.points.map(function (point) {
            return (
              point.series.name +
              ": " +
              // numberFormat(data, decimal)
              Highcharts.numberFormat(point.y, 2)
            );
          })
        : []
    );
  },
},
like image 179
Samphan Sitthiwantana Avatar answered Jan 11 '23 06:01

Samphan Sitthiwantana


Just following Nishith Kant Chaturvedi's answer, and since there is no jsfiddle example available, here you can see how to implement that answer.

Highcharts.setOptions({
    lang: {
      decimalPoint: '.',
      thousandsSep: ','
    }
});

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: ''
    },
    xAxis: {
        categories: ['Salary']
    },
    yAxis: {
        title: {
            text: ''
        },
        stackLabels: {
            enabled: true,
            format: '{total:,.2f} $us'
        },
        labels: {
            format: "{value:,.2f} $us",
        }
    },
    legend: {
        backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
        borderColor: '#CCC',
        borderWidth: 1,
    },
    tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                    format: '{point.y:,.2f} $us',
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            }
        }
    },
    series: [{
            type: 'column',
        name: 'Tomas',
        data: [60000]
    }, {
            type: 'column',
        name: 'Amy',
        data: [18000]
    }, {
            type: 'column',
        name: 'Jenny',
        data: [85000]
    }]
});

http://jsfiddle.net/zrc5skLy/

like image 42
David J Avatar answered Jan 11 '23 06:01

David J