Chart.purs 12.9 KB
Newer Older
1
module Chart where
2

Mael NICOLAS's avatar
Mael NICOLAS committed
3
import Prelude (($), (<<<), (<$>))
4 5

import CSS (Color, white)
Mael NICOLAS's avatar
Mael NICOLAS committed
6
import Data.Maybe (Maybe(..))
7 8
import React as R
import React.DOM (p)
Mael NICOLAS's avatar
Mael NICOLAS committed
9
import React.DOM.Props (Props, unsafeFromPropsArray, unsafeMkProps)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

-- eCharts Props

-- className :: String -> from React DOM Props
--    style     :: String,  -- object,

theme :: String -> Props
theme = unsafeMkProps "theme"

group :: String -> Props
group = unsafeMkProps "group"

    -- group     :: String,
    -- option    :: Option, --  PropTypes.object.isRequired,
    -- initOpts  :: String, -- PropTypes.object,
    -- notMerge  :: Boolean,
    -- lazyUpdate:: Boolean,
    -- loading   :: Boolean,
    -- optsLoading::  OptsLoading, --  PropTypes.object,
    -- onReady    :: String, --  PropTypes.func,
    -- resizable  :: Boolean, -- PropTypes.bool,
    -- onEvents   :: String --  PropTypes.object

type EchartsProps eff =
34
  { className   :: String,
Mael NICOLAS's avatar
Mael NICOLAS committed
35
    style       :: String,  -- objealect-black-altdarkmincnaquadahherry-blossomect,
36 37 38 39 40 41 42 43 44 45 46
    theme       :: String,
    group       :: String,
    option      :: Option, --  PropTypes.object.isRequired,
    initOpts    :: String, -- PropTypes.object,
    notMerge    :: Boolean,
    lazyUpdate  :: Boolean,
    loading     :: Boolean,
    optsLoading :: OptsLoading, --  PropTypes.object,
    onReady     :: String, --  PropTypes.func,
    resizable   :: Boolean, -- PropTypes.bool,
    onEvents    :: String --  PropTypes.object
47 48 49
  }

type OptsLoading =
50 51
  { text      :: String,
    color     :: Color,  --- color
52
    textColor :: Color, --color
53 54
    maskColor :: Color, --color
    zlevel    :: Int
55 56
  }

Mael NICOLAS's avatar
Mael NICOLAS committed
57 58 59
type OpTest =
  {option :: Option}

60
type Option =
Mael NICOLAS's avatar
Mael NICOLAS committed
61
  { title    :: Maybe Title
Mael NICOLAS's avatar
Mael NICOLAS committed
62
  , legend   :: Maybe Legend
63 64 65 66 67 68
  , tooltip  :: Tooltip
  , grid     :: Grid
  , xAxis    :: XAxis
  , yAxis    :: YAxis
  , series   :: Array Series
  , dataZoom :: Array DataZoom
69 70 71 72
  }


type DataZoom =
73 74 75 76 77
  {"type"      :: String
  , xAxisIndex :: Int
  , filterMode :: String
  , start      :: Int
  , end        :: Int
78 79 80 81 82 83 84
  }

type Grid =
  {containLabel :: Boolean
  }

type Legend =
Mael NICOLAS's avatar
Mael NICOLAS committed
85 86
  {"type"  :: String
  , show   :: Boolean
Mael NICOLAS's avatar
Mael NICOLAS committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  , zlevel :: Maybe Number
  , z      :: Maybe Number
  , left   :: Maybe Number
  , top    :: Maybe Number
  , right  :: Maybe Number
  , bottom :: Maybe Number
  , width  :: Maybe Number
  , height :: Maybe Number
  , orient :: Maybe String
  , align  :: Maybe String
  , padding       :: Maybe Number
  , itemGap       :: Maybe Number
  , itemWidth     :: Maybe Number
  , itemHeight    :: Maybe Number
  , formatter     :: Maybe String
  , selectedMode  :: Maybe Boolean
  , inactiveColor :: Maybe Color
  , selected      :: Maybe String -- object
Mael NICOLAS's avatar
Mael NICOLAS committed
105
  , "data"        :: Maybe (Array Data)
106 107 108
  }

