Module

Hylograph.Unified.Attribute

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

Hylograph.Unified.Attribute - Bridge between Display and Attributes

This module bridges the Display profunctor system with Hylograph's existing Attribute type, enabling gradual migration to the new display system.

Two Styles, One System

Old style (still works):

elem Circle
  [ cx (num d.x)
  , cy (num d.y)
  , r (num d.radius)
  ]

New style (using Display):

elem Circle
  [ attr "cx" _.x showNumD
  , attr "cy" _.y showNumD
  , attr "r" _.radius (roundD 1 >>> showNumD)
  ]

Both can be used together in the same visualization!

Benefits of New Style

  1. Composable formatting: Chain displays with >>>
  2. Reusable displays: Define once, use everywhere
  3. Type-safe adaptation: lmapD adapts to different data types
  4. Same display in sheet and viz: Unified formatting

#attr Source

attr :: forall datum a. String -> (datum -> a) -> Display a String -> Attribute datum

Create an attribute from extraction function and display

This is the primary way to create attributes in the new style.

Example:

attr "cx" _.x (scaleD 10.0 >>> showNumD)
attr "fill" _.category idD
attr "opacity" _.importance (clampD 0.0 1.0 >>> showNumD)

#attrStatic Source

attrStatic :: forall datum. String -> String -> Attribute datum

Create a static attribute (same for all data)

Example:

attrStatic "fill" "steelblue"
attrStatic "stroke-width" "2"

#attrIndexed Source

attrIndexed :: forall datum a. String -> (datum -> Int -> a) -> Display a String -> Attribute datum

Create an indexed attribute (uses datum and index)

Example:

attrIndexed "fill" (\d i -> if i `mod` 2 == 0 then "red" else "blue") idD

#cxD Source

cxD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

cx attribute with display formatting

#cyD Source

cyD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

cy attribute with display formatting

#xD Source

xD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

x attribute with display formatting

#yD Source

yD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

y attribute with display formatting

#rD Source

rD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

r (radius) attribute with display formatting

#widthD Source

widthD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

width attribute with display formatting

#heightD Source

heightD :: forall datum. (datum -> Number) -> Display Number String -> Attribute datum

height attribute with display formatting

#fillD Source

fillD :: forall datum. (datum -> String) -> Attribute datum

fill attribute with display formatting

#strokeD Source

strokeD :: forall datum. (datum -> String) -> Attribute datum

stroke attribute with display formatting

#opacityD Source

opacityD :: forall datum. (datum -> Number) -> Attribute datum

opacity attribute with display formatting

#textContentD Source

textContentD :: forall datum a. (datum -> a) -> Display a String -> Attribute datum

text content with display formatting

#transformD Source

transformD :: forall datum. (datum -> String) -> Attribute datum

transform attribute with display formatting

#toAttribute Source

toAttribute :: forall datum a. AttributeName -> (datum -> a) -> Display a String -> Attribute datum

Convert a Display-based spec to an Attribute

This is useful when you have a Display and want to use it with existing attribute-based code.

#fromDisplay Source

fromDisplay :: forall a. AttributeName -> Display a String -> Attribute a

Create an attribute that just uses a Display for formatting

The extraction is identity - useful when datum is already the right type.

Re-exports from Hylograph.Internal.Attribute

#AttributeValue Source

data AttributeValue

Attribute values

We support the most common value types. The ADT ensures type safety when setting attributes.

Instances

#AttributeName Source

newtype AttributeName

Attribute names (SVG/HTML properties)

We use a newtype to prevent typos and enable IDE autocomplete. The String inside is the actual DOM attribute name.

Instances

#Attribute Source

data Attribute datum

Type-safe attribute with datum phantom type

Attributes can be:

  • Static: Same value for all elements
  • Data-driven: Value computed from datum (with source metadata)
  • Indexed: Value computed from datum and index (with source metadata)

The phantom type datum ensures attributes are only applied to selections with matching data types.

The AttrSource field enables interpreters to inspect attribute origins without evaluating the functions.

Instances

  • Show (Attribute datum)
  • Contravariant Attribute

    Contravariant instance for Attribute

    Attributes consume data (they're data sinks), making them naturally contravariant. This enables attribute reuse via cmap:

    -- Define attribute for specific type
    radiusAttr :: Attribute Number
    radiusAttr = DataAttr (AttributeName "r") NumberValue
    
    -- Adapt to work with richer type
    type Circle = { radius :: Number, x :: Number, y :: Number }
    circleRadiusAttr :: Attribute Circle
    circleRadiusAttr = cmap _.radius radiusAttr
    

    The key insight: cmap composes the projection function with the attribute's data accessor, allowing attributes written for simple types to work with complex types via field selection.

#AttrSource Source

data AttrSource

Source metadata for attributes

Describes where the attribute value comes from, enabling interpreters like MetaAST to show meaningful information about attribute bindings.

This is automatically captured when using the DSL (field, num, etc.) but is UnknownSource for raw PureScript functions (escape hatches).

Instances

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