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

Commit 0dd4eaf

Browse files
committed
brush event augmentation, some types in events
1 parent 1ada6b1 commit 0dd4eaf

File tree

9 files changed

+163
-37
lines changed

9 files changed

+163
-37
lines changed

example/src/Bar.purs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ options inp = do
7373
E.magicTiled
7474
E.dataView $ pure unit
7575

76+
7677
E.xAxis do
7778
E.name "X Axis"
7879
E.axisLine do
@@ -86,9 +87,9 @@ options inp = do
8687
E.inverse true
8788
E.splitArea E.hidden
8889

89-
E.grid do
90-
E.left $ ET.Pixel 100
91-
90+
-- E.grid do
91+
-- E.left $ ET.Pixel 100
92+
{-
9293
E.visualMap do
9394
E.continuous do
9495
E.dimension 1
@@ -107,7 +108,7 @@ options inp = do
107108
E.controller do
108109
E.inRange do
109110
F.for_ (C.fromHexString "#2f4554") E.color
110-
111+
-}
111112
E.series do
112113
E.bar do
113114
E.name "bar"
@@ -152,20 +153,21 @@ chart = do
152153
ch ← EC.init el
153154
inp ← genInp
154155
EC.setOption (options inp) ch
156+
EC.getOption ch >>= DT.traceAnyA
155157
EE.listenAll ch DT.traceAnyA
156-
EE.dispatch (V.inj (SProxy SProxy "brush") $ toForeign evt) ch
158+
-- EE.dispatch (V.inj (SProxy ∷ SProxy "brush") $ toForeign evt) ch
157159
where
158160
evt =
159161
{ batch:
160162
[ { areas:
161163
[ { brushType: "rect"
162164
, brushMode: "single"
163-
, range:
164-
[ [ 610
165-
, 885
165+
, coordRange:
166+
[ [ 0
167+
, 1
166168
]
167-
, [ 236
168-
, 423
169+
, [ 0
170+
, 1
169171
]
170172
]
171173
}

example/src/Pie.purs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Control.Monad.Eff.Exception (EXCEPTION)
77
import Data.Foreign (toForeign)
88
import Data.Maybe (Maybe(..))
99
import Data.Symbol (SProxy(..))
10+
import Data.StrMap as SM
1011
import Data.Variant as V
1112
import Debug.Trace as DT
1213
import DOM (DOM)
@@ -89,9 +90,15 @@ chart = do
8990
EE.listenAll ch DT.traceAnyA
9091
EE.dispatch
9192
(V.inj (SProxy SProxy "pieselected")
92-
$ toForeign { seriesIndex: [1]
93-
, seriesName: []
94-
, dataIndex: Nothing
95-
, name: Just "seven"
96-
})
93+
$ { seriesIndex: 1
94+
, seriesName: "Outer"
95+
, name: "seven"
96+
, dataIndex: 4
97+
})
9798
ch
99+
-- EE.dispatch
100+
-- (V.inj (SProxy ∷ SProxy "legendselectchanged")
101+
-- $ { name: "seven"
102+
-- , selected: SM.empty
103+
-- })
104+
-- ch

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var echarts = require("echarts");
33
exports.initImpl = function(theme) {
44
return function(el) {
55
return function() {
6-
return echarts.init(el, theme);
6+
return echarts.init(el,theme);
77
};
88
};
99
};
@@ -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
};
@@ -38,6 +38,8 @@ exports.setOptionImpl = function(option) {
3838
exports.resetOptionImpl = function(option) {
3939
return function(chart) {
4040
return function() {
41+
console.log("reset option")
42+
console.log(option)
4143
chart.setOption(option, true, false);
4244
return {};
4345
};

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: 6 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

src/ECharts/Event.js

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,91 @@
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) {
6-
console.log(e);
7-
callback(e)();
8-
});
86+
var modifiedE = eName === "brush" ? addAll(this.getOption(), e) : e;
87+
callback(modifiedE)();
88+
});
989
};
1090
};
1191
};

src/ECharts/Event.purs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ dispatch vaction chart =
6565
, Tuple "mapSelect" ["name", "dataIndex"]
6666
, Tuple "mapUnSelect" ["name", "dataIndex"]
6767
, Tuple "mapToggleSelect" ["name", "dataIndex"]
68-
, Tuple "pieSelect" ["name", "dataIndex"]
69-
, Tuple "pieUnSelect" ["name", "dataIndex"]
70-
, Tuple "pieToggleSelect" ["name", "dataIndex"]
7168
, Tuple "focusNodeAdjacency" ["seriesId", "seriesIndex", "seriesName"]
7269
, Tuple "unfocusNodeAdjacency" ["seriesId", "seriesIndex", "seriesName"]
7370
]

src/ECharts/Types.purs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ 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(..))
97
import Data.Variant as V
108
import Data.StrMap as SM
119

@@ -182,35 +180,68 @@ newtype Coord = Coord Foreign
182180
coord Number Number Coord
183181
coord x y = Coord $ toForeign [ x, y ]
184182

183+
type LegendEventR =
184+
{ name String
185+
, selected SM.StrMap Boolean
186+
}
187+
188+
type DataRangeEventR =
189+
{ visualMapId String
190+
, selected Array Number
191+
}
192+
193+
type ClickEventR =
194+
{ seriesName String
195+
, name String
196+
, dataIndex Int
197+
, seriesIndex Int
198+
}
199+
200+
type BrushEventAreaR =
201+
{ brushType String
202+
, panelId String
203+
, coordRange Array Foreign
204+
, xAxisData Array String
205+
, yAxisData Array String
206+
, gridIndex Int
207+
}
208+
209+
type BrushEventR =
210+
{ areas Array BrushEventAreaR
211+
, titleTexts Array String
212+
}
213+
185214
type EChartsEventR =
186-
( click Foreign
215+
( click ClickEventR
187216
, dblclick Foreign
188217
, mousedown Foreign
189218
, mousemove Foreign
190219
, mouseup Foreign
191220
, mouseover Foreign
192221
, mouseout Foreign
193-
, legendselectchanged Foreign
194-
, legendselected Foreign
195-
, legendunselected Foreign
222+
, legendselectchanged LegendEventR
223+
, legendselected LegendEventR
224+
, legendunselected LegendEventR
196225
, datazoom Foreign
197-
, datarangeselected Foreign
226+
, datarangeselected DataRangeEventR
198227
, timelinechanged Foreign
199228
, timelineplaychanged Foreign
200229
, restore Foreign
201230
, dataviewchanged Foreign
202231
, magictypechanged Foreign
203-
, pieselectchanged Foreign
204-
, pieselected Foreign
205-
, pieunselected Foreign
232+
-- This is not absolutely accurate, but working with pieselectchanged
233+
-- is too painful
234+
, pieselectchanged ClickEventR
235+
, pieselected ClickEventR
236+
, pieunselected ClickEventR
206237
, mapselectchanged Foreign
207238
, mapselected Foreign
208239
, mapunselected Foreign
209240
, axisareaselected Foreign
210241
, focusNodeAdjancency Foreign
211242
, unfocusNodeAdjacency Foreign
212-
, brush Foreign
213-
, brushselected Foreign
243+
, brush BrushEventR
244+
, brushselected BrushEventR
214245
, pieSelect Foreign
215246
, pieUnSelect Foreign
216247
)

0 commit comments

Comments
 (0)