terezka/elm-plot/2.1.0

NOTE: this package is not compatible with Elm 0.19.1

Plot

This library aims to allow you to visualize a variety of graphs in an intuitve manner without comprimising flexibility regarding configuration. It is insprired by the elm-html api, using the element attrs children pattern.

Elements

type Element msg

Represents child element of the plot.

plot : List MetaAttr -> List (Element msg) -> Svg msg

This is the function processing your entire plot configuration. Pass your meta attributes and plot elements to this function and a svg plot will be returned!

line : List LineAttr -> List Point -> Element msg

This returns a line element resulting in an line serie rendered in your plot.

main =
    plot [] [ line [] [ ( 0, 1 ), ( 2, 2 ), ( 3, 4 ) ] ]
area : List AreaAttr -> List Point -> Element msg

This returns an area element resulting in an area serie rendered in your plot.

main =
    plot [] [ area []  [ ( 0, -2 ), ( 2, 0 ), ( 3, 1 ) ] ]
xAxis : List (AxisAttr msg) -> Element msg

This returns an axis element resulting in an x-axis being rendered in your plot.

main =
    plot [] [ xAxis [] ]
yAxis : List (AxisAttr msg) -> Element msg

This returns an axis element resulting in an y-axis being rendered in your plot.

main =
    plot [] [ yAxis [] ]
type alias Point =
( Float, Float )

Convinience type to represent coordinates.

type alias Style =
List ( String, String )

Convinience type to represent style.

Configuration

Meta configuration

type alias MetaAttr =
MetaConfig -> MetaConfig

The type representing an a meta configuration.

size : ( Int, Int ) -> MetaConfig -> MetaConfig

Specify the size of your plot in pixels.

Default: ( 800, 500 )

padding : ( Int, Int ) -> MetaConfig -> MetaConfig

Add padding to your plot, meaning extra space below and above the lowest and highest point in your plot. The unit is pixels.

Default: ( 0, 0 )

plotClasses : List String -> MetaConfig -> MetaConfig

Add classes to the svg element.

Default: []

plotStyle : Style -> MetaConfig -> MetaConfig

Add styles to the svg element.

Default: [ ( "padding", "30px" ), ( "stroke", "#000" ) ]

Line configuration

type alias LineAttr =
LineConfig -> LineConfig

The type representing a line configuration.

lineStyle : Style -> LineConfig -> LineConfig

Add styles to your line serie.

main =
    plot
        []
        [ line
            [ lineStyle [ ( "fill", "deeppink" ) ] ]
            lineDataPoints
        ]

Area configuration

type alias AreaAttr =
AreaConfig -> AreaConfig

The type representing an area configuration.

areaStyle : Style -> AreaConfig -> AreaConfig

Add styles to your area serie.

main =
    plot
        []
        [ area
            [ areaStyle
                [ ( "fill", "deeppink" )
                , ( "stroke", "deeppink" )
                , ( "opacity", "0.5" ) ]
                ]
            ]
            areaDataPoints
        ]

Axis configuration

type alias AxisAttr msg =
AxisConfig msg -> AxisConfig msg

The type representing an axis configuration.

axisClasses : List String -> AxisConfig msg -> AxisConfig msg

Add classes to the container holding your axis.

main =
    plot
        []
        [ xAxis [ axisClasses [ "my-class" ] ] ]

Default: []

axisStyle : Style -> AxisConfig msg -> AxisConfig msg

Add style to the container holding your axis. Most properties are conveniently inherited by your ticks and labels.

main =
    plot
        []
        [ xAxis [ axisStyle [ ( "stroke", "red" ) ] ] ]

Default: []

axisLineStyle : Style -> AxisConfig msg -> AxisConfig msg

Add styling to the axis line.

main =
    plot
        []
        [ xAxis [ axisLineStyle [ ( "stroke", "blue" ) ] ] ]

Default: []

Tick values configuration

tickValues : List Float -> AxisConfig msg -> AxisConfig msg

Defines what ticks will be shown on the axis by specifying a list of values.

main =
    plot
        []
        [ xAxis [ tickValues [ 0, 1, 2, 4, 8 ] ] ]

Note: If in the list of axis attributes, this attribute is followed by a tickDelta attribute, then this attribute will have no effect.

tickDelta : Float -> AxisConfig msg -> AxisConfig msg

Defines what ticks will be shown on the axis by specifying the delta between the ticks. The delta will be added from zero.

main =
    plot
        []
        [ xAxis [ tickDelta 4 ] ]

Note: If in the list of axis attributes, this attribute is followed by a tickValues attribute, then this attribute will have no effect.

tickRemoveZero : AxisConfig msg -> AxisConfig msg

Remove tick at origin. Useful when two axis' are crossing and you do not want the origin the be cluttered with labels.

