Module

Hylograph.Scale.FP

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

Hylograph.Scale.FP - Functional Programming Abstractions for Scales

This module provides higher-level functional programming idioms built on top of the core Scale module.

Profunctor-like Operations

Scales are naturally profunctorial: contravariant in domain, covariant in range.

-- Pre-process domain values
normalizedScale = rawScale # contramapDomain normalize

-- Post-process range values
offsetScale = pixelScale # mapRange (_ + margin)

-- Both at once
transformedScale = scale # dimapScale preprocess postprocess

Scale as Semigroup/Monoid (Modifiers)

Scale modifiers compose with <>:

niceAndClamped = niceModifier <> clampModifier
myScale = linear # niceAndClamped # domain [...] # range [...]

Tick Generation as Unfold

Generate ticks lazily using unfold semantics:

import Data.Unfoldable (unfoldr)

-- Custom tick generation
logTicks = ticksUnfold logScale  -- Lazy stream of tick values

Scale Sampling

Sample a scale at regular intervals (useful for gradients, paths):

-- Sample 100 points along a color scale
gradient = sample 100 viridisScale

#ScaleModifier Source

type ScaleModifier d r k = Scale d r k -> Scale d r k

A scale modifier transforms a scale while preserving its type Modifiers form a Semigroup under composition

#niceModifier Source

niceModifier :: forall r k. ScaleModifier Number r k

Modifier that makes the domain nice (rounds to clean values)

#clampModifier Source

clampModifier :: forall d r k. ScaleModifier d r k

Modifier that enables clamping

#roundModifier Source

roundModifier :: forall d r k. ScaleModifier d r k

Modifier that enables rounding

#combineModifiers Source

combineModifiers :: forall d r k. Array (ScaleModifier d r k) -> ScaleModifier d r k

Combine multiple modifiers (apply left to right)

combined = combineModifiers [niceModifier, clampModifier, roundModifier]
myScale = linear # combined # domain [...] # range [...]

#sample Source

sample :: forall r. Int -> Scale Number r Continuous -> Array r

Sample a scale at n evenly-spaced points in [0, 1]

Useful for generating color gradients, paths, etc.

-- Generate 256 colors for a gradient
colors = sample 256 viridisColorScale

#sampleRange Source

sampleRange :: forall r. Int -> Number -> Number -> Scale Number r Continuous -> Array r

Sample within a specific range

-- Sample 50 points between 0.2 and 0.8
samples = sampleRange 50 0.2 0.8 scale

#sampleWithDomain Source

sampleWithDomain :: forall r. Int -> Scale Number r Continuous -> Array (Tuple Number r)

Sample and return both domain and range values

-- Get (input, output) pairs
pairs = sampleWithDomain 10 scale

#tickPositions Source

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

Get tick positions as pixel coordinates

-- Get where to draw tick marks
positions = tickPositions 10 xScale

#tickLabels Source

tickLabels :: forall r k. Int -> String -> Scale Number r k -> Array String

Get formatted tick labels

#ticksWithLabels Source

ticksWithLabels :: forall r k. Int -> String -> Scale Number r k -> Array { label :: String, position :: r }

Get ticks with both position and label

-- For rendering axis ticks
tickData = ticksWithLabels 10 ".0f" xScale
-- Returns: [{ position: 0.0, label: "0" }, ...]

#blendInterpolators Source

blendInterpolators :: Number -> Interpolator String -> Interpolator String -> Interpolator String

Blend two interpolators together

-- 50% viridis, 50% plasma
blended = blendInterpolators 0.5 interpolateViridis interpolatePlasma

#reverseInterpolator Source

reverseInterpolator :: forall a. Interpolator a -> Interpolator a

Reverse an interpolator (1-t instead of t)

reversed = reverseInterpolator interpolateViridis

#clampInterpolator Source

clampInterpolator :: forall a. Interpolator a -> Interpolator a

Clamp interpolator input to [0, 1]

#cycleInterpolator Source

cycleInterpolator :: forall a. Interpolator a -> Interpolator a

Make an interpolator cycle (values outside [0,1] wrap)

#normalize Source

normalize :: Number -> Number -> ContinuousScale

Create a normalizing scale (maps domain to [0, 1])

normalizer = normalize 0.0 100.0
normalizer 50.0  -- Returns 0.5

#quantize Source

quantize :: forall a. NonEmptyArray a -> Number -> Number -> Number -> a

Create a quantizing scale (continuous → discrete buckets)

colorBuckets = quantize (NEA.cons' "low" ["medium", "high"])
colorBuckets 0.0 100.0 33.0  -- Returns "low"
colorBuckets 0.0 100.0 66.0  -- Returns "medium"

#threshold Source

threshold :: forall a. Array Number -> NonEmptyArray a -> Number -> a

Create a threshold scale with custom breakpoints

rating = threshold [0.0, 60.0, 80.0, 90.0] (NEA.cons' "F" ["D", "C", "B", "A"])
rating 75.0  -- Returns "C"

#scaleExtent Source

scaleExtent :: ContinuousScale -> { max :: Number, min :: Number }

Get the extent (min, max) of a scale's output for inputs in [0, 1]

#scaleMidpoint Source

scaleMidpoint :: ContinuousScale -> Number

Get the midpoint of a scale

#scaleInRange Source

scaleInRange :: ContinuousScale -> Number -> Boolean

Check if a value is within a scale's output range

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