Skip to content

Commit c5f69f0

Browse files
author
chmerdon
committed
moved legend and limits to outside labels, removed padding in gridplots
1 parent b3c1cde commit c5f69f0

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

ext/GridVisualizeUnicodePlotsExt.jl

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,16 @@ function reveal(ctx::SubVisualizer, TP::Type{UnicodePlotsType})
4242
end
4343

4444

45-
function region_legend!(canvas, title, x, y, colors, text_color)
45+
function region_legend!(plt, title, y0, colors)
4646
# legend by annotate!
47-
for (i, char) in enumerate(title)
48-
UnicodePlots.char_point!(canvas, x + i - 1, y, char, text_color, false)
49-
end
50-
startx = x + length(title)
47+
UnicodePlots.label!(plt, :r, y0, title)
5148
for r in 1:length(colors)
5249
red, green, blue = UInt32.(colors[r])
5350
uint_color = (red << 16) | (green << 8) | blue
54-
reg_string = "$r "
55-
for char in reg_string
56-
startx += 1
57-
UnicodePlots.char_point!(canvas, startx, y, char, uint_color, false)
58-
end
51+
y0 += 1
52+
UnicodePlots.label!(plt, :r, y0, " " * string(r), uint_color)
5953
end
60-
return
54+
return y0
6155
end
6256

6357
gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{3}}, grid) = @warn "3D gridplots are not implemented for the UnicodePlots backend"
@@ -84,7 +78,7 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
8478
# determine resolution (divided by 10, to reduce pixel count in the terminal)
8579
layout = ctx[:layout]
8680
resolution = ctx[:size] ./ 12 ./ (layout[2], layout[1])
87-
legend_space = 4
81+
legend_space = 0
8882
aspect = ctx[:aspect] * resolution[1] / (resolution[1] + legend_space)
8983

9084
if (true) # auto scale feature, do we want this?
@@ -102,7 +96,7 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
10296
resolution = @. Int(round(resolution))
10397

10498
# create UnicodePlots.Canvas
105-
padding = 0.1 * max(ex[2] - ex[1], ey[2] - ey[1])
99+
padding = 0 #0.1 * max(ex[2] - ex[1], ey[2] - ey[1])
106100
ex = (ex[1] - 2 * padding, ex[2] + 0.5 * padding)
107101
ey = (ey[1] - padding, ey[2] + padding)
108102
CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ?
@@ -198,21 +192,26 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
198192
)
199193
end
200194

201-
text_color = UnicodePlots.ansi_color(:normal)
202195

203-
region_legend!(canvas, "cell regions: ", 2, 1, colors, text_color)
204-
region_legend!(canvas, "bface regions:", 2, 2, bcolors, text_color)
196+
plt = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border])
197+
198+
y0 = region_legend!(plt, " cell", 1, [])
199+
y0 = region_legend!(plt, "regions", 2, colors)
200+
region_legend!(plt, " bface", y0 + 2, [])
201+
region_legend!(plt, "regions", y0 + 3, bcolors)
205202

