Skip to content

Commit 079d070

Browse files
feat: added mapReady,zoomLevelChanged,centerPositionChange events for echarts map
1 parent c5e0c86 commit 079d070

File tree

6 files changed

+107
-26
lines changed

6 files changed

+107
-26
lines changed

client/packages/lowcoder-comps/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lowcoder-comps",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"type": "module",
55
"license": "MIT",
66
"dependencies": {

client/packages/lowcoder-comps/src/comps/chartComp/chartComp.tsx

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
withViewFn,
2828
ThemeContext,
2929
chartColorPalette,
30-
loadScript,
3130
} from "lowcoder-sdk";
3231
import { getEchartsLocale, trans } from "i18n/comps";
3332
import { ItemColorComp } from "comps/chartComp/chartConfigs/lineChartConfig";
@@ -49,7 +48,13 @@ let ChartTmpComp = (function () {
4948
ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
5049
const apiKey = comp.children.mapApiKey.getView();
5150
const mode = comp.children.mode.getView();
52-
const onEvent = comp.children.onEvent.getView();
51+
const mapCenterPosition = {
52+
lng: comp.children.mapCenterLng.getView(),
53+
lat: comp.children.mapCenterLat.getView(),
54+
}
55+
const mapZoomlevel = comp.children.mapZoomLevel.getView();
56+
const onUIEvent = comp.children.onUIEvent.getView();
57+
const onMapEvent = comp.children.onMapEvent.getView();
5358

5459
const echartsCompRef = useRef<ReactECharts | null>();
5560
const [chartSize, setChartSize] = useState<ChartSize>();
@@ -81,15 +86,15 @@ ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
8186
//log.log("chart select change", param);
8287
if (param.fromAction === "select") {
8388
comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option)));
84-
onEvent("select");
89+
onUIEvent("select");
8590
} else if (param.fromAction === "unselect") {
8691
comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option)));
87-
onEvent("unselect");
92+
onUIEvent("unselect");
8893
}
8994
});
9095
// unbind
9196
return () => echartsCompInstance?.off("selectchanged");
92-
}, [mode, onEvent]);
97+
}, [mode, onUIEvent]);
9398

9499
const echartsConfigChildren = _.omit(comp.children, echartsConfigOmitChildren);
95100
const option = useMemo(() => {
@@ -103,36 +108,50 @@ ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
103108
return mapScriptLoaded || window?.google;
104109
}, [mapScriptLoaded])
105110

