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

Commit 1ada6b1

Browse files
committed
unified actions and events
1 parent 8ccb3a7 commit 1ada6b1

File tree

7 files changed

+182
-20
lines changed

7 files changed

+182
-20
lines changed

example/src/Bar.purs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import Color as C
77
import Control.Monad.Eff (Eff)
88
import Control.Monad.Eff.Exception (EXCEPTION)
99
import Control.Monad.Eff.Random (RANDOM, random)
10-
10+
import DOM (DOM)
11+
import DOM.Node.Types (ElementId(..))
1112
import Data.Array as Arr
12-
import Data.Traversable as F
13+
import Data.Foreign (toForeign)
1314
import Data.Maybe (Maybe(..))
14-
15+
import Data.Newtype (wrap)
16+
import Data.Symbol (SProxy(..))
17+
import Data.Traversable as F
18+
import Data.Tuple (Tuple(..))
19+
import Data.Variant as V
1520
import Debug.Trace as DT
16-
17-
import DOM (DOM)
18-
import DOM.Node.Types (ElementId(..))
19-
2021
import ECharts.Chart as EC
21-
import ECharts.Types as ET
22-
import ECharts.Monad (DSL)
23-
import ECharts.Types.Phantom as ETP
2422
import ECharts.Commands as E
2523
import ECharts.Event as EE
26-
24+
import ECharts.Monad (DSL)
25+
import ECharts.Types as ET
26+
import ECharts.Types.Phantom as ETP
2727
import Utils as U
2828

2929
itemStyle DSL ETP.ItemStyleI
@@ -153,3 +153,23 @@ chart = do
153153
inp ← genInp
154154
EC.setOption (options inp) ch
155155
EE.listenAll ch DT.traceAnyA
156+
EE.dispatch (V.inj (SProxy SProxy "brush") $ toForeign evt) ch
157+
where
158+
evt =
159+
{ batch:
160+
[ { areas:
161+
[ { brushType: "rect"
162+
, brushMode: "single"
163+
, range:
164+
[ [ 610
165+
, 885
166+
]
167+
, [ 236
168+
, 423
169+
]
170+
]
171+
}
172+
]
173+
}
174+
]
175+
}

example/src/Pie.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import Prelude
44

55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Exception (EXCEPTION)
7+
import Data.Foreign (toForeign)
78
import Data.Maybe (Maybe(..))
9+
import Data.Symbol (SProxy(..))
10+
import Data.Variant as V
811
import Debug.Trace as DT
912
import DOM (DOM)
1013
import DOM.Node.Types (ElementId(..))
@@ -46,6 +49,7 @@ options = do
4649
E.name "three"
4750
E.pie do
4851
E.name "Outer"
52+
E.selectedMode ET.Multiple
4953
E.radius $ ET.Radius { start: ET.Percent 40.0, end: ET.Percent 55.0 }
5054
E.buildItems do
5155
E.addItem do
@@ -83,3 +87,11 @@ chart = do
8387
ch ← EC.init el
8488
EC.setOption options ch
8589
EE.listenAll ch DT.traceAnyA
90+
EE.dispatch
91+
(V.inj (SProxy SProxy "pieselected")
92+
$ toForeign { seriesIndex: [1]
93+
, seriesName: []
94+
, dataIndex: Nothing
95+
, name: Just "seven"
96+
})
97+
ch

example/src/Scatter.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import ECharts.Types as ET
2222
import ECharts.Types.Phantom as ETP
2323
import ECharts.Commands as E
2424
import ECharts.Monad (DSL)
25-
2625
import Math (cos, sin, (%))
27-
2826
import Utils as U
27+
import Unsafe.Coerce (unsafeCoerce)
2928

3029
genSinData e. Eff (random RANDOM|e) (Array ET.Item)
3130
genSinData = do

src/ECharts/Commands.purs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,33 +430,69 @@ textMiddle = set "textBaseline" $ toForeign "middle"
430430
brush i. DSL TP.BrushI DSL (brush I|i)
431431
brush = set "brush" <<< buildObj
432432

433+
brushType i. DSL TP.BrushToolboxI DSL (brushType I|i)
434+
brushType a = set "type" $ buildArr a
435+
433436
brushToolbox i. DSL TP.BrushToolboxI DSL (brushToolbox I|i)
434437
brushToolbox a = set "toolbox" $ buildArr a
435438