type Data =
109
  { name      :: String
Mael NICOLAS's avatar
Mael NICOLAS committed
110 111
  , icon      :: Maybe String
  , textStyle :: Maybe {}
112 113 114
  }

type SubtextStyle =
115 116
  { color      :: Color
  , fontStyle  :: String
117 118
  , fontWeight :: String
  , fontFamily :: String
119 120
  , fontSize   :: Int
  , align      :: String
121
  , verticalAlign :: String
122 123 124
  , lineHeight    :: Number
  , width         :: Number
  , height        :: Number
125 126 127
  , textBorderColor :: String
  , textBorderWidth :: Number
  , textShadowColor :: String
128
  , textShadowBlur  :: Number
129 130
  , textShadowOffsetX :: Number
  , textShadowOffsetY :: Number
131
  , rich              :: Rich
132 133 134 135
  }


type Tooltip =
136
  { trigger   :: String
137
  , formatter :: Maybe String -- TODO function
138 139 140
  }

type XAxis =
Mael NICOLAS's avatar
Mael NICOLAS committed
141
  { "data"   :: Array Data
142
  , "type"   :: String
143 144
  , axisTick :: AxisTick
  }
145

146 147 148 149 150 151
type AxisTick =
  {
    alignWithLabel :: Boolean
  }

type YAxis =
152 153 154 155
  { "type"    :: String
  , name      :: String
  , min       :: Int
  , position  :: String
156 157 158 159 160 161 162 163 164
  , axisLabel :: AxisLabel
  }

type AxisLabel =
  { formatter :: String -- string or function
  }


type Series =
165
  { name   :: String
166 167 168 169 170
  , "type" :: String
  , "data" :: Array Int
  }

type Title =
171 172 173 174 175 176 177 178
  { text         :: String
  , show         :: Boolean
  , link         :: String
  , target       :: String
  , textStyle    :: TextStyle
  , subtext      :: String
  , sublink      :: String
  , subtarget    :: String
179
  , subtextStyle :: SubtextStyle
180 181 182 183 184 185 186 187
  , padding      :: Number
  , itemGap      :: Number
  , zlevel       :: Number
  , z            :: Number
  , left         :: Number
  , top          :: Number
  , right        :: Number
  , bottom       :: Number
188
  , backgroundColor :: Color
189 190 191 192 193 194 195
  , borderColor     :: Color
  , borderWidth     :: Number
  , borderRadius    :: Number -- NumberOrArray
  , shadowBlur      :: Number
  , shadowColor     :: Color
  , shadowOffsetX   :: Number
  , shadowOffsetY   :: Number
196 197 198 199 200 201 202 203 204
  }

-- data NumberOrArray = Number | Array Number



type Rich = {}


205
foreign import eChartsClass :: forall props. R.ReactClass props
Mael NICOLAS's avatar
Mael NICOLAS committed
206
foreign import eChartsClass2 :: R.ReactClass OpTest
207

208 209
echarts :: forall eff. Array Props -> R.ReactElement
echarts p = R.createElementDynamic eChartsClass (unsafeFromPropsArray p) []
210

Mael NICOLAS's avatar
Mael NICOLAS committed
211
echarts' :: forall eff. Option -> R.ReactElement
Mael NICOLAS's avatar
Mael NICOLAS committed
212
echarts' chart = R.createElementDynamic eChartsClass2 {option: chart} []
Mael NICOLAS's avatar
Mael NICOLAS committed
213

214 215 216 217 218 219
-- Props

loading :: Boolean -> Props
loading = unsafeMkProps "loading"

