R编程Highchart数字格式化

huangapple go评论90阅读模式
英文:

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)
英文:

R编程Highchart数字格式化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() %&gt;% 
      hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = &quot;line&quot;,dataLabels = list(
        enabled = TRUE,
        format = &quot;{point.y} &quot;  )
                    ) %&gt;%
      hc_tooltip(cros[![enter image description here][1]][1]shairs = TRUE, borderWidth = 1.5,headerFormat= &quot;&quot;,
                 pointFormat = paste(&quot;Year: &lt;b&gt;{point.x:%b-%y}&lt;/b&gt; &lt;br&gt; Population: &lt;b&gt;{point.y}&lt;/b&gt;&quot;)) %&gt;%
      hc_title(text = &quot;Population  by year&quot;) %&gt;% 
      hc_subtitle(text = &quot;2016-2020&quot;) %&gt;%
      hc_xAxis(type = &quot;datetime&quot;, title = list(text = &quot;Year&quot;)) %&gt;%
      hc_yAxis(title = list(text = &quot;count per year&quot;))      %&gt;%
      hc_legend(enabled = FALSE) %&gt;% 
      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 = &quot;line&quot;,dataLabels = list(
    enabled = TRUE,
    formatter = JS(&quot;function() {
      	return (this.y / 1000000).toFixed(2) + &#39;M&#39;
      }&quot;) )
  ) %&gt;%

JS example: https://jsfiddle.net/BlackLabel/o49zcjLv

Let me know if it worked.

Edit: The whole working code with sample data:

library(highcharter)

data &lt;- data.frame(
  y = c(54324232,85325324,10424324,44234324,74324234, 44321413))

highchart() %&gt;% 
  hc_add_series(data, type = &quot;line&quot;, hcaes(y = y), dataLabels = list(
    enabled = TRUE,
    formatter = JS(&quot;function() {
        return (this.y / 1000000).toFixed(2) + &#39;M&#39;
      }&quot;
    )))

答案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 &lt;- format(round(data$Total / 1e6, 1), trim = TRUE)

Next, in order to add 'Million' after your numbers in Highcharter, change format from format = &quot;{point.y} &quot; to format = paste(&quot;{point.y} Million&quot;) while creating your plot. Your numbers should now be displayed in the format "X.XX Million".

huangapple
  • 本文由 发表于 2020年1月6日 20:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定