439+
brushModeSingle i. DSL (brushMode I|i)
440+
brushModeSingle = set "brushMode" $ toForeign "single"
441+
442+
brushIcons i. DSL TP.BFFieldI DSL (bfIcon I|i)
443+
brushIcons a = set "icon" $ buildObj a
444+
445+
brushTitle i. DSL TP.BFFieldI DSL (bfTitle I|i)
446+
brushTitle a = set "title" $ buildObj a
447+
448+
brushModeMultiple i. DSL (brushMode I|i)
449+
brushModeMultiple = set "brushMode" $ toForeign "multiple"
450+
436451
rect i. DSL (tool I|i)
437452
rect = set "" $ toForeign "rect"
438453

454+
setRect i. String DSL (rect I|i)
455+
setRect a = set "rect" $ toForeign a
456+
439457
polygon i. DSL (tool I|i)
440458
polygon = set "" $ toForeign "polygon"
441459

460+
setPolygon i. String DSL (polygon I|i)
461+
setPolygon a = set "polygon" $ toForeign a
462+
442463
lineX i. DSL (tool I|i)
443464
lineX = set "" $ toForeign "lineX"
444465

466+
setLineX i. String DSL (lineX I|i)
467+
setLineX a = set "lineX" $ toForeign a
468+
445469
lineY i. DSL (tool I|i)
446470
lineY = set "" $ toForeign "lineY"
447471

472+
setLineY i. String DSL (lineY I|i)
473+
setLineY a = set "lineY" $ toForeign a
474+
448475
keep i. DSL (tool I|i)
449476
keep = set "" $ toForeign "keep"
450477

478+
setKeep i. String DSL (keep I|i)
479+
setKeep a = set "keep" $ toForeign a
480+
451481
clear i. DSL (tool I|i)
452482
clear = set "" $ toForeign "clear"
453483

484+
setClear i. String DSL (clear I|i)
485+
setClear a = set "clear" $ toForeign a
486+
454487
toolbox i. DSL TP.ToolboxI DSL (toolbox I|i)
455488
toolbox a = set "toolbox" $ buildObj a
456489

457490
feature i. DSL TP.FeatureI DSL (feature I|i)
458491
feature a = set "feature" $ buildObj a
459492

493+
brushFeature i. DSL TP.BrushFeatureI DSL (brush I|i)
494+
brushFeature a = set "brush" $ buildObj a
495+
460496
magicType i. DSL TP.MagicTypeI DSL (magicType I|i)
461497
magicType a = set "magicType" $ buildObj a
462498

src/ECharts/Event.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,41 @@ exports.on_ = function(chart) {
33
return function(callback) {
44
return function() {
55
return chart.on(eName, function(e) {
6+
console.log(e);
67
callback(e)();
78
});
89
};
910
};
1011
};
1112
};
13+
14+
exports.dispatchAction_ = function(maybeKeys) {
15+
return function(maybe) {
16+
return function(rawAction) {
17+
return function(chart) {
18+
return function() {
19+
var keys = Object.keys(rawAction),
20+
i, key, value, keysArr,
21+
action = {};
22+
for (i = 0; i < keys.length; i++) {
23+
key = keys[i];
24+
if (typeof maybeKeys[rawAction.type] === "undefined") {
25+
keysArr = [];
26+
} else {
27+
keysArr = maybeKeys[rawAction.type];
28+
}
29+
if (keysArr.indexOf(key) == -1) {
30+
value = rawAction[key];
31+
} else {
32+
value = maybe(undefined)(function(x) {return x;})(rawAction[key]);
33+
}
34+
if (typeof value !== "undefined") {
35+
action[key] = value;
36+
}
37+
}
38+
chart.dispatchAction(action);
39+
};
40+
};
41+
};
42+
};
43+
}

src/ECharts/Event.purs

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ module ECharts.Event where
33
import Prelude
44

55
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Class (liftEff, class MonadEff)
67
import Control.Monad.Except (runExcept)
78
import Data.Foldable (for_)
89
import Data.Foreign (Foreign, readString)
910
import Data.Foreign.Index (readProp)
11+
import Data.Maybe as M
1012
import Data.List as L
11-
import Data.Tuple (Tuple(..))
13+
import Data.Record.Unsafe as R
14+
import Data.StrMap as SM
15+
import Data.Tuple (Tuple(..), fst, snd)
1216
import Data.Variant.Internal (RLProxy(..), variantTags, class VariantTags)
1317
import ECharts.Types (EChartsEvent, EChartsEventR, Chart, ECHARTS)
1418
import Type.Row (class RowToList)
@@ -21,13 +25,13 @@ foreign import on_
2125
( Foreign Eff (echarts ECHARTS|e) Unit )
2226
Eff (echarts ECHARTS|e) Unit
2327

