Chart.purs 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
module Chart where

import Prelude

import CSS (Color, white)
import React (ReactClass, ReactElement, createElementDynamic)
import React.DOM.Props (Props, unsafeFromPropsArray, unsafeMkProps)
import Control.Monad.Eff (Eff)
import DOM (DOM)
import DOM.HTML (window) as DOM
import DOM.HTML.Types (htmlDocumentToParentNode) as DOM
import DOM.HTML.Window (document) as DOM
import DOM.Node.ParentNode (QuerySelector(..))
import DOM.Node.ParentNode (querySelector) as DOM
import Data.Maybe (fromJust)
import Partial.Unsafe (unsafePartial)
import React (ReactElement)
import React as R
import React.DOM (p)
import React.DOM.Props (Props, className, unsafeFromPropsArray)
import ReactDOM as RDOM
import Thermite (Render, Spec, createReactSpec, defaultPerformAction, simpleSpec)

-- 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 =
47 48 49 50 51 52 53 54 55 56 57 58 59
  { className   :: String,
    style       :: String,  -- object,
    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
60 61 62
  }

type OptsLoading =
63 64
  { text      :: String,
    color     :: Color,  --- color
65
    textColor :: Color, --color
66 67
    maskColor :: Color, --color
    zlevel    :: Int
68 69 70
  }

type Option =
71 72 73 74 75 76 77
  { title    :: Title
  , legend   :: Legend
  , tooltip  :: Tooltip
  , grid     :: Grid
  , xAxis    :: Array XAxis
  , yAxis    :: Array YAxis
  , series   :: Array Series
78 79 80 81 82
  , dataZoom :: Array DataZoom
  }


type DataZoom =
83 84 85 86 87
  {"type"      :: String
  , xAxisIndex :: Int
  , filterMode :: String
  , start      :: Int
  , end        :: Int
88 89 90 91 92 93 94
  }

type Grid =
  {containLabel :: Boolean
  }

type Legend =
95 96
  {"type"  :: String
  , show   :: Boolean
97
  , zlevel :: Number
98 99 100 101
  , z      :: Number
  , left   :: Number
  , top    :: Number
  , right  :: Number
102
  , bottom :: Number
103
  , width  :: Number
104 105
  , height :: Number
  , orient :: String
106 107 108 109 110 111 112
  , align  :: String
  , padding       :: Number
  , itemGap       :: Number
  , itemWidth     :: Number
  , itemHeight    :: Number
  , formatter     :: String
  , selectedMode  :: Boolean
113
  , inactiveColor :: Color
114 115
  , selected      :: String -- object
  , "data"        :: Array Data
116 117 118
  }

type Data =
119 120
  { name      :: String
  , icon      :: String
121 122 123 124 125 126
  , textStyle :: {}
  }



type SubtextStyle =
127 128
  { color      :: Color
  , fontStyle  :: String
129 130
  , fontWeight :: String
  , fontFamily :: String
131 132
  , fontSize   :: Int
  , align      :: String
133
  , verticalAlign :: String
134 135 136
  , lineHeight    :: Number
  , width         :: Number
  , height        :: Number
137 138 139
  , textBorderColor :: String
  , textBorderWidth :: Number
  , textShadowColor :: String
140
  , textShadowBlur  :: Number
141 142
  , textShadowOffsetX :: Number
  , textShadowOffsetY :: Number
143
  , rich              :: Rich
144 145 146 147
  }


type Tooltip =
148
  { trigger   :: String
149 150 151 152
  , formatter :: String -- TODO function
  }

type XAxis =
153 154
  { "data"   :: Array String
  , "type"   :: String
155 156 157 158 159 160 161 162
  , axisTick :: AxisTick
  }
type AxisTick =
  {
    alignWithLabel :: Boolean
  }

type YAxis =
163 164 165 166
  { "type"    :: String
  , name      :: String
  , min       :: Int
  , position  :: String
167 168 169 170 171 172 173 174 175
  , axisLabel :: AxisLabel
  }

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


type Series =
176
  { name   :: String
177 178 179 180 181
  , "type" :: String
  , "data" :: Array Int
  }

