Module

Hylograph.Scale

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

Hylograph.Scale - Type-safe scales with functional programming idioms

This module provides D3-compatible scales with PureScript type safety and functional programming abstractions. Uses d3-scale-chromatic.js via FFI.

Basic Usage

import Hylograph.Scale (linear, domain, range, apply, ticks)

-- Create a linear scale
myScale = linear # domain [0.0, 100.0] # range [0.0, 800.0]

-- Apply the scale
pixelX = apply myScale 50.0  -- Returns 400.0

-- Get tick marks
tickValues = ticks 10 myScale  -- Returns nice tick values

Functional Idioms

Scales can be composed and transformed:

-- Compose scales
timeToPixel = timeScale `andThen` positionScale

-- Transform with Profunctor-like operations
celsiusScale = fahrenheitScale # contramap fahrenheitToCelsius

#Scale Source

data Scale t0 t1 t2

A scale maps values from a domain to a range The phantom types track:

  • domain - the input type
  • range - the output type
  • kind - the scale kind (Continuous, Ordinal, Band, etc.)

#Continuous Source

data Continuous

Phantom type for continuous scales (Linear, Log, Pow, etc.)

#Ordinal Source

data Ordinal

Phantom type for ordinal scales

#Band Source

data Band

Phantom type for band scales

#Point Source

data Point

Phantom type for point scales

#Time Source

data Time

Phantom type for time scales

#ContinuousScale Source

type ContinuousScale = Scale Number Number Continuous

Convenient type aliases

#OrdinalScale Source

type OrdinalScale domain range = Scale domain range Ordinal

#BandScale Source

type BandScale domain = Scale domain Number Band

#linear Source

linear :: ContinuousScale

Create a linear scale Maps domain values linearly to range values

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

#log Source

log :: ContinuousScale

Create a logarithmic scale Uses log transformation - domain must not include zero

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

#pow Source

pow :: ContinuousScale

Create a power scale with configurable exponent

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

#sqrt Source

sqrt :: ContinuousScale

Create a square root scale (pow with exponent 0.5)

Useful for sizing circles by area

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

#symlog Source

symlog :: ContinuousScale

Create a symlog scale (symmetric log) Handles negative values and zero gracefully

scale = symlog # constant 1.0 # domain [-100.0, 100.0] # range [0.0, 500.0]

#JSDate Source

data JSDate

JavaScript Date object for time scales Compatible with native JS Date, can be created via FFI or js-date library

#scaleTime Source

scaleTime :: TimeScale

Create a time scale for local time

#scaleUtc Source

scaleUtc :: TimeScale

Create a UTC time scale

#Sequential Source

data Sequential

Phantom type for sequential scales (color gradients)

#Diverging Source

data Diverging

Phantom type for diverging scales (two-directional color gradients)

#Quantize Source

data Quantize

Phantom type for quantize scales (continuous → discrete buckets)

#Quantile Source

data Quantile

Phantom type for quantile scales (continuous → discrete based on quantiles)

#QuantizeScale Source

#QuantileScale Source

#scaleQuantize Source

scaleQuantize :: forall r. QuantizeScale r

Create a quantize scale Maps continuous domain to discrete range (uniform intervals)

colorScale = scaleQuantize
  # domain [0.0, 100.0]
  # range ["low", "medium", "high"]

#scaleQuantile Source

scaleQuantile :: forall r. QuantileScale r

Create a quantile scale Maps continuous domain to discrete range based on quantiles

colorScale = scaleQuantile
  # domain [0.0, 25.0, 50.0, 75.0, 100.0]
  # range ["Q1", "Q2", "Q3", "Q4"]

#ordinal Source

ordinal :: forall d r. OrdinalScale d r

Create an ordinal scale Maps discrete domain values to discrete range values

colorScale = ordinal
  # domain ["a", "b", "c"]
  # range ["red", "green", "blue"]

#band Source

band :: forall d. BandScale d

Create a band scale Maps discrete domain values to continuous bands with configurable padding

xScale = band
  # domain ["Mon", "Tue", "Wed", "Thu", "Fri"]
  # range [0.0, 500.0]
  # padding 0.1

#point Source

point :: forall d. BandScale d

Create a point scale Like band but with zero bandwidth - just points

xScale = point
  # domain ["A", "B", "C"]
  # range [0.0, 500.0]

#domain Source

domain :: forall d r k. Array d -> Scale d r k -> Scale d r k

Set the domain (input extent) of a scale

For continuous scales: [min, max] (can include intermediate values for piecewise) For ordinal/band: array of discrete values

#range Source

range :: forall d r k. Array r -> Scale d r k -> Scale d r k

Set the range (output extent) of a scale

For continuous scales: [min, max] For band scales: [start, end] of the band space

#clamp Source

clamp :: forall d r k. Boolean -> Scale d r k -> Scale d r k

Enable or disable clamping When enabled, output is constrained to the range even for out-of-domain inputs

#nice Source

nice :: forall r k. Scale Number r k -> Scale Number r k