type TextStyle =
220 221
  { color      :: Color
  , fontStyle  :: String
222 223
  , fontWeight :: String
  , fontFamily :: String
224 225 226 227 228 229
  , fontSize   :: Int
  , align      :: String
  , verticalAlign   :: String
  , lineHeight      :: Int
  , width           :: Int
  , height          :: Int
230 231 232
  , textBorderColor :: String
  , textBorderWidth :: Int
  , textShadowColor :: String
233
  , textShadowBlur  :: Int
234 235
  , textShadowOffsetX :: Int
  , textShadowOffsetY :: Int
236
  , rich              :: Rich
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  }

foreign import data TextStyleProps :: Type

textStyle :: Array Props -> Props
textStyle = unsafeMkProps "textStyle"

subTextStyle :: Array Props -> Props
subTextStyle = unsafeMkProps "subTextStyle"

color :: Color -> Props
color = unsafeMkProps "color"

align :: String -> Props
align = unsafeMkProps "align"

option :: Array Props -> Props
option = unsafeMkProps "option" <<< unsafeFromPropsArray

ts :: Props
ts = textStyle [color white, align "left"]

title :: Array Props -> Props
title = unsafeMkProps "title" <<< unsafeFromPropsArray

text :: String -> Props
text = unsafeMkProps "text"


tooltip :: Array Props -> Props
tooltip = unsafeMkProps "tooltip" <<< unsafeFromPropsArray

trigger :: String -> Props
trigger = unsafeMkProps "trigger"

grid :: Array Props -> Props
grid = unsafeMkProps "grid" <<< unsafeFromPropsArray

containLabel :: Boolean -> Props
containLabel = unsafeMkProps "containLabel"

legend :: Array Props -> Props
legend = unsafeMkProps "legend" <<< unsafeFromPropsArray

data' :: forall a. a -> Props
data' = unsafeMkProps "data"

xAxis :: Array Props -> Props
xAxis ap = unsafeMkProps "xAxis" [unsafeFromPropsArray ap]

type' :: String -> Props
type' = unsafeMkProps "type"

axisTick :: Array Props -> Props
axisTick = unsafeMkProps "axisTick" <<< unsafeFromPropsArray

alignWithLabel :: Boolean -> Props
alignWithLabel = unsafeMkProps "alignWithLabel"

xAxisIndex :: Int -> Props
xAxisIndex = unsafeMkProps "xAxisIndex"

filterMode :: String -> Props
filterMode = unsafeMkProps "filterMode"

start :: Int -> Props
start = unsafeMkProps "start"

end :: Int -> Props
end = unsafeMkProps "end"

dataZoom :: Array Props -> Props
dataZoom = unsafeMkProps "dataZoom"

name :: String -> Props
name = unsafeMkProps "name"

position :: String -> Props
position = unsafeMkProps "position"

axisLabel :: Array Props -> Props
axisLabel = unsafeMkProps "axisLabel" <<< unsafeFromPropsArray

formatter :: String -> Props
formatter = unsafeMkProps "formatter"

min :: Int -> Props
min = unsafeMkProps "min"

yAxis :: Array Props -> Props
yAxis = unsafeMkProps "yAxis"

series :: Array Props -> Props
series = unsafeMkProps "series"

label :: Array Props -> Props
label = unsafeMkProps "label"

normal :: Array Props -> Props
normal = unsafeMkProps "normal"

showp :: Boolean -> Props
showp = unsafeMkProps "show"

lineStyle :: Array Props -> Props
lineStyle = unsafeMkProps "lineStyle" <<< unsafeFromPropsArray

width :: Int -> Props
width = unsafeMkProps "width"

shadowColor :: String -> Props
shadowColor = unsafeMkProps "shadowColor"

shadowBlur :: Int -> Props
shadowBlur = unsafeMkProps "shadowBlur"

shadowOffsetY :: Int -> Props
shadowOffsetY = unsafeMkProps "shadowOffsetY"

yAxisIndex :: Int -> Props
yAxisIndex = unsafeMkProps "yAxisIndex"



