悬停散点图时显示变量名称

{echarts4r}这是来自GitHub 上这个答案的情节:


library(echarts4r)

library(tibble)


mtcars %>%

  rownames_to_column("model") %>%  

  e_charts(mpg) %>%

  e_y_axis(drat) %>%

  e_scatter(drat, symbol_size = 15, bind = model, scale = NULL) %>%

  e_tooltip(formatter = htmlwidgets::JS("

              function(params){

                return('<strong>' + params.name +  

              '</strong><br />x-axis: ' + params.value[0] +  

                '<br />y-axis: ' + params.value[1]

                )} 

              "))

如您所见,将鼠标悬停在一个点上时会显示汽车型号的名称和值。但是,我不知道如何以相同的方式显示变量名。我知道我可以用它们的名称手动替换“x 轴”和“y 轴”,但我想自动执行。


我检查了关于这个的 echarts 的文档,但是使用{a}(例如)不起作用。


任何的想法?


编辑:我用echarts4r 0.3.3


holdtom
浏览 235回答 3
3回答

qq_花开花谢_0

您要求的东西不存在于为 e_tooltip 传递的参数中。您可以通过传递params到console.log并在浏览器中检查此日志来访问它们:mtcars %>%&nbsp; rownames_to_column("model") %>%&nbsp;&nbsp;&nbsp; e_charts(mpg) %>%&nbsp; e_y_axis(drat) %>%&nbsp; e_scatter(drat, symbol_size = 15, bind = model, scale = NULL) %>%&nbsp; e_tooltip(formatter = htmlwidgets::JS("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function(params){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var obj_str = JSON.stringify(params);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(obj_str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return('<strong>' + params.name +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</strong><br />' + params.seriesName + ': ' + params.value[0] +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<br />mpg: ' + params.value[1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "))您会注意到 x 轴名称在下方params.seriesName,但未找到 y 轴名称。这是您检查console.logfor时会发现的示例params:{"componentType":"series",&nbsp; "componentSubType":"scatter",&nbsp; "componentIndex":0,&nbsp; "seriesType":"scatter",&nbsp; "seriesIndex":0,&nbsp; "seriesId":"\u0000drat\u00000",&nbsp; "seriesName":"drat",&nbsp; "name":"Ford Pantera L",&nbsp; "dataIndex":9,&nbsp; "data":{"value":[15.8,4.22],&nbsp; "name":"Ford Pantera L"},&nbsp; "value":[15.8,4.22],&nbsp; "color":"#c23531",&nbsp; "dimensionNames":["x","y"],&nbsp; "encode":{"x":[0],"y":[1]},&nbsp; "marker":"<span style=\"display:inline-block;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; margin-right:5px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-radius:10px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width:10px;height:10px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color:#c23531;\"></span>",&nbsp; "$vars":["seriesName","name","value"]}在这种情况下,paste0下面@Edo 的做法似乎很合理。

MYYA

我试图了解我是否在做你可能真正需要的东西......我想你想要灵活名称的原因是让它有一个单独的功能,以便在你需要时给你打电话......那么这个怎么样?它使用一种变通方法来避免名称(在您将字符串而不是未加引号的名称传递给函数的意义上)。但是,如果您需要使用名称,我们可以尝试其他方法。library(echarts4r)library(tibble)plot_escatter <- function(df, x_axis, y_axis, bind){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; df %>%&nbsp; &nbsp; &nbsp; &nbsp; e_charts_ (x_axis) %>%&nbsp; &nbsp; &nbsp; &nbsp; e_y_axis_ (y_axis) %>%&nbsp; &nbsp; &nbsp; &nbsp; e_scatter_(y_axis, symbol_size = 15, bind = bind, scale = NULL) %>%&nbsp; &nbsp; &nbsp; &nbsp; e_tooltip(formatter = htmlwidgets::JS(paste0("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function(params){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return('<strong>' + params.name + '</strong><br />", x_axis, ": ' + params.value[0] +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<br />", y_axis, ": ' + params.value[1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ")))&nbsp; &nbsp;&nbsp;}mtcars %>%&nbsp;&nbsp; &nbsp; rownames_to_column("model") %>%&nbsp;&nbsp;&nbsp; &nbsp; plot_escatter("mpg", "drat", "model")

慕容3067478

请找到下面的代码:您需要使用params.seriesName来访问列名&nbsp; &nbsp; mtcars %>%&nbsp; &nbsp;&nbsp; tibble::rownames_to_column("model") %>%&nbsp;&nbsp;&nbsp; e_charts(wt) %>%&nbsp;&nbsp;&nbsp; e_scatter(mpg, qsec, bind = model, scale = NULL) %>%&nbsp;&nbsp; e_tooltip(formatter = htmlwidgets::JS("&nbsp;&nbsp; &nbsp; function(params){&nbsp;&nbsp; &nbsp; &nbsp; return('<strong>' + params.seriesName +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</strong><br />wt: ' + params.value[0] +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<br />mpg: ' + params.value[1] + '<br> qsec:' + params.value[2]&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "))&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript