Skip to content

Commit

Permalink
fix possible invalidations (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Jan 14, 2023
1 parent 8006245 commit c7f2f95
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 116 deletions.
11 changes: 4 additions & 7 deletions src/UnicodePlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function __init__()
end

# COV_EXCL_START
function precompile_workload(ctx)
function precompile_workload(io::IO = IOContext(devnull, :color => Base.get_have_color()))
surf(x, y) = sinc((x^2 + y^2))
for T in ( # most common types
Float64,
Expand Down Expand Up @@ -167,15 +167,12 @@ function precompile_workload(ctx)
surfaceplot((-I):I, (-2I):(2I), surf.(meshgrid((-I):I, (-2I):(2I))...)),
isosurface((-I):4, I:2, I:3, (x, y, z) -> float(x^2 + y^2 - z^2 - 1)),
)
foreach(p -> show(ctx, p), plots)
foreach(p -> show(io, p), plots)
end
end

@precompile_setup begin
ctx = IOContext(devnull, :color => Base.get_have_color())
@precompile_all_calls begin
precompile_workload(ctx)
end
@precompile_all_calls begin
precompile_workload()
end
# COV_EXCL_STOP

Expand Down
28 changes: 15 additions & 13 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ as_float(x::AbstractVector{<:AbstractFloat}) = x
as_float(x) = float.(x)

roundable(x::Number) = isinteger(x) && (typemin(Int32) x typemax(Int32))
compact_repr(x::Number) = repr(x, context = :compact => true)
compact_repr(x::Number) = repr(x; context = :compact => true)

function default_formatter(kw)
unicode_exponent = get(kw, :unicode_exponent, PLOT_KEYWORDS.unicode_exponent)
Expand Down Expand Up @@ -352,23 +352,23 @@ function ceil_neg_log10(x)
end

round_up_subtick(x::T, m) where {T} = T(x == 0 ? 0 : if x > 0
ceil(x, digits = ceil_neg_log10(m) + 1)
ceil(x; digits = ceil_neg_log10(m) + 1)
else
-floor(-x, digits = ceil_neg_log10(m) + 1)
-floor(-x; digits = ceil_neg_log10(m) + 1)
end)

round_down_subtick(x::T, m) where {T} = T(x == 0 ? 0 : if x > 0
floor(x, digits = ceil_neg_log10(m) + 1)
floor(x; digits = ceil_neg_log10(m) + 1)
else
-ceil(-x, digits = ceil_neg_log10(m) + 1)
-ceil(-x; digits = ceil_neg_log10(m) + 1)
end)

float_round_log10(x::Integer, m) = float_round_log10(float(x), m)
float_round_log10(x) = x > 0 ? float_round_log10(x, x) : float_round_log10(x, -x)
float_round_log10(x::T, m) where {T<:AbstractFloat} = T(x == 0 ? 0 : if x > 0
+round(+x, digits = ceil_neg_log10(m) + 1)
+round(+x; digits = ceil_neg_log10(m) + 1)
else
-round(-x, digits = ceil_neg_log10(m) + 1)
-round(-x; digits = ceil_neg_log10(m) + 1)
end)

floor_base(x, b) = round_base(x, b, RoundDown)
Expand Down Expand Up @@ -413,14 +413,16 @@ function plotting_range_narrow(xmin, xmax)
float(round_down_subtick(xmin, Δ)), float(round_up_subtick(xmax, Δ))
end

is_auto(lims) = all(iszero, lims)

autolims(lims) = is_auto(lims) ? SVector(-1.0, 1.0) : SVector(as_float(lims))
autolims(lims, vec::AbstractVector) =
is_auto(lims) && length(vec) > 0 ? SVector(extrema(vec)) : SVector(as_float(lims))

scale_callback(scale::Symbol) = FSCALES[scale]
scale_callback(scale::Function) = scale

is_auto(lims::AbstractVector) = lims == [0, 0]
is_auto(lims::Tuple) = lims == (0, 0)

extend_limits(vec, lims) = extend_limits(vec, lims, :identity)

function extend_limits(vec, lims, scale::Union{Symbol,Function})
scale = scale_callback(scale)
mi, ma = as_float(extrema(lims))
Expand All @@ -438,7 +440,7 @@ function extend_limits(vec, lims, scale::Union{Symbol,Function})
end
end

sort_by_keys(dict::Dict) = sort!(collect(dict), by = x -> first(x))
sort_by_keys(dict::Dict) = sort!(collect(dict), by = first)

function sorted_keys_values(dict::Dict; k2s = true)
if k2s # check and force key type to be of AbstractString type if necessary
Expand Down Expand Up @@ -609,7 +611,7 @@ function colormap_callback(cmap::AbstractVector)
length(cmap)
else
1 + round(Int, ((z - minz) / (maxz - minz)) * (length(cmap) - 1))
end
end::Int
ansi_color(cmap[i])
end::ColorType
end
Expand Down
4 changes: 2 additions & 2 deletions src/freetype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ function kerning(face::FTFont, glyphspecs...)
kerning2d = Ref{FT_Vector}()
err = FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, kerning2d)
# can error if font has no kerning! Since that's somewhat expected, we just return 0
err == 0 || return SVector{2}(0, 0)
err == 0 || return SVector(0.0, 0.0)
divisor = 64 # 64 since metrics are in 1/64 units (units to 26.6 fractional pixels)
SVector{2}(kerning2d[].x / divisor, kerning2d[].y / divisor)
SVector(kerning2d[].x / divisor, kerning2d[].y / divisor)
end

