purescript-d3
Example
Here is the JavaScript code from part 1 of Mike Bostock's Let's Make a Bar Chart series of tutorials for D3:
var data = [4, 8, 15, 16, 23, 42];
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, 420]);
d3.select(".chart")
.selectAll("div")
.data(data)
.enter().append("div")
.style("width", function(d) { return x(d) + "px"; })
.text(function(d) { return d; });
And here is the PureScript equivalent:
array = [4, 8, 15, 16, 23, 42]
main = do
x <- linearScale
.. domain [0, max id array]
.. range [0, 420]
.. toFunction
rootSelect ".chart"
.. selectAll "div"
.. bindData array
.. enter .. append "div"
.. style' "width" (\d -> show (x d) ++ "px")
.. text' show
Note that ..
is an alias for >>=
. The fluent interface is just a poor man's programmable semicolon!
The PureScript D3 bindings statically enforce several properties of D3's selection semantics; for instance, if you were to remove the .. append "div"
above you would get a type error, because the code following it would be attempting to set things on the unrealized nodes of an enter selection. Similarly, if you removed the .. bindData array
line you would get a type error because you can only obtain an enter selection from an update selection (the selection produced by calling data
in JavaScript or bindData
in PureScript). In JavaScript you would have to wait until runtime to see these kinds of errors.
Selections also carry information about the type of data bound to them (if any). Until data is bound to a selection it is only possible to set constant attributes on it; afterwards you can use well-typed functions of the data.
You can find more examples here.
Development
You will need the following pre-requisites installed:
- PureScript
- nodejs
- bower (e.g.,
npm install -g bower
) - gulp.js (e.g.,
npm install -g gulp
)
Once you have these installed you can run the following in a cloned repo:
npm install # install dependencies from package.json
bower update # install dependencies from bower.json
gulp # compile the code
Module Graphics.D3.Base
D3
data D3 :: !
D3Eff
type D3Eff a = forall e. Eff (d3 :: D3 | e) a
Module Graphics.D3.Interpolate
Interpolator
data Interpolator :: * -> *
makeInterpolator
makeInterpolator :: forall a. (a -> a -> Number -> a) -> Interpolator a
Module Graphics.D3.Layout.Base
GraphLayout
class GraphLayout l where
nodes :: forall a. Array a -> l -> D3Eff l
links :: forall a. Array a -> l -> D3Eff l
size :: forall d. { width :: Number, height :: Number | d } -> l -> D3Eff l
Module Graphics.D3.Layout.Force
ForceLayout
data ForceLayout :: *
Instances
instance forceGraphLayout :: GraphLayout ForceLayout
forceLayout
forceLayout :: D3Eff ForceLayout
linkDistance
linkDistance :: Number -> ForceLayout -> D3Eff ForceLayout
linkStrength
linkStrength :: Number -> ForceLayout -> D3Eff ForceLayout
friction
friction :: Number -> ForceLayout -> D3Eff ForceLayout
charge
charge :: Number -> ForceLayout -> D3Eff ForceLayout
chargeDistance
chargeDistance :: Number -> ForceLayout -> D3Eff ForceLayout
theta
theta :: Number -> ForceLayout -> D3Eff ForceLayout
gravity
gravity :: Number -> ForceLayout -> D3Eff ForceLayout
start
start :: ForceLayout -> D3Eff ForceLayout
alpha
alpha :: Number -> ForceLayout -> D3Eff ForceLayout
resume
resume :: ForceLayout -> D3Eff ForceLayout
stop
stop :: ForceLayout -> D3Eff ForceLayout
tick
tick :: ForceLayout -> D3Eff ForceLayout
onTick
onTick :: forall e r. (Foreign -> Eff e r) -> ForceLayout -> D3Eff ForceLayout
onDragStart
onDragStart :: forall e r. (Foreign -> Eff e r) -> ForceLayout -> D3Eff ForceLayout
drag
drag :: ForceLayout -> D3Eff ForceLayout
createDrag
createDrag :: forall s. ForceLayout -> Selection s -> D3Eff (Selection s)
Module Graphics.D3.Request
RequestError
type RequestError = { status :: Number, statusText :: String }
csv
csv :: forall e a. String -> (Either RequestError (Array Foreign) -> Eff (d3 :: D3 | e) a) -> D3Eff Unit
tsv
tsv :: forall e a. String -> (Either RequestError (Array Foreign) -> Eff (d3 :: D3 | e) a) -> D3Eff Unit
json
json :: forall e a. String -> (Either RequestError Foreign -> Eff (d3 :: D3 | e) a) -> D3Eff Unit
Module Graphics.D3.Scale
Scale
class Scale s where
domain :: forall d r. Array d -> s d r -> D3Eff (s d r)
range :: forall d r. Array r -> s d r -> D3Eff (s d r)
copy :: forall d r. s d r -> D3Eff (s d r)
toFunction :: forall d r. s d r -> D3Eff (d -> r)
Instances
instance scaleLinear :: Scale LinearScale
instance scalePower :: Scale PowerScale
instance scaleLog :: Scale LogScale
instance scaleQuantize :: Scale QuantizeScale
instance scaleQuantile :: Scale QuantileScale
instance scaleThreshold :: Scale ThresholdScale
instance scaleOrdinal :: Scale OrdinalScale
Quantitative
class Quantitative s where
invert :: s Number Number -> D3Eff (Number -> Number)
rangeRound :: Array Number -> s Number Number -> D3Eff (s Number Number)
interpolate :: forall r. Interpolator r -> s Number r -> D3Eff (s Number r)
clamp :: forall r. Boolean -> s Number r -> D3Eff (s Number r)
nice :: forall r. Maybe Number -> s Number r -> D3Eff (s Number r)
getTicks :: forall r. Maybe Number -> s Number r -> D3Eff (Array Number)
getTickFormat :: forall r. Number -> Maybe String -> s Number r -> D3Eff (Number -> String)
Instances
instance quantitativeLinear :: Quantitative LinearScale
instance quantitativePower :: Quantitative PowerScale
instance quantitativeLog :: Quantitative LogScale
LinearScale
data LinearScale :: * -> * -> *
Instances
instance scaleLinear :: Scale LinearScale
instance quantitativeLinear :: Quantitative LinearScale
PowerScale
data PowerScale :: * -> * -> *
Instances
instance scalePower :: Scale PowerScale
instance quantitativePower :: Quantitative PowerScale
LogScale
data LogScale :: * -> * -> *
Instances
instance scaleLog :: Scale LogScale
instance quantitativeLog :: Quantitative LogScale
QuantizeScale
data QuantizeScale :: * -> * -> *
Instances
instance scaleQuantize :: Scale QuantizeScale
QuantileScale
data QuantileScale :: * -> * -> *
Instances
instance scaleQuantile :: Scale QuantileScale
ThresholdScale
data ThresholdScale :: * -> * -> *
Instances
instance scaleThreshold :: Scale ThresholdScale
OrdinalScale
data OrdinalScale :: * -> * -> *
Instances
instance scaleOrdinal :: Scale OrdinalScale
linearScale
linearScale :: forall r. D3Eff (LinearScale Number r)
powerScale
powerScale :: forall r. D3Eff (PowerScale Number r)
sqrtScale
sqrtScale :: forall r. D3Eff (PowerScale Number r)
logScale
logScale :: forall r. D3Eff (LogScale Number r)
quantizeScale
quantizeScale :: forall r. D3Eff (QuantizeScale Number r)
quantileScale
quantileScale :: forall r. D3Eff (QuantileScale Number r)
thresholdScale
thresholdScale :: forall r. D3Eff (ThresholdScale Number r)
ordinalScale
ordinalScale :: forall d r. D3Eff (OrdinalScale d r)
exponent
exponent :: forall r. Number -> PowerScale Number r -> D3Eff (PowerScale Number r)
base
base :: forall r. Number -> LogScale Number r -> D3Eff (LogScale Number r)
rangePoints
rangePoints :: forall d. Number -> Number -> Number -> OrdinalScale d Number -> D3Eff (OrdinalScale d Number)
rangeBands
rangeBands :: forall d. Number -> Number -> Number -> Number -> OrdinalScale d Number -> D3Eff (OrdinalScale d Number)
rangeRoundBands
rangeRoundBands :: forall d. Number -> Number -> Number -> Number -> OrdinalScale d Number -> D3Eff (OrdinalScale d Number)
rangeBand
rangeBand :: forall d r. OrdinalScale d Number -> D3Eff Number
rangeExtent
rangeExtent :: forall d r. OrdinalScale d Number -> D3Eff (Tuple Number Number)
Module Graphics.D3.Selection
Selection
data Selection :: * -> *
Instances
instance appendableSelection :: Appendable Selection
instance existingSelection :: Existing Selection
instance clickableSelection :: Clickable (Selection a)
Update
data Update :: * -> *
Instances
instance appendableUpdate :: Appendable Update
instance existingUpdate :: Existing Update
Enter
data Enter :: * -> *
Instances
instance appendableEnter :: Appendable Enter
Transition
data Transition :: * -> *
Instances
instance existingTransition :: Existing Transition
Exit
type Exit d = Selection d
Void
data Void
AttrValue
class AttrValue a
Instances
instance attrValNumber :: AttrValue Number
instance attrValString :: AttrValue String
rootSelect
rootSelect :: String -> D3Eff (Selection Void)
rootSelectAll
rootSelectAll :: String -> D3Eff (Selection Void)
select
select :: forall d. String -> Selection d -> D3Eff (Selection d)
selectAll
selectAll :: forall d. String -> Selection d -> D3Eff (Selection Void)
bindData
bindData :: forall oldData newData. Array newData -> Selection oldData -> D3Eff (Update newData)
enter
enter :: forall d. Update d -> D3Eff (Enter d)
exit
exit :: forall d. Update d -> D3Eff (Exit d)
transition
transition :: forall s d. (Existing s) => s d -> D3Eff (Transition d)
delay
delay :: forall d. Number -> Transition d -> D3Eff (Transition d)
delay'
delay' :: forall d. (d -> Number) -> Transition d -> D3Eff (Transition d)
delay''
delay'' :: forall d. (d -> Number -> Number) -> Transition d -> D3Eff (Transition d)
duration
duration :: forall d. Number -> Transition d -> D3Eff (Transition d)
duration'
duration' :: forall d. (d -> Number) -> Transition d -> D3Eff (Transition d)
duration''
duration'' :: forall d. (d -> Number -> Number) -> Transition d -> D3Eff (Transition d)
Appendable
class Appendable s where
append :: forall d. String -> s d -> D3Eff (Selection d)
Instances
instance appendableSelection :: Appendable Selection
instance appendableUpdate :: Appendable Update
instance appendableEnter :: Appendable Enter
Existing
class Existing s where
attr :: forall d v. (AttrValue v) => String -> v -> s d -> D3Eff (s d)
attr' :: forall d v. (AttrValue v) => String -> (d -> v) -> s d -> D3Eff (s d)
attr'' :: forall d v. (AttrValue v) => String -> (d -> Number -> v) -> s d -> D3Eff (s d)
style :: forall d. String -> String -> s d -> D3Eff (s d)
style' :: forall d. String -> (d -> String) -> s d -> D3Eff (s d)
style'' :: forall d. String -> (d -> Number -> String) -> s d -> D3Eff (s d)
text :: forall d. String -> s d -> D3Eff (s d)
text' :: forall d. (d -> String) -> s d -> D3Eff (s d)
text'' :: forall d. (d -> Number -> String) -> s d -> D3Eff (s d)
remove :: forall d. s d -> D3Eff Unit
Instances
instance existingSelection :: Existing Selection
instance existingUpdate :: Existing Update
instance existingTransition :: Existing Transition
Clickable
class Clickable c where
onClick :: forall eff r. (Foreign -> Eff eff r) -> c -> D3Eff c
onDoubleClick :: forall eff r. (Foreign -> Eff eff r) -> c -> D3Eff c
Instances
instance clickableSelection :: Clickable (Selection a)
Module Graphics.D3.SVG.Axis
Axis
data Axis :: *
axis
axis :: D3Eff Axis
scale
scale :: forall s d. (Scale s) => s d Number -> Axis -> D3Eff Axis
orient
orient :: String -> Axis -> D3Eff Axis
ticks
ticks :: Number -> Axis -> D3Eff Axis
tickFormat
tickFormat :: String -> Axis -> D3Eff Axis
renderAxis
renderAxis :: forall s d. (Existing s) => Axis -> s d -> D3Eff (Selection d)
Module Graphics.D3.Time
TimeScale
data TimeScale :: * -> * -> *
Instances
instance scaleTime :: Scale TimeScale
timeScale
timeScale :: forall r. D3Eff (TimeScale JSDate r)
Module Graphics.D3.Util
Magnitude
class Magnitude n
Instances
instance numberMagnitude :: Magnitude Number
instance dateMagnitude :: Magnitude JSDate
min'
min' :: forall d m. (Magnitude m) => (d -> m) -> Array d -> m
max'
max' :: forall d m. (Magnitude m) => (d -> m) -> Array d -> m
min
min :: forall m. (Magnitude m) => Array m -> m
max
max :: forall d m. (Magnitude m) => Array m -> m
extent
extent :: forall m. (Magnitude m) => Array m -> Array m
extent'
extent' :: forall d m. (Magnitude m) => (d -> m) -> Array d -> Array m