5

I've made a timeline chart in highcharter, the R api for Highcharter. I want it to display a legend item for each of the color segments, but only once. The way it's rendering now, it repeats for every name. I've found that you can "link" series in higcharts in JavaScript but haven't figured out how to that in R.

Here's what I'm doing:

df %>% 
    hchart("timeline", hcaes(x = date,
                           label = paste0("<b>", company, "</b>"),
                           color = segment,
                           name = paste0("<i>", segment, "</i>")),
         dataLabels = list(allowOverlap = FALSE),
         linkedTo = "color",
         showInLegend = TRUE) %>% 
  hc_yAxis(visible = FALSE) %>% 
  hc_xAxis(type = "datetime")

And some data:

df <- structure(list(date = c(1446681600000, 1420070400000, 1.419984e+12, 
1356998400000, 1348531200000, 1310342400000, 1.238544e+12, 1.238544e+12, 
1.222992e+12, 1187913600000), company = c("SymphonyAI - Ayasdi", 
"Longbow (Orlando)", "CFD Research", "DB CyberTech", "M5 Network Security", 
"Daylight Solutions", "Sonoma Photonics", "Swift Engineering", 
"3001 International", "Scaled Composites"), segment = c("Comms & Cyber", 
"Not JADO Related", "Training & Simulation", "Comms & Cyber", 
"Comms & Cyber", "Not JADO Related", "Not JADO Related", "Not JADO Related", 
"Not JADO Related", "Not JADO Related")), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))

This is what it looks like now: enter image description here

I certainly don't need the same categories to keep repeating, right?

Ben G
  • 4,148
  • 2
  • 22
  • 42
  • Did you manage to achieve it in JavaScript? If so it will also be helpful if you submit this code in an editor such as JSFiddle to better visualize what it should look like and operate. – madepiet Oct 22 '20 at 08:59
  • My JavaScript skills leave much to be desired. I couldn’t find any examples online of a highcharter timeline with a legend. – Ben G Oct 22 '20 at 10:34
  • 1
    Here a lot depends on how you would like it to work, because if you want to keep the functionality of clicking on the legend and hiding all these elements, then you would most likely need a custom configuration to extend the default functionality: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts – madepiet Oct 23 '20 at 11:21
  • However, if it were to serve only to display these individual elements, then here it seems to me that it would be enough to separate these series, then operate on the values of showInLegend: https://api.highcharts.com/highcharts/series.timeline.showInLegend https://jsfiddle.net/BlackLabel/cn1kxf7o/ – madepiet Oct 23 '20 at 11:21
  • @madepiet ideally I would have the functionality where you hide the elements, but I’ll be very happy with just display – Ben G Oct 23 '20 at 12:55
  • Not an answer, but I took an initial look into this and it seems pretty tricky. Since you already have the legend in the labels, would it hurt to simply remove the legend altogether? – Rich Pauloo Oct 28 '20 at 00:50
  • @RichPauloo, sure, I don't NEED the legend. But I love the functionality of clicking on it and it removing the items from the plot. – Ben G Oct 28 '20 at 01:57
  • 1
    @BenG If you are interested I can provide a JS snippet. As timeline chart displays legend for each point in timeline, you can hide legend from highcharts but display your custom list and onclick of that list item, you can toggle all data points related to toggled company. – Dipen Shah Oct 29 '20 at 04:47
  • @DipenShah , sure that would be awesome – Ben G Oct 29 '20 at 10:57

0 Answers0