Javascript Highcharts:用逗号格式化所有数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29209491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:59:19  来源:igfitidea点击:

Highcharts: Format all numbers with comma?

javascripthighcharts

提问by Richard

I'm using Highcharts and I want to format all numbers showed anywhere in the chart (tooltips, axis labels...) with comma-separated thousands.

我正在使用 Highcharts,我想用逗号分隔的千位格式化图表中任何位置显示的所有数字(工具提示、轴标签......)。

Otherwise, the default tooltips and labels are great, and i want to keep them exactly the same.

否则,默认的工具提示和标签很棒,我想保持它们完全相同。

For example, in this chart, the number should be 2,581,326.31but otherwise exactly the same.

例如,在此图表中,数字应该2,581,326.31相同,但在其他方面完全相同。

enter image description here

在此处输入图片说明

How can I do this?

我怎样才能做到这一点?

I tried adding:

我尝试添加:

    tooltip: {
        pointFormat: "{point.y:,.0f}"
    }

But this got rid of the nice circle and series label in the tooltip - I'd like to keep that. And ideally I'd prefer to use a single option to set global number formatting, across the whole chart.

但这摆脱了工具提示中漂亮的圆圈和系列标签 - 我想保留它。理想情况下,我更喜欢使用单个选项来设置整个图表的全局数字格式。

回答by Halvor Holsten Strand

This can be set with the thousandSep(API) global option.

这可以使用thousandSep( API) 全局选项进行设置。

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

See this JSFiddle example.

请参阅此 JSFiddle 示例

回答by Alia

In case you want to show numbers without commas and spaces.

如果您想显示没有逗号和空格的数字。

eg. by default 9800 shows as 9 800.

例如。默认情况下 9800 显示为 9 800。

If you want 9800 to be shown as 9800

如果您希望 9800 显示为 9800

you can try this in tooltip:

您可以在工具提示中尝试此操作:

    tooltip: {
     pointFormat: '<span>{point.y:.f}</span>'
   }

回答by Hien Nguyen

This way worked with me.

这种方式对我有用。

I configured in yAxis option.

我在 yAxis 选项中进行了配置。

yAxis: {
  labels: {
    formatter: function() {
        return Highcharts.numberFormat(this.value, 2);
    }
  }
}