unicodeplots cleanup: ijulia output fixes, ascii canvas
This commit is contained in:
parent
3d7d8caa82
commit
c8ed611c9c
@ -4,12 +4,12 @@
|
|||||||
supported_args(::UnicodePlotsBackend) = merge_with_base_supported([
|
supported_args(::UnicodePlotsBackend) = merge_with_base_supported([
|
||||||
:label,
|
:label,
|
||||||
:legend,
|
:legend,
|
||||||
:seriescolor, :seriesalpha,
|
:seriescolor,
|
||||||
|
:seriesalpha,
|
||||||
:linestyle,
|
:linestyle,
|
||||||
:markershape,
|
:markershape,
|
||||||
:bins,
|
:bins,
|
||||||
:title,
|
:title,
|
||||||
:window_title,
|
|
||||||
:guide, :lims,
|
:guide, :lims,
|
||||||
])
|
])
|
||||||
supported_types(::UnicodePlotsBackend) = [
|
supported_types(::UnicodePlotsBackend) = [
|
||||||
@ -28,19 +28,19 @@ warnOnUnsupported_args(pkg::UnicodePlotsBackend, d::KW) = nothing
|
|||||||
# --------------------------------------------------------------------------------------
|
# --------------------------------------------------------------------------------------
|
||||||
|
|
||||||
function _initialize_backend(::UnicodePlotsBackend; kw...)
|
function _initialize_backend(::UnicodePlotsBackend; kw...)
|
||||||
@eval begin
|
@eval begin
|
||||||
import UnicodePlots
|
import UnicodePlots
|
||||||
export UnicodePlots
|
export UnicodePlots
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
# convert_size_from_pixels(sz) =
|
|
||||||
|
|
||||||
# do all the magic here... build it all at once, since we need to know about all the series at the very beginning
|
# do all the magic here... build it all at once, since we need to know about all the series at the very beginning
|
||||||
function rebuildUnicodePlot!(plt::Plot)
|
function rebuildUnicodePlot!(plt::Plot)
|
||||||
plt.o = []
|
plt.o = []
|
||||||
|
|
||||||
for sp in plt.subplots
|
for sp in plt.subplots
|
||||||
xaxis = sp[:xaxis]
|
xaxis = sp[:xaxis]
|
||||||
yaxis = sp[:yaxis]
|
yaxis = sp[:yaxis]
|
||||||
@ -58,12 +58,14 @@ function rebuildUnicodePlot!(plt::Plot)
|
|||||||
|
|
||||||
# create a plot window with xlim/ylim set, but the X/Y vectors are outside the bounds
|
# create a plot window with xlim/ylim set, but the X/Y vectors are outside the bounds
|
||||||
width, height = plt[:size]
|
width, height = plt[:size]
|
||||||
o = UnicodePlots.Plot(x, y;
|
canvas_type = isijulia() ? UnicodePlots.AsciiCanvas : UnicodePlots.BrailleCanvas
|
||||||
|
o = UnicodePlots.Plot(x, y, canvas_type;
|
||||||
width = width,
|
width = width,
|
||||||
height = height,
|
height = height,
|
||||||
title = sp[:title],
|
title = sp[:title],
|
||||||
xlim = xlim,
|
xlim = xlim,
|
||||||
ylim = ylim
|
ylim = ylim,
|
||||||
|
border = isijulia() ? :ascii : :solid
|
||||||
)
|
)
|
||||||
|
|
||||||
# set the axis labels
|
# set the axis labels
|
||||||
@ -80,213 +82,82 @@ function rebuildUnicodePlot!(plt::Plot)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# # do all the magic here... build it all at once, since we need to know about all the series at the very beginning
|
|
||||||
# function rebuildUnicodePlot!(plt::Plot)
|
|
||||||
#
|
|
||||||
# # figure out the plotting area xlim = [xmin, xmax] and ylim = [ymin, ymax]
|
|
||||||
# sargs = plt.seriesargs
|
|
||||||
# iargs = plt.attr
|
|
||||||
#
|
|
||||||
# # get the x/y limits
|
|
||||||
# if get(iargs, :xlims, :auto) == :auto
|
|
||||||
# xlim = [Inf, -Inf]
|
|
||||||
# for d in sargs
|
|
||||||
# _expand_limits(xlim, d[:x])
|
|
||||||
# end
|
|
||||||
# else
|
|
||||||
# xmin, xmax = iargs[:xlims]
|
|
||||||
# xlim = [xmin, xmax]
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# if get(iargs, :ylims, :auto) == :auto
|
|
||||||
# ylim = [Inf, -Inf]
|
|
||||||
# for d in sargs
|
|
||||||
# _expand_limits(ylim, d[:y])
|
|
||||||
# end
|
|
||||||
# else
|
|
||||||
# ymin, ymax = iargs[:ylims]
|
|
||||||
# ylim = [ymin, ymax]
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# # we set x/y to have a single point, since we need to create the plot with some data.
|
|
||||||
# # since this point is at the bottom left corner of the plot, it shouldn't actually be shown
|
|
||||||
# x = Float64[xlim[1]]
|
|
||||||
# y = Float64[ylim[1]]
|
|
||||||
#
|
|
||||||
# # create a plot window with xlim/ylim set, but the X/Y vectors are outside the bounds
|
|
||||||
# width, height = iargs[:size]
|
|
||||||
# o = UnicodePlots.Plot(x, y; width = width,
|
|
||||||
# height = height,
|
|
||||||
# title = iargs[:title],
|
|
||||||
# # labels = iargs[:legend],
|
|
||||||
# xlim = xlim,
|
|
||||||
# ylim = ylim)
|
|
||||||
#
|
|
||||||
# # set the axis labels
|
|
||||||
# UnicodePlots.xlabel!(o, iargs[:xguide])
|
|
||||||
# UnicodePlots.ylabel!(o, iargs[:yguide])
|
|
||||||
#
|
|
||||||
# # now use the ! functions to add to the plot
|
|
||||||
# for d in sargs
|
|
||||||
# addUnicodeSeries!(o, d, iargs[:legend] != :none, xlim, ylim)
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
# # save the object
|
|
||||||
# plt.o = o
|
|
||||||
# end
|
|
||||||
|
|
||||||
|
|
||||||
# add a single series
|
# add a single series
|
||||||
function addUnicodeSeries!(o, d::KW, addlegend::Bool, xlim, ylim)
|
function addUnicodeSeries!(o, d::KW, addlegend::Bool, xlim, ylim)
|
||||||
|
|
||||||
# get the function, or special handling for step/bar/hist
|
# get the function, or special handling for step/bar/hist
|
||||||
st = d[:seriestype]
|
st = d[:seriestype]
|
||||||
|
if st == :histogram2d
|
||||||
# handle hline/vline separately
|
|
||||||
if st in (:hline,:vline)
|
|
||||||
for yi in d[:y]
|
|
||||||
if st == :hline
|
|
||||||
UnicodePlots.lineplot!(o, xlim, [yi,yi])
|
|
||||||
else
|
|
||||||
UnicodePlots.lineplot!(o, [yi,yi], ylim)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return
|
|
||||||
|
|
||||||
# elseif st == :bar
|
|
||||||
# UnicodePlots.barplot!(o, d[:x], d[:y])
|
|
||||||
# return
|
|
||||||
|
|
||||||
# elseif st == :histogram
|
|
||||||
# UnicodePlots.histogram!(o, d[:y], bins = d[:bins])
|
|
||||||
# return
|
|
||||||
|
|
||||||
elseif st == :histogram2d
|
|
||||||
UnicodePlots.densityplot!(o, d[:x], d[:y])
|
UnicodePlots.densityplot!(o, d[:x], d[:y])
|
||||||
return
|
return
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
stepstyle = :post
|
if st == :path
|
||||||
if st == :path
|
func = UnicodePlots.lineplot!
|
||||||
func = UnicodePlots.lineplot!
|
elseif st == :scatter || d[:markershape] != :none
|
||||||
elseif st == :scatter || d[:markershape] != :none
|
func = UnicodePlots.scatterplot!
|
||||||
func = UnicodePlots.scatterplot!
|
else
|
||||||
elseif st == :steppost
|
error("Linestyle $st not supported by UnicodePlots")
|
||||||
func = UnicodePlots.stairs!
|
end
|
||||||
elseif st == :steppre
|
|
||||||
func = UnicodePlots.stairs!
|
|
||||||
stepstyle = :pre
|
|
||||||
else
|
|
||||||
error("Linestyle $st not supported by UnicodePlots")
|
|
||||||
end
|
|
||||||
|
|
||||||
# get the series data and label
|
# get the series data and label
|
||||||
x, y = [collect(float(d[s])) for s in (:x, :y)]
|
x, y = [collect(float(d[s])) for s in (:x, :y)]
|
||||||
label = addlegend ? d[:label] : ""
|
label = addlegend ? d[:label] : ""
|
||||||
|
|
||||||
# if we happen to pass in allowed color symbols, great... otherwise let UnicodePlots decide
|
# if we happen to pass in allowed color symbols, great... otherwise let UnicodePlots decide
|
||||||
color = d[:linecolor] in UnicodePlots.color_cycle ? d[:linecolor] : :auto
|
color = d[:linecolor] in UnicodePlots.color_cycle ? d[:linecolor] : :auto
|
||||||
|
|
||||||
# add the series
|
# add the series
|
||||||
func(o, x, y; color = color, name = label, style = stepstyle)
|
func(o, x, y; color = color, name = label)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# function handlePlotColors(::UnicodePlotsBackend, d::KW)
|
|
||||||
# # TODO: something special for unicodeplots, since it doesn't take kindly to people messing with its color palette
|
|
||||||
# d[:color_palette] = [RGB(0,0,0)]
|
|
||||||
# end
|
|
||||||
|
|
||||||
# -------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
# function _create_plot(pkg::UnicodePlotsBackend, d::KW)
|
|
||||||
# plt = Plot(nothing, pkg, 0, d, KW[])
|
|
||||||
|
|
||||||
function _create_backend_figure(plt::Plot{UnicodePlotsBackend})
|
|
||||||
# do we want to give a new default size?
|
|
||||||
# if !haskey(plt.attr, :size) || plt.attr[:size] == default(:size)
|
|
||||||
# plt.attr[:size] = (60,20)
|
|
||||||
# end
|
|
||||||
w, h = plt[:size]
|
|
||||||
plt.attr[:size] = div(w, 10), div(h, 20)
|
|
||||||
plt.attr[:color_palette] = [RGB(0,0,0)]
|
|
||||||
nothing
|
|
||||||
|
|
||||||
# plt
|
|
||||||
end
|
|
||||||
|
|
||||||
# function _series_added(plt::Plot{UnicodePlotsBackend}, series::Series)
|
|
||||||
# d = series.d
|
|
||||||
# # TODO don't need these once the "bar" series recipe is done
|
|
||||||
# if d[:seriestype] in (:sticks, :bar)
|
|
||||||
# d = barHack(; d...)
|
|
||||||
# elseif d[:seriestype] == :histogram
|
|
||||||
# d = barHack(; histogramHack(; d...)...)
|
|
||||||
# end
|
|
||||||
# # push!(plt.seriesargs, d)
|
|
||||||
# # plt
|
|
||||||
# end
|
|
||||||
#
|
|
||||||
#
|
|
||||||
# function _update_plot_object(plt::Plot{UnicodePlotsBackend}, d::KW)
|
|
||||||
# for k in (:title, :xguide, :yguide, :xlims, :ylims)
|
|
||||||
# if haskey(d, k)
|
|
||||||
# plt.attr[k] = d[k]
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
# since this is such a hack, it's only callable using `png`... should error during normal `writemime`
|
# since this is such a hack, it's only callable using `png`... should error during normal `writemime`
|
||||||
function png(plt::AbstractPlot{UnicodePlotsBackend}, fn::AbstractString)
|
function png(plt::AbstractPlot{UnicodePlotsBackend}, fn::AbstractString)
|
||||||
fn = addExtension(fn, "png")
|
fn = addExtension(fn, "png")
|
||||||
|
|
||||||
# make some whitespace and show the plot
|
# make some whitespace and show the plot
|
||||||
println("\n\n\n\n\n\n")
|
println("\n\n\n\n\n\n")
|
||||||
gui(plt)
|
gui(plt)
|
||||||
|
|
||||||
# @osx_only begin
|
# @osx_only begin
|
||||||
@compat @static if is_apple()
|
@compat @static if is_apple()
|
||||||
# BEGIN HACK
|
# BEGIN HACK
|
||||||
|
|
||||||
# wait while the plot gets drawn
|
# wait while the plot gets drawn
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
|
|
||||||
# use osx screen capture when my terminal is maximized and cursor starts at the bottom (I know, right?)
|
# use osx screen capture when my terminal is maximized and cursor starts at the bottom (I know, right?)
|
||||||
# TODO: compute size of plot to adjust these numbers (or maybe implement something good??)
|
# TODO: compute size of plot to adjust these numbers (or maybe implement something good??)
|
||||||
run(`screencapture -R50,600,700,420 $fn`)
|
run(`screencapture -R50,600,700,420 $fn`)
|
||||||
|
|
||||||
# END HACK (phew)
|
# END HACK (phew)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
error("Can only savepng on osx with UnicodePlots (though even then I wouldn't do it)")
|
error("Can only savepng on osx with UnicodePlots (though even then I wouldn't do it)")
|
||||||
end
|
end
|
||||||
|
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
# we don't do very much for subplots... just stack them vertically
|
# we don't do very much for subplots... just stack them vertically
|
||||||
|
|
||||||
# function _create_subplot(subplt::Subplot{UnicodePlotsBackend}, isbefore::Bool)
|
function _update_plot_object(plt::Plot{UnicodePlotsBackend})
|
||||||
# isbefore && return false
|
w, h = plt[:size]
|
||||||
# true
|
plt.attr[:size] = div(w, 10), div(h, 20)
|
||||||
# end
|
plt.attr[:color_palette] = [RGB(0,0,0)]
|
||||||
|
|
||||||
|
|
||||||
function _display(plt::Plot{UnicodePlotsBackend})
|
|
||||||
rebuildUnicodePlot!(plt)
|
rebuildUnicodePlot!(plt)
|
||||||
|
end
|
||||||
|
|
||||||
|
function _writemime(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
|
||||||
map(show, plt.o)
|
map(show, plt.o)
|
||||||
nothing
|
nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function _display(plt::Plot{UnicodePlotsBackend})
|
||||||
|
map(show, plt.o)
|
||||||
|
nothing
|
||||||
|
end
|
||||||
|
|
||||||
# function Base.display(::PlotsDisplay, subplt::Subplot{UnicodePlotsBackend})
|
|
||||||
# for plt in subplt.plts
|
|
||||||
# gui(plt)
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
|
|||||||
@ -119,11 +119,13 @@ const _mimeformats = Dict(
|
|||||||
"application/pdf" => "pdf",
|
"application/pdf" => "pdf",
|
||||||
"image/png" => "png",
|
"image/png" => "png",
|
||||||
"application/postscript" => "ps",
|
"application/postscript" => "ps",
|
||||||
"image/svg+xml" => "svg"
|
"image/svg+xml" => "svg",
|
||||||
|
"text/plain" => "txt",
|
||||||
)
|
)
|
||||||
|
|
||||||
const _best_html_output_type = KW(
|
const _best_html_output_type = KW(
|
||||||
:pyplot => :png,
|
:pyplot => :png,
|
||||||
|
:unicodeplots => :txt,
|
||||||
)
|
)
|
||||||
|
|
||||||
# a backup for html... passes to svg or png depending on the html_output_format arg
|
# a backup for html... passes to svg or png depending on the html_output_format arg
|
||||||
@ -138,6 +140,8 @@ function Base.writemime(io::IO, ::MIME"text/html", plt::Plot)
|
|||||||
elseif output_type == :svg
|
elseif output_type == :svg
|
||||||
# info("writing svg to html output")
|
# info("writing svg to html output")
|
||||||
writemime(io, MIME("image/svg+xml"), plt)
|
writemime(io, MIME("image/svg+xml"), plt)
|
||||||
|
elseif output_type == :txt
|
||||||
|
writemime(io, MIME("text/plain"), plt)
|
||||||
else
|
else
|
||||||
error("only png or svg allowed. got: $output_type")
|
error("only png or svg allowed. got: $output_type")
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user