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

Commit 12eba86

Browse files
authored
Merge pull request #45 from cryogenian/events
Events/Actions
2 parents 3a63c19 + 71d23d4 commit 12eba86

File tree

10 files changed

+252
-40
lines changed

10 files changed

+252
-40
lines changed

example/src/Bar.purs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@ 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
1313
import Data.Maybe (Maybe(..))
14-
14+
import Data.Traversable as F
1515
import Debug.Trace as DT
16-
17-
import DOM (DOM)
18-
import DOM.Node.Types (ElementId(..))
19-
2016
import ECharts.Chart as EC
21-
import ECharts.Types as ET
22-
import ECharts.Monad (DSL)
23-
import ECharts.Types.Phantom as ETP
2417
import ECharts.Commands as E
2518
import ECharts.Event as EE
26-
19+
import ECharts.Monad (DSL)
20+
import ECharts.Types as ET
21+
import ECharts.Types.Phantom as ETP
2722
import Utils as U
2823

2924
itemStyle DSL ETP.ItemStyleI
@@ -73,6 +68,7 @@ options inp = do
7368
E.magicTiled
7469
E.dataView $ pure unit
7570

71+
7672
E.xAxis do
7773
E.name "X Axis"
7874
E.axisLine do
@@ -107,7 +103,6 @@ options inp = do
107103
E.controller do
108104
E.inRange do
109105
F.for_ (C.fromHexString "#2f4554") E.color
110-
111106
E.series do
112107
E.bar do
113108
E.name "bar"
@@ -152,4 +147,5 @@ chart = do
152147
ch ← EC.init el
153148
inp ← genInp
154149
EC.setOption (options inp) ch
150+
EC.getOption ch >>= DT.traceAnyA
155151
EE.listenAll ch DT.traceAnyA

example/src/Pie.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Exception (EXCEPTION)
77
import Data.Maybe (Maybe(..))
8+
import Data.Symbol (SProxy(..))
9+
import Data.Variant as V
810
import Debug.Trace as DT
911
import DOM (DOM)
1012
import DOM.Node.Types (ElementId(..))
@@ -46,6 +48,7 @@ options = do
4648
E.name "three"
4749
E.pie do
4850
E.name "Outer"
51+
E.selectedMode ET.Multiple
4952
E.radius $ ET.Radius { start: ET.Percent 40.0, end: ET.Percent 55.0 }
5053
E.buildItems do
5154
E.addItem do
@@ -83,3 +86,11 @@ chart = do
8386
ch ← EC.init el
8487
EC.setOption options ch
8588
EE.listenAll ch DT.traceAnyA
89+
EE.dispatch
90+
(V.inj (SProxy SProxy "pieselected")
91+
$ { seriesIndex: 1
92+
, seriesName: "Outer"
93+
, name: "seven"
94+
, dataIndex: 4
95+
})
96+
ch

example/src/Scatter.purs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Exception (EXCEPTION)
77
import Control.Monad.Eff.Random (RANDOM)
8-
98
import Data.Array (zipWith)
109
import Data.Maybe (Maybe(..), maybe)
1110
import Data.NonEmpty as NE
1211
import Data.Tuple (Tuple(..), uncurry)
13-
1412
import Debug.Trace as DT
15-
1613
import DOM (DOM)
1714
import DOM.Node.Types (ElementId(..))
18-
1915
import ECharts.Chart as EC
2016
import ECharts.Theme as ETheme
2117
import ECharts.Types as ET
2218
import ECharts.Types.Phantom as ETP
2319
import ECharts.Commands as E
2420
import ECharts.Monad (DSL)
25-
2621
import Math (cos, sin, (%))
27-
2822
import Utils as U
2923