type Title =
182 183 184 185 186 187 188 189
  { text         :: String
  , show         :: Boolean
  , link         :: String
  , target       :: String
  , textStyle    :: TextStyle
  , subtext      :: String
  , sublink      :: String
  , subtarget    :: String
190
  , subtextStyle :: SubtextStyle
191 192 193 194 195 196 197 198
  , padding      :: Number
  , itemGap      :: Number
  , zlevel       :: Number
  , z            :: Number
  , left         :: Number
  , top          :: Number
  , right        :: Number
  , bottom       :: Number
199
  , backgroundColor :: Color
200 201 202 203 204 205 206
  , borderColor     :: Color
  , borderWidth     :: Number
  , borderRadius    :: Number -- NumberOrArray
  , shadowBlur      :: Number
  , shadowColor     :: Color
  , shadowOffsetX   :: Number
  , shadowOffsetY   :: Number
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

-- data NumberOrArray = Number | Array Number



type Rich = {}


foreign import eChartsClass :: forall props. ReactClass props

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

-- Props

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

type TextStyle =
227 228
  { color      :: Color
  , fontStyle  :: String
229 230
  , fontWeight :: String
  , fontFamily :: String
231 232 233 234 235 236
  , fontSize   :: Int
  , align      :: String
  , verticalAlign   :: String
  , lineHeight      :: Int
  , width           :: Int
  , height          :: Int
237 238 239
  , textBorderColor :: String
  , textBorderWidth :: Int
  , textShadowColor :: String
240
  , textShadowBlur  :: Int
241 242
  , textShadowOffsetX :: Int
  , textShadowOffsetY :: Int
243
  , rich              :: Rich
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 369 370 371 372 373 374 375
  }

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''
      -- ]

376 377
histogram :: ReactElement
histogram = echarts
378 379 380
     [ option
       [ tooltip [trigger "axis"]
       , grid [containLabel true]
381 382
       , legend [data' ["Map terms coverage", "Favorites", "All"]]
       -- , legend [data' ["Map Terms coverage", "Favorites", "All"]]
383 384 385
       , xAxis
         [ type' "category"
         , axisTick [alignWithLabel true]
386 387 388 389
         , data' ["Jan" , "Feb", "Mar" , "Apr"
                 , "May", "Jun", "July", "Aug"
                 , "Sep", "Oct", "Nov" , "Dec"
                 ]
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
         ]
       , dataZoom [dz1,dz1,dz2,dz2]
       , yAxis [ya1, ya2]
       , series [sd1, sd2, sd3]
       ]
     ]

dz1 = unsafeFromPropsArray
      [ type' "slider"
      , xAxisIndex 0
      , filterMode "empty"
      , start 0
      , end 100
      ]

dz2 = unsafeFromPropsArray
      [ type' "inside"
      , xAxisIndex 0
      , filterMode "empty"
      , start 0
      , end 100
      ]

ya1 = unsafeFromPropsArray
      [ type' "value"
415
      , name "Score metric"
416
      , min 0
417
      , position "right"
418
      , axisLabel [formatter "{value}"]
419 420 421 422
      ]

ya2 = unsafeFromPropsArray
      [ type' "value"
423
      , name "Publications (by year)"
424
      , min 0
425
      , position "left"
426
      , axisLabel [formatter "{value}"]
427 428 429
      ]

sd1 = unsafeFromPropsArray
430
      [ name "Map terms coverage"
431 432 433 434 435 436 437 438
      , type' "line"
      , label [normal[showp true, position "top"]]
      , lineStyle [ normal
                    [ width 3
                    , shadowColor "rgba(0,0,0,0.4)"
                    , shadowBlur 10
                    , shadowOffsetY 10
                    ]]
439
      , data' [95, 80, 75, 35, 30, 50, 70, 80, 95, 95, 95, 99]
440 441 442
      ]

sd3 = unsafeFromPropsArray
443
      [ name "All"
444 445 446 447 448 449 450
      , type' "bar"
      , label [normal[showp true, position "top"]]
      , yAxisIndex 1
      , data' [201, 222, 223, 777, 244, 255, 555, 879, 938, 1364, 1806, 2324]
      ]


451 452 453 454 455 456 457 458
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]
      ]

459 460 461

p'' :: ReactElement
p'' = p [] []