Module

Hylograph.Scale.Pure

Package
purescript-hylograph-selection
Repository
afcondon/purescript-hylograph-selection

Hylograph.Scale.Pure - Pure PureScript implementation of continuous scales

This module provides the same API as Hylograph.Scale for continuous scales but without any FFI or D3 dependency. Scales are represented as records containing forward/inverse functions plus metadata.

The key insight: a scale is just a function with metadata. The domain and range define a linear (or transformed) mapping; configuration functions rebuild the internal functions accordingly.

Basic Usage

import Hylograph.Scale.Pure

myScale = linear # domain [0.0, 100.0] # range [0.0, 800.0]
pixelX = applyScale myScale 50.0  -- Returns 400.0
tickValues = ticks 10 myScale     -- Returns nice tick values

Pipe-style Configuration

All configuration functions are designed for # pipe syntax:

scale = linear
  # domain [0.0, 100.0]
  # range [0.0, 500.0]
  # clamp true
  # nice

#Scale Source

newtype Scale :: forall k. Type -> Type -> k -> Typenewtype Scale domain range kind

A pure scale is a record holding the forward mapping function, an inverse function, the current domain and range arrays, clamping state, a tick generator, and transform-specific parameters.

The phantom types mirror those in Hylograph.Scale:

  • domain - the input type (always Number for continuous scales)
  • range - the output type (always Number for continuous scales)
  • kind - the scale kind (always Continuous here)

Constructors

#ContinuousScale Source

type ContinuousScale = Scale Number Number Continuous

Convenient alias matching Hylograph.Scale.ContinuousScale

#Continuous Source

data Continuous

Phantom type for continuous scales

#linear Source

linear :: ContinuousScale

Create a linear scale with default domain [0, 1] and range [0, 1]

scale = linear # domain [0.0, 100.0] # range [0.0, 500.0]
applyScale scale 50.0  -- Returns 250.0

#pow Source

pow :: ContinuousScale

Create a power scale with configurable exponent (default exponent 1.0)

scale = pow # exponent 2.0 # domain [0.0, 10.0] # range [0.0, 100.0]

#sqrt Source

sqrt :: ContinuousScale

Create a square root scale (power scale with exponent 0.5)

Useful for sizing circles by area:

radiusScale = sqrt # domain [0.0, maxValue] # range [0.0, 50.0]

#log Source

log :: ContinuousScale

Create a logarithmic scale (default base 10)

Domain must not include zero.

scale = log # domain [1.0, 1000.0] # range [0.0, 300.0]

#domain Source

domain :: Array Number -> ContinuousScale -> ContinuousScale

Set the domain (input extent) of a scale

myScale = linear # domain [0.0, 100.0] # range [0.0, 500.0]

#range Source

range :: Array Number -> ContinuousScale -> ContinuousScale

Set the range (output extent) of a scale

myScale = linear # domain [0.0, 100.0] # range [0.0, 500.0]

#clamp Source

clamp :: Boolean -> ContinuousScale -> ContinuousScale

Enable or disable clamping

When enabled, output values are constrained to the range even for out-of-domain inputs.

clamped = linear # domain [0.0, 100.0] # range [0.0, 500.0] # clamp true
applyScale clamped 200.0  -- Returns 500.0, not 1000.0

#nice Source

nice :: ContinuousScale -> ContinuousScale

Extend the domain to nice round values

Uses 10 as the default tick count hint for computing step size.

niceScale = linear # domain [3.0, 97.0] # nice
-- Domain becomes approximately [0.0, 100.0]

#niceCount Source

niceCount :: Int -> ContinuousScale -> ContinuousScale

Extend the domain to nice round values with a specified tick count hint

niceScale = linear # domain [3.0, 97.0] # niceCount 5

#exponent Source

exponent :: Number -> ContinuousScale -> ContinuousScale

Set the exponent for a power scale

squareScale = pow # exponent 2.0 # domain [0.0, 10.0] # range [0.0, 100.0]

#base Source

base :: Number -> ContinuousScale -> ContinuousScale

Set the base for a logarithmic scale (default 10)

scale = log # base 2.0 # domain [1.0, 1024.0] # range [0.0, 10.0]

#round Source

round :: Boolean -> ContinuousScale -> ContinuousScale

Enable or disable output rounding

When enabled, output values are rounded to the nearest integer.

rounded = linear # domain [0.0, 100.0] # range [0.0, 500.0] # round true
applyScale rounded 33.3  -- Returns 167.0, not 166.5

#applyScale Source

applyScale :: ContinuousScale -> Number -> Number

Apply a scale to a domain value, producing a range value

scale = linear # domain [0.0, 100.0] # range [0.0, 500.0]
applyScale scale 50.0  -- Returns 250.0

#invert Source

invert :: ContinuousScale -> Number -> Maybe Number

Invert a continuous scale (range value back to domain value)

Returns Nothing if the inversion is not defined (e.g. division by zero).