Extend the domain to nice round values

#niceCount Source

niceCount :: forall r k. Int -> Scale Number r k -> Scale Number r k

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

#padding Source

padding :: forall d. Number -> BandScale d -> BandScale d

Set padding for band scales (both inner and outer)

#paddingInner Source

paddingInner :: forall d. Number -> BandScale d -> BandScale d

Set inner padding (between bands) for band scales

#paddingOuter Source

paddingOuter :: forall d. Number -> BandScale d -> BandScale d

Set outer padding (before first and after last band) for band scales

#align Source

align :: forall d. Number -> BandScale d -> BandScale d

Set alignment for band scales (0 = left, 0.5 = center, 1 = right)

#round Source

round :: forall d r k. Boolean -> Scale d r k -> Scale d r k

Enable rounding of output values

#base Source

base :: forall r. Number -> Scale Number r Continuous -> Scale Number r Continuous

Set the base for logarithmic scales (default 10)

#exponent Source

exponent :: forall r. Number -> Scale Number r Continuous -> Scale Number r Continuous

Set the exponent for power scales

#constant Source

constant :: forall r. Number -> Scale Number r Continuous -> Scale Number r Continuous

Set the constant for symlog scales

#applyScale Source

applyScale :: forall d r k. Scale d r k -> d -> r

Apply a scale to a value

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

#invert Source

invert :: forall r. Scale Number r Continuous -> r -> Maybe Number

Invert a continuous scale (range value → domain value)

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

#ticks Source

ticks :: forall r k. Int -> Scale Number r k -> Array Number

Generate tick values for a continuous scale

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 :: forall r k. Int -> String -> Scale Number r k -> (Number -> String)

Get a tick formatter function

#bandwidth Source

bandwidth :: forall d. BandScale d -> Number

Get the bandwidth of a band scale

#step Source

step :: forall d. BandScale d -> Number

Get the step size of a band scale (bandwidth + padding)

#copy Source

copy :: forall d r k. Scale d r k -> Scale d r k

Create a copy of a scale

#andThen Source

andThen :: forall a b c k1 k2. Scale a b k1 -> Scale b c k2 -> (a -> c)

Compose two scales: apply first, then second

-- Time → normalized → pixels
combined = timeScale `andThen` pixelScale

#contramap Source

