Skip to content

Commit 18d8e7c

Browse files
chmerdonpjaap
authored andcommitted
gridplots don't ignore xlimits and ylimits anymore, 1d plot only plots nodes if resolution is high enough, fixed text_color in 1d scalarplot, removed old padding code
1 parent 0a1738f commit 18d8e7c

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

ext/GridVisualizeUnicodePlotsExt.jl

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,26 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
6161

6262
# find bounding box
6363
coords = grid[Coordinates]
64-
ex = extrema(view(coords, 1, :))
65-
ey = extrema(view(coords, 2, :))
64+
xlimits = ctx[:xlimits]
65+
ylimits = ctx[:ylimits]
66+
if xlimits[1] < xlimits[2]
67+
ex = xlimits
68+
else
69+
ex = extrema(view(coords, 1, :))
70+
end
71+
if ylimits[1] < ylimits[2]
72+
ey = ylimits
73+
else
74+
ey = extrema(view(coords, 2, :))
75+
end
6676

6777
# line color for interior edges
6878
edge_color = UnicodePlots.ansi_color(:normal)
6979

7080
# determine resolution (divided by 10, to reduce pixel count in the terminal)
7181
layout = ctx[:layout]
7282
resolution = ctx[:size] ./ 12 ./ (layout[2], layout[1])
73-
legend_space = 0
74-
aspect = ctx[:aspect] * resolution[1] / (resolution[1] + legend_space)
83+
aspect = ctx[:aspect] * resolution[1] / (resolution[1])
7584

7685
if (true) # auto scale feature, do we want this?
7786
wx = ex[2] - ex[1]
@@ -88,14 +97,11 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
8897
resolution = @. Int(round(resolution))
8998

9099
# create UnicodePlots.Canvas
91-
padding = 0 #0.1 * max(ex[2] - ex[1], ey[2] - ey[1])
92-
ex = (ex[1] - 2 * padding, ex[2] + 0.5 * padding)
93-
ey = (ey[1] - padding, ey[2] + padding)
94100
CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ?
95101
canvas = CanvasType(
96-
resolution[2], resolution[1] + legend_space, # number of rows and columns (characters)
102+
resolution[2], resolution[1], # number of rows and columns (characters)
97103
origin_y = ey[1], origin_x = ex[1], # position in virtual space
98-
height = (ey[2] - ey[1]) / (resolution[1] / (resolution[1] + legend_space)), width = ex[2] - ex[1]; blend = false
104+
height = (ey[2] - ey[1]), width = ex[2] - ex[1]; blend = false
99105
)
100106

101107
linewidth = ctx[:linewidth]
@@ -220,8 +226,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
220226
region_legend!(plt, "regions", y0 + 3, bcolors)
221227

222228
# corner coordinates
223-
ex = extrema(view(coords, 1, :))
224-
ey = extrema(view(coords, 2, :))
225229
UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
226230
UnicodePlots.label!(plt, :b, "x")
227231
UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
@@ -237,11 +241,15 @@ end
237241

238242
function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
239243
UnicodePlots = ctx[:Plotter]
240-
text_color = UnicodePlots.ansi_color(:normal)
241244

242245
# find bounding box
246+
xlimits = ctx[:xlimits]
243247
coords = grid[Coordinates]
244-
ex = extrema(view(coords, 1, :))
248+
if xlimits[1] < xlimits[2]
249+
ex = xlimits
250+
else
251+
ex = extrema(view(coords, 1, :))
252+
end
245253

246254
# determine resolution (divided by 5, to reduce pixel count in the terminal)
247255
ncellregions = num_cellregions(grid)
@@ -250,12 +258,9 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
250258
resolution = (Int(round(ctx[:size][1] / 6 / layout[2])), max(7, 5 + ncellregions + nbregions))
251259

252260
# create UnicodePlots.Canvas
253-
legend_space = 0 #5
254-
padding = 0 #0.05 * (ex[2] - ex[1])
255-
ex = (ex[1] - padding, ex[2] + padding)
256261
CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ?
257262
canvas = CanvasType(
258-
resolution[2], resolution[1] + legend_space, # number of rows and columns (characters)
263+
resolution[2], resolution[1], # number of rows and columns (characters)
259264
origin_y = 0, origin_x = ex[1], # position in virtual space
260265
height = 1, width = ex[2] - ex[1]; blend = false
261266
)
@@ -275,6 +280,12 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
275280
cellgeoms = grid[CellGeometries]
276281
ncells = num_cells(grid)
277282
nnodes = num_nodes(grid)
283+
text_color = UnicodePlots.ansi_color(:normal)
284+
if nnodes < resolution[1] / 2
285+
for j in 1:nnodes
286+
UnicodePlots.annotate!(canvas, coords[1, j], 0.5, "", text_color, false)
287+
end
288+
end
278289
for j in 1:ncells
279290
cen = local_celledgenodes(cellgeoms[j])
280291
r = cellregions[j]
@@ -287,9 +298,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
287298
)
288299
end
289300
end
290-
for j in 1:nnodes
291-
UnicodePlots.annotate!(canvas, coords[1, j], 0.5, "", text_color, false)
292-
end
293301

294302
# plot boundary nodes with bregion_cmap colors
295303
bcmap = bregion_cmap(nbregions)
@@ -307,7 +315,7 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
307315
for j in 1:nbfaces
308316
red, green, blue = UInt32.(bcolors[bfaceregions[j]])
309317
uint_color = (red << 16) | (green << 8) | blue
310-
UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.5, "", UInt32(uint_color), false)
318+
UnicodePlots.annotate!(canvas, coords[1, bfacenodes[1, j]], 0.5, "", uint_color, false)
311319
end
312320

313321
plt = UnicodePlots.Plot(canvas; title = ctx[:title], border = ctx[:border])
@@ -319,7 +327,6 @@ function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
319327

320328

321329
# corner coordinates
322-
ex = extrema(view(coords, 1, :))
323330
UnicodePlots.label!(plt, :bl, string(Float16(ex[1])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
324331
UnicodePlots.label!(plt, :b, "x")
325332
UnicodePlots.label!(plt, :br, string(Float16(ex[2])), UnicodePlots.ansi_color(UnicodePlots.BORDER_COLOR[]))
@@ -384,7 +391,6 @@ function scalarplot!(
384391
color = ctx[:color]
385392
end
386393

387-
388394
for ifunc in 1:nfuncs
389395
func = funcs[ifunc]
390396
grid = grids[ifunc]

0 commit comments

Comments
 (0)