Skip to content

Commit

Permalink
tweakbars
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed May 28, 2020
1 parent 450edeb commit 2320dc8
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/bars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -46,38 +48,52 @@ 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
barwidth = (barchartwidth - 2bargap)/length(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]
Expand Down

0 comments on commit 2320dc8

Please sign in to comment.