main =
    plot
        []
        [ xAxis [ tickRemoveZero ] ]

Tick view configuration

type alias TickViewAttr =
TickViewConfig -> TickViewConfig

Type representing a tick view configuration attribute.

tickConfigView : List TickViewAttr -> AxisConfig msg -> AxisConfig msg

Defines how the tick will be displayed by specifying a list of tick view attributes.

main =
    plot
        []
        [ xAxis
            [ tickConfigView
                [ tickLength 10
                , tickWidth 2
                , tickStyle [ ( "stroke", "red" ) ]
                ]
            ]
        ]

If you do not define another view configuration, the default will be [ tickLength 7, tickWidth 1, tickStyle [] ]

Note: If in the list of axis attributes, this attribute is followed by a tickCustomView, tickConfigViewFunc or a tickCustomViewIndexed attribute, then this attribute will have no effect.

tickConfigViewFunc : TickAttrFunc -> AxisConfig msg -> AxisConfig msg

Defines how the tick will be displayed by specifying a list of tick view attributes.

toTickConfig : Int -> Float -> List TickViewAttr
toTickConfig index tick =
    if isOdd index then
        [ tickLength 7
        , tickStyle [ ( "stroke", "#e4e3e3" ) ]
        ]
    else
        [ tickLength 10
        , tickStyle [ ( "stroke", "#b9b9b9" ) ]
        ]


main =
    plot
        []
        [ xAxis
            [ tickConfigViewFunc toTickConfig ]
        ]

Note: If in the list of axis attributes, this attribute is followed by a tickConfigView, tickCustomView or a tickCustomViewIndexed attribute, then this attribute will have no effect.

tickLength : Int -> TickViewConfig -> TickViewConfig

Set the length of the tick.

main =
    plot
        []
        [ xAxis
            [ tickConfigView [ tickLength 10 ] ]
        ]
tickWidth : Int -> TickViewConfig -> TickViewConfig

Set the width of the tick.

main =
    plot
        []
        [ xAxis
            [ tickConfigView [ tickWidth 2 ] ]
        ]
tickClasses : List String -> TickViewConfig -> TickViewConfig

Add classes to the tick.

main =
    plot
        []
        [ xAxis
            [ tickConfigView
                [ tickClasses [ "my-class" ] ]
            ]
        ]
tickStyle : Style -> TickViewConfig -> TickViewConfig

Sets the style of the tick

main =
    plot
        []
        [ xAxis
            [ tickConfigView
                [ tickStyle [ ( "stroke", "blue" ) ] ]
            ]
        ]
tickCustomView : (Float -> Svg msg) -> AxisConfig msg -> AxisConfig msg

Defines how the tick will be displayed by specifying a function which returns your tick html.

viewTick : Float -> Svg.Svg a
viewTick tick =
    text_
        [ transform ("translate(-5, 10)") ]
        [ tspan [] [ text "✨" ] ]


main =
    plot [] [ xAxis [ tickCustomView viewTick ] ]

Note: If in the list of axis attributes, this attribute is followed by a tickConfigView or a tickCustomViewIndexed attribute, then this attribute will have no effect.

tickCustomViewIndexed : (Int -> Float -> Svg msg) -> AxisConfig msg -> AxisConfig msg

Same as tickCustomConfig, but the functions is also passed a value which is how many ticks away the current tick is from the zero tick.

viewTick : Int -> Float -> Svg.Svg a
viewTick index tick =
    text_
        [ transform ("translate(-5, 10)") ]
        [ tspan
            []
            [ text (if isOdd index then "🌟" else "⭐") ]
        ]


main =
    plot [] [ xAxis [ tickCustomViewIndexed viewTick ] ]

Note: If in the list of axis attributes, this attribute is followed by a tickConfigView or a tickCustomView attribute, then this attribute will have no effect.

Label values configuration

labelValues : List Float -> AxisConfig msg -> AxisConfig msg

Add a list of values where labels will be added.

main =
    plot
        []
        [ xAxis [ labelValues [ 20, 40, 60 ] ] ]
labelFilter : (Int -> Float -> Bool) -> AxisConfig msg -> AxisConfig msg

Add a filter determining which of the ticks are added a label. The first argument passed to the filter is a number describing how many ticks a way the current tick is. The second argument is the value of the tick.

onlyEvenTicks : Int -> Float -> Bool
onlyEvenTicks index value =
    rem 2 index == 0


main =
    plot
        []
        [ xAxis [ labelValues onlyEvenTicks ] ]

Default: (\a b -> True)

Note: If in the list of axis attributes, this attribute is followed by a labelValues attribute, then this attribute will have no effect.

Label values configuration

type alias LabelViewAttr =
LabelViewConfig -> LabelViewConfig

