I was trying to create a polar barchart with the echarts4r
library and the chart is generated properly. I have x
, y
, and z
fields in the dataset. To generate the chart we need only the x
and y
fields. Now I want to show x
, y
and z
in the tooltip. x
and y
are showing well in the tooltip, however the z
value is undefined. See below an example code:
library(echarts4r)
# create example dataset
df <- data.frame(x = c("A", "B", "C"), y = c(10, 20, 30), z = c("foo", "bar", "baz"))
# create polar bar chart
df %>%
e_charts(x) %>%
e_bar(y, coord_system = "polar") %>%
e_angle_axis(startAngle = 90, max = 150, axisLine = list(show = FALSE), axisTick = list(show = FALSE),
splitLine = list(show = FALSE), axisLabel = list(show = FALSE)) %>%
e_radius_axis(type = "category", data = df$x, axisLabel = list(interval =0,margin = 50, fontSize= 15, color = 'black'),
axisLine = list(show = FALSE), axisTick = list(show = FALSE), z = 10) %>%
e_polar() %>%
e_legend(show = FALSE) %>%
e_tooltip(formatter = htmlwidgets::JS("
function(params) {
return params.name + '<br>' + params.value + '<br>' + params.data.z;
}
"))
How can I also show the z
value in the tooltip?
You first have to include the values of z
into your chart. This is usually not very difficult using e_add_nested()
, however, there seems to be a bug in combination with e_polar()
.
In order to get it work, I used the fix_add_nested
function from this very good answer. If you include this, you can access the z
values using params.data.itemStyle.z
.
library(echarts4r)
# create example dataset
df <- data.frame(x = c("A", "B", "C"), y = c(10, 20, 30), z = c("foo", "bar", "baz"))
fix_add_nested <- function(e) {
e$x$opts$series[[1]]$data <- lapply(
e$x$opts$series[[1]]$data,
function(x) { names(x) <- c("value", "itemStyle"); x }
)
e
}
# create polar bar chart
df %>%
e_charts(x) %>%
e_bar(y, coord_system = "polar") %>%
e_angle_axis(startAngle = 90, max = 150, axisLine = list(show = FALSE), axisTick = list(show = FALSE),
splitLine = list(show = FALSE), axisLabel = list(show = FALSE)) %>%
e_radius_axis(type = "category", data = df$x, axisLabel = list(interval =0,margin = 50, fontSize= 15, color = 'black'),
axisLine = list(show = FALSE), axisTick = list(show = FALSE), z = 10) %>%
e_polar() %>%
e_legend(show = FALSE) %>%
e_add_nested("values", z) %>%
fix_add_nested() %>%
e_tooltip(formatter = htmlwidgets::JS("
function(params) {
return params.name + '<br>' + params.value + '<br>' + params.data.itemStyle.z;
}
"))