首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向Highcharter工具提示中添加逗号格式

向Highcharter工具提示中添加逗号格式
EN

Stack Overflow用户
提问于 2017-03-19 01:45:42
回答 1查看 2.2K关注 0票数 3

使用约书亚·康特的高租R包,我试图添加一个自定义工具提示和格式数字,这样成千上万的人就有了一个逗号分隔符。使用以下代码时,如果不自定义工具提示,则Y轴和工具提示格式正确:

代码语言:javascript
复制
snow <- read.csv("https://gist.githubusercontent.com/smach/d4188d200b465cba822405c323f1501c/raw/58c3785c34304ccc5dbcef632d3acb9d6dbad40c/BosChiNYCsnowfalls.csv", stringsAsFactors = FALSE)
library("highcharter")

hcoptslang <- getOption("highcharter.lang")
hcoptslang$thousandsSep <- ","
options(highcharter.lang = hcoptslang)

highchart() %>%
  hc_chart(type = "line") %>%
  hc_title(text = "Snowfall") %>%
  hc_xAxis(categories = snow$Winter) %>%
  hc_add_series(data = snow$Boston * 10, name = "Boston") %>%
  hc_yAxis(labels = list(format = "{value:,.0f}"))

但是,一旦我添加了一个格式化程序,比如

代码语言:javascript
复制
hc_tooltip(formatter = JS("function () { return '<b>' + this.series.name + '</b><br /> ' + this.point.y + ' <br />' + this.x;}"))

工具提示号不再有逗号。我认为我需要在格式化程序中对this.point.y做一些事情,但我尝试过的几件事情都没有奏效。有人知道我需要向格式化程序函数中添加什么才能使工具提示同时显示y值的逗号吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-19 11:25:04

您可以尝试在this.point.y函数中包装您的Highcharts.numberFormat。您可以在API链接中阅读有关它的内容,也可以阅读以下源代码描述:

代码语言:javascript
复制
/**
 * Format a number and return a string based on input settings.
 *
 * @function #numberFormat
 * @memberOf Highcharts
 * @param {Number} number - The input number to format.
 * @param {Number} decimals - The amount of decimals. A value of -1 preserves
 *        the amount in the input number.
 * @param {String} [decimalPoint] - The decimal point, defaults to the one given
 *        in the lang options.
 * @param {String} [thousandsSep] - The thousands separator, defaults to the one
 *        given in the lang options.
 * @returns {String} The formatted number.
 */
H.numberFormat = function(number, decimals, decimalPoint, thousandsSep) {
    // ...
}

在我的尝试中,我会包括第一个参数,使用-1来保留小数。这意味着您应该对此格式化程序没有意见:

代码语言:javascript
复制
hc_tooltip(formatter = JS("function () { return '<b>' + this.series.name + '</b><br /> ' + Highcharts.numberFormat(this.point.y, -1) + ' <br />' + this.x;}"))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42882018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档