function load_glyph(face::FTFont, glyph)
Expand Down
8 changes: 4 additions & 4 deletions src/interface/boxplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $(arguments(
# Examples
```julia-repl
julia> boxplot([1, 2, 3, 7], title = "Test")
julia> boxplot([1, 2, 3, 7]; title = "Test")
Test
┌ ┐
╷ ┌────┬─────────┐ ╷
Expand Down Expand Up @@ -73,9 +73,9 @@ function boxplot(

min_x_str, mean_x_str, max_x_str =
nice_repr.((min_x, (min_x + max_x) / 2, max_x), Ref(plot))
label!(plot, :bl, min_x_str, color = BORDER_COLOR[])
label!(plot, :b, mean_x_str, color = BORDER_COLOR[])
label!(plot, :br, max_x_str, color = BORDER_COLOR[])
label!(plot, :bl, min_x_str; color = BORDER_COLOR[])
label!(plot, :b, mean_x_str; color = BORDER_COLOR[])
label!(plot, :br, max_x_str; color = BORDER_COLOR[])

for (i, name) enumerate(text)
# Find end of last 3-line region, then add 2 for center of current
Expand Down
2 changes: 1 addition & 1 deletion src/interface/densityplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $(arguments((; dscale = "density scale function"); add = (:x, :y), remove = (:gr
# Examples
```julia-repl
julia> densityplot(randn(1_000), randn(1_000), title = "Density Plot")
julia> densityplot(randn(1_000), randn(1_000); title = "Density Plot")
Density Plot
┌────────────────────────────────────────┐
4 │ │
Expand Down
3 changes: 1 addition & 2 deletions src/interface/heatmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ function heatmap(
end .+ xoffset

# set the axis limits automatically
autolims(lims, vec) = is_auto(lims) && length(vec) > 0 ? extrema(vec) : lims

ylim = autolims(ylim, Y)
xlim = autolims(xlim, X)

# select a subset of A based on the supplied limits
subset(lims, vec) = begin
first = findfirst(x -> x lims[1], vec)
last = findlast(x -> x lims[2], vec)
last = findlast(x -> x lims[2], vec)
(first nothing || last nothing) ? (1:0) : (first:last)
end

Expand Down
11 changes: 7 additions & 4 deletions src/interface/lineplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $(arguments(
# Examples
```julia-repl
julia> lineplot([1, 2, 7], [9, -6, 8], title = "My Lineplot")
julia> lineplot([1, 2, 7], [9, -6, 8]; title = "My Lineplot")
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀My Lineplot⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
┌────────────────────────────────────────┐
10 │⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│
Expand Down Expand Up @@ -159,6 +159,10 @@ end

# ---------------------------------------------------------------------------- #
# date time

format_date(dt, ::Nothing) = string(dt)
format_date(dt, format::AbstractString) = Dates.format(dt, format)

function lineplot(
x::AbstractVector{D},
y::AbstractVector;
Expand All @@ -170,9 +174,8 @@ function lineplot(
dlim = Dates.value.(D.(xlim))
plot = lineplot(Dates.value.(x), y; xlim = dlim, xticks = xticks, kw...)
if xticks
fmt(dt) = format nothing ? string(dt) : Dates.format(dt, format)
label!(plot, :bl, fmt(xlim[1]), color = BORDER_COLOR[])
label!(plot, :br, fmt(xlim[2]), color = BORDER_COLOR[])
label!(plot, :bl, format_date(xlim[1], format); color = BORDER_COLOR[])
label!(plot, :br, format_date(xlim[2], format); color = BORDER_COLOR[])
end
plot
end
Expand Down
16 changes: 8 additions & 8 deletions src/interface/polarplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $(arguments(
# Examples
```julia-repl
julia> polarplot(range(0, 2π, length = 20), range(0, 2, length = 20))
julia> polarplot(range(0, 2π; length = 20), range(0, 2; length = 20))
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀90°⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡤⠤⠒⠒⠉⠉⠉⠉⠉⡏⠉⠉⠉⠉⠓⠒⠦⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand Down Expand Up @@ -95,9 +95,9 @@ end
mr, Mr = is_auto(rlim) ? extrema(r) : rlim

# grid
theta = range(0, 2π, length = 360)
theta = range(0, 2π; length = 360)
grid_color = BORDER_COLOR[]
lineplot!(plot, Mr * cos.(theta), Mr * sin.(theta), color = grid_color)
lineplot!(plot, Mr * cos.(theta), Mr * sin.(theta); color = grid_color)

for theta 0:/ 4):(2π)
lineplot!(plot, [mr, Mr] .* cos(theta), [mr, Mr] .* sin(theta); color = grid_color)
Expand All @@ -108,12 +108,12 @@ end

# labels
row = ceil(Int, nrows(plot.graphics) / 2)
label!(plot, :r, row, degrees ? "" : "0", color = grid_color)
label!(plot, :t, degrees ? "90°" : "π / 2", color = grid_color)
label!(plot, :l, row, degrees ? "180°" : "π", color = grid_color)
label!(plot, :b, degrees ? "270°" : "3π / 4", color = grid_color)
label!(plot, :r, row, degrees ? "" : "0"; color = grid_color)
label!(plot, :t, degrees ? "90°" : "π / 2"; color = grid_color)
label!(plot, :l, row, degrees ? "180°" : "π"; color = grid_color)
label!(plot, :b, degrees ? "270°" : "3π / 4"; color = grid_color)

for r range(mr, Mr, length = num_rad_lab)
for r range(mr, Mr; length = num_rad_lab)
annotate!(
plot,
r * cos(ang_rad_lab),
Expand Down
35 changes: 13 additions & 22 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,28 +203,19 @@ function Plot(
xscale = scale_callback(xscale)
yscale = scale_callback(yscale)

if projection nothing # 3D
mvp = create_MVP(projection, x, y, z; kw...)

(mx, Mx), (my, My) = if is_enabled(mvp)
(xscale identity || yscale identity) &&
throw(ArgumentError("`xscale` or `yscale` are unsupported in 3D"))

mvp = if projection isa Symbol
MVP(x, y, z; projection, kw...)
else
projection
end
grid = blend = false

# normalized coordinates, but allow override (artifact for zooming):
# using `xlim = (-0.5, 0.5)` & `ylim = (-0.5, 0.5)`
# should be close to using `zoom = 2`
autolims(lims) = is_auto(lims) ? (-1.0, 1.0) : as_float(lims)
mx, Mx = autolims(xlim)
my, My = autolims(ylim)

grid = blend = false
else # 2D
mvp = MVP()
mx, Mx = extend_limits(x, xlim, xscale)
my, My = extend_limits(y, ylim, yscale)
# should be close to using `zoom = 2`.
autolims(xlim), autolims(ylim)
else
extend_limits(x, xlim, xscale), extend_limits(y, ylim, yscale)
end

can = canvas(
Expand Down Expand Up @@ -270,21 +261,21 @@ function Plot(
bc = BORDER_COLOR[]
if xticks
base_x_str = base_x nothing ? "" : base_x * (unicode_exponent ? "" : "^")
label!(plot, :bl, base_x_str * (xflip ? M_x : m_x), color = bc)
label!(plot, :br, base_x_str * (xflip ? m_x : M_x), color = bc)
label!(plot, :bl, base_x_str * (xflip ? M_x : m_x); color = bc)
label!(plot, :br, base_x_str * (xflip ? m_x : M_x); color = bc)
end
if yticks
base_y_str = base_y nothing ? "" : base_y * (unicode_exponent ? "" : "^")
label!(plot, :l, nrows(can), base_y_str * (yflip ? M_y : m_y), color = bc)
label!(plot, :l, 1, base_y_str * (yflip ? m_y : M_y), color = bc)
label!(plot, :l, nrows(can), base_y_str * (yflip ? M_y : m_y); color = bc)
label!(plot, :l, 1, base_y_str * (yflip ? m_y : M_y); color = bc)
end
end
if grid && (xscale identity && yscale identity)
my < 0 < My && lines!(plot, mx, 0.0, Mx, 0.0)
mx < 0 < Mx && lines!(plot, 0.0, my, 0.0, My)
end

(is_enabled(mvp) && axes3d) && draw_axes!(plot, 0.8 .* [mx, my])
(is_enabled(mvp) && axes3d) && draw_axes!(plot, 0.8 * mx, 0.8 * my, nothing)

plot
end
Expand Down
Loading

0 comments on commit c7f2f95

Please sign in to comment.