-------------------


      -- [ p''
      -- , ex1
      -- , p''
      -- ]

369 370
legend' :: Legend
legend' =
Mael NICOLAS's avatar
Mael NICOLAS committed
371
  {
Mael NICOLAS's avatar
Mael NICOLAS committed
372 373 374 375 376 377 378
    "type": "plain"
   , show: true
   , zlevel: Nothing
   , z: Nothing
   , left: Nothing
   , top: Nothing
   , right: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
379
   , bottom: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
380
   , width: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
381 382
   , height: Nothing
   , orient: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
383 384 385 386 387 388 389
   , align: Nothing
   , padding: Nothing
   , itemGap: Nothing
   , itemWidth: Nothing
   , itemHeight: Nothing
   , formatter: Nothing
   , selectedMode: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
390
   , inactiveColor: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
391 392
   , selected: Nothing
   , "data": Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
393 394 395 396 397 398 399 400 401 402 403
  }

data1 :: Data
data1 = {name: "Map terms coverage", icon: Nothing, textStyle: Nothing}

data2 :: Data
data2 = {name: "Favorites", icon: Nothing, textStyle: Nothing}

data3 :: Data
data3 = {name: "All", icon: Nothing, textStyle: Nothing}

404 405
xAxis' :: XAxis
xAxis' =
Mael NICOLAS's avatar
Mael NICOLAS committed
406
 {
407 408 409 410 411 412 413
   "data": [xData1, xData2, xData3]
 , "type": "category"
 , axisTick: {alignWithLabel: true}
 }

xData1 :: Data
xData1 = {name: "Jan", icon: Nothing, textStyle: Nothing}
Mael NICOLAS's avatar
Mael NICOLAS committed
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428
xData2 :: Data
xData2 = {name: "Feb", icon: Nothing, textStyle: Nothing}

xData3 :: Data
xData3 = {name: "Mar", icon: Nothing, textStyle: Nothing}

yData1 :: YAxis
yData1 =
  {
    "type": "value"
  , name: "Score metric"
  , min: 0
  , position: "right"
  , axisLabel: {formatter: "{value}"}
Mael NICOLAS's avatar
Mael NICOLAS committed
429 430
  }

431 432 433 434 435 436 437 438 439 440 441 442 443
tooltip' :: Tooltip
tooltip' =
  {
    trigger: "axis"
  , formatter: Nothing
  }


series' :: Series
series' =
  {
    name: "All"
  , "type": "bar"
Mael NICOLAS's avatar
Mael NICOLAS committed
444
  , "data": [201, 777, 879]
445 446
  }

Mael NICOLAS's avatar
Mael NICOLAS committed
447 448 449
opt :: Option
opt =
  {
Mael NICOLAS's avatar
Mael NICOLAS committed
450 451
    title: Nothing
    ,legend: Nothing
Mael NICOLAS's avatar
Mael NICOLAS committed
452
    ,tooltip: tooltip'
Mael NICOLAS's avatar
Mael NICOLAS committed
453
    ,grid: {containLabel: true}
454 455 456 457
    ,xAxis: xAxis'
    ,yAxis: yData1
    ,series: [series']
    ,dataZoom: [dz1', dz1', dz2', dz2']
Mael NICOLAS's avatar
Mael NICOLAS committed
458 459
  }

460 461
histogram2 :: R.ReactElement
histogram2 = echarts' opt
Mael NICOLAS's avatar
Mael NICOLAS committed
462

463
histogram :: R.ReactElement
464
histogram = echarts
465 466 467
     [ option
       [ tooltip [trigger "axis"]
       , grid [containLabel true]
468
       , legend [data' ["TEST MUDADA", "Favorites", "All"]]
469
       -- , legend [data' ["Map Terms coverage", "Favorites", "All"]]
470 471 472
       , xAxis
         [ type' "category"
         , axisTick [alignWithLabel true]
473 474 475 476
         , data' ["Jan" , "Feb", "Mar" , "Apr"
                 , "May", "Jun", "July", "Aug"
                 , "Sep", "Oct", "Nov" , "Dec"
                 ]
477
         ]
478
       , dataZoom' [dz1', dz1', dz2', dz2']
479
       , yAxis [ya1, ya2]
Mael NICOLAS's avatar
Mael NICOLAS committed
480
       , series [sd1, sd2, sd3]
481 482 483
       ]
     ]

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{-
type DataZoom =
  {"type"      :: String
  , xAxisIndex :: Int
  , filterMode :: String
  , start      :: Int
  , end        :: Int
  }
-}

dz1' :: DataZoom
dz1' = {
  "type": "slider"
  ,xAxisIndex: 0
  ,filterMode: "empty"
  ,start: 0
  ,end: 100
  }

dz2' :: DataZoom
dz2' = {
  "type": "inside"
  ,xAxisIndex: 0
  ,filterMode: "empty"
  ,start: 0
  ,end: 100
  }

