Last active
October 4, 2016 13:57
-
-
Save afcondon/0bdf2341c1768b0e087be3ab17471a3b to your computer and use it in GitHub Desktop.
purescript-d3v4 block test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import D3.Axis | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (CONSOLE) | |
import D3.Base (PolyValue(Value), AttrSetter(SetAttr, AttrFn), DataBind(Data), D3, (..), (...)) | |
import D3.Collections (D3Collection(D3Range, D3ArrT)) | |
import D3.Scale (ScaleType(Linear, Band), scaleBy, bandwidth, domain, rangeRound, d3Scale, padding) | |
import D3.Selection (attr, append, enter, dataBind, selectAll, text, getAttr, d3Select) | |
import Data.Maybe (Maybe(Just)) | |
import Prelude (map, Unit, unit, pure, bind, show, (-), (<>)) | |
-- define a margin, look to purescript-css for more sophisticated definition | |
margin :: { top::Number, right::Number, bottom::Number, left::Number } | |
margin = { top: 20.0, right: 20.0, bottom: 30.0, left: 40.0} | |
main :: ∀ e. Eff (d3::D3,console::CONSOLE|e) Unit | |
main = do | |
svg <- d3Select ".svg" | |
-- general setup | |
w <- svg ... getAttr "width" | |
h <- svg ... getAttr "height" | |
let width = w - margin.left - margin.right | |
let height = h - margin.top - margin.bottom | |
-- creating scales and initializing axes with those scales | |
x <- d3Scale Band | |
.. rangeRound 0.0 width | |
.. padding 0.1 | |
.. domain (D3ArrT (map (\d -> d.letter) frequencies)) | |
xaxis <- d3AxisBottom x | |
y <- d3Scale Linear | |
.. rangeRound height 0.0 | |
.. domain (D3Range 0.0 0.12702) -- implement max lookup later TODO | |
yaxis <- d3AxisLeft y | |
.. axisTicks (Count 10.0 (Just "%")) | |
-- main body of chart svg.svg>g.transform> | |
g <- svg ... append "g" | |
.. attr "transform" (SetAttr ("translate(" <> show margin.left <> "," <> show margin.top <> ")")) | |
-- add an x-axis as child of "g" | |
g ... append "g" | |
.. attr "class" (SetAttr "axis axis--x") | |
.. attr "transform" (SetAttr ("translate(0," <> show height <> ")")) | |
.. renderAxis xaxis | |
-- add a y-axis as sibling of x-axis | |
g ... append "g" | |
.. attr "class" (SetAttr "axis axis--y") | |
.. renderAxis yaxis | |
.. append "text" | |
.. attr "transform" (SetAttr "rotate(-90)") | |
.. attr "y" (SetAttr 6.0) | |
.. attr "dy" (SetAttr "0.71em") | |
.. attr "text-anchor" (SetAttr "end") | |
.. text (Value "Frequency") | |
-- add the bars as siblings of the axes | |
g ... selectAll (".bar") | |
.. dataBind (Data frequencies) | |
.. enter .. append "rect" | |
.. attr "class" (SetAttr "bar") | |
.. attr "x" (AttrFn (\d i nodes el -> scaleBy x d.letter)) | |
.. attr "y" (AttrFn (\d i nodes el -> scaleBy y d.frequency)) | |
.. attr "width" (SetAttr (bandwidth x)) | |
.. attr "height" (AttrFn (\d i nodes el -> do scaled <- scaleBy y d.frequency | |
pure (height - scaled))) | |
pure unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment