Series.purs 4.9 KB
Newer Older
Sudhir Kumar's avatar
Sudhir Kumar committed
1
module Gargantext.Components.Charts.Options.Series where
2

3
import Effect.Exception (error, Error(..))
4
import Unsafe.Coerce (unsafeCoerce)
Sudhir Kumar's avatar
Sudhir Kumar committed
5 6 7
import Prelude

import Gargantext.Components.Charts.Options.Data (DataS)
8

9

10 11
newtype SeriesType = SeriesType String

12 13
type SeriesName = String

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

data Chart = Line
           | Bar | PictorialBar
           | Pie
           | Scatter | EffectScatter
           | Radar
           | Trees
           | Sunburst
           | Boxplot
           | Candlestick
           | Heatmap
           | Map
           | Parallel
           | Lines
           | Graph
           | Sankey
           | Funnel
           | Gauge
           | ThemeRiver
-- Trees

instance showChart :: Show Chart where
36
  show Bar      = "bar"
37
  show EffectScatter = "effectScatter" -- ^ https://ecomfe.github.io/echarts-examples/public/editor.html?c=scatter-effect
38 39
  show Funnel   = "funnel"
  show Heatmap  = "heatmap"
40 41 42
  show Line     = "line"
  show Pie      = "pie"
  show Sankey   = "sankey"
43
  show Scatter  = "scatter"            -- ^ https://ecomfe.github.io/echarts-examples/public/editor.html?c=scatter-simple
44 45
  show Sunburst = "sunburst"
  show _        = "not implemented yet: should throw error here"
46

47
seriesType :: Chart -> SeriesType
48 49
seriesType = SeriesType <<< show

50

51
type Series = {}
52
data Serie = SeriesD1 D1 | SeriesD2 D2 | SerieSankey Sankey | SerieTree Tree
53 54

type D1 =
55
  { name   :: String
56
  , "type" :: SeriesType
57
  , "data" :: Array DataS
58
  }
59

60
-- | Scatter Dimension 2 data
61 62 63 64
type D2 =
  { "symbolSize" :: Number
  , "data" :: Array (Array Number)
  , "type" :: SeriesType
65
  }
66

67
toSeries :: Serie -> Series
68 69
toSeries (SeriesD1 a)    = unsafeCoerce a
toSeries (SeriesD2 a)    = unsafeCoerce a
70 71
toSeries (SerieSankey a) = unsafeCoerce a
toSeries (SerieTree   a) = unsafeCoerce a
72

73
-- | Sankey Chart
74
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=sankey-simple
75 76 77 78 79 80 81 82 83 84
type Sankey = { "type" :: SeriesType
              , layout :: String
              , "data" :: Array Node
              , "links" :: Array Link
              }

type Node = { name :: String}
type Link = { source :: String
            , target :: String
            , value  :: Number
85
            }
86

87
mkSankey :: Array Node -> Array Link -> Serie
88 89 90 91 92
mkSankey ns ls = SerieSankey { "type"  : seriesType Sankey
                             , layout  : "none"
                             , "data"  : ns
                             , "links" : ls
                             }
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107
-- | * Trees Chart
-- All these Trees are hierarchical Trees structure (or diagram)
-- https://en.wikipedia.org/wiki/Tree_structure

-- Tree types
data Trees = TreeLine | TreeRadial | TreeMap

instance showTrees :: Show Trees where
  show TreeLine    = "tree"           -- ^ https://ecomfe.github.io/echarts-examples/public/editor.html?c=tree-radial
  show TreeRadial  = "tree"           -- ^ https://ecomfe.github.io/echarts-examples/public/editor.html?c=scatter-simple
  show TreeMap     = "treemap"        -- ^ https://ecomfe.github.io/echarts-examples/public/editor.html?c=treemap-simple


-- TreeLine is a 1-Dimension horizontal hierchical Tree
108

109 110 111 112 113 114 115
-- TreeRadial is 1-Dimension radial (as circle) Tree with no surface meaning
-- https://en.wikipedia.org/wiki/Radial_tree
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=tree-radial

-- TreeMap is a is 2-Dimension Tree with surface meaning
-- TreeMap example implementation:
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=treemap-simple
116

117 118 119 120
type Tree = { "type" :: SeriesType
            , "data" :: Array TreeData
            , layout :: String
            }
121

122 123 124 125 126 127 128 129 130 131 132
mkTree :: Trees -> Array TreeData -> Serie
mkTree t ts = SerieTree { "type" : SeriesType (show t)
                        , "data" : map toJsTree ts
                        , layout : layout
                        }
              where
                layout = case t of
                           TreeRadial -> "radial"
                           _          -> "none"

-- ** Data Structure of the Trees
133 134
data TreeData = TreeLeaf TreeLeaf
              | TreeNode TreeNode
135

136 137 138 139 140 141
toJsTree :: TreeData -> TreeData
toJsTree (TreeLeaf x) = unsafeCoerce x
toJsTree (TreeNode x) = unsafeCoerce { name : x.name
                                     , value : x.value
                                     , children : (map toJsTree x.children)
                                     }
142

143 144 145 146
type TreeNode =  { name     :: String
                 , value    :: Number
                 , children :: Array TreeData
                 }
147

148 149 150
type TreeLeaf = { name :: String
                , value :: Number
                }
151

152 153
treeNode :: String -> Number -> Array TreeData -> TreeData
treeNode n v ts = TreeNode {name : n, value:v, children:ts}
154

155 156
treeLeaf :: String -> Number -> TreeData
treeLeaf n v = TreeLeaf { name : n, value : v}
157 158


159
-- | TODO
160 161 162
-- https://ecomfe.github.io/echarts-examples/public/data/asset/data/life-expectancy-table.json
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=scatter3D-dataset&gl=1