contramap :: forall a a' b k. (a' -> a) -> Scale a b k -> (a' -> b)

Transform the domain type (contravariant)

-- Convert Celsius input to a Fahrenheit scale
celsiusScale = fahrenheitScale # contramap celsiusToFahrenheit

#map Source

map :: forall a b b' k. (b -> b') -> Scale a b k -> (a -> b')

Transform the range type (covariant/functor-like)

-- Add offset to all positions
offsetScale = positionScale # map (_ + 50.0)

#dimap Source

dimap :: forall a a' b b' k. (a' -> a) -> (b -> b') -> Scale a b k -> (a' -> b')

Transform both domain and range (profunctor-like)

transformed = scale # dimap preprocess postprocess

#Interpolator Source

type Interpolator a = Number -> a

An interpolator maps [0, 1] to a value

#interpolateNumber Source

interpolateNumber :: Number -> Number -> Interpolator Number

Linear number interpolation

#interpolateRgb Source

interpolateRgb :: String -> String -> Interpolator String

RGB color interpolation

#schemeCategory10 Source

schemeCategory10 :: Array String

Category10 color scheme (10 colors)

#schemeCategory10At Source

schemeCategory10At :: Int -> String

Get a Category10 color by index (wraps around modularly) Useful for coloring nodes by group number

fill = schemeCategory10At node.group

#schemeTableau10 Source

schemeTableau10 :: Array String

Tableau10 color scheme (10 colors) Modern, accessible categorical palette

#schemeTableau10At Source

schemeTableau10At :: Int -> String

Get a Tableau10 color by index (wraps around modularly) Useful for coloring categories in visualizations

fill = schemeTableau10At categoryIndex

#schemePaired Source

schemePaired :: Array String

Paired color scheme (12 colors)

#schemePairedAt Source

schemePairedAt :: Int -> String

Get a Paired color by index (wraps around modularly)

#schemeSet1 Source

schemeSet1 :: Array String

Set1 color scheme (9 colors)

#schemeSet2 Source

schemeSet2 :: Array String

Set2 color scheme (8 colors)

#schemeSet3 Source

schemeSet3 :: Array String

Set3 color scheme (12 colors)

#schemeAccent Source

schemeAccent :: Array String

Accent color scheme (8 colors)

#schemeDark2 Source

schemeDark2 :: Array String

Dark2 color scheme (8 colors)

#schemePastel1 Source

schemePastel1 :: Array String

Pastel1 color scheme (9 colors)

#schemePastel2 Source

schemePastel2 :: Array String

Pastel2 color scheme (8 colors)

#interpolateBlues Source

interpolateBlues :: Interpolator String

Blues single-hue sequential

#interpolateGreens Source

interpolateGreens :: Interpolator String

Greens single-hue sequential

#interpolateGreys Source

interpolateGreys :: Interpolator String

Greys single-hue sequential

#interpolateOranges Source

interpolateOranges :: Interpolator String

Oranges single-hue sequential

#interpolatePurples Source

interpolatePurples :: Interpolator String

Purples single-hue sequential

#interpolateReds Source

interpolateReds :: Interpolator String

Reds single-hue sequential

#interpolateViridis Source

interpolateViridis :: Interpolator String

Viridis perceptually-uniform colormap

#interpolatePlasma Source

#interpolateInferno Source

#interpolateMagma Source

#interpolateTurbo Source

interpolateTurbo :: Interpolator String

Turbo colormap (rainbow-like but more perceptual)

#interpolateWarm Source

interpolateWarm :: Interpolator String

Warm colormap (red to yellow)

#interpolateCool Source

interpolateCool :: Interpolator String

Cool colormap (cyan to purple)

#interpolateRainbow Source

#interpolateCividis Source

interpolateCividis :: Interpolator String

Cividis colormap (colorblind-friendly)

#interpolateCubehelixDefault Source

interpolateCubehelixDefault :: Interpolator String

Cubehelix default colormap

#interpolateBuGn Source

interpolateBuGn :: Interpolator String

Blue-Green sequential

#interpolateBuPu Source

interpolateBuPu :: Interpolator String

Blue-Purple sequential

#interpolateGnBu Source

interpolateGnBu :: Interpolator String

Green-Blue sequential

#interpolateOrRd Source

interpolateOrRd :: Interpolator String

Orange-Red sequential

#interpolatePuBuGn Source

interpolatePuBuGn :: Interpolator String

Purple-Blue-Green sequential

#interpolatePuBu Source

interpolatePuBu :: Interpolator String

Purple-Blue sequential

#interpolatePuRd Source

interpolatePuRd :: Interpolator String

Purple-Red sequential

#interpolateRdPu Source

interpolateRdPu :: Interpolator String

Red-Purple sequential

#interpolateYlGnBu Source

interpolateYlGnBu :: Interpolator String

Yellow-Green-Blue sequential

#interpolateYlGn Source

interpolateYlGn :: Interpolator String

Yellow-Green sequential

#interpolateYlOrBr Source

interpolateYlOrBr :: Interpolator String

Yellow-Orange-Brown sequential

#interpolateYlOrRd Source

interpolateYlOrRd :: Interpolator String

Yellow-Orange-Red sequential

#interpolateRdYlGn Source

interpolateRdYlGn :: Interpolator String

Red-Yellow-Green diverging

#interpolateRdBu Source

interpolateRdBu :: Interpolator String

Red-Blue diverging

#interpolatePiYG Source

interpolatePiYG :: Interpolator String

Pink-Yellow-Green diverging

#interpolateBrBG Source

interpolateBrBG :: Interpolator String

Brown-Blue-Green diverging

#interpolatePRGn Source

interpolatePRGn :: Interpolator String

Purple-Red-Green diverging

#interpolateSpectral Source

#interpolateRdGy Source

interpolateRdGy :: Interpolator String

Red-Grey diverging

#interpolateRdYlBu Source

interpolateRdYlBu :: Interpolator String

Red-Yellow-Blue diverging

#interpolateSinebow Source

interpolateSinebow :: Interpolator String

Sinebow cyclical colormap

Modules
Data.DependencyGraph
Hylograph.AST
Hylograph.Axis.Axis
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.Capabilities.Selection
Hylograph.Internal.Capabilities.Transition
Hylograph.Internal.FFI
Hylograph.Internal.Selection.Join
Hylograph.Internal.Selection.Operations
Hylograph.Internal.Selection.Operations.Conversions
Hylograph.Internal.Selection.Operations.Helpers
Hylograph.Internal.Selection.Operations.Selection
Hylograph.Internal.Selection.Query
Hylograph.Internal.Selection.Types
Hylograph.Internal.Transition.FFI
Hylograph.Internal.Transition.Manager
Hylograph.Internal.Transition.Scene
Hylograph.Internal.Transition.Types
Hylograph.Internal.Types
Hylograph.Interpreter.D3
Hylograph.Interpreter.English
Hylograph.Interpreter.Mermaid
Hylograph.Interpreter.MetaAST
Hylograph.Interpreter.SemiQuine
Hylograph.Interpreter.SemiQuine.TreeToCode
Hylograph.Interpreter.SemiQuine.Types
Hylograph.Render
Hylograph.Scale
Hylograph.Scale.FP
Hylograph.Shape.Arc
Hylograph.Shape.Pie
Hylograph.Shape.Polygon
Hylograph.Tooltip
Hylograph.Transform
Hylograph.TreeDSL
Hylograph.TreeDSL.ShapeTree
Hylograph.Unified
Hylograph.Unified.Attribute
Hylograph.Unified.DataDSL
Hylograph.Unified.Display
Hylograph.Unified.Examples
Hylograph.Unified.Join
Hylograph.Unified.Sugar