3434
3535conversion_trait (x:: Type{<:Violin} ) = SampleBased ()
3636
37- function getviolincolor (c, i:: Int )
38- c isa NamedTuple || return c
39- return i == 1 ? c. right : c. left
37+ getuniquevalue (v, idxs) = v
38+
39+ function getuniquevalue (v:: AbstractVector , idxs)
40+ u = view (v, idxs)
41+ f = first (u)
42+ msg = " Collection must have the same value across all indices"
43+ all (isequal (f), u) || throw (ArgumentError (msg))
44+ return f
4045end
4146
4247function plot! (plot:: Violin )
@@ -63,7 +68,8 @@ function plot!(plot::Violin)
6368 l1, l2 = limits isa Function ? limits (v) : limits
6469 i1, i2 = searchsortedfirst (k. x, l1), searchsortedlast (k. x, l2)
6570 kde = (x = view (k. x, i1: i2), density = view (k. density, i1: i2))
66- return (x = key. x, side = key. side, kde = kde, median = median (v))
71+ c = getuniquevalue (color, idxs)
72+ return (x = key. x, side = key. side, color = to_color (c), kde = kde, median = median (v))
6773 end
6874
6975 max = if max_density === automatic
@@ -77,7 +83,7 @@ function plot!(plot::Violin)
7783
7884 vertices = Vector{Point2f0}[]
7985 lines = Pair{Point2f0, Point2f0}[]
80- side = Int []
86+ colors = RGBA{Float32} []
8187
8288 for spec in specs
8389 scale = 0.5 * violinwidth/ max
@@ -108,26 +114,19 @@ function plot!(plot::Violin)
108114 push! (lines, median_left => median_right)
109115 end
110116
111- push! (side , spec. side )
117+ push! (colors , spec. color )
112118 end
113119
114- return (vertices = vertices, lines = lines, side = side )
120+ return (vertices = vertices, lines = lines, colors = colors )
115121 end
116122
117- for i in - 1 : 1
118- sidevertices = lift (s -> s. vertices[s. side .== i], signals)
119- sidecolor = lift (c -> getviolincolor (c, i), color)
120- # TODO : fix empty `poly` to avoid this check
121- if ! isempty (sidevertices[])
122- poly! (
123- plot,
124- sidevertices,
125- color = sidecolor,
126- strokecolor = plot[:strokecolor ],
127- strokewidth = plot[:strokewidth ],
128- )
129- end
130- end
123+ poly! (
124+ plot,
125+ lift (s -> s. vertices, signals),
126+ color = lift (s -> s. colors, signals),
127+ strokecolor = plot[:strokecolor ],
128+ strokewidth = plot[:strokewidth ],
129+ )
131130 linesegments! (
132131 plot,
133132 lift (s -> s. lines, signals),
0 commit comments