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

Sudhir Kumar's avatar
Sudhir Kumar committed
3
import Prelude
4

5
import CSS (italic)
6
import CSS.Common (normal)
7
import Gargantext.Components.Charts.Options.Color (transparent, violet, black)
8 9
import Gargantext.Components.Charts.Options.Data (DataLegend, dataSerie)
import Gargantext.Components.Charts.Options.Font (IconOptions(..), Shape(..), TextStyle, chartFontStyle, chartFontWeight, icon, mkTooltip, Tooltip, mkToolBox)
Sudhir Kumar's avatar
Sudhir Kumar committed
10 11
import Gargantext.Components.Charts.Options.Legend (legendType, LegendMode(..), PlainOrScroll(..), selectedMode, Orientation(..), orient)
import Gargantext.Components.Charts.Options.Position (Align(..), LeftRelativePosition(..), TopRelativePosition(..), numberPosition, percentPosition, relativePosition)
12
import Gargantext.Components.Charts.Options.Series (Series, seriesPieD1)
13
import Gargantext.Components.Charts.Options.Type (DataZoom, Echarts, Legend, Option, Title, XAxis, YAxis, xAxis, yAxis)
14 15 16
import React (ReactClass, unsafeCreateElementDynamic)
import Reactix as R
import Gargantext.Utils.Reactix as R2
Sudhir Kumar's avatar
Sudhir Kumar committed
17
import Unsafe.Coerce (unsafeCoerce)
Sudhir Kumar's avatar
Sudhir Kumar committed
18

19
foreign import eChartsClass :: ReactClass Echarts
20

21
chart :: Options -> R.Element
22 23 24
chart = echarts <<< chartWith <<< opts

chartWith :: Option -> Echarts
25 26
chartWith option =
  { option
27 28 29 30 31 32 33 34 35 36 37 38
--, className : Nothing
--, style     : Nothing
--, theme     : Nothing
--, group     : Nothing
--, initOpts  : Nothing
--, notMerge  : Nothing
--, lazyUpdate: Nothing
--, loading   : Nothing
--, optsLoading: Nothing
--, onReady    : Nothing
--, resizable  : Nothing
--, onEvents   : Nothing
39
  }
40

41 42
echarts :: Echarts -> R.Element
echarts c = R2.buff $ unsafeCreateElementDynamic (unsafeCoerce eChartsClass) c []
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

type MainTitle = String
type SubTitle  = String

title :: MainTitle -> SubTitle -> Title
title mainTitle subTitle =
  {
    id: ""
   ,show: true
   ,text: mainTitle
   ,link: ""
   ,target: "blank"
   ,textStyle: textStyle
   ,subtext: subTitle
   ,sublink: ""
   ,subtarget: "blank"
   ,subtextStyle: textStyle2
   ,padding: 10.0
   ,itemGap: 0.0
   ,zlevel: 2.0
   ,z: 2.0
64
   ,left: relativePosition (Relative RightPos)
65 66 67
   ,top: relativePosition (Relative Top)
   ,right: numberPosition 60.0
   ,bottom: percentPosition 40.0
68 69
   ,backgroundColor: transparent
   ,borderColor: transparent
70 71 72
   ,borderWidth: 0.0
   ,borderRadius: 0.0
   ,shadowBlur: 0.0
73
   ,shadowColor: transparent
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
   ,shadowOffsetX: 0.0
   ,shadowOffsetY: 0.0
  }

legend :: Legend
legend =
  {
    id: "Muda"
   ,"type": legendType Plain
   , show: true
   , zlevel: 0.0
   , z: 2.0
   , left: relativePosition Auto
   , top: relativePosition Auto
   , right: relativePosition Auto
   , bottom: relativePosition Auto
   , width: relativePosition Auto
   , height: relativePosition Auto
   , orient: orient Horizontal
   , align: relativePosition Auto
   , padding: 5.0
   , itemGap: 10.0
   , itemWidth: 25.0
   , itemHeight: 14.0
98
 --, formatter: Nothing
99
   , selectedMode: selectedMode $ Bool true
100
   , inactiveColor: violet
101
 --- selected: Nothing
102 103 104 105
   , textStyle: textStyle
   , "data": [data1]
  }

