简体   繁体   English

Plotly 自定义模式栏图标更改颜色和形状,并在 R 的新窗口中打开链接

[英]Plotly custom modebar icon change color and shape, and open link in a new window from R

I am using a custom modebar icon to open a link in a new window, and have some related problems我正在使用自定义模式栏图标在新窗口中打开链接,并且遇到了一些相关问题

Problem 1) Custom icon is not a square.问题 1)自定义图标不是正方形。 If rescaled to fit in a square would be too small.如果重新缩放以适应正方形将太小。 How do you get it to display properly?你如何让它正确显示? To edit in the html that you get after knitting your rmarkdown is straightforward, by right clicking on your icon, and selecting inspect, you can change the width from 1em to 2em to get a wide rectangle.要在编织 rmarkdown 后获得的 html 中进行编辑很简单,通过右键单击您的图标,然后选择检查,您可以将宽度从 1em 更改为 2em 以获得一个宽矩形。

<svg viewBox="0 0  NaN" class="icon" height="1em" width="2em">

How do you control this in R?你如何在 R 中控制它?

Problem 2) Custom icon comes in default drab gray.问题 2)自定义图标默认为单调灰色。 How to get your preferred color?如何获得你喜欢的颜色? To edit in the html that you get after knitting your rmarkdown is straightforward, by right clicking on your icon, and selecting inspect, and adding the style = fill... code below, your color can be used:要在编织 rmarkdown 后获得的 html 中进行编辑很简单,只需右键单击您的图标,然后选择检查,然后在下面添加 style = fill... 代码,就可以使用您的颜色:

transform="scale(1) translate(0, -8)" style="fill:#7ac143"></path>

How do you control this in R?你如何在 R 中控制它? Example code below doesn't use the colour setting.下面的示例代码不使用颜色设置。

Problem 3) Icon has a link which ideally opens in a new window.问题 3) Icon 有一个链接,理想情况下可以在新窗口中打开。 The plotly button does open a new window. plotly 按钮会打开一个新窗口。 Example code below for the custom icon doesn't use the "blank" option to open in a new window.下面的自定义图标示例代码不使用“空白”选项在新窗口中打开。

How can you make this open in a new window?你怎么能在新窗口中打开它?

---
title: "Custom icons in Plotly"
output: html_document
---

