使用约书亚·昆特的Highcharter R 包 http://jkunst.com/highcharter/,我正在尝试添加自定义工具提示and格式化数字,以便千位有逗号分隔符。如果我不自定义工具提示,当我使用以下代码时,Y 轴和工具提示格式正确:
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}"))
但是,一旦我添加了像这样的格式化程序
hc_tooltip(formatter = JS("function () { return '<b>' + this.series.name + '</b><br /> ' + this.point.y + ' <br />' + this.x;}"))
工具提示编号不再包含逗号。我想我需要做一些事情this.point.y
在格式化程序中,但我尝试过的几件事都不起作用。有谁知道我需要在格式化程序函数中添加什么才能使工具提示也显示 y 值的逗号?谢谢。
你可以尝试包裹你的this.point.y
in the Highcharts.numberFormat http://api.highcharts.com/highcharts/Highcharts.numberFormat功能。您可以在 API 链接中了解它或阅读此源描述:
/**
* 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 来保留小数位数。这意味着您应该可以使用此格式化程序:
hc_tooltip(formatter = JS("function () { return '<b>' + this.series.name + '</b><br /> ' + Highcharts.numberFormat(this.point.y, -1) + ' <br />' + this.x;}"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)