Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit dd5db69

Browse files
authored
Merge pull request #30 from safareli/safareli-theme
Add theme support
2 parents b9cc034 + 1cdfcb6 commit dd5db69

File tree

6 files changed

+104
-12
lines changed

6 files changed

+104
-12
lines changed

example/src/Scatter.purs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Control.Monad.Eff.Exception (EXCEPTION)
77
import Control.Monad.Eff.Random (RANDOM)
88

99
import Data.Array (zipWith)
10-
import Data.Maybe (Maybe(..))
10+
import Data.Maybe (Maybe(..), maybe)
1111
import Data.NonEmpty as NE
1212
import Data.Tuple (Tuple(..), uncurry)
1313

@@ -17,6 +17,7 @@ import DOM (DOM)
1717
import DOM.Node.Types (ElementId(..))
1818

1919
import ECharts.Chart as EC
20+
import ECharts.Theme as ETheme
2021
import ECharts.Types as ET
2122
import ECharts.Types.Phantom as ETP
2223
import ECharts.Commands as E
@@ -93,11 +94,17 @@ options sinData cosData = do
9394

9495
chart e. Eff (dom DOM, echarts ET.ECHARTS, err EXCEPTION, random RANDOM|e) Unit
9596
chart = do
96-
mbEl ← U.getElementById $ ElementId "scatter"
97+
chart' (ElementId "scatter-1") Nothing
98+
chart' (ElementId "scatter-2") (Just (ETheme.dark))
99+
chart' (ElementId "scatter-3") (Just (ETheme.macarons))
100+
101+
chart' e. ElementId Maybe ETheme.Theme Eff (dom DOM, echarts ET.ECHARTS, err EXCEPTION, random RANDOM|e) Unit
102+
chart' id theme = do
103+
mbEl ← U.getElementById id
97104
case mbEl of
98105
NothingDT.traceAnyA "There is no element with scatter id"
99106
Just el → do
100-
ch ← EC.init el
107+
ch ← maybe EC.init EC.initWithTheme theme el
101108
sinData ← genSinData
102109
cosData ← genCosData
103110
EC.setOption (options sinData cosData) ch

public/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
<body>
2222
<div id="line" class="example">
2323
</div>
24-
<div id="scatter" class="example">
24+
<div id="scatter-1" class="example">
25+
</div>
26+
<div id="scatter-2" class="example">
27+
</div>
28+
<div id="scatter-3" class="example">
2529
</div>
2630
<div id="pie" class="example">
2731
</div>

src/ECharts/Chart.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
var echarts = require("echarts");
22

3-
exports.initImpl = function(el) {
4-
return function() {
5-
return echarts.init(el);
3+
exports.initImpl = function(theme) {
4+
return function(el) {
5+
return function() {
6+
return echarts.init(el, theme);
7+
};
8+
};
9+
};
10+
11+
function isObject(o) {
12+
return null != o &&
13+
typeof o === 'object' &&
14+
Object.prototype.toString.call(o) === '[object Object]';
15+
}
16+
17+
exports.registerTheme = function(name) {
18+
return function(theme) {
19+
return function() {
20+
// if value is not `undefined` and is not of type `string` then it must be `Foreign` for which we only permit plain Objects.
21+
if (theme !== undefined && typeof theme !== 'string' && !isObject(theme)) {
22+
throw new TypeError('Theme must be an Object')
23+
}
24+
echarts.registerTheme(name, theme);
25+
};
626
};
727
};
828

src/ECharts/Chart.purs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module ECharts.Chart
22
( init
3+
, initWithTheme
4+
, registerTheme
35
, setOption
46
, resetOption
57
, resize
@@ -8,30 +10,48 @@ module ECharts.Chart
810
, getOption
911
) where
1012

11-
import Prelude
12-
13+
import Prelude
14+
1315
import Control.Monad.Eff (Eff)
1416
import Control.Monad.Eff.Class (class MonadEff, liftEff)
1517
import Control.Monad.Eff.Exception (EXCEPTION)
1618

17-
import Data.Foreign (Foreign)
19+
import Data.Foreign (Foreign, toForeign)
1820

1921
import DOM (DOM)
2022
import DOM.HTML.Types (HTMLElement)
2123

24+
import ECharts.Internal (undefinedValue)
2225
import ECharts.Monad (DSL, buildObj)
26+
import ECharts.Theme (Theme(..))
2327
import ECharts.Types.Phantom (OptionI)
2428
import ECharts.Types (Chart, ECHARTS)
2529

2630
foreign import initImpl
27-
e. HTMLElement Eff (dom DOM, echarts ECHARTS, err EXCEPTION|e) Chart
31+
e. Foreign
32+
HTMLElement
33+
Eff (dom DOM, echarts ECHARTS, err EXCEPTION|e) Chart
2834

2935
init
3036
m e
3137
. MonadEff (dom DOM, echarts ECHARTS, err EXCEPTION|e) m
3238
HTMLElement
3339
m Chart
34-
init el = liftEff $ initImpl el
40+
init el = liftEff $ initImpl undefinedValue el
41+
42+
initWithTheme
43+
m e
44+
. MonadEff (dom DOM, echarts ECHARTS, err EXCEPTION|e) m
45+
Theme
46+
HTMLElement
47+
m Chart
48+
initWithTheme (ByName name) el = liftEff $ initImpl (toForeign name) el
49+
initWithTheme (FromObject theme) el = liftEff $ initImpl (toForeign theme) el
50+
51+
foreign import registerTheme
52+
e. String
53+
Foreign
54+
Eff (echarts ECHARTS|e) Unit
3555

3656
foreign import setOptionImpl
3757
e. Foreign Chart Eff (echarts ECHARTS, err EXCEPTION|e) Unit

src/ECharts/Theme.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require("echarts/theme/dark")
2+
require("echarts/theme/infographic")
3+
require("echarts/theme/macarons")
4+
require("echarts/theme/roma")
5+
require("echarts/theme/shine")
6+
require("echarts/theme/vintage")
7+
8+
exports.forceExport = null

src/ECharts/Theme.purs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module ECharts.Theme
2+
( Theme(..)
3+
, dark
4+
, infographic
5+
, macarons
6+
, roma
7+
, shine
8+
, vintage
9+
) where
10+
11+
import Data.Foreign (Foreign)
12+
13+
foreign import forceExport :: Foreign
14+
15+
data Theme = ByName String | FromObject Foreign
16+
17+
dark Theme
18+
dark = ByName "dark"
19+
20+
infographic Theme
21+
infographic = ByName "infographic"
22+
23+
macarons Theme
24+
macarons = ByName "macarons"
25+
26+
roma Theme
27+
roma = ByName "roma"
28+
29+
shine Theme
30+
shine = ByName "shine"
31+
32+
vintage Theme
33+
vintage = ByName "vintage"

0 commit comments

Comments
 (0)