|
| 1 | +module FunnelChart exposing (main) |
| 2 | + |
| 3 | +{-| Shows how to build a Funnel chart. It is quite similar to a bar chart, but with |
| 4 | +the addition of an extra shape connecting the bars. |
| 5 | +
|
| 6 | +The design here follows [this great best practices guide](https://www.atlassian.com/data/charts/funnel-chart-complete-guide). |
| 7 | +
|
| 8 | +@category Basics |
| 9 | +
|
| 10 | +-} |
| 11 | + |
| 12 | +import Color exposing (Color) |
| 13 | +import Float.Extra |
| 14 | +import List.Extra |
| 15 | +import Scale exposing (BandScale, ContinuousScale, SequentialScale, defaultBandConfig) |
| 16 | +import Scale.Color |
| 17 | +import TypedSvg exposing (g, polygon, rect, svg, text_) |
| 18 | +import TypedSvg.Attributes exposing (fill, fillOpacity, fontFamily, points, stroke, textAnchor, transform, viewBox) |
| 19 | +import TypedSvg.Attributes.InPx exposing (fontSize, height, strokeWidth, width, x, y) |
| 20 | +import TypedSvg.Core exposing (Svg, text) |
| 21 | +import TypedSvg.Types exposing (AnchorAlignment(..), Opacity(..), Paint(..), Transform(..)) |
| 22 | + |
| 23 | + |
| 24 | +w : Float |
| 25 | +w = |
| 26 | + 900 |
| 27 | + |
| 28 | + |
| 29 | +h : Float |
| 30 | +h = |
| 31 | + 450 |
| 32 | + |
| 33 | + |
| 34 | +titleWidth : Float |
| 35 | +titleWidth = |
| 36 | + 100 |
| 37 | + |
| 38 | + |
| 39 | +padding : Float |
| 40 | +padding = |
| 41 | + 30 |
| 42 | + |
| 43 | + |
| 44 | +type alias Datum = |
| 45 | + { title : String |
| 46 | + , value : Float |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +type Shape |
| 51 | + = Bar Int Datum |
| 52 | + | Connector Datum Datum |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +-- Here we preprocess the data to create the shapes we want to draw in an interleaved list |
| 57 | +-- [Datum "A" 1, Datum "B" 2, Datum "C" 3] |
| 58 | +-- -> [ Bar 0 (Datum "A" 1), Connector (Datum "A" 1) (Datum "B" 2), |
| 59 | +-- Bar 1 (Datum "B" 2), Connector (Datum "B" 2) (Datum "C" 3), |
| 60 | +-- Bar 2 (Datum "C" 3) ] |
| 61 | + |
| 62 | + |
| 63 | +preprocessed : List Shape |
| 64 | +preprocessed = |
| 65 | + List.map2 Connector data (List.drop 1 data) |
| 66 | + |> List.Extra.interweave (List.indexedMap Bar data) |
| 67 | + |
| 68 | + |
| 69 | +maxValue : Float |
| 70 | +maxValue = |
| 71 | + data |
| 72 | + |> List.map .value |
| 73 | + |> List.maximum |
| 74 | + |> Maybe.withDefault 0 |
| 75 | + |
| 76 | + |
| 77 | +xScale : ContinuousScale Float |
| 78 | +xScale = |
| 79 | + Scale.linear ( 0, w - 2 * padding - 2 * titleWidth ) ( 0, maxValue ) |
| 80 | + |
| 81 | + |
| 82 | +yScale : BandScale String |
| 83 | +yScale = |
| 84 | + data |
| 85 | + |> List.map .title |
| 86 | + |> Scale.band { defaultBandConfig | paddingInner = 0.2 } ( padding, h - padding ) |
| 87 | + |
| 88 | + |
| 89 | +colorScale : SequentialScale Color |
| 90 | +colorScale = |
| 91 | + Scale.sequential Scale.Color.bluesInterpolator ( -1, toFloat (List.length data - 1) ) |
| 92 | + |
| 93 | + |
| 94 | +view : Svg msg |
| 95 | +view = |
| 96 | + List.map |
| 97 | + (\shape -> |
| 98 | + case shape of |
| 99 | + Bar index datum -> |
| 100 | + let |
| 101 | + color = |
| 102 | + Scale.convert colorScale (toFloat index) |
| 103 | + in |
| 104 | + g [] |
| 105 | + [ rect |
| 106 | + [ x (-(Scale.convert xScale datum.value) / 2) |
| 107 | + , y (Scale.convert yScale datum.title) |
| 108 | + , width (Scale.convert xScale datum.value) |
| 109 | + , height (Scale.bandwidth yScale) |
| 110 | + , fill (Paint color) |
| 111 | + ] |
| 112 | + [] |
| 113 | + , text_ |
| 114 | + [ x (-w / 2 + padding) |
| 115 | + , y (Scale.convert yScale datum.title + Scale.bandwidth yScale / 2) |
| 116 | + , transform [ Translate 0 5 ] |
| 117 | + ] |
| 118 | + [ text datum.title ] |
| 119 | + , text_ |
| 120 | + [ x (w / 2 - padding) |
| 121 | + , y (Scale.convert yScale datum.title + Scale.bandwidth yScale / 2) |
| 122 | + , transform [ Translate 0 5 ] |
| 123 | + , textAnchor AnchorEnd |
| 124 | + ] |
| 125 | + [ text (Float.Extra.toFixedDecimalPlaces 1 (100 * datum.value / maxValue) ++ "%") ] |
| 126 | + , text_ |
| 127 | + [ x 0 |
| 128 | + , y (Scale.convert yScale datum.title + Scale.bandwidth yScale / 2) |
| 129 | + , transform [ Translate 0 5 ] |
| 130 | + , textAnchor AnchorMiddle |
| 131 | + , fill |
| 132 | + (Paint |
| 133 | + -- This is a simple way to make sure the text |
| 134 | + -- is readable |
| 135 | + -- However, you may want a more sophisticated |
| 136 | + -- algorithm for other color scales |
| 137 | + (if (Color.toHsla color).lightness > 0.5 then |
| 138 | + Color.black |
| 139 | + |
| 140 | + else |
| 141 | + Color.white |
| 142 | + ) |
| 143 | + ) |
| 144 | + , stroke (Paint color) |
| 145 | + , strokeWidth 3 |
| 146 | + , TypedSvg.Core.attribute "paint-order" "stroke" |
| 147 | + , fillOpacity (Opacity 0.9) |
| 148 | + ] |
| 149 | + [ text (String.fromFloat datum.value) ] |
| 150 | + ] |
| 151 | + |
| 152 | + Connector top bottom -> |
| 153 | + g [] |
| 154 | + [ polygon |
| 155 | + [ points |
| 156 | + [ ( -(Scale.convert xScale top.value) / 2, Scale.convert yScale top.title + Scale.bandwidth yScale ) |
| 157 | + , ( Scale.convert xScale top.value / 2, Scale.convert yScale top.title + Scale.bandwidth yScale ) |
| 158 | + , ( Scale.convert xScale bottom.value / 2, Scale.convert yScale bottom.title ) |
| 159 | + , ( -(Scale.convert xScale bottom.value) / 2, Scale.convert yScale bottom.title ) |
| 160 | + ] |
| 161 | + , fill (Paint (Scale.convert colorScale -1)) |
| 162 | + ] |
| 163 | + [] |
| 164 | + , text_ |
| 165 | + [ x 0 |
| 166 | + , y (Scale.convert yScale top.title + Scale.bandwidth yScale) |
| 167 | + , transform [ Translate 0 14 ] |
| 168 | + , textAnchor AnchorMiddle |
| 169 | + , fontSize 12 |
| 170 | + , fillOpacity (Opacity 0.7) |
| 171 | + ] |
| 172 | + [ text (Float.Extra.toFixedDecimalPlaces 1 (100 * bottom.value / top.value) ++ "%") ] |
| 173 | + ] |
| 174 | + ) |
| 175 | + preprocessed |
| 176 | + |> svg [ viewBox (-w / 2) 0 w h, fontFamily [ "sans-serif" ] ] |
| 177 | + |
| 178 | + |
| 179 | +data : List Datum |
| 180 | +data = |
| 181 | + [ { title = "Total" |
| 182 | + , value = 54809 |
| 183 | + } |
| 184 | + , { title = "Helpful" |
| 185 | + , value = 29434 |
| 186 | + } |
| 187 | + , { title = "Qualified" |
| 188 | + , value = 10345 |
| 189 | + } |
| 190 | + , { title = "Successful" |
| 191 | + , value = 3432 |
| 192 | + } |
| 193 | + ] |
| 194 | + |
| 195 | + |
| 196 | +main = |
| 197 | + view |
0 commit comments