scale = linear # domain [0.0, 100.0] # range [0.0, 500.0]
invert scale 250.0  -- Returns Just 50.0

#ticks Source

ticks :: Int -> ContinuousScale -> Array Number

Generate nice tick positions for a scale

Uses D3's tick algorithm to find "nice" numbers (multiples of 1, 2, or 5 times a power of 10).

scale = linear # domain [0.0, 100.0] # range [0.0, 500.0]
ticks 5 scale  -- Returns [0.0, 20.0, 40.0, 60.0, 80.0, 100.0]

#tickFormat Source

tickFormat :: Int -> String -> ContinuousScale -> (Number -> String)

Get a tick formatter function

The specifier argument is accepted for API compatibility but this pure implementation uses show for formatting.

#copy Source

copy :: ContinuousScale -> ContinuousScale

Create a copy of a scale (identity operation for pure data, included for API compatibility with the D3 FFI module)

#andThen Source

andThen :: ContinuousScale -> ContinuousScale -> (Number -> Number)

Compose two scales: apply the first, then the second

normalize = linear # domain [0.0, 100.0] # range [0.0, 1.0]
toPixels  = linear # domain [0.0, 1.0]   # range [0.0, 500.0]
combined  = normalize `andThen` toPixels
combined 50.0  -- Returns 250.0

#contramap Source

contramap :: forall a. (a -> Number) -> ContinuousScale -> (a -> Number)

Transform the input before applying the scale (contravariant mapping)

fahrenheitScale = linear # domain [32.0, 212.0] # range [0.0, 100.0]
celsiusScale = contramap (\c -> c * 9.0 / 5.0 + 32.0) fahrenheitScale
celsiusScale 100.0  -- Returns 100.0

#map Source

map :: forall b. (Number -> b) -> ContinuousScale -> (Number -> b)

Transform the output after applying the scale (covariant/functor-like)

baseScale = linear # domain [0.0, 100.0] # range [0.0, 500.0]
offsetScale = map (_ + 50.0) baseScale
offsetScale 0.0  -- Returns 50.0

#dimap Source

dimap :: forall a b. (a -> Number) -> (Number -> b) -> ContinuousScale -> (a -> b)

Transform both input and output (profunctor-like)

transformed = scale # dimap preprocess postprocess

#Interpolator Source

type Interpolator a = Number -> a

An interpolator maps [0, 1] to a value

Modules
Data.DependencyGraph
Hylograph.Brush
Hylograph.Brush.FFI
Hylograph.Brush.Types
Hylograph.Classify
Hylograph.Data.Graph
Hylograph.Data.Graph.Algorithms
Hylograph.Data.Node
Hylograph.Data.Tree
Hylograph.Expr.Animation
Hylograph.Expr.Attr
Hylograph.Expr.Datum
Hylograph.Expr.Expr
Hylograph.Expr.Friendly
Hylograph.Expr.Integration
Hylograph.Expr.Interpreter.CodeGen
Hylograph.Expr.Interpreter.Eval
Hylograph.Expr.Interpreter.Meta
Hylograph.Expr.Interpreter.PureSVG
Hylograph.Expr.Interpreter.SVG
Hylograph.Expr.Path
Hylograph.Expr.Path.Generators
Hylograph.Expr.Sugar
Hylograph.Expr.Units
Hylograph.HATS
Hylograph.HATS.Friendly
Hylograph.HATS.InterpreterTick
Hylograph.HATS.Transitions
Hylograph.Interaction.Brush
Hylograph.Interaction.Coordinated
Hylograph.Interaction.Pointer
Hylograph.Interaction.Zoom
Hylograph.Internal.Attribute
Hylograph.Internal.Behavior.FFI
Hylograph.Internal.Behavior.Types
Hylograph.Internal.Element.Operations
Hylograph.Internal.Element.Types
Hylograph.Internal.FFI
Hylograph.Internal.Transition.FFI
Hylograph.Internal.Transition.Manager
Hylograph.Internal.Transition.Scene
Hylograph.Internal.Transition.Types
Hylograph.Internal.Types
Hylograph.Interpreter.English
Hylograph.Interpreter.Mermaid
Hylograph.Interpreter.MetaHATS
Hylograph.Interpreter.SemiQuine
Hylograph.Interpreter.SemiQuine.TreeToCode
Hylograph.Interpreter.SemiQuine.Types
Hylograph.Scale
Hylograph.Scale.ColorSchemes
Hylograph.Scale.FP
Hylograph.Scale.Interpolation
Hylograph.Scale.Pure
Hylograph.Scale.Sequential
Hylograph.Shape.Arc
Hylograph.Shape.Pie
Hylograph.Shape.Polygon
Hylograph.Tooltip
Hylograph.Transform
Hylograph.Unified
Hylograph.Unified.Attribute
Hylograph.Unified.DataDSL
Hylograph.Unified.Display
Hylograph.Unified.Sugar