From 2320dc8bbafb9c063968b90f8c8d782a022692e9 Mon Sep 17 00:00:00 2001 From: cormullion Date: Thu, 28 May 2020 11:04:51 +0100 Subject: [PATCH] tweakbars --- src/bars.jl | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/bars.jl b/src/bars.jl index ff4ac399..9c9869cc 100644 --- a/src/bars.jl +++ b/src/bars.jl @@ -20,6 +20,8 @@ will be scaled to fit in a bounding box. Text labels are drawn if the keyword `labels=true`. +# Extended help + The function returns a vector of points; each is the bottom center of a bar. Draw a Fibonacci sequence as a barchart: @@ -46,22 +48,34 @@ barchart(vals, barfunction=mybarfunction) barchart(vals, labelfunction=mylabelfunction) ``` +``` +function myprologfunction(values, basepoint, minbarrange, maxbarrange, barchartheight) + @layer begin + setline(0.2) + for i in 0:10:maximum(values) + rule(boxbottomcenter(basepoint) + (0, -(rescale(i, minbarrange, maxbarrange) * barchartheight))) + end + end +end +``` """ function barchart(values; - boundingbox = BoundingBox(O + (-250, -120), O + (250, 120)), - bargap=10, - margin = 5, - border=false, - labels=false, labelfunction = (values, i, lowpos, highpos, barwidth, scaledvalue) -> begin - label(string(values[i]), :n, highpos, offset=10) - end, - barfunction = (values, i, lowpos, highpos, barwidth, scaledvalue) -> begin - @layer begin - setline(barwidth) - line(lowpos, highpos, :stroke) - end - end) - # start + boundingbox = BoundingBox(O + (-250, -120), O + (250, 120)), + bargap=10, + margin = 5, + border=false, + labels=false, + labelfunction = (values, i, lowpos, highpos, barwidth, scaledvalue) -> begin + label(string(values[i]), :n, highpos, offset=10) + end, + barfunction = (values, i, lowpos, highpos, barwidth, scaledvalue) -> begin + @layer begin + setline(barwidth) + line(lowpos, highpos, :stroke) + end + end, + prologfunction = (values, basepoint, minbarrange, maxbarrange, barchartheight) -> () + ) minvalue, maxvalue = extrema(values) barchartwidth = boxwidth(boundingbox) - 2bargap - 2margin barchartheight = boxheight(boundingbox) - 2margin @@ -69,15 +83,17 @@ function barchart(values; # if all bars are equal height, this will force a range minbarrange = minvalue - abs(minvalue) maxbarrange = maxvalue + abs(maxvalue) + basepoint = boundingbox - (0, margin) hpositions = between.( - boxbottomleft(boundingbox - (0, margin)), - boxbottomright(boundingbox - (0, margin)), + boxbottomleft(basepoint), + boxbottomright(basepoint), # skip first and last, then take every other one, which is at halfway - range(0.0, 1.0, length=2length(values) + 1))[2:2:end-1] + range(0.0, stop=1.0, length=2length(values) + 1))[2:2:end-1] @layer begin if border box(boundingbox, :stroke) end + prologfunction(values, basepoint, minbarrange, maxbarrange, barchartheight) for i in 1:length(values) scaledvalue = rescale(values[i], minbarrange, maxbarrange) * barchartheight lowposition = hpositions[i]