UnicodePlots: fix NaN segments plot (#3794)

This commit is contained in:
t-bltg 2021-09-04 12:46:34 +02:00 committed by GitHub
parent 0d2bc3f227
commit 748104c807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 39 deletions

View File

@ -19,7 +19,7 @@ _canvas_map() = (
# do all the magic here... build it all at once, since we need to know about all the series at the very beginning
function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
plt.o = UnicodePlots.Plot[]
canvas_map = _canvas_map()
for sp in plt.subplots
xaxis = sp[:xaxis]
yaxis = sp[:yaxis]
@ -36,17 +36,16 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
y = Float64[ylim[1]]
# create a plot window with xlim/ylim set, but the X/Y vectors are outside the bounds
ct = _canvas_type[]
canvas_type = if ct == :auto
isijulia() ? UnicodePlots.AsciiCanvas : UnicodePlots.BrailleCanvas
canvas_type = if (ct = _canvas_type[]) == :auto
isijulia() ? :ascii : :braille
else
_canvas_map()[ct]
ct
end
o = UnicodePlots.Plot(
x,
y,
canvas_type;
canvas_map[canvas_type];
title = sp[:title],
xlim = xlim,
ylim = ylim,
@ -54,7 +53,6 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
ylabel = yaxis[:guide],
border = isijulia() ? :ascii : :solid,
)
for series in series_list(sp)
o = addUnicodeSeries!(sp, o, series, sp[:legend] != :none, xlim, ylim)
end
@ -66,18 +64,26 @@ end
# add a single series
function addUnicodeSeries!(
sp::Subplot{UnicodePlotsBackend},
o,
up::UnicodePlots.Plot,
series,
addlegend::Bool,
xlim,
ylim,
)
attrs = series.plotattributes
st = attrs[:seriestype]
st = series[:seriestype]
# special handling
# get the series data and label
x, y = if st == :straightline
straightline_data(series)
elseif st == :shape
shape_data(series)
else
float(series[:x]), float(series[:y])
end
# special handling (src/interface)
if st == :histogram2d
return UnicodePlots.densityplot!(o, attrs[:x], attrs[:y])
return UnicodePlots.densityplot(x, y)
elseif st == :heatmap
rng = range(0, 1, length = length(UnicodePlots.COLOR_MAP_DATA[:viridis]))
cmap = [(red(c), green(c), blue(c)) for c in get(get_colorgradient(series), rng)]
@ -92,29 +98,19 @@ function addUnicodeSeries!(
end
# now use the ! functions to add to the plot
if st in (:path, :straightline)
if st in (:path, :straightline, :shape)
func = UnicodePlots.lineplot!
elseif st == :scatter || attrs[:markershape] != :none
elseif st == :scatter || series[:markershape] != :none
func = UnicodePlots.scatterplot!
# elseif st == :bar
# func = UnicodePlots.barplot!
elseif st == :shape
func = UnicodePlots.lineplot!
else
error("Series type $st not supported by UnicodePlots")
end
# get the series data and label
x, y = if st == :straightline
straightline_data(attrs)
elseif st == :shape
shape_data(attrs)
else
[collect(float(attrs[s])) for s in (:x, :y)]
end
label = addlegend ? attrs[:label] : ""
label = addlegend ? series[:label] : ""
lc = attrs[:linecolor]
for (n, segment) in enumerate(series_segments(series, st; check = true))
i, rng = segment.attr_index, segment.range
lc = get_linecolor(series, i)
if typeof(lc) <: UnicodePlots.UserColorType
color = lc
elseif typeof(lc) <: RGBA
@ -124,7 +120,9 @@ function addUnicodeSeries!(
color = :auto
end
func(o, x, y; color = color, name = label)
up = func(up, x[rng], y[rng]; color = color, name = n == 1 ? label : "")
end
return up
end
# -------------------------------

View File

@ -1250,8 +1250,8 @@ _backend_skips = Dict(
],
:inspectdr => [4, 6, 10, 22, 24, 28, 30, 38, 43, 45, 47, 48, 49, 50, 51, 55],
:unicodeplots => [
5, # log scales unsupported
6, # embedded images unsupported
10, # histogram2d
13, # markers unsupported
16, # nested layout unsupported
20, # annotations unsupported
@ -1263,7 +1263,6 @@ _backend_skips = Dict(
33, # grid lines unsupported
34, # framestyle unsupported
37, # ribbons / filled unsupported
38, # histogram2D
43, # heatmap with DateTime
45, # error bars
47, # mesh3D unsupported