106-
const loadGoogleMapsData = () => {
107-
setTimeout(() => {
108-
const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
109-
if (!echartsCompInstance) {
110-
return _.noop;
111-
}
111+
const loadGoogleMapData = () => {
112+
const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
113+
if (!echartsCompInstance) {
114+
return _.noop;
115+
}
112116

113-
let mapInstance = undefined;
114-
mapInstance = echartsCompInstance?.getModel()?.getComponent("gmap")?.getGoogleMap();
115-
comp.dispatch(changeChildAction("mapInstance", mapInstance));
116-
}, 500)
117+
comp.children.mapInstance.dispatch(changeValueAction(echartsCompInstance))
118+
onMapEvent('mapReady')
117119
}
118120

121+
const handleOnMapScriptLoad = () => {
122+
setMapScriptLoaded(true);
123+
loadGoogleMapData();
124+
}
125+
119126
useEffect(() => {
120127
if( mode !== 'map') {
121-
comp.dispatch(changeChildAction("mapInstance", undefined));
128+
comp.children.mapInstance.dispatch(changeValueAction(undefined))
122129
return;
123130
}
124131

125132
const gMapScript = loadGoogleMapsScript(apiKey);
126133
if(isMapScriptLoaded) {
127-
loadGoogleMapsData();
134+
handleOnMapScriptLoad();
128135
return;
129136
}
130-
gMapScript.addEventListener('load', function () {
131-
setMapScriptLoaded(true);
132-
loadGoogleMapsData();
133-
});
137+
gMapScript.addEventListener('load', handleOnMapScriptLoad);
138+
return () => {
139+
gMapScript.removeEventListener('load', handleOnMapScriptLoad);
140+
}
134141
}, [mode, apiKey, option])
135142

143+
useEffect(() => {
144+
if(mode === 'map') {
145+
onMapEvent('centerPositionChange')
146+
}
147+
}, [mode, mapCenterPosition.lat, mapCenterPosition.lng])
148+
149+
useEffect(() => {
150+
if(mode === 'map') {
151+
onMapEvent('zoomLevelChange')
152+
}
153+
}, [mode, mapZoomlevel])
154+
136155
return (
137156
<ReactResizeDetector
138157
onResize={(w, h) => {
@@ -287,8 +306,38 @@ ChartComp = withMethodExposing(ChartComp, [
287306
name: "getMapInstance",
288307
},
289308
execute: (comp) => {
290-
return comp.children.mapInstance.getView()
309+
return new Promise(resolve => {
310+
let intervalCount = 0;
311+
const mapInstanceInterval = setInterval(() => {
312+
const instance = comp.children.mapInstance.getView();
313+
const mapInstance = instance?.getModel()?.getComponent("gmap")?.getGoogleMap()
314+
if(mapInstance || intervalCount === 10) {
315+
clearInterval(mapInstanceInterval)
316+
resolve(mapInstance)
317+
}
318+
intervalCount++;
319+
}, 1000);
320+
})
321+
}
322+
},
323+
{
324+
method: {
325+
name: "getMapZoomLevel",
326+
},
327+
execute: (comp) => {
328+
return comp.children.mapZoomLevel.getView();
329+
}
330+
},
331+
{
332+
method: {
333+
name: "getMapCenterPosition",
291334
},
335+
execute: (comp) => {
336+
return {
337+
lng: comp.children.mapCenterLng.getView(),
338+
lat: comp.children.mapCenterLat.getView(),
339+
};
340+
}
292341
},
293342
])
294343

client/packages/lowcoder-comps/src/comps/chartComp/chartConstants.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const chartModeOptions = [
5050
},
5151
] as const;
5252

53-
export const EventOptions = [
53+
export const UIEventOptions = [
5454
{
5555
label: trans("chart.select"),
5656
value: "select",
@@ -64,6 +64,24 @@ export const EventOptions = [
6464
},
6565
] as const;
6666

67+
export const MapEventOptions = [
68+
{
69+
label: trans("chart.mapReady"),
70+
value: "mapReady",
71+
description: trans("chart.mapReadyDesc"),
72+
},
73+
{
74+
label: trans("chart.zoomLevelChange"),
75+
value: "zoomLevelChange",
76+
description: trans("chart.zoomLevelChangeDesc"),
77+
},
78+
{
79+
label: trans("chart.centerPositionChange"),
80+
value: "centerPositionChange",
81+
description: trans("chart.centerPositionChangeDesc"),
82+
},
83+
] as const;
84+
6785
export const XAxisDirectionOptions = [
6886
{
6987
label: trans("chart.horizontal"),
@@ -220,8 +238,8 @@ export const chartUiModeChildren = {
220238
xConfig: XAxisConfig,
221239
yConfig: YAxisConfig,
222240
legendConfig: LegendConfig,
223-
onEvent: eventHandlerControl(EventOptions),
224241
chartConfig: ChartOptionComp,
242+
onUIEvent: eventHandlerControl(UIEventOptions),
225243
};
226244

227245
const chartMapModeChildren = {
@@ -232,6 +250,7 @@ const chartMapModeChildren = {
232250
mapCenterLng: withDefault(NumberControl, 15.932644),
233251
mapCenterLat: withDefault(NumberControl, 50.942063),
234252
mapOptions: jsonControl(toObject, i18nObjs.defaultMapJsonOption),
253+
onMapEvent: eventHandlerControl(MapEventOptions),
235254
}
236255

237256
export const chartChildrenMap = {

client/packages/lowcoder-comps/src/comps/chartComp/chartPropertyView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function chartPropertyView(
105105
dataIndex={(s) => s.getView().dataIndex}
106106
/>
107107
</Section>
108-
<Section name={sectionNames.interaction}>{children.onEvent.getPropertyView()}</Section>
108+
<Section name={sectionNames.interaction}>{children.onUIEvent.getPropertyView()}</Section>
109109
<Section name={sectionNames.layout}>
110110
{children.title.propertyView({ label: trans("chart.title") })}
111111
{children.chartConfig.children.compType.getView() !== "pie" && (
@@ -186,6 +186,7 @@ export function chartPropertyView(
186186
),
187187
})}
188188
</Section>
189+
<Section name={sectionNames.interaction}>{children.onMapEvent.getPropertyView()}</Section>
189190
<Section name={sectionNames.layout}>{hiddenPropertyView(children)}</Section>
190191
</>
191192
);

client/packages/lowcoder-comps/src/i18n/comps/locales/en.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ export const en = {
7373
arrow: "Arrow",
7474
pointColorLabel: "Point color",
7575
pointColorTooltip: `Set the point color according to the series name and current point value, optional variables: seriesName, value. Example: '{{value < 25000 ? "red" : "green"}}'`,
76+
mapReady: "Map Ready",
77+
mapReadyDesc: "Triggers when map is ready",
78+
zoomLevelChange: "Zoom Level Change",
79+
zoomLevelChangeDesc: "Triggers when map zoom level changed",
80+
centerPositionChange: "Center Position Change",
81+
centerPositionChangeDesc: "Triggers when map center position changed"
7682
},
7783
imageEditor: {
7884
defaultSrc: "",

client/packages/lowcoder-comps/src/i18n/comps/locales/zh.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export const zh = {
7272
arrow: "箭头",
7373
pointColorLabel: "数据点颜色",
7474
pointColorTooltip: `根据系列名称和当前数据点值设置数据点颜色,可选变量:seriesName、value.示例:'{{value < 25000 ? "red" : "green"}}'`,
75+
mapReady: "地图就绪",
76+
mapReadyDesc: "地图准备好时触发",
77+
zoomLevelChange: "缩放级别更改",
78+
zoomLevelChangeDesc: "地图缩放级别更改时触发",
79+
centerPositionChange: "中心位置变化",
80+
centerPositionChangeDesc: "地图中心位置改变时触发"
7581
},
7682
imageEditor: {
7783
defaultSrc: "",

0 commit comments

Comments
 (0)