3

I am using Highcharter's Polar graphs and was wondering if it has the ability to change colors based on criteria. I am using the graph to look at budget versus spend. When spend < budget, I would like the column to be green. When spend > budget, I would like the column to be red. Is this possible?

For the example in the code: 'Logistics' and 'IT & Telecom' spend would be green, all other spend categories would be red.

The answer needs to be dynamic as what is over or under plan will be changing constantly.

Below is a simplified version of the code.

library (shiny)
library (highcharter)

hc <- highchart() %>% 
  hc_chart(polar = TRUE) %>% 
  hc_title(text = "Budget vs Spending") %>% 
  hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
                          "Logistics", "Office Products", "Plant Consumables",
                          "Temp Labor", "Travel", "Other"),
           tickmarkPlacement = "on",
           lineWidth = 0) %>% 
  hc_yAxis(gridLineInterpolation = "polygon",
           lineWidth = 0,
           min = 0) %>% 
  hc_series(
    list(
      name = "Spend",
      data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
      pointPlacement = "on",
      type = "column"
    ),
    list(
      name = "Budget",
      data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
      pointPlacement = "on",
      type = "line"
    )
  )

hc
Kevin
  • 1,974
  • 1
  • 19
  • 51
  • I don't know much about R, but in Highcharts JS I'd put code responsible for updating in `chart.events.render` property: http://jsfiddle.net/kkulig/88mb6mq8/ I use `redrawEnabled` flag to prevent infinite recursive loop that is caused by redrawing the chart within this event (`render` calls `redraw` event by itself). – Kamil Kulig Dec 26 '17 at 08:37

1 Answers1

4

You want to set colorByPoint to TRUE and then use an ifelse to set the colors, like this:

library(shiny)
library(highcharter)
library(tidyverse)
hc <- highchart() %>% 
  hc_chart(polar = TRUE) %>% 
  hc_title(text = "Budget vs Spending") %>% 
  hc_xAxis(categories = c("Energy", "Facilties", "IT & Telecom",
                          "Logistics", "Office Products", "Plant Consumables",
                          "Temp Labor", "Travel", "Other"),
           tickmarkPlacement = "on",
           lineWidth = 0) %>% 
  hc_yAxis(gridLineInterpolation = "polygon",
           lineWidth = 0,
           min = 0) %>% 
  hc_series(
    list(
      name = "Spend",
      data = c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000),
      pointPlacement = "on",
      colorByPoint = TRUE,
      type = "column",
      colors = ifelse(c(50000, 39000, 42000, 31000, 26000, 14000, 26000, 26000, 26000) > c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),"#F00","#0F0")
    ),
    list(
      name = "Budget",
      data = c(43000, 19000, 60000, 35000, 17000, 10000,10000,10000,10000),
      pointPlacement = "on",
      type = "line"
    )
  )

hc

hope this helps

jkr
  • 17,119
  • 2
  • 42
  • 68
Bertil Baron
  • 4,923
  • 1
  • 15
  • 24