206203
# corner coordinates
207204
ex = extrema(view(coords, 1, :))
208205
ey = extrema(view(coords, 2, :))
209-
UnicodePlots.annotate!(canvas, ex[1], ey[1], "$(Float16(ex[1]))", text_color, false; valign = :top)
210-
UnicodePlots.annotate!(canvas, ex[2], ey[1], "$(Float16(ex[2]))", text_color, false; valign = :top, halign = :right)
211-
UnicodePlots.annotate!(canvas, ex[1] - 1.5 * padding, ey[1], "$(Float16(ey[1]))", text_color, false; halign = :left)
212-
UnicodePlots.annotate!(canvas, ex[1] - 1.5 * padding, ey[2], "$(Float16(ey[2]))", text_color, false; halign = :left)
206+
UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
207+
UnicodePlots.label!(plt, :b, "x")
208+
UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
209+
UnicodePlots.label!(plt, :l, 1, string(Float16(ey[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
210+
UnicodePlots.label!(plt, :l, round(Int, (resolution[2] + 1) / 2), "y")
211+
UnicodePlots.label!(plt, :l, resolution[2], string(Float16(ey[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
213212

214213
# plot
215-
ctx[:figure] = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border])
214+
ctx[:figure] = plt
216215
return reveal(ctx, TP)
217216
end
218217

@@ -237,12 +236,14 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
237236
end
238237

239238
# determine resolution (divided by 5, to reduce pixel count in the terminal)
239+
ncellregions = num_cellregions(grid)
240+
nbregions = num_bfaceregions(grid)
240241
layout = ctx[:layout]
241-
resolution = (Int(round(ctx[:size][1] / 6 / layout[2])), 5)
242+
resolution = (Int(round(ctx[:size][1] / 6 / layout[2])), max(7, 5 + ncellregions + nbregions))
242243

243244
# create UnicodePlots.Canvas
244-
legend_space = 5
245-
padding = 0.05 * (ex[2] - ex[1])
245+
legend_space = 0 #5
246+
padding = 0 #0.05 * (ex[2] - ex[1])
246247
ex = (ex[1] - padding, ex[2] + padding)
247248
CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ?
248249
canvas = CanvasType(
@@ -253,7 +254,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
253254

254255
# plot all edges in the grid
255256
cellregions = grid[CellRegions]
256-
ncellregions = num_cellregions(grid)
257257
cmap = region_cmap(max(2, ncellregions))
258258
ctx[:cmap] = cmap
259259
colors = [
@@ -273,18 +273,17 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
273273
for k in 1:size(cen, 2)
274274
UnicodePlots.lines!(
275275
canvas,
276-
coords[1, cellnodes[cen[1, k], j]], 0.3,
277-
coords[1, cellnodes[cen[2, k], j]], 0.3;
276+
coords[1, cellnodes[cen[1, k], j]], 0.5,
277+
coords[1, cellnodes[cen[2, k], j]], 0.5;
278278
color = colors[r]
279279
)
280280
end
281281
end
282282
for j in 1:nnodes
283-
UnicodePlots.annotate!(canvas, coords[1, j], 0.4, "", text_color, false)
283+
UnicodePlots.annotate!(canvas, coords[1, j], 0.5, "", text_color, false)
284284
end
285285

286286
# plot boundary nodes with bregion_cmap colors
287-
nbregions = num_bfaceregions(grid)
288287
bcmap = bregion_cmap(nbregions)
289288
ctx[:bcmap] = bcmap
290289
bcolors = [
@@ -300,20 +299,25 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
300299
for j in 1:nbfaces
301300
red, green, blue = UInt32.(bcolors[bfaceregions[j]])
302301
uint_color = (red << 16) | (green << 8) | blue
303-
UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.4, "", UInt32(uint_color), false)
302+
UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.5, "", UInt32(uint_color), false)
304303
end
305304

305+
plt = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border])
306306

307-
region_legend!(canvas, "cell regions: ", 2, 1, colors, text_color)
308-
region_legend!(canvas, "bface regions:", 2, 2, bcolors, text_color)
307+
y0 = region_legend!(plt, " cell", 1, [])
308+
y0 = region_legend!(plt, "regions", 2, colors)
309+
region_legend!(plt, " bface", y0 + 2, [])
310+
region_legend!(plt, "regions", y0 + 3, bcolors)
309311

310312

313+
# corner coordinates
311314
ex = extrema(view(coords, 1, :))
312-
UnicodePlots.annotate!(canvas, 0, 0.1, "$(Float16(ex[1]))", text_color, false)
313-
UnicodePlots.annotate!(canvas, ex[2], 0.1, "$(Float16(ex[2]))", text_color, false)
315+
UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
316+
UnicodePlots.label!(plt, :b, "x")
317+
UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
314318

315319
# plot
316-
ctx[:figure] = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border])
320+
ctx[:figure] = plt
317321

318322
return reveal(ctx, TP)
319323
end

0 commit comments

Comments
 (0)