dz1 :: forall props. props
513 514 515 516 517 518 519 520
dz1 = unsafeFromPropsArray
      [ type' "slider"
      , xAxisIndex 0
      , filterMode "empty"
      , start 0
      , end 100
      ]

521
dz2 :: forall props. props
522 523 524 525 526 527 528 529
dz2 = unsafeFromPropsArray
      [ type' "inside"
      , xAxisIndex 0
      , filterMode "empty"
      , start 0
      , end 100
      ]

530
ya1 :: forall props. props
531 532
ya1 = unsafeFromPropsArray
      [ type' "value"
533
      , name "Score metric"
534
      , min 0
535
      , position "right"
536
      , axisLabel [formatter "{value}"]
537
      ]
538
ya2 :: forall props. props
539 540
ya2 = unsafeFromPropsArray
      [ type' "value"
541
      , name "Publications (by year)"
542
      , min 0
543
      , position "left"
544
      , axisLabel [formatter "{value}"]
545 546
      ]

547
sd1 :: forall props. props
548
sd1 = unsafeFromPropsArray
549
      [ name "Map terms coverage"
550 551 552 553 554 555 556 557
      , type' "line"
      , label [normal[showp true, position "top"]]
      , lineStyle [ normal
                    [ width 3
                    , shadowColor "rgba(0,0,0,0.4)"
                    , shadowBlur 10
                    , shadowOffsetY 10
                    ]]
558
      , data' [95, 80, 75, 35, 30, 50, 70, 80, 95, 95, 95, 99]
559 560
      ]

561
sd3 :: forall props. props
562
sd3 = unsafeFromPropsArray
563
      [ name "All"
564 565 566 567 568 569
      , type' "bar"
      , label [normal[showp true, position "top"]]
      , yAxisIndex 1
      , data' [201, 222, 223, 777, 244, 255, 555, 879, 938, 1364, 1806, 2324]
      ]

Mael NICOLAS's avatar
Mael NICOLAS committed
570 571 572 573 574 575 576 577 578 579 580 581 582
dataZoom' :: Array DataZoom -> Props
dataZoom' dzs = unsafeMkProps "dataZoom" $ dzToProps <$> dzs

dzToProps :: forall props. DataZoom -> props
dzToProps dz = unsafeFromPropsArray
               [ type' dz."type"
               , xAxisIndex dz.xAxisIndex
               , filterMode dz.filterMode
               , start dz.start
               , end dz.end
               ]


583

584
sd2 :: forall props. props
585 586 587 588 589 590 591 592
sd2 = unsafeFromPropsArray
      [ name "Favorites"
      , type' "bar"
      , label [normal[showp true, position "top"]]
      , yAxisIndex 1
      , data' [22, 22, 23, 77, 24, 55, 139, 350, 150, 164, 106, 224]
      ]

593

594
p'' :: R.ReactElement
595
p'' = p [] []