Giter Site home page Giter Site logo

Comments (2)

pvictor avatar pvictor commented on June 4, 2024 1

Hello Simon,

You have to use some JavaScript in tooltip's formatter, here's an example:

library(dplyr)
library(tidyr)
library(apexcharter)

my_data <- mtcars %>% 
  select(mpg, qsec) %>% 
  add_rownames("car") %>% 
  mutate(diff_mpg = round(mpg - lag(mpg), 1), diff_qsec = round(qsec - lag(qsec), 1)) %>%
  select(car, diff_mpg, diff_qsec) %>% 
  gather(var, value, -car)

my_chart <- apex(my_data, type = "bar", aes(car, value, fill = var)) %>% 
  ax_plotOptions(bar = bar_opts(
    horizontal = FALSE,
    endingShape = "flat",
    columnWidth = "70%",
    dataLabels = list(
      position = "top"
    ))
  ) 

# Color value
my_chart %>% 
  ax_tooltip(
    y = list(
      formatter = JS(
        "function(value) {",
        "var color = value >= 0 ? 'forestgreen' : 'firebrick';",
        "return '<span style=\"font-weight: 700; color:' + color + ';\">' +  value + '</span>';",
        "}"
      )
    )
  )

# Use an icon: first we need to add FontAwesome dependency
my_chart$dependencies <- htmltools::htmlDependencies(shiny::icon("home")) # add FontAwesome HTML dependency
my_chart %>% 
  ax_tooltip(
    y = list(
      formatter = JS(
        "function(value) {",
        
        # https://fontawesome.com/v4.7.0/icon/arrow-down
        "var icon_up = '<i class=\"fa fa-arrow-up\" aria-hidden=\"true\"></i>';",
        "var icon_down = '<i class=\"fa fa-arrow-down\" aria-hidden=\"true\"></i>';",
        
        "var icon = value >= 0 ? icon_up : icon_down;",
        "return value + '  ' + icon;",
        "}"
      )
    )
  )

With colors it gives:
image

And with icon:
image

Of course you can combine the two or color the icon.

Victor

from apexcharter.

systats avatar systats commented on June 4, 2024

thanks a lot. Works perfect.

from apexcharter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.