简体   繁体   English

如何在 highcharter 中使用图像/图标作为标签? R shiny

[英]How to use images/icons as labels in highcharter? R shiny

I am using highcharter to make a bar plot, in a shiny app, but I am having troubles displaying icons as labels on the x-axis.我正在使用 highcharter 在 shiny 应用程序中制作条形 plot,但我无法在 x 轴上将图标显示为标签。 The goal is to use svg images from a file placed in the working directory.目标是使用位于工作目录中的文件中的 svg 图像。

I have attached an example below and I have searched inspiration in the following two links but I am having troubles getting the JS-function into R shiny - setup.我在下面附上了一个示例,并在以下两个链接中搜索了灵感,但我无法将 JS 函数导入 R shiny - 设置。 https://www.highcharts.com/demo/column-comparison https://www.highcharts.com/forum/viewtopic.php?t=16609 https://www.highcharts.com/demo/column-comparison https://www.highcharts.com/forum/viewtopic.php?t=16609

I though the problem was the backslach before quotation marks, but when I display the icon in my app just using tags$div(HTML("<img src = \"logoA.svg\">")) , it displays perfectly (logoA.svg is placed in the www folder)我虽然问题是引号前的后斜线,但是当我在我的应用程序中显示图标时只使用tags$div(HTML("<img src = \"logoA.svg\">")) ,它显示完美 (logoA .svg放在www文件夹中)

Any suggestions how to solve this?任何建议如何解决这个问题?

  
library(shiny)
library(highcharter)
library(dplyr)


## app.R ##
server <- function(input, output) {
  output$plot<- renderHighchart({
    
    Label1<- c("A","A","B","B")
    Label2<- c("1","2","1","2")
    Val<- runif(4,0,100)
    col<-c("#d21e1e","#009beb","#ff5a1a","#009beb")
    Data<-data.frame(Label1,Label2,Val,col)
 
    highchart(type="chart") %>%
      hc_add_series(data = Data,type = "column",
                    hcaes(x = Label1,
                          y = Val,
                          group = Label2,
                          color = col),
                    dataLabels = list(enabled = TRUE, format='{point.mean}'))%>%
      hc_legend(enabled = F)%>% 
      hc_xAxis(type= 'category', useHTML=T, labels=list(formatter = JS("function(){
                                                          if(this.value == 'A'){
                                                            return '<img src=\"logoA.svg\"></img>';
                                                          }else if(this.value == 'B')
                                                            return '<img src=\"logoB.svg\"></img>';
                                                          }")) )

  })
}

ui <- fluidPage(
  tags$div(
    HTML("<img src=\"logoA.svg\"></img>")
  ),
  
  highchartOutput("plot")
  
)

shinyApp(ui = ui, server = server)

You were almost there, just move the useHTML=T parameter from the hc_xAxis function to the labels list:你快到了,只需将useHTML=T参数从hc_xAxis function 移动到labels列表:

    highchart(type = "chart") %>%
        hc_add_series(
            data = Data,
            type = "column",
            hcaes(
                x = Label1,
                y = Val,
                group = Label2,
                color = col
            ),
            dataLabels = list(enabled = TRUE, format = '{point.mean}')
        ) %>%
        hc_legend(enabled = F) %>%
        hc_xAxis(type = 'category',
                 labels = list(
                     formatter = JS(
                         "function(){
                              if(this.value == 'A'){
                                return '<img src=\"logoA.svg\"></img>';
                              }else if(this.value == 'B')
                                return '<img src=\"logoB.svg\"></img>';
                              }"
                     ),
                     useHTML = T
                 ))

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

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