You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在echarts4r日历图中格式化tooltip提示框

Fixing Tooltip Formatting for echarts4r Calendar Heatmap

Got it, let's get that tooltip looking exactly how you want it! The key here is leveraging ECharts' formatter option in the tooltip, which lets you customize the display text either with a simple template string or a more flexible JavaScript function.

Option 1: Simple Template String (Easiest)

This is the quickest way to format your tooltip. You can use placeholders like {c} to reference the numeric value, and add formatting rules to round it to one decimal place.

Here's your modified code:

library(echarts4r)

dates <- seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)
year <- data.frame(date = dates, values = values)

year %>% 
  e_charts(date) %>% 
  e_calendar(range = "2018") %>% 
  e_heatmap(values, coord.system = "calendar") %>% 
  e_visual_map(max = 30) %>% 
  e_title("Calendar", "Heatmap") %>% 
  e_tooltip(
    trigger = "item", 
    show = TRUE,
    formatter = "my value is {c:.1f}" # This line handles the formatting
  )

Option 2: JavaScript Callback (For More Complex Logic)

If you ever need to add custom conditional logic or more advanced formatting, you can use a JS function. This gives you direct access to the hovered item's data via the params object:

library(echarts4r)
library(htmlwidgets) # Required for the JS() helper function

dates <- seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)
year <- data.frame(date = dates, values = values)

year %>% 
  e_charts(date) %>% 
  e_calendar(range = "2018") %>% 
  e_heatmap(values, coord.system = "calendar") %>% 
  e_visual_map(max = 30) %>% 
  e_title("Calendar", "Heatmap") %>% 
  e_tooltip(
    trigger = "item", 
    show = TRUE,
    formatter = JS("function(params) {
      // params.value holds your numeric value; toFixed(1) rounds to 1 decimal place
      return 'my value is ' + params.value.toFixed(1);
    }")
  )

What Do a, b, c, d Mean?

You mentioned confusion about these placeholders in ECharts' docs—here's a clear breakdown for your calendar heatmap use case:

  • {a}: The series name (in your code, this is "values" since that's the column passed to e_heatmap)
  • {b}: The item name (here, the date string like "2018-01-01")
  • {c}: The numeric value of the hovered cell (your values column data)
  • {d}: In calendar coordinates, this is usually the timestamp of the date—you probably won't need this for your current setup

Either option above will give you the "my value is 1.2" format you're looking for. Start with the template string since it's simpler and fits your needs perfectly!

内容的提问来源于stack exchange,提问作者Vlad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:54:03