英文:
R programming Highchart Number format
问题
I am trying to draw a line chart using Highchart. I need the data format in million format. For example, for the first point in the screenshot, 2423175 should be shown as 2.42 million. How do I change the format = "{point.y}" to show in millions?
highchart() %>%
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line", dataLabels = list(
enabled = TRUE,
format = "{point.y:.2f} Million" )
) %>%
hc_tooltip(crosshairs = TRUE, borderWidth = 1.5, headerFormat= "",
pointFormat = "Year: <b>{point.x:%b-%y}</b><br> Population: <b>{point.y:.2f} Million</b>"
) %>%
hc_title(text = "Population by year") %>%
hc_subtitle(text = "2016-2020") %>%
hc_xAxis(type = "datetime", title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "count per year")) %>%
hc_legend(enabled = FALSE) %>%
hc_add_theme(custom_theme)
英文:
I am trying to draw line chart using Highchart . I need data format in Million format . Ex for the First point in screenshot 2423175 should be shown as 2.42 Million .How do i change format = "{point.y}" to show in Millions
highchart() %>%
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line",dataLabels = list(
enabled = TRUE,
format = "{point.y} " )
) %>%
hc_tooltip(cros[![enter image description here][1]][1]shairs = TRUE, borderWidth = 1.5,headerFormat= "",
pointFormat = paste("Year: <b>{point.x:%b-%y}</b> <br> Population: <b>{point.y}</b>")) %>%
hc_title(text = "Population by year") %>%
hc_subtitle(text = "2016-2020") %>%
hc_xAxis(type = "datetime", title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "count per year")) %>%
hc_legend(enabled = FALSE) %>%
hc_add_theme(custom_theme)
答案1
得分: 1
你可以使用dataLabels.formatter来格式化你的数据标签,具体信息请参考:https://api.highcharts.com/highcharts/series.line.dataLabels.formatter。我知道如何在JavaScript中执行这个操作,并将这段代码嵌入到R中的JS()函数中:
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line", dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}")
)) %>%
JS示例:https://jsfiddle.net/BlackLabel/o49zcjLv
如果有效,请告诉我。
编辑:完整的可运行代码和示例数据如下:
library(highcharter)
data <- data.frame(
y = c(54324232, 85325324, 10424324, 44234324, 74324234, 44321413))
highchart() %>%
hc_add_series(data, type = "line", hcaes(y = y), dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}"
)))
不要回答我要翻译的问题。
英文:
You can use dataLabels.formatter: https://api.highcharts.com/highcharts/series.line.dataLabels.formatter to format your dataLabels. I know how to do it in JavaScript and inject this code inside JS() function in R:
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line",dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}") )
) %>%
JS example: https://jsfiddle.net/BlackLabel/o49zcjLv
Let me know if it worked.
Edit: The whole working code with sample data:
library(highcharter)
data <- data.frame(
y = c(54324232,85325324,10424324,44234324,74324234, 44321413))
highchart() %>%
hc_add_series(data, type = "line", hcaes(y = y), dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}"
)))
答案2
得分: 1
这是一个两步的操作方式:
首先,您需要在创建图表之前将您的数字格式化为从2423175样式变为2.42样式。
data$Total <- format(round(data$Total / 1e6, 1), trim = TRUE)
接下来,在使用Highcharter创建图表时,为了在数字后面添加“百万”,将格式从format = "{point.y} "
更改为format = paste("{point.y} Million")
。您的数字现在应该以“X.XX Million”的格式显示。
英文:
Here is a 2 step way of doing it:
First, you need to format your numbers from looking like 2423175 to 2.42 before you create your plot.
data$Total <- format(round(data$Total / 1e6, 1), trim = TRUE)
Next, in order to add 'Million' after your numbers in Highcharter, change format from format = "{point.y} "
to format = paste("{point.y} Million")
while creating your plot. Your numbers should now be displayed in the format "X.XX Million".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论