24-
2528
listenAll
26-
e
27-
. Chart
29+
e m
30+
. MonadEff ( echarts ECHARTS |e ) m
31+
Chart
2832
( EChartsEvent Eff (echarts ECHARTS|e) Unit )
29-
Eff (echarts ECHARTS|e) Unit
30-
listenAll chart cb =
33+
m Unit
34+
listenAll chart cb = liftEff $
3135
for_ eventNames \en → on_ chart en \frn →
3236
for_ (runExcept $ readProp "type" frn >>= readString) \tp →
3337
when (tp == en) $ cb $ toEChartsEvent $ Tuple tp frn
@@ -37,3 +41,58 @@ listenAll chart cb =
3741

3842
toEChartsEvent ω. Tuple String ω EChartsEvent
3943
toEChartsEvent = unsafeCoerce
44+
45+
foreign import dispatchAction_
46+
e a b action
47+
. SM.StrMap (Array String) -- mapping from action type to keys that are maybe's
48+
( b (a b) M.Maybe a b ) -- maybe
49+
action
50+
Chart
51+
Eff ( echarts ECHARTS |e ) Unit
52+
53+
dispatch
54+
e m
55+
. MonadEff ( echarts ECHARTS | e ) m
56+
EChartsEvent
57+
Chart
58+
m Unit
59+
dispatch vaction chart =
60+
liftEff $ dispatchAction_ strmapConfig M.maybe action chart
61+
where
62+
strmapConfig = SM.fromFoldable
63+
[ Tuple "highlight" ["name", "dataIndex"]
64+
, Tuple "downplay" ["name", "dataIndex"]
65+
, Tuple "mapSelect" ["name", "dataIndex"]
66+
, Tuple "mapUnSelect" ["name", "dataIndex"]
67+
, Tuple "mapToggleSelect" ["name", "dataIndex"]
68+
, Tuple "pieSelect" ["name", "dataIndex"]
69+
, Tuple "pieUnSelect" ["name", "dataIndex"]
70+
, Tuple "pieToggleSelect" ["name", "dataIndex"]
71+
, Tuple "focusNodeAdjacency" ["seriesId", "seriesIndex", "seriesName"]
72+
, Tuple "unfocusNodeAdjacency" ["seriesId", "seriesIndex", "seriesName"]
73+
]
74+
75+
variantPair Tuple String {}
76+
variantPair = unsafeCoerce vaction
77+
78+
actionType String
79+
actionType = case fst variantPair of
80+
"legendselectchanged""legendToggleSelect"
81+
"legendselected""legendSelect"
82+
"legendunselected""legendUnSelect"
83+
"datazoom""dataZoom"
84+
"datarangeselected""selectDataRange"
85+
"timelinechanged""timelineChange"
86+
"timelineplaychanged""timelinePlayChange"
87+
"pieselectchanged""pieToggleSelect"
88+
"pieselected""pieSelect"
89+
"pieunselected""pieUnSelect"
90+
"mapselectchanged""mapToggleSelect"
91+
"mapselected""mapSelect"
92+
"mapunselected""mapUnSelect"
93+
"focusnodeadjacency""focusNodeAdjacency"
94+
"unfocusnodeadjacency""unfocusNodeAdjacency"
95+
s → s
96+
97+
action ω. { "type" String | ω }
98+
action = R.unsafeSet "type" actionType $ snd variantPair

src/ECharts/Types.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import Prelude
44

55
import Control.Monad.Eff (kind Effect)
66
import Data.Foreign (Foreign, toForeign)
7+
import Data.Maybe (Maybe)
8+
import Data.Symbol (SProxy(..))
79
import Data.Variant as V
10+
import Data.StrMap as SM
811

912
foreign import data ChartType
1013

@@ -208,7 +211,8 @@ type EChartsEventR =
208211
, unfocusNodeAdjacency Foreign
209212
, brush Foreign
210213
, brushselected Foreign
214+
, pieSelect Foreign
215+
, pieUnSelect Foreign
211216
)
212217

213-
214218
type EChartsEvent = V.Variant EChartsEventR

0 commit comments

Comments
 (0)