为自定义格式的highcharts工具提示添加千位分隔符

时间:2015-10-08 09:15:29

标签: highcharts

我在我的应用程序中使用highcharts,并希望添加千位分隔符的工具提示,就像我在数据标签中所做的那样。我使用工具提示的自定义格式,所以我应该改变什么来实现这个

highcharts中的工具提示选项

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

JS FIDDLE

3 个答案:

答案 0 :(得分:9)

在lang中使用千位分隔符

$(function () {
Highcharts.setOptions({

    lang: {
      decimalPoint: '.',
      thousandsSep: ','
    }
});

并在tooltip-formatter中使用

   tooltip: {
             pointFormat: '<b>{point.x} :</b>' + 'Count: <b>{point.y:,.0f}</b>',

            shared: true,
            useHTML: true
        }

Updated fiddle with separator without decimal point

答案 1 :(得分:1)

仅遵循Nishith Kant Chaturvedi的答案,并且由于没有可用的jsfiddle示例,因此您可以在此处看到如何实现该答案。

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/

答案 2 :(得分:0)

在 point.x 或 point.y 中使用 Highcharts.numberFormat()

示例:

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)
            );
          })
        : []
    );
  },
},