```{r echo = FALSE, message = FALSE}
library(plotly)
library(htmlwidgets)
library(ggplot2)

data <- data.frame(
    Time = round(runif(10), 2),
    Value = round(runif(10), 2),
    Type = rep(c("A", "B"), each = 5)
    )

gg <- ggplot(data = data) +
    theme(legend.title = element_blank()) +
    geom_point(mapping = aes(x = Time, y = Value, colour = Type), size = 2)

# (1) svg path copied from svg file opened in notepad

# logo-mastercard.svg from https://github.com/plotly/plotly-icons/tree/master/src/svg
mc_icon_svg_path = "M29.375 16c0-1.438-0.375-2.813-1.063-4.063-0.75-1.188-1.75-2.125-2.938-2.875-1.313-0.688-2.625-1.063-4-1.063-1.813 0-3.438 0.563-4.875 1.625 1.313 1.188 2.125 2.563 2.563 4.188h-0.75c-0.375-1.375-1.125-2.688-2.313-3.75-1.188 1.063-1.938 2.375-2.313 3.75h-0.75c0.438-1.625 1.25-3 2.563-4.188-1.438-1.063-3.063-1.625-4.875-1.625-1.375 0-2.688 0.375-4 1.063-1.188 0.75-2.188 1.688-2.938 2.875-0.688 1.25-1.063 2.625-1.063 4.063s0.375 2.813 1.063 4.063c0.75 1.188 1.75 2.125 2.938 2.875 1.313 0.688 2.625 1.063 4 1.063 1.813 0 3.438-0.563 4.875-1.625-1.188-1.063-2-2.313-2.5-3.875h0.75c0.438 1.313 1.188 2.5 2.25 3.438 1.063-0.938 1.813-2.125 2.25-3.438h0.75c-0.5 1.563-1.313 2.813-2.5 3.875 1.438 1.063 3.063 1.625 4.875 1.625 1.375 0 2.688-0.375 4-1.063 1.188-0.75 2.188-1.688 2.938-2.875 0.688-1.25 1.063-2.625 1.063-4.063zM6.125 14.063h1.25l-0.625 3.875h-0.813l0.5-2.938-1.063 2.938h-0.625v-2.938l-0.5 2.938h-0.813l0.688-3.875h1.188v2.375zM9.875 15.688c0 0.188-0.063 0.375-0.063 0.563-0.063 0.313-0.125 0.625-0.188 0.875 0 0.25-0.063 0.438-0.125 0.625v0.188h-0.625v-0.375c-0.188 0.313-0.5 0.438-0.875 0.438-0.25 0-0.375-0.063-0.5-0.188-0.188-0.188-0.25-0.438-0.25-0.688 0-0.375 0.125-0.688 0.375-0.875 0.313-0.188 0.688-0.313 1.125-0.313h0.313v-0.188c0-0.188-0.188-0.25-0.563-0.25-0.188 0-0.5 0-0.813 0.125 0-0.188 0.063-0.438 0.125-0.688 0.313-0.125 0.625-0.188 0.938-0.188 0.75 0 1.125 0.313 1.125 0.938zM8.938 16.5h-0.188c-0.438 0-0.688 0.188-0.688 0.5 0 0.188 0.063 0.313 0.25 0.313s0.313-0.063 0.438-0.188c0.125-0.125 0.188-0.313 0.188-0.625zM12.188 14.813l-0.125 0.75c-0.188-0.063-0.375-0.063-0.625-0.063s-0.375 0.063-0.375 0.25c0 0.063 0 0.125 0.063 0.188l0.25 0.125c0.375 0.25 0.563 0.5 0.563 0.875 0 0.688-0.375 1.063-1.25 1.063-0.375 0-0.688-0.063-0.813-0.063 0-0.188 0.063-0.438 0.125-0.75 0.313 0.063 0.563 0.125 0.688 0.125 0.313 0 0.5-0.063 0.5-0.25 0-0.063-0.063-0.188-0.063-0.188-0.125-0.125-0.188-0.188-0.375-0.188-0.375-0.188-0.563-0.5-0.563-0.875 0-0.688 0.375-1.063 1.188-1.063 0.375 0 0.688 0 0.813 0.063zM13.438 14.813h0.375l-0.063 0.813h-0.438c0 0.188-0.063 0.375-0.063 0.563 0 0.063-0.063 0.125-0.063 0.25 0 0.188-0.063 0.25-0.125 0.313v0.25c0 0.188 0.125 0.25 0.313 0.25 0.063 0 0.125 0 0.25-0.063l-0.125 0.75c-0.125 0-0.313 0.063-0.625 0.063-0.438 0-0.625-0.25-0.625-0.688 0-0.25 0-0.563 0.125-0.875l0.313-2.063h0.813zM16.375 15.875c0 0.313 0 0.563-0.063 0.813h-1.625c0 0.188 0.063 0.375 0.125 0.438 0.125 0.125 0.313 0.188 0.563 0.188 0.313 0 0.563-0.063 0.875-0.25l-0.125 0.813c-0.188 0.063-0.5 0.125-0.875 0.125-0.875 0-1.375-0.5-1.375-1.375 0-0.625 0.125-1.063 0.438-1.375 0.25-0.313 0.563-0.5 0.938-0.5s0.688 0.125 0.875 0.313c0.188 0.188 0.25 0.438 0.25 0.813zM14.75 16h0.938v-0.188l-0.063-0.125c0-0.063-0.063-0.125-0.063-0.125-0.063 0-0.125-0.063-0.188-0.063h-0.125c-0.25 0-0.438 0.125-0.5 0.5zM18.438 14.813c-0.063 0.063-0.125 0.375-0.313 0.938-0.188-0.063-0.313 0.063-0.5 0.313-0.125 0.5-0.188 1.125-0.313 1.875h-0.875l0.063-0.188c0.188-1.25 0.313-2.25 0.438-2.938h0.813l-0.125 0.438c0.188-0.188 0.313-0.313 0.438-0.375 0.125-0.125 0.25-0.125 0.375-0.063zM21.25 14.188l-0.188 0.813c-0.25-0.125-0.5-0.188-0.688-0.188-0.375 0-0.625 0.125-0.813 0.375s-0.25 0.563-0.25 1.063c0 0.313 0.063 0.563 0.188 0.688 0.125 0.188 0.313 0.25 0.563 0.25 0.188 0 0.438-0.063 0.688-0.188l-0.125 0.875c-0.188 0.063-0.438 0.125-0.75 0.125-0.438 0-0.75-0.188-1.063-0.5-0.25-0.25-0.375-0.625-0.375-1.188 0-0.625 0.188-1.188 0.563-1.625 0.313-0.438 0.75-0.688 1.313-0.688 0.188 0 0.5 0.063 0.938 0.188zM23.625 15.688c0 0-0.063 0.125-0.063 0.25s0 0.25 0 0.313c-0.063 0.25-0.125 0.563-0.188 0.938 0 0.375-0.063 0.625-0.125 0.75h-0.625v-0.375c-0.188 0.313-0.5 0.438-0.875 0.438-0.25 0-0.375-0.063-0.5-0.188-0.188-0.188-0.25-0.438-0.25-0.688 0-0.375 0.125-0.688 0.375-0.875 0.313-0.188 0.625-0.313 1.063-0.313h0.313c0.063-0.063 0.063-0.125 0.063-0.188 0-0.188-0.188-0.25-0.5-0.25-0.25 0-0.563 0-0.875 0.125 0-0.188 0-0.438 0.125-0.688 0.375-0.125 0.625-0.188 0.938-0.188 0.75 0 1.125 0.313 1.125 0.938zM22.688 16.5h-0.188c-0.438 0-0.688 0.188-0.688 0.5 0 0.188 0.125 0.313 0.25 0.313 0.188 0 0.313-0.063 0.438-0.188s0.188-0.313 0.188-0.625zM25.625 14.813c-0.125 0.188-0.25 0.5-0.313 0.938-0.188-0.063-0.313 0.063-0.438 0.313s-0.188 0.875-0.375 1.875h-0.813l0.063-0.188c0.188-1 0.313-2 0.375-2.938h0.813c0 0.188-0.063 0.313-0.063 0.438 0.125-0.188 0.25-0.313 0.375-0.375 0.188-0.063 0.313-0.125 0.375-0.063zM27.688 14.063h0.875l-0.688 3.875h-0.75l0.063-0.313c-0.188 0.25-0.438 0.375-0.75 0.375-0.375 0-0.563-0.125-0.688-0.375-0.25-0.25-0.375-0.563-0.375-0.875 0-0.625 0.188-1.063 0.5-1.438 0.188-0.313 0.5-0.5 0.875-0.5 0.313 0 0.563 0.125 0.813 0.375zM27.375 16.125c0-0.375-0.188-0.563-0.438-0.563-0.188 0-0.375 0.063-0.5 0.313-0.063 0.125-0.125 0.375-0.125 0.75s0.125 0.563 0.375 0.563c0.188 0 0.375-0.063 0.5-0.25s0.188-0.438 0.188-0.813z"

# (2) Scale/translate works, after some trial and error

# Notes
# (3) Size of viewbox ideally is controlled via here to show rectangular icon neatly.
# (4) Colour control doesn't work
# (5) Open link in new window doesn't work 

mc_button <- list(
    name = "MasterCard",
    icon = list(
        path = mc_icon_svg_path,
        transform = "scale(1) translate(0, -8)",
        style = "fill:#7ac143"
        ),
        click = htmlwidgets::JS(
        "function(gd) {window.location.href = 'http://www.mastercard.com', target='_blank';}"
    )
)

# (6) Some additional customisation
ggplotly(gg) %>%
    config(modeBarButtonsToAdd = list(mc_button)) %>%
    config(displayModeBar = TRUE)

Problem 1) I dont really know a good solution for this, except trying to rescale it until its ok.问题 1)我真的不知道对此有什么好的解决方案,除了尝试重新调整它直到正常为止。 But you can certainly set the width to "2em" using the htmlwidgets::onRender method.但是您当然可以使用htmlwidgets::onRender方法将宽度设置为“2em”。 I use it for changing the fill aswell.我也用它来改变填充。

Problem 2) You can use onRender to change the styling of the svg path .问题 2)您可以使用onRender来更改svg path的样式。 Inside the function I define 2 css selectors using the data-title MasterCard .在函数内部,我使用data-title MasterCard定义了 2 个 css 选择器。 One for the svg and one for the path inside the svg and then you can change their css settings with Jquery's css() .一个用于 svg,一个用于 svg 内的路径,然后您可以使用 Jquery 的css()更改它们的 css 设置。

Problem 3) Try to use window.open instead.问题 3)尝试使用window.open代替。 That will open the page in a new tab.这将在新选项卡中打开页面。

mc_button <- list(
  name = "MasterCard",
  icon = list(
    path = mc_icon_svg_path[[1]]
    ,transform = "scale(0.6 0.6) translate(-3, -2)"
  ),
  click = htmlwidgets::JS(
    "function(gd) {window.open('http://www.mastercard.com', '_blank')}"
  )
)

ggplotly(gg) %>%
  config(modeBarButtonsToAdd = list(mc_button)) %>%
  config(displayModeBar = TRUE) %>% 
  htmlwidgets::onRender('function(el, x) {
                         $("[data-title=\'MasterCard\'] svg path").css("fill", "#7ac143");
                         $("[data-title=\'MasterCard\'] svg").css("width","2em");
                        }')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM