如何在 highcharter-histogram 工具提示中编辑得分范围
How to edit the score ranges in highcharter-histogram tooltip
我正在使用 highcharter 包制作直方图。我想摆脱默认分数范围显示 -(x, y]
- 并将其替换为以下内容:score ranges: x to y
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
hchart(apple, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
pointFormat = "Score Range: {point.x} to {point.x} <br> Number of Apples: {point.y}") %>%
hc_legend(enabled = FALSE)
例如,在下图中,我想去掉标题 (30, 35]
并用 Score Range: 30 to 35
替换它。
首先你需要知道你的直方图的区间长度是多少:
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
h <- hist(apple, breaks = 20)
d <- diff(h$breaks)[1]
d
> d
[1] 5
现在,您需要使用 pointFormatter
而不是 pointFormat
,因为这样您可以更好地控制工具提示的输出。 pointFormat
需要一个字符串模板,pointFormatter
需要一个 javascript 函数。
您将 delta
放入该函数中以获得每个间隔的正确限制。显然你可以做得更优雅,但这就是我的想法。
hchart(h, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
headerFormat = "",
pointFormatter = JS("function() {
return 'Score Range:' + (this.x - 5/2) + ' to ' + (this.x + 5/2) + '<br> Number of Apples:' + this.y;
}")) %>%
hc_legend(enabled = FALSE)
最后您使用 headerFormat = ""
删除了 header。
我正在使用 highcharter 包制作直方图。我想摆脱默认分数范围显示 -(x, y]
- 并将其替换为以下内容:score ranges: x to y
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
hchart(apple, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
pointFormat = "Score Range: {point.x} to {point.x} <br> Number of Apples: {point.y}") %>%
hc_legend(enabled = FALSE)
例如,在下图中,我想去掉标题 (30, 35]
并用 Score Range: 30 to 35
替换它。
首先你需要知道你的直方图的区间长度是多少:
library(highcharter)
apple <- c(0, 22, 5, 32, 34, 35, 56, 67, 42, 67, 12, 99, 46, 78, 43, 67, 33, 11)
h <- hist(apple, breaks = 20)
d <- diff(h$breaks)[1]
d
> d
[1] 5
现在,您需要使用 pointFormatter
而不是 pointFormat
,因为这样您可以更好地控制工具提示的输出。 pointFormat
需要一个字符串模板,pointFormatter
需要一个 javascript 函数。
您将 delta
放入该函数中以获得每个间隔的正确限制。显然你可以做得更优雅,但这就是我的想法。
hchart(h, color = "#a40c19", breaks = 20) %>%
hc_yAxis(title = list(text = "Number of Apples")) %>%
hc_xAxis(title = list(text = "Score (0-100)")) %>%
hc_tooltip(borderWidth = 1, sort = TRUE, crosshairs = TRUE,
headerFormat = "",
pointFormatter = JS("function() {
return 'Score Range:' + (this.x - 5/2) + ' to ' + (this.x + 5/2) + '<br> Number of Apples:' + this.y;
}")) %>%
hc_legend(enabled = FALSE)
最后您使用 headerFormat = ""
删除了 header。