Type representing a label view configuration attribute.

labelConfigView : List LabelViewAttr -> AxisConfig msg -> AxisConfig msg

Configure the label view specifying a list of label view attributes.

main =
    plot
        []
        [ xAxis
            [ labelConfigView
                [ labelFormat (\t -> toString t ++ " s") ]
            ]
        ]
labelConfigViewFunc : LabelAttrFunc -> AxisConfig msg -> AxisConfig msg

Configure the label view specifying a function returning a list of label view attributes. The function will be passed:

  1. An integer representing the amount of ticks away from the origin, the current tick is.

  2. A float value represeting the value of the tick.

    toLabelConfig : Int -> Float -> List TickViewAttr toLabelConfig index tick = if isOdd index then [ labelFormat (\t -> toString t ++ " s") ] else [ labelFormat (always "") ]

    main = plot [] [ xAxis [ labelConfigViewFunc toLabelConfig ] ]

labelFormat : (Float -> String) -> LabelViewConfig -> LabelViewConfig

Format the label based on its value.

main =
    plot
        []
        [ xAxis
            [ labelConfigView
                [ labelFormat (\l -> toString l ++ " DKK") ]
            ]
        ]
labelDisplace : ( Int, Int ) -> LabelViewConfig -> LabelViewConfig

Move the position of the label.

main =
    plot
        []
        [ xAxis
            [ labelConfigView [ labelDisplace ( 0, 27 ) ] ]
        ]
labelClasses : List String -> LabelViewConfig -> LabelViewConfig

Add classes to the label.

main =
    plot
        []
        [ xAxis
            [ labelConfigView
                [ labelClasses [ "my-class" ] ]
            ]
        ]
labelStyle : Style -> LabelViewConfig -> LabelViewConfig

Move the position of the label.

main =
    plot
        []
        [ xAxis
            [ labelConfigView
                [ labelStyle [ ("stroke", "blue" ) ] ]
            ]
        ]
labelCustomView : (Float -> Svg msg) -> AxisConfig msg -> AxisConfig msg

Add a custom view for rendering your label.

viewLabel : Float -> Svg.Svg a
viewLabel tick =
    text_ mySpecialAttributes mySpecialLabelDisplay


main =
    plot
        []
        [ xAxis [ labelCustomView viewLabel ] ]

Note: If in the list of axis attributes, this attribute is followed by a labelFormat attribute, then this attribute will have no effect.

labelCustomViewIndexed : (Int -> Float -> Svg msg) -> AxisConfig msg -> AxisConfig msg

Same as labelCustomView, except this view is also passed the value being the amount of ticks the current tick is away from zero.

viewLabel : Int -> Float -> Svg.Svg a
viewLabel fromZero tick =
    let
        attrs =
            if isOdd fromZero then oddAttrs
            else evenAttrs
    in
        text_ attrs labelHtml


main =
    plot
        []
        [ xAxis [ labelCustomViewIndexed viewLabel ] ]

Note: If in the list of axis attributes, this attribute is followed by a labelFormat attribute, then this attribute will have no effect.

Grid configuration

verticalGrid : List GridAttr -> Element msg

This returns an axis element resulting in horizontal grid lines being rendered in your plot.

main =
    plot [] [ verticalGrid [] ]
horizontalGrid : List GridAttr -> Element msg

This returns an grid element resulting in vertical grid lines being rendered in your plot.

main =
    plot [] [ horizontalGrid [] ]
gridMirrorTicks : GridConfig -> GridConfig

Adds grid lines where the ticks on the corresponding axis are.

main =
    plot
        []
        [ verticalGrid [ gridMirrorTicks ]
        , xAxis []
        ]

Note: If in the list of axis attributes, this attribute is followed by a gridValues attribute, then this attribute will have no effect.

gridValues : List Float -> GridConfig -> GridConfig

Specify a list of ticks where you want grid lines drawn.

plot [] [ verticalGrid [ gridValues [ 1, 2, 4, 8 ] ] ]

Note: If in the list of axis attributes, this attribute is followed by a gridMirrorTicks attribute, then this attribute will have no effect.

gridClasses : List String -> GridConfig -> GridConfig

Specify classes for the grid.

plot
    []
    [ verticalGrid
        [ gridMirrorTicks
        , gridClasses [ "my-class" ]
        ]
    ]

Remember that if you do not specify either gridMirrorTicks or gridValues, then we will default to not showing any grid lines.

gridStyle : Style -> GridConfig -> GridConfig

Specify styles for the gridlines.

plot
    []
    [ verticalGrid
        [ gridMirrorTicks
        , gridStyle myGridStyles
        ]
    ]

Remember that if you do not specify either gridMirrorTicks or gridValues, then we will default to not showing any grid lines.