Merge pull request #2043 from yha/colorbar-warn-less

Only warn for series sharing a colorbar when it matters.
This commit is contained in:
Michael Krabbe Borregaard 2019-06-02 19:30:00 +02:00 committed by GitHub
commit bbf8b50ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -421,23 +421,20 @@ function gr_set_viewport_polar()
end end
struct GRColorbar struct GRColorbar
gradient::Ref{Union{Series,Nothing}} gradients
fill::Ref{Union{Series,Nothing}} fills
lines::Ref{Union{Series,Nothing}} lines
GRColorbar() = new(nothing,nothing,nothing) GRColorbar() = new([],[],[])
end end
function gr_update_colorbar!(cbar::GRColorbar, series::Series) function gr_update_colorbar!(cbar::GRColorbar, series::Series)
style = colorbar_style(series) style = colorbar_style(series)
style === nothing && return style === nothing && return
ref = style == cbar_gradient ? cbar.gradient : list = style == cbar_gradient ? cbar.gradients :
style == cbar_fill ? cbar.fill : style == cbar_fill ? cbar.fills :
style == cbar_lines ? cbar.lines : style == cbar_lines ? cbar.lines :
error("Unknown colorbar style: $style.") error("Unknown colorbar style: $style.")
if ref[] !== nothing push!(list, series)
@warn "Overwriting colorbar entry"
end
ref[] = series
end end
function gr_contour_levels(series::Series, clims) function gr_contour_levels(series::Series, clims)
@ -466,6 +463,16 @@ function gr_colorbar_colors(series::Series, clims)
round.(Int,colors) round.(Int,colors)
end end
function _cbar_unique(values, propname)
out = last(values)
if any(x != out for x in values)
@warn "Multiple series with different $propname share a colorbar. " *
"Colorbar may not refelct all series correctly."
end
out
end
# add the colorbar # add the colorbar
function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims) function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
GR.savestate() GR.savestate()
@ -474,16 +481,19 @@ function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
gr_set_viewport_cmap(sp) gr_set_viewport_cmap(sp)
GR.setscale(0) GR.setscale(0)
GR.setwindow(xmin, xmax, zmin, zmax) GR.setwindow(xmin, xmax, zmin, zmax)
if (series = cbar.gradient[]) !== nothing if !isempty(cbar.gradients)
gr_set_gradient(series) series = cbar.gradients
gr_set_transparency(get_fillalpha(series)) gr_set_gradient(_cbar_unique(gr_get_color.(series),"color"))
gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha"))
GR.cellarray(xmin, xmax, zmax, zmin, 1, 256, 1000:1255) GR.cellarray(xmin, xmax, zmax, zmin, 1, 256, 1000:1255)
end end
if (series = cbar.fill[]) !== nothing if !isempty(cbar.fills)
gr_set_gradient(series) series = cbar.fills
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
levels = contour_levels(series, clims) gr_set_gradient(_cbar_unique(gr_get_color.(series), "color"))
gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha"))
levels = _cbar_unique(contour_levels.(series, Ref(clims)), "levels")
# GR implicitly uses the maximal z value as the highest level # GR implicitly uses the maximal z value as the highest level
if levels[end] < clims[2] if levels[end] < clims[2]
@warn("GR: highest contour level less than maximal z value is not supported.") @warn("GR: highest contour level less than maximal z value is not supported.")
@ -491,20 +501,22 @@ function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
# promotion in case levels is an integer array # promotion in case levels is an integer array
levels = [levels[1:end-1]; clims[2]] levels = [levels[1:end-1]; clims[2]]
end end
colors = gr_colorbar_colors(series, clims) colors = gr_colorbar_colors(last(series), clims)
for (from, to, color) in zip(levels[1:end-1], levels[2:end], colors) for (from, to, color) in zip(levels[1:end-1], levels[2:end], colors)
GR.setfillcolorind(color) GR.setfillcolorind(color)
gr_set_transparency(get_fillalpha(series))
GR.fillrect( xmin, xmax, from, to ) GR.fillrect( xmin, xmax, from, to )
end end
end end
if (series = cbar.lines[]) !== nothing if !isempty(cbar.lines)
gr_set_gradient(series) series = cbar.lines
gr_set_line(get_linewidth(series), get_linestyle(series), get_linecolor(series, clims)) gr_set_gradient(_cbar_unique(gr_get_color.(series),"color"))
gr_set_transparency(get_linealpha(series)) gr_set_line(_cbar_unique(get_linewidth.(series), "line width"),
levels = contour_levels(series, clims) _cbar_unique(get_linestyle.(series), "line style"),
colors = gr_colorbar_colors(series, clims) _cbar_unique(get_linecolor.(series, Ref(clims)), "line color"))
gr_set_transparency(_cbar_unique(get_linealpha.(series), "line alpha"))
levels = _cbar_unique(contour_levels.(series, Ref(clims)), "levels")
colors = gr_colorbar_colors(last(series), clims)
for (line, color) in zip(levels, colors) for (line, color) in zip(levels, colors)
GR.setlinecolorind(color) GR.setlinecolorind(color)
GR.polyline([xmin,xmax], [line,line] ) GR.polyline([xmin,xmax], [line,line] )
@ -594,17 +606,22 @@ function gr_set_gradient(c)
end end
function gr_set_gradient(series::Series) function gr_set_gradient(series::Series)
color = gr_get_color(series)
color !== nothing && gr_set_gradient(color)
end
function gr_get_color(series::Series)
st = series[:seriestype] st = series[:seriestype]
if st in (:surface, :heatmap) || isfilledcontour(series) if st in (:surface, :heatmap) || isfilledcontour(series)
gr_set_gradient(series[:fillcolor]) series[:fillcolor]
elseif st in (:contour, :wireframe) elseif st in (:contour, :wireframe)
gr_set_gradient(series[:linecolor]) series[:linecolor]
elseif series[:marker_z] != nothing elseif series[:marker_z] != nothing
gr_set_gradient(series[:markercolor]) series[:markercolor]
elseif series[:line_z] != nothing elseif series[:line_z] != nothing
gr_set_gradient(series[:linecolor]) series[:linecolor]
elseif series[:fill_z] != nothing elseif series[:fill_z] != nothing
gr_set_gradient(series[:fillcolor]) series[:fillcolor]
end end
end end