106
data1 :: DataLegend
107 108
data1 = {name: "Map terms coverage", icon: icon $ Shape Circle, textStyle: textStyle'}

109
data2 :: DataLegend
110 111
data2 = {name: "Favorites", icon: icon $ Shape Circle, textStyle: textStyle'}

112
data3 :: DataLegend
113 114
data3 = {name: "Test", icon: icon $ Shape Diamond, textStyle: textStyle'}

115

116 117 118
yAxisVoid :: YAxis
yAxisVoid = yAxis
  { "type": ""
119 120 121 122 123 124 125
  , name: ""
  , min: 0
  , position: ""
  , axisLabel: {formatter: ""}
  , show: false
  }

126
xAxis' :: Array String -> XAxis
127
xAxis' [] = unsafeCoerce {show:false}
128
xAxis' xs = xAxis
129
            { "data": xs
130 131
            , "type": "category"
            , axisTick: {alignWithLabel: true}
132
            , show: true
133
            , axisLabel: {formatter: "{value}"}
134 135 136 137 138
            }

-- TODO try to use Optional
yAxis' :: { position :: String
          , show     :: Boolean
139
          , min      :: Int
140
          } -> YAxis
141
yAxis' {position, show, min} = yAxis
142
  { "type": "value"
143
  , name: ""
144
  , min: min
145 146 147
  , axisLabel: {formatter: "{value}"}
  , position
  , show
148 149
  }

150 151 152 153 154 155 156
data Options = Options
  { mainTitle :: MainTitle
  , subTitle  :: SubTitle
  , xAxis     :: XAxis
  , yAxis     :: YAxis
  , series    :: Array Series
  , addZoom   :: Boolean
157
  , tooltip   :: Tooltip
158
  }
159

160
tooltipTriggerAxis :: Tooltip
161
tooltipTriggerAxis = mkTooltip { trigger: "axis"}
162

163
opts :: Options -> Option
164 165 166 167 168
opts (Options { mainTitle
              , subTitle
              , xAxis
              , yAxis
              , series
169
              , tooltip
170 171
              , addZoom
              }) =
172
  { title: title mainTitle subTitle
173
  , legend
174
  , tooltip
175 176 177 178
  , grid: {containLabel: true}
  , series
  , xAxis
  , yAxis
179
  , dataZoom: if addZoom then [zoom Slider, zoom Inside] else []
180
  , children : unsafeCoerce [] -- TODO
181
  , toolbox: mkToolBox
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  }

data Zoom = Slider | Inside

instance showZoom :: Show Zoom where
  show Slider = "slider"
  show Inside = "inside"

zoom :: Zoom -> DataZoom
zoom z = {
  "type": show z
  ,xAxisIndex: 0
  ,filterMode: "empty"
  ,start: 0
  ,end: 100
  }


200
seriesPie :: Series
201 202 203 204 205 206 207 208
seriesPie = seriesPieD1
  { name: "Pie name" }
  (dataSerie <$> [ {name: "t1", value: 50.0}
                 , {name: "t2", value: 45.0}
                 , {name: "t3", value: 65.0}
                 , {name: "t4", value: 15.0}
                 , {name: "t5", value: 23.0}
                 ])
209 210 211 212


textStyle2 :: TextStyle
textStyle2 =
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  { color: black
  , fontStyle: chartFontStyle italic
  , fontWeight: chartFontWeight normal
  , fontFamily: "sans-serif"
  , fontSize: 11
  , align: relativePosition $ Relative RightPos
  , verticalAlign: relativePosition $ Relative Bottom
  , lineHeight: percentPosition 0.0
  , width: percentPosition 100.0
  , height: percentPosition 100.0
  , textBorderColor: black
  , textBorderWidth: 0.0
  , textShadowColor: black
  , textShadowBlur: black
  , textShadowOffsetX: 0.0
  , textShadowOffsetY: 0.0
229 230 231 232
  }

textStyle' :: TextStyle
textStyle' =
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  { color: black
  , fontStyle: chartFontStyle normal
  , fontWeight: chartFontWeight normal
  , fontFamily: "sans-serif"
  , fontSize: 15
  , align: relativePosition $ Relative LeftPos
  , verticalAlign: relativePosition $ Relative Top
  , lineHeight: percentPosition 0.0
  , width: percentPosition 100.0
  , height: percentPosition 100.0
  , textBorderColor: black
  , textBorderWidth: 1.0
  , textShadowColor: black
  , textShadowBlur: black
  , textShadowOffsetX: 0.0
  , textShadowOffsetY: 0.0
249 250 251 252
  }

textStyle :: TextStyle
textStyle =
253 254 255 256
  { color: black
  , fontStyle: chartFontStyle normal
  , fontWeight: chartFontWeight normal
  , fontFamily: "sans-serif"
257
  , fontSize: 10
258 259
  , align: relativePosition $ Relative LeftPos
  , verticalAlign: relativePosition $ Relative Top
260
  , lineHeight: percentPosition 10.0
261 262 263 264 265 266 267 268
  , width: percentPosition 100.0
  , height: percentPosition 100.0
  , textBorderColor: black
  , textBorderWidth: 1.0
  , textShadowColor: black
  , textShadowBlur: black
  , textShadowOffsetX: 0.0
  , textShadowOffsetY: 0.0
269
  }