3024
genSinData e. Eff (random RANDOM|e) (Array ET.Item)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "purescript-echarts",
33
"private": true,
44
"devDependencies": {
5-
"echarts": "3.5.4",
5+
"echarts": "cryogenian/echarts#item-value-unshift-prebuild",
66
"express": "^4.14.0"
77
},
88
"scripts": {

src/ECharts/Chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports.registerTheme = function(name) {
1919
return function() {
2020
// if value is not `undefined` and is not of type `string` then it must be `Foreign` for which we only permit plain Objects.
2121
if (theme !== undefined && typeof theme !== 'string' && !isObject(theme)) {
22-
throw new TypeError('Theme must be an Object')
22+
throw new TypeError('Theme must be an Object');
2323
}
2424
echarts.registerTheme(name, theme);
2525
};

src/ECharts/Chart.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ initWithTheme
4343
Theme
4444
HTMLElement
4545
m Chart
46-
initWithTheme theme el = liftEff $ initImpl (either (toForeign <<< builtInThemeName) toForeign theme) el
46+
initWithTheme theme el =
47+
liftEff $ initImpl (either (toForeign <<< builtInThemeName) toForeign theme) el
4748

4849
foreign import registerTheme
4950
e. String

src/ECharts/Commands.purs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ xAxisIndex a = set "xAxisIndex" $ toForeign a
220220
yAxisIndex i. Int DSL (yAxisIndex I|i)
221221
yAxisIndex a = set "yAxisIndex" $ toForeign a
222222

223+
xAxisAllIndices i. DSL (xAxisIndex I|i)
224+
xAxisAllIndices = set "xAxisIndex" $ toForeign "all"
225+
226+
yAxisAllIndices i. DSL (yAxisIndex I|i)
227+
yAxisAllIndices = set "yAxisIndex" $ toForeign "all"
228+
223229
polarIndex i. Int DSL (polarIndex I|i)
224230
polarIndex a = set "polarIndex" $ toForeign a
225231

@@ -430,33 +436,69 @@ textMiddle = set "textBaseline" $ toForeign "middle"
430436
brush i. DSL TP.BrushI DSL (brush I|i)
431437
brush = set "brush" <<< buildObj
432438

439+
brushType i. DSL TP.BrushToolboxI DSL (brushType I|i)
440+
brushType a = set "type" $ buildArr a
441+
433442
brushToolbox i. DSL TP.BrushToolboxI DSL (brushToolbox I|i)
434443
brushToolbox a = set "toolbox" $ buildArr a
435444

445+
brushModeSingle i. DSL (brushMode I|i)
446+
brushModeSingle = set "brushMode" $ toForeign "single"
447+
448+
brushIcons i. DSL TP.BFFieldI DSL (bfIcon I|i)
449+
brushIcons a = set "icon" $ buildObj a
450+
451+
brushTitle i. DSL TP.BFFieldI DSL (bfTitle I|i)
452+
brushTitle a = set "title" $ buildObj a
453+
454+
brushModeMultiple i. DSL (brushMode I|i)
455+
brushModeMultiple = set "brushMode" $ toForeign "multiple"
456+
436457
rect i. DSL (tool I|i)
437458
rect = set "" $ toForeign "rect"
438459

460+
setRect i. String DSL (rect I|i)
461+
setRect a = set "rect" $ toForeign a
462+
439463
polygon i. DSL (tool I|i)
440464
polygon = set "" $ toForeign "polygon"
441465

466+
setPolygon i. String DSL (polygon I|i)
467+
setPolygon a = set "polygon" $ toForeign a
468+
442469
lineX i. DSL (tool I|i)
443470
lineX = set "" $ toForeign "lineX"
444471

472+
setLineX i. String DSL (lineX I|i)
473+
setLineX a = set "lineX" $ toForeign a
474+
445475
lineY i. DSL (tool I|i)
446476
lineY = set "" $ toForeign "lineY"
447477

478+
setLineY i. String DSL (lineY I|i)
479+
setLineY a = set "lineY" $ toForeign a
480+
448481
keep i. DSL (tool I|i)
449482
keep = set "" $ toForeign "keep"
450483

484+
setKeep i. String DSL (keep I|i)
485+
setKeep a = set "keep" $ toForeign a
486+
451487
clear i. DSL (tool I|i)
452488
clear = set "" $ toForeign "clear"
453489

490+
setClear i. String DSL (clear I|i)
491+
setClear a = set "clear" $ toForeign a
492+
454493
toolbox i. DSL TP.ToolboxI DSL (toolbox I|i)
455494
toolbox a = set "toolbox" $ buildObj a
456495

457496
feature i. DSL TP.FeatureI DSL (feature I|i)
458497
feature a = set "feature" $ buildObj a
459498

499+
brushFeature i. DSL TP.BrushFeatureI DSL (brush I|i)
500+
brushFeature a = set "brush" $ buildObj a
501+
460502
magicType i. DSL TP.MagicTypeI DSL (magicType I|i)
461503
magicType a = set "magicType" $ buildObj a
462504

src/ECharts/Event.js

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,102 @@
1+
function addData(area, axis, setKey) {
2+
if (axis.type !== "value") {
3+
area[setKey] = axis.data;
4+
} else {
5+
area[setKey] = [ ];
6+
}
7+
return;
8+
}
9+
10+
// ECharts doesn't send x-axis data in brush events but we need it
11+
// because sorting algo may be opaque
12+
function addAxisData(option, e, axisKey, setKey, axisIndexKey) {
13+
var i, j, gridIndex;
14+
for (i = 0; i < e.areas.length; i++) {
15+
gridIndex = parseInt(e.areas[i].panelId.match(/\d+$/g)[0]);
16+
e.areas[i].gridIndex = gridIndex;
17+
if (typeof option === "undefined") {
18+
e.areas[i][setKey] = [ ];
19+
e.areas[i][axisIndexKey] = 0;
20+
} else if (typeof option[axisKey].length === "undefined" && gridIndex === 0) {
21+
addData(e.areas[i], option[axisKey], setKey);
22+
e.areas[i][axisIndexKey] = 0;
23+
} else if (option[axisKey].length === 1 && gridIndex === 0) {
24+
addData(e.areas[i], option[axisKey][0], setKey);
25+
e.areas[i][axisIndexKey] = 0;
26+
} else {
27+
for (j = 0; j < option[axisKey].length; j++) {
28+
if (option[axisKey][j].gridIndex === gridIndex) {
29+
addData(e.areas[i], option[axisKey][j], setKey);
30+
e.areas[i][axisIndexKey] = j;
31+
}
32+
}
33+
}
34+
}
35+
return e;
36+
}
37+
38+
function addXAxisData(option, e) {
39+
return addAxisData(option, e, "xAxis", "xAxisData", "xAxisIndex");
40+
}
41+
42+
function addYAxisData(option, e) {
43+
return addAxisData(option, e, "yAxis", "yAxisData", "yAxisIndex");
44+
}
45+
46+
function addSeriesAndTitles(option, e) {
47+
var i, xIx, yIx, serie, ai, area;
48+
for (ai = 0; ai < e.areas.length; ai++) {
49+
area = e.areas[ai];
50+
area.serieNames = [];
51+
area.serieIndices = [];
52+
for (i = 0; i < option.series.length; i++) {
53+
serie = option.series[i];
54+
xIx = serie.xAxisIndex || 0;
55+
yIx = serie.xAxisIndex || 0;
56+
if (xIx == area.xAxisIndex && yIx == area.yAxisIndex) {
57+
area.serieNames.push(serie.name);
58+
area.serieIndices.push(i);
59+
}
60+
}
61+
}
62+
var titles, titleTexts = [];
63+
if (!option.title) {
64+
titles = [];
65+
} else if (typeof option.title.length === "undefined") {
66+
titles = [option.title];
67+
} else {
68+
titles = option.title;
69+
}
70+
for (i = 0; i < titles.length; i++) {
71+
if (titles[i].text) titleTexts.push(titles[i].text);
72+
}
73+
e.titleTexts = titleTexts;
74+
return e;
75+
}
76+
77+
function addAll(option, e) {
78+
return addSeriesAndTitles(option, addYAxisData(option, addXAxisData(option, e)));
79+
}
80+
181
exports.on_ = function(chart) {
282
return function(eName) {
383
return function(callback) {
484
return function() {
585
return chart.on(eName, function(e) {
86+
if (eName === "brush") {
87+
addAll(this.getOption(), e);
88+
}
689
callback(e)();
7-
});
90+
});
891
};
992
};
1093
};
1194
};
95+
96+
exports.dispatchAction_ = function(action) {
97+
return function(chart) {
98+
return function() {
99+
return chart.dispatchAction(action);
100+
};
101+
};
102+
}

0 commit comments

Comments
 (0)