Skip to content

Commit

Permalink
Merge pull request #12 from michaelbjames/pr/scales
Browse files Browse the repository at this point in the history
Pr/scales
  • Loading branch information
seliopou committed Jul 17, 2014
2 parents e3b292e + af3960e commit f34325d
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/D3/Scale.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module D3.Scale where
{-| Basic scales in elm-d3. These are for scales that take a value
from the Reals and map it to the Reals
# Make a scale
@docs linear, identity, pow, sqrt, log
# Change the properties of a scale
@docs domain, range, nice, clamp
# Do things with a scale
@docs convert, invert, ticks
-}
import Native.D3.Scale


data Scale a = Scale


{-| Make a linear scale. Nothing fancy here. The default is the identity mapping.
-}
linear : Scale Float
linear = Native.D3.Scale.linear

identity : Scale Float
identity = Native.D3.Scale.identity

{-| Make a sqrt scale.
-}
sqrt : Scale Float
sqrt = Native.D3.Scale.sqrt

{-| Make a power scale. You must specify the exponent.
-}
pow : Float -> Scale Float
pow = Native.D3.Scale.pow

{-| Make a log scale. You must specify the base.
-}
log : Float -> Scale Float
log = Native.D3.Scale.log



{-| Change the domain of the function. You may pass in any number of values into
the list and they will be interpolated between.
yScale = linear |> domain [0,100] |> range [-150,150]
-}
domain : [Float] -> Scale a -> Scale a
domain = Native.D3.Scale.domain

{-| Change the range of the function. You may pass in any number of values into
the list and they will be interpolated between.
-}
range : [a] -> Scale a -> Scale a
range = Native.D3.Scale.range

{-| Extend the domain to start and end on "nice" values.
linear |> domain [0.201, 0.934] |> nice == linear |> domain [0.2, 1]
-}
nice : Scale Float -> Scale Float
nice = Native.D3.Scale.nice

{-| Clamp the domain so that values beyond the domain will be brough back to the
most extreme value specified in the domain.
linear |> clamp True |> convert 3 == 1
-}
clamp : Bool -> Scale Float -> Scale Float
clamp = Native.D3.Scale.clamp



{-| Apply the scale to a value and it will map the input value to the output range.
let cToF = linear |> domain [0,100] |> range [32,212] |> convert
in cToF -40 => -40
-}
convert : Scale b -> Float -> b
convert = Native.D3.Scale.convert

{-| Apply the scale to a value in its range to get the corresponding value in
the domain.
-}
invert : Scale Float -> Float -> Float
invert = Native.D3.Scale.invert

{-| Get a list of where the ticks are on the scale, given a number of ticks you want.
(linear |> domain [0,10] |> ticks) 3 == [0, 5, 10]
-}
ticks : Scale Float -> Int -> [Float]
ticks = Native.D3.Scale.ticks
71 changes: 71 additions & 0 deletions src/Native/D3/Scale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Elm.Native.D3.Scale = {};
Elm.Native.D3.Scale.make = function (elm) {
'use strict';

elm.Native = elm.Native || {};
elm.Native.D3 = elm.Native.D3 || {};
elm.Native.D3.Scale = elm.Native.D3.Scale || {};
if (elm.Native.D3.Scale.values) return elm.Native.D3.Scale.values;

var JS = Elm.Native.D3.JavaScript.make(elm);

function pow(exponent) {
return d3.scale.pow().exponent(JS.toFloat(exponent));
}

function log(base) {
return d3.scale.log().base(JS.toFloat(base));
}

function domain(domArray, scale) {
var newScale = scale.copy();
var newDomain = JS.fromList(domArray);
return newScale.domain(newDomain);
}

function range(rangeArray, scale) {
var newScale = scale.copy();
var newRange = JS.fromList(rangeArray);
return newScale.range(newRange);
}

function ticks(scale, nTicks) {
var tempScale = scale.copy();
return tempScale.ticks(JS.toInt(nTicks));
}

function nice(scale) {
var newScale = scale.copy();
return newScale.nice();
}

function clamp(bool, scale) {
var newScale = scale.copy();
return newScale.clamp(JS.toBool(bool));
}

function convert(scale, n) {
var number = JS.toFloat(n);
return scale(number);
}

function invert(scale, n) {
var number = JS.toFloat(n);
return scale.invert(number);
}

return elm.Native.D3.Scale.values =
{ linear : d3.scale.linear()
, identity : d3.scale.identity()
, sqrt : d3.scale.sqrt()
, pow : pow
, log : log
, nice : nice
, clamp : F2(clamp)
, domain : F2(domain)
, range : F2(range)
, ticks : F2(ticks)
, convert : F2(convert)
, invert : F2(invert)
};
};
1 change: 1 addition & 0 deletions src/Native/D3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import "Selection"
import "Event"
import "Transition"
import "Voronoi"
import "Scale"

0 comments on commit f34325d

Please sign in to comment.