在Highcharter工具提示中添加逗号格式 [英] Adding comma format to Highcharter tooltips

查看:78
本文介绍了在Highcharter工具提示中添加逗号格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Joshua Kunt的 Highcharter R软件包,我正在尝试添加自定义工具提示格式数字,因此成千上万的数字使用逗号分隔.使用以下代码时,如果我没有自定义工具提示,则Y轴和工具提示的格式正确:

Using Joshua Kunt's Highcharter R package, I'm trying to add a custom tooltip and format numbers so thousands have a comma separator. The Y axis and tooltips format correctly if I don't customize the tooltip, when I use the following code:

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值的逗号吗?谢谢.

The tooltip number no longer has the comma. I think there's something I need to do to this.point.y in the formatter, but the few things I've tried haven't worked. Does anyone know what I need to add to the formatter function to make the tooltip also display the comma for the y value? Thanks.

推荐答案

您可以尝试将this.point.y包装在 Highcharts.numberFormat 函数.您可以在API链接中阅读有关此内容的信息,也可以阅读以下源代码说明:

You can try wrapping your this.point.y in the Highcharts.numberFormat function. You can read about it in the API link or read this source description:

/**
 * 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保留小数位数.这意味着您使用此格式化程序应该没问题:

In my attempt I'd include the two first parameters, using -1 to preserve number of decimals. That means you should be fine with this formatter:

hc_tooltip(formatter = JS("function () { return '<b>' + this.series.name + '</b><br /> ' + Highcharts.numberFormat(this.point.y, -1) + ' <br />' + this.x;}"))

这篇关于在Highcharter工具提示中添加逗号格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