echarts4r
echarts4r copied to clipboard
e_add doesn't work with lineStyle
I am trying to change the color of lines of line charts based on the data. Typically, I've used e_add to create data-driven stylistic updates (i.e. it works well with itemStyle), however, it does not appear to work with line charts. In this example below, I have three lines (1,2,3). I want to use the color column in the data to actually lead the lines to change. The rest of the nested options work - for example using symbol within e_line changes the symbol and using the color/borderColor options for itemStyle work. However, the adjustment to lineStyle color does not work. I also checked with lineStyle type, although in this specific example, there is no direct need for it to be data-driven. e_color does work as intended, however, it does not work with a column of data, it just works for a fixed set of inputs.
data.frame(
group = c(1,1,1,2,2,2,3,3,3),
x = c(1,2,3,1,2,3,1,2,3),
y = c(4,5,6,10,8,2,3,7,4),
color = c(replicate(3,"blue"),replicate(3,"green"),replicate(3,"red")),
borderColor = "yellow",
type = "dashed"
) %>%
group_by(group) %>%
e_charts(x) %>%
e_line(y,symbol="triangle") %>%
e_add(
"itemStyle",color,borderColor
) %>%
e_add(
"lineStyle",color,type
)
Note how the line colors haven't changed (even though the the little triangle points have). The same lack of change occurs even if you remove the e_add("itemStyle") arguments.
Yes I know I'm sorry, this is to poor design choices that I made when creating the package. Had a I applied an aes
function like ggplot2 this would easily be doable.
The issue here is that itemStyle
is actually the style of the points, being a line chart though there is not lineStyle
option, this is bumped one level and therefore should be e_line(color='blue')
but currently cannot work with echarts4r given color
only accepts a character vector of length one.
I have to find a way to easily be able to apply such options from data
but conceptually I'm yet to find a reasonable solution that would provide a clean interface to do so (the code is easy but the API design more difficult)
I'm not sure I explain this well.
If you have any idea how this could work nicely let me know. I'm not behind a laptop right now but can look into this in the morning.
This makes sense to me. It appears you have some ways to edit lineStyle
when you use e_lines. Based on this example, https://gist.github.com/JohnCoene/0742920d08ba00c66a5373aff062b387, where you manipulate width, I also confirmed that color works. I know this is using e_lines
instead of e_line
and so where that actually occurs may be different.
In the absence of being able to directly manipulate the data
argument, I do wonder if there is a way to make functions like e_color
be data-driven (or other arguments like e_line(color='blue')
. I could imagine that you could have something like:
data.frame(
group = c(1,1,1,2,2,2,3,3,3),
x = c(1,2,3,1,2,3,1,2,3),
y = c(4,5,6,10,8,2,3,7,4),
color = c(replicate(3,"blue"),replicate(3,"green"),replicate(3,"red"))
) %>%
group_by(group) %>%
e_charts(x) %>%
e_line(y,symbol="triangle",color=color) %>%
e_add(
"itemStyle",color,borderColor
) %>%
e_add(
"lineStyle",color,type
)
I think the key here would be ensuring that whatever value is used is simplified accordingly. In this example, you might want to ensure that color is the same for every group (and could spit out an error if you attempt to use a column that has different values).
I'll think on this more conceptually as well.