1

I'm trying to make a line bar to specific criteria. I'd like the lines themselves to be colored black, and the area below the lines will be colored differently. However, I'm having trouble setting up the legend to show what's going on in an intuitive way. Here's some made up data:

library(echarts4r)
library(tidyverse)

dataset <- data.frame(company_id = 1:5, high_priority = seq(10, 50, length.out = 5), total = seq(200, 500, length.out = 5))

dataset %>%
  e_charts(x = company_id) %>%
  e_line(serie = total,
         name = "Total queries",
         showSymbol = F,
         lineStyle = list(
           color = "black"
         ),
         emphasis = list(disabled = T),
         areaStyle = list(
           color = "blue"
         )) %>%
  e_line(serie = high_priority,
         name = "High priority",
         showSymbol = F,
         lineStyle = list(
           color = "black"
         ),
         emphasis = list(disabled = T),
         areaStyle = list(
           color = "red"
         )) %>%
  e_color(color = c("blue", "red")) %>%
  e_legend(right = 20, orient = "vertical")

e_color sets the color of the legend symbols, however, the chart itself doesn't have symbols. I want the legend to be one blue line for "Total queries" and one red line for "High priority". It should not have circles or any other symbol, and the lines in the chart should remain black.

I've also tried supplying a data argument to e_legend(), but I haven't been able to make it work. I don't understand what data structure should be provided to that argument. But maybe that's how this could work, linking the names with specific color etc. settings.

mkranj
  • 144
  • 8

1 Answers1

1

Unfortunately, the original echarts support legend drawing only as icons. See issue 188.

You may want to try using the rectangle icon, which is easy, just add icon = list("rect", "rect") to the e_legend:

e_legend(right = 20, orient = "vertical", icon = list("rect", "rect"))

If you want lines, the solution is to create your own line-icon, one of the possibilities being to use SVG PathData, like:

e_legend(right = 20, orient = "vertical", icon = replicate(2, "path://M180 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z"))
kikon
  • 3,670
  • 3
  • 5
  • 20
  • Thank you. What is happening in icon = replicate(...)? Can I read more about it somewhere? – mkranj Apr 28 '23 at 06:28
  • @mkranj I used `replicate(2, "x")` to create a vector with the same string repeated twice. Maybe I should've used `rep("x", 2)`, since rep is simpler; see [replicate](https://www.rdocumentation.org/packages/Rpdb/versions/2.2/topics/replicate) and [rep](https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/rep). Each legend item requires its own icon; for the rectangle case I used `list("rect", "rect")` (I manually replicated it :)) but the pathdata string was too long so I had to use some function to repeat it for me. – kikon Apr 28 '23 at 22:59