Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Last active August 29, 2015 14:07
Show Gist options
  • Save rrichardson/cfdc1767e5ff6846a71e to your computer and use it in GitHub Desktop.
Save rrichardson/cfdc1767e5ff6846a71e to your computer and use it in GitHub Desktop.
main : Element
main = plotAxis 580 300 "time - minutes" "events"
plotAxis w h xLabel yLabel =
let lineWidth = 2
labelHeight = 10
arrowSize = 6
ylen = toFloat (h-lineWidth-2*arrowSize-4*labelHeight)
xlen = toFloat (w-lineWidth-2*arrowSize-4*labelHeight)
maxX = 100
maxY = 30
xscale = (xlen - labelHeight) / toFloat maxX
yscale = (ylen - labelHeight) / toFloat maxY
axesStyle = { defaultLine | width <- lineWidth, join <- Smooth, cap <- Round }
axeLines = group <| map (traced axesStyle << path) <| [[(0,0),(0,ylen)], [(0,0),(xlen,0)]]
arrowPoint = scale arrowSize <| filled black <| polygon [(0,0.7), (0.6,-0.3), (0,-0), (-0.6,-0.3)]
axeArrows = group [move (0, ylen) arrowPoint, move (xlen, 0) <| rotate (degrees 270) arrowPoint]
xLabelForm = move (xlen/2,-2*labelHeight) <| toForm <| plainText xLabel
yLabelForm = move (-3*labelHeight,ylen/2) <| rotate (degrees 90) <| toForm <| plainText yLabel
xTick i = move (toFloat i * xscale,-lineWidth/2) <| filled black <| rect (lineWidth/2) (lineWidth*2)
xLabelTicks = group <| map xTick [0..maxX]
xTickLabel i = scale 0.5 <| move (toFloat i * xscale, -labelHeight) <| toForm <| plainText (show i)
xTickLabels = group <| map xTickLabel <| map ((*) 4) [0..(maxX // 4)]
yTick i = move (-lineWidth/2, toFloat i * yTickScale) <| filled black <| rect (lineWidth*2) (lineWidth/2)
noYTicks = floor (ylen / labelHeight)
yTickScale = ylen / toFloat noYTicks
yLabelTicks = group <| map yTick [0..noYTicks-1]
yTickLabel i = scale 0.5 <| move (-labelHeight, toFloat i * yTickScale*2)
<| toForm <| plainText <| show <| round <| yTickScale*2 * toFloat i / yscale
yTickLabels = group <| map yTickLabel <| [0..(noYTicks // 2) - 1]
axes = group [axeLines, axeArrows, xLabelForm, yLabelForm, xLabelTicks, xTickLabels, yLabelTicks, yTickLabels]
moveOrigin = move (-xlen/2 + lineWidth + 2*labelHeight, -ylen/2 + lineWidth + 2*labelHeight)
in collage w h [moveOrigin axes]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment