lowcoder-comps 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/04ea1009.js +985 -0
  2. package/0a1968f4.js +832 -0
  3. package/256b619e.js +92 -0
  4. package/2768fdea.js +275 -0
  5. package/2ff2c7a6.js +6 -0
  6. package/3087113d.js +1032 -0
  7. package/31b1cdd2.js +236 -0
  8. package/39f71758.js +849 -0
  9. package/3bd738e9.js +212 -0
  10. package/450c6bd6.js +447 -0
  11. package/4d1cf087.js +2679 -0
  12. package/54511d22.js +451 -0
  13. package/59cbb8ee.js +2456 -0
  14. package/62c916e0.js +86 -0
  15. package/74645131.js +1246 -0
  16. package/75095038.js +159 -0
  17. package/7c62ef63.js +34 -0
  18. package/8219433d.js +18619 -0
  19. package/86a4a706.js +1118 -0
  20. package/88c87bf8.js +943 -0
  21. package/8d8e98a7.js +365 -0
  22. package/9200edf7.js +70 -0
  23. package/9f3281b9.js +117321 -0
  24. package/a6e7fb9e.js +326 -0
  25. package/acdad8f2.js +7 -0
  26. package/b38c288b.js +24 -0
  27. package/ba44e76c.js +46688 -0
  28. package/cbee3d44.js +607 -0
  29. package/cc0f1351.js +793 -0
  30. package/e614be0d.js +91 -0
  31. package/f40ee59a.js +940 -0
  32. package/f576ac90.js +798 -0
  33. package/f9637058.js +16 -0
  34. package/fb09e069.js +1602 -0
  35. package/fc5c5300.js +2103 -0
  36. package/fe636892.js +823 -0
  37. package/fea654db.js +2827 -0
  38. package/index.js +5 -0
  39. package/package.json +4 -3
  40. package/README.md +0 -27
  41. package/index.html +0 -26
  42. package/index.tsx +0 -19
  43. package/jest.config.js +0 -6
  44. package/src/__test__/allComp.test.tsx +0 -61
  45. package/src/app-env.d.ts +0 -3
  46. package/src/comps/calendarComp/calendarComp.tsx +0 -443
  47. package/src/comps/calendarComp/calendarConstants.tsx +0 -898
  48. package/src/comps/chartComp/chartComp.tsx +0 -356
  49. package/src/comps/chartComp/chartConfigs/barChartConfig.tsx +0 -51
  50. package/src/comps/chartComp/chartConfigs/cartesianAxisConfig.tsx +0 -307
  51. package/src/comps/chartComp/chartConfigs/chartUrls.tsx +0 -9
  52. package/src/comps/chartComp/chartConfigs/legendConfig.tsx +0 -55
  53. package/src/comps/chartComp/chartConfigs/lineChartConfig.tsx +0 -96
  54. package/src/comps/chartComp/chartConfigs/pieChartConfig.tsx +0 -83
  55. package/src/comps/chartComp/chartConfigs/scatterChartConfig.tsx +0 -62
  56. package/src/comps/chartComp/chartConstants.tsx +0 -288
  57. package/src/comps/chartComp/chartPropertyView.tsx +0 -218
  58. package/src/comps/chartComp/chartUtils.ts +0 -291
  59. package/src/comps/chartComp/reactEcharts/core.tsx +0 -194
  60. package/src/comps/chartComp/reactEcharts/index.ts +0 -21
  61. package/src/comps/chartComp/reactEcharts/types.ts +0 -76
  62. package/src/comps/chartComp/seriesComp.tsx +0 -119
  63. package/src/comps/imageEditorComp/imageEditorClass.tsx +0 -52
  64. package/src/comps/imageEditorComp/imageEditorConstants.tsx +0 -109
  65. package/src/comps/imageEditorComp/index.tsx +0 -184
  66. package/src/comps/mermaidComp/index.tsx +0 -44
  67. package/src/comps/mermaidComp/mermaid.tsx +0 -29
  68. package/src/global.ts +0 -1
  69. package/src/i18n/comps/index.tsx +0 -29
  70. package/src/i18n/comps/locales/en.ts +0 -150
  71. package/src/i18n/comps/locales/enObj.tsx +0 -198
  72. package/src/i18n/comps/locales/index.ts +0 -7
  73. package/src/i18n/comps/locales/types.tsx +0 -10
  74. package/src/i18n/comps/locales/zh.ts +0 -145
  75. package/src/i18n/comps/locales/zhObj.tsx +0 -4
  76. package/src/index.ts +0 -11
  77. package/tsconfig.json +0 -22
  78. package/vite.config.js +0 -10
@@ -1,218 +0,0 @@
1
- import { changeChildAction, CompAction } from "lowcoder-core";
2
- import { ChartCompChildrenType, ChartTypeOptions, getDataKeys } from "./chartConstants";
3
- import { newSeries } from "./seriesComp";
4
- import {
5
- CustomModal,
6
- Dropdown,
7
- hiddenPropertyView,
8
- Option,
9
- RedButton,
10
- Section,
11
- sectionNames,
12
- controlItem,
13
- } from "lowcoder-sdk";
14
- import { trans } from "i18n/comps";
15
- import { examplesUrl, mapExamplesUrl, mapOptionUrl, optionUrl } from "./chartConfigs/chartUrls";
16
-
17
- export function chartPropertyView(
18
- children: ChartCompChildrenType,
19
- dispatch: (action: CompAction) => void
20
- ) {
21
- const series = children.series.getView();
22
- const columnOptions = getDataKeys(children.data.getView()).map((key) => ({
23
- label: key,
24
- value: key,
25
- }));
26
-
27
- const uiModePropertyView = (
28
- <>
29
- <Section name={trans("chart.data")}>
30
- {children.data.propertyView({
31
- label: trans("chart.data"),
32
- })}
33
- <Dropdown
34
- value={children.chartConfig.children.compType.getView()}
35
- options={ChartTypeOptions}
36
- label={trans("chart.chartType")}
37
- onChange={(value) => {
38
- // keep the previous value
39
- if (children.chartConfig.children.comp.children.hasOwnProperty("showLabel")) {
40
- children.chartConfig.dispatchChangeValueAction({
41
- compType: value as any,
42
- comp: {
43
- showLabel: (
44
- children.chartConfig.children.comp.children as any
45
- ).showLabel.toJsonValue(),
46
- },
47
- });
48
- } else {
49
- children.chartConfig.dispatchChangeValueAction({
50
- compType: value,
51
- });
52
- }
53
- }}
54
- />
55
- <Dropdown
56
- value={children.xAxisKey.getView()}
57
- options={columnOptions}
58
- label={trans("chart.xAxis")}
59
- onChange={(value) => {
60
- dispatch(changeChildAction("xAxisKey", value));
61
- }}
62
- />
63
- <Option
64
- items={series}
65
- title={trans("chart.chartSeries")}
66
- itemTitle={(s) => s.getView().seriesName}
67
- popoverTitle={(s) => s.getView().columnName}
68
- content={(s, index) => (
69
- <>
70
- {s.getPropertyViewWithData(columnOptions)}
71
- {
72
- <RedButton
73
- onClick={() => {
74
- CustomModal.confirm({
75
- title: trans("chart.delete"),
76
- content: trans("chart.confirmDelete") + `${s.getView().seriesName}?`,
77
- onConfirm: () =>
78
- children.series.dispatch(children.series.deleteAction(index)),
79
- confirmBtnType: "delete",
80
- okText: trans("chart.delete"),
81
- });
82
- }}
83
- >
84
- {trans("chart.delete")}
85
- </RedButton>
86
- }
87
- </>
88
- )}
89
- onAdd={() => {
90
- if (columnOptions.length <= 0) {
91
- return;
92
- }
93
- children.series.dispatch(
94
- children.series.pushAction(
95
- newSeries(trans("chart.customSeries"), columnOptions[0].value)
96
- )
97
- );
98
- }}
99
- onMove={(fromIndex, toIndex) => {
100
- const action = children.series.arrayMoveAction(fromIndex, toIndex);
101
- children.series.dispatch(action);
102
- }}
103
- hide={(s) => s.getView().hide}
104
- onHide={(s, hide) => s.children.hide.dispatchChangeValueAction(hide)}
105
- dataIndex={(s) => s.getView().dataIndex}
106
- />
107
- </Section>
108
- <Section name={sectionNames.interaction}>{children.onUIEvent.getPropertyView()}</Section>
109
- <Section name={sectionNames.layout}>
110
- {children.title.propertyView({ label: trans("chart.title") })}
111
- {children.chartConfig.children.compType.getView() !== "pie" && (
112
- <>
113
- {children.xAxisDirection.propertyView({
114
- label: trans("chart.xAxisDirection"),
115
- radioButton: true,
116
- })}
117
- {children.xConfig.getPropertyView()}
118
- {children.yConfig.getPropertyView()}
119
- </>
120
- )}
121
- {children.legendConfig.getPropertyView()}
122
- {hiddenPropertyView(children)}
123
- </Section>
124
- <Section name={sectionNames.style}>{children.chartConfig.getPropertyView()}</Section>
125
- </>
126
- );
127
-
128
- const jsonModePropertyView = (
129
- <>
130
- <Section name={trans("chart.config")}>
131
- {children.echartsOption.propertyView({
132
- label: trans("chart.echartsOptionLabel"),
133
- styleName: "higher",
134
- tooltip: (
135
- <div>
136
- <a href={optionUrl} target="_blank" rel="noopener noreferrer">
137
- {trans("chart.echartsOptionTooltip")}
138
- </a>
139
- <br />
140
- <a href={examplesUrl} target="_blank" rel="noopener noreferrer">
141
- {trans("chart.echartsOptionExamples")}
142
- </a>
143
- </div>
144
- ),
145
- })}
146
- </Section>
147
- <Section name={sectionNames.layout}>{hiddenPropertyView(children)}</Section>
148
- </>
149
- );
150
-
151
- const mapModePropertyView = (
152
- <>
153
- <Section name={'Map Configuration'}>
154
- {children.mapApiKey.propertyView({
155
- label: "API Key"
156
- })}
157
- {children.mapZoomLevel.propertyView({
158
- label: "Zoom Level"
159
- })}
160
- {controlItem({}, (
161
- <b style={{marginTop: '8px'}}>
162
- {'Center Position'}
163
- </b>
164
- ))}
165
- {children.mapCenterLng.propertyView({
166
- label: "Longitude"
167
- })}
168
- {children.mapCenterLat.propertyView({
169
- label: "Latitude"
170
- })}
171
- {children.showCharts.propertyView({
172
- label: "Show Charts"
173
- })}
174
- </Section>
175
- <Section name={'Map Data'}>
176
- {children.mapOptions.propertyView({
177
- label: trans("chart.echartsOptionLabel"),
178
- styleName: "higher",
179
- tooltip: (
180
- <div>
181
- <a href={mapOptionUrl} target="_blank" rel="noopener noreferrer">
182
- {trans("chart.echartsMapOptionTooltip")}
183
- </a>
184
- <br />
185
- <a href={mapExamplesUrl} target="_blank" rel="noopener noreferrer">
186
- {trans("chart.echartsMapOptionExamples")}
187
- </a>
188
- </div>
189
- ),
190
- })}
191
- </Section>
192
- <Section name={sectionNames.interaction}>{children.onMapEvent.getPropertyView()}</Section>
193
- <Section name={sectionNames.layout}>{hiddenPropertyView(children)}</Section>
194
- </>
195
- );
196
-
197
- const getChatConfigByMode = (mode: string) => {
198
- switch(mode) {
199
- case "ui":
200
- return uiModePropertyView;
201
- case "json":
202
- return jsonModePropertyView;
203
- case "map":
204
- return mapModePropertyView;
205
- }
206
- }
207
- return (
208
- <>
209
- <Section name={trans("chart.mode")}>
210
- {children.mode.propertyView({
211
- label: "",
212
- radioButton: true,
213
- })}
214
- </Section>
215
- {getChatConfigByMode(children.mode.getView())}
216
- </>
217
- );
218
- }
@@ -1,291 +0,0 @@
1
- import {
2
- CharOptionCompType,
3
- ChartCompPropsType,
4
- ChartSize,
5
- noDataAxisConfig,
6
- noDataPieChartConfig,
7
- } from "comps/chartComp/chartConstants";
8
- import { getPieRadiusAndCenter } from "comps/chartComp/chartConfigs/pieChartConfig";
9
- import { EChartsOptionWithMap } from "./reactEcharts/types";
10
- import _ from "lodash";
11
- import { chartColorPalette, isNumeric, JSONObject, loadScript } from "lowcoder-sdk";
12
- import { calcXYConfig } from "comps/chartComp/chartConfigs/cartesianAxisConfig";
13
- import Big from "big.js";
14
- import { googleMapsApiUrl } from "./chartConfigs/chartUrls";
15
-
16
- export function transformData(
17
- originData: JSONObject[],
18
- xAxis: string,
19
- seriesColumnNames: string[]
20
- ) {
21
- // aggregate data by x-axis
22
- const transformedData: JSONObject[] = [];
23
- originData.reduce((prev, cur) => {
24
- if (cur === null || cur === undefined) {
25
- return prev;
26
- }
27
- const groupValue = cur[xAxis] as string;
28
- if (!prev[groupValue]) {
29
- // init as 0
30
- const initValue: any = {};
31
- seriesColumnNames.forEach((name) => {
32
- initValue[name] = 0;
33
- });
34
- prev[groupValue] = initValue;
35
- transformedData.push(prev[groupValue]);
36
- }
37
- // remain the x-axis data
38
- prev[groupValue][xAxis] = groupValue;
39
- seriesColumnNames.forEach((key) => {
40
- if (key === xAxis) {
41
- return;
42
- } else if (isNumeric(cur[key])) {
43
- const bigNum = Big(cur[key]);
44
- prev[groupValue][key] = bigNum.add(prev[groupValue][key]).toNumber();
45
- } else {
46
- prev[groupValue][key] += 1;
47
- }
48
- });
49
- return prev;
50
- }, {} as any);
51
- return transformedData;
52
- }
53
-
54
- const notAxisChartSet: Set<CharOptionCompType> = new Set(["pie"] as const);
55
- export const echartsConfigOmitChildren = [
56
- "hidden",
57
- "selectedPoints",
58
- "onUIEvent",
59
- "mapInstance"
60
- ] as const;
61
- type EchartsConfigProps = Omit<ChartCompPropsType, typeof echartsConfigOmitChildren[number]>;
62
-
63
- export function isAxisChart(type: CharOptionCompType) {
64
- return !notAxisChartSet.has(type);
65
- }
66
-
67
- export function getSeriesConfig(props: EchartsConfigProps) {
68
- const visibleSeries = props.series.filter((s) => !s.getView().hide);
69
- const seriesLength = visibleSeries.length;
70
- return visibleSeries.map((s, index) => {
71
- if (isAxisChart(props.chartConfig.type)) {
72
- let encodeX: string, encodeY: string;
73
- const horizontalX = props.xAxisDirection === "horizontal";
74
- let itemStyle = props.chartConfig.itemStyle;
75
- // FIXME: need refactor... chartConfig returns a function with paramters
76
- if (props.chartConfig.type === "bar") {
77
- // barChart's border radius, depend on x-axis direction and stack state
78
- const borderRadius = horizontalX ? [2, 2, 0, 0] : [0, 2, 2, 0];
79
- if (props.chartConfig.stack && index === visibleSeries.length - 1) {
80
- itemStyle = { ...itemStyle, borderRadius: borderRadius };
81
- } else if (!props.chartConfig.stack) {
82
- itemStyle = { ...itemStyle, borderRadius: borderRadius };
83
- }
84
- }
85
- if (horizontalX) {
86
- encodeX = props.xAxisKey;
87
- encodeY = s.getView().columnName;
88
- } else {
89
- encodeX = s.getView().columnName;
90
- encodeY = props.xAxisKey;
91
- }
92
- return {
93
- name: s.getView().seriesName,
94
- selectedMode: "single",
95
- select: {
96
- itemStyle: {
97
- borderColor: "#000",
98
- },
99
- },
100
- encode: {
101
- x: encodeX,
102
- y: encodeY,
103
- },
104
- // each type of chart's config
105
- ...props.chartConfig,
106
- itemStyle: itemStyle,
107
- label: {
108
- ...props.chartConfig.label,
109
- ...(!horizontalX && { position: "outside" }),
110
- },
111
- };
112
- } else {
113
- // pie
114
- const radiusAndCenter = getPieRadiusAndCenter(seriesLength, index, props.chartConfig);
115
- return {
116
- ...props.chartConfig,
117
- radius: radiusAndCenter.radius,
118
- center: radiusAndCenter.center,
119
- name: s.getView().seriesName,
120
- selectedMode: "single",
121
- encode: {
122
- itemName: props.xAxisKey,
123
- value: s.getView().columnName,
124
- },
125
- };
126
- }
127
- });
128
- }
129
-
130
- // https://echarts.apache.org/en/option.html
131
- export function getEchartsConfig(props: EchartsConfigProps, chartSize?: ChartSize): EChartsOptionWithMap {
132
- if (props.mode === "json") {
133
- return props.echartsOption ? props.echartsOption : {};
134
- }
135
- if(props.mode === "map") {
136
- const {
137
- mapZoomLevel,
138
- mapCenterLat,
139
- mapCenterLng,
140
- mapOptions,
141
- showCharts,
142
- } = props;
143
-
144
- const echartsOption = mapOptions && showCharts ? mapOptions : {};
145
- return {
146
- gmap: {
147
- center: [mapCenterLng, mapCenterLat],
148
- zoom: mapZoomLevel,
149
- renderOnMoving: true,
150
- echartsLayerZIndex: showCharts ? 2019 : 0,
151
- roam: true
152
- },
153
- ...echartsOption,
154
- }
155
- }
156
- // axisChart
157
- const axisChart = isAxisChart(props.chartConfig.type);
158
- const gridPos = {
159
- left: 20,
160
- right: props.legendConfig.left === "right" ? "10%" : 20,
161
- top: 50,
162
- bottom: 35,
163
- };
164
- let config: EChartsOptionWithMap = {
165
- title: { text: props.title, left: "center" },
166
- tooltip: {
167
- confine: true,
168
- trigger: axisChart ? "axis" : "item",
169
- },
170
- legend: props.legendConfig,
171
- grid: {
172
- ...gridPos,
173
- containLabel: true,
174
- },
175
- };
176
- if (props.data.length <= 0) {
177
- // no data
178
- return {
179
- ...config,
180
- ...(axisChart ? noDataAxisConfig : noDataPieChartConfig),
181
- };
182
- }
183
- const yAxisConfig = props.yConfig();
184
- const seriesColumnNames = props.series
185
- .filter((s) => !s.getView().hide)
186
- .map((s) => s.getView().columnName);
187
- // y-axis is category and time, data doesn't need to aggregate
188
- const transformedData =
189
- yAxisConfig.type === "category" || yAxisConfig.type === "time"
190
- ? props.data
191
- : transformData(props.data, props.xAxisKey, seriesColumnNames);
192
- config = {
193
- ...config,
194
- dataset: [
195
- {
196
- source: transformedData,
197
- sourceHeader: false,
198
- },
199
- ],
200
- series: getSeriesConfig(props),
201
- };
202
- if (axisChart) {
203
- // pure chart's size except the margin around
204
- let chartRealSize;
205
- if (chartSize) {
206
- const rightSize =
207
- typeof gridPos.right === "number"
208
- ? gridPos.right
209
- : (chartSize.w * parseFloat(gridPos.right)) / 100.0;
210
- chartRealSize = {
211
- // actually it's self-adaptive with the x-axis label on the left, not that accurate but work
212
- w: chartSize.w - gridPos.left - rightSize,
213
- // also self-adaptive on the bottom
214
- h: chartSize.h - gridPos.top - gridPos.bottom,
215
- right: rightSize,
216
- };
217
- }
218
- const finalXyConfig = calcXYConfig(
219
- props.xConfig,
220
- yAxisConfig,
221
- props.xAxisDirection,
222
- transformedData.map((d) => d[props.xAxisKey]),
223
- chartRealSize
224
- );
225
- config = {
226
- ...config,
227
- // @ts-ignore
228
- xAxis: finalXyConfig.xConfig,
229
- // @ts-ignore
230
- yAxis: finalXyConfig.yConfig,
231
- };
232
- }
233
- // log.log("Echarts transformedData and config", transformedData, config);
234
- return config;
235
- }
236
-
237
- export function getSelectedPoints(param: any, option: any) {
238
- const series = option.series;
239
- const dataSource = _.isArray(option.dataset) && option.dataset[0]?.source;
240
- if (series && dataSource) {
241
- return param.selected.flatMap((selectInfo: any) => {
242
- const seriesInfo = series[selectInfo.seriesIndex];
243
- if (!seriesInfo || !seriesInfo.encode) {
244
- return [];
245
- }
246
- return selectInfo.dataIndex.map((index: any) => {
247
- const commonResult = {
248
- seriesName: seriesInfo.name,
249
- };
250
- if (seriesInfo.encode.itemName && seriesInfo.encode.value) {
251
- return {
252
- ...commonResult,
253
- itemName: dataSource[index][seriesInfo.encode.itemName],
254
- value: dataSource[index][seriesInfo.encode.value],
255
- };
256
- } else {
257
- return {
258
- ...commonResult,
259
- x: dataSource[index][seriesInfo.encode.x],
260
- y: dataSource[index][seriesInfo.encode.y],
261
- };
262
- }
263
- });
264
- });
265
- }
266
- return [];
267
- }
268
-
269
- export function loadGoogleMapsScript(apiKey?: string) {
270
- const mapsUrl = `${googleMapsApiUrl}&key=${apiKey}`;
271
- const scripts = document.getElementsByTagName('script');
272
- // is script already loaded
273
- let scriptIndex = _.findIndex(scripts, (script) => script.src.endsWith(mapsUrl));
274
- if(scriptIndex > -1) {
275
- return scripts[scriptIndex];
276
- }
277
- // is script loaded with diff api_key, remove the script and load again
278
- scriptIndex = _.findIndex(scripts, (script) => script.src.startsWith(googleMapsApiUrl));
279
- if(scriptIndex > -1) {
280
- scripts[scriptIndex].remove();
281
- }
282
-
283
- const script = document.createElement("script");
284
- script.type = "text/javascript";
285
- script.src = mapsUrl;
286
- script.async = true;
287
- script.defer = true;
288
- window.document.body.appendChild(script);
289
-
290
- return script;
291
- }
@@ -1,194 +0,0 @@
1
- import type { ECharts } from "echarts";
2
- import { PureComponent } from "react";
3
- import isEqual from "fast-deep-equal";
4
- import { EChartsReactProps, EChartsInstance } from "./types";
5
- import _ from "lodash";
6
- import log from "loglevel";
7
-
8
- function isString(v: any): boolean {
9
- return typeof v === "string";
10
- }
11
-
12
- function isFunction(v: any): boolean {
13
- return typeof v === "function";
14
- }
15
-
16
- /**
17
- * core component for echarts binding
18
- */
19
- export default class EChartsReactCore extends PureComponent<EChartsReactProps> {
20
- /**
21
- * echarts render container
22
- */
23
- public ele: HTMLElement | null;
24
-
25
- /**
26
- * echarts library entry
27
- */
28
- protected echarts: any;
29
-
30
- constructor(props: EChartsReactProps) {
31
- super(props);
32
-
33
- this.echarts = props.echarts;
34
- this.ele = null;
35
- }
36
-
37
- componentDidMount() {
38
- this.renderNewEcharts();
39
- }
40
-
41
- // update
42
- componentDidUpdate(prevProps: EChartsReactProps) {
43
- /**
44
- * if shouldSetOption return false, then return, not update echarts options
45
- * default is true
46
- */
47
- const { shouldSetOption } = this.props;
48
- if (shouldSetOption && isFunction(shouldSetOption) && !shouldSetOption(prevProps, this.props)) {
49
- return;
50
- }
51
-
52
- /**
53
- * the props below need to dispose before re-render
54
- * 1. when switching theme
55
- * 2. when modifying opts
56
- * 3. when modifying onEvents, thus the binded event issue #151 can be cancel
57
- */
58
- if (
59
- !isEqual(prevProps.theme, this.props.theme) ||
60
- !isEqual(prevProps.opts, this.props.opts) ||
61
- !isEqual(prevProps.onEvents, this.props.onEvents) ||
62
- !isEqual(prevProps.mode, this.props.mode)
63
- // this.props.option.gmap
64
- ) {
65
- this.dispose();
66
-
67
- this.renderNewEcharts(); // re-render
68
- return;
69
- }
70
-
71
- if(this.props.mode === "json") {
72
- this.updateEChartsOption();
73
- return;
74
- }
75
-
76
- // when these props are not isEqual, update echarts
77
- const pickKeys = ["option", "notMerge", "lazyUpdate", "showLoading", "loadingOption"];
78
- if (!isEqual(_.pick(this.props, pickKeys), _.pick(prevProps, pickKeys))) {
79
- this.updateEChartsOption();
80
- }
81
- }
82
-
83
- componentWillUnmount() {
84
- this.dispose();
85
- }
86
-
87
- /**
88
- * return the echart object
89
- * 1. if exist, return the existed instance
90
- * 2. or new one instance
91
- */
92
- public getEchartsInstance(): ECharts {
93
- return (
94
- this.echarts.getInstanceByDom(this.ele) ||
95
- this.echarts.init(this.ele, this.props.theme, this.props.opts)
96
- );
97
- }
98
-
99
- /**
100
- * dispose echarts and clear size-sensor
101
- */
102
- private dispose() {
103
- if (this.ele) {
104
- // dispose echarts instance
105
- this.echarts.dispose(this.ele);
106
- }
107
- }
108
-
109
- /**
110
- * render a new echarts instance
111
- */
112
- private renderNewEcharts() {
113
- const { onEvents, onChartReady } = this.props;
114
-
115
- // 1. new echarts instance
116
- const echartsInstance = this.updateEChartsOption();
117
-
118
- // 2. bind events
119
- this.bindEvents(echartsInstance, onEvents || {});
120
-
121
- // 3. on chart ready
122
- if (onChartReady && isFunction(onChartReady)) onChartReady(echartsInstance);
123
- }
124
-
125
- // bind the events
126
- private bindEvents(instance: any, events: EChartsReactProps["onEvents"]) {
127
- function _bindEvent(eventName: string, func: Function) {
128
- // ignore the event config which not satisfy
129
- if (isString(eventName) && isFunction(func)) {
130
- // binding event
131
- instance.on(eventName, (param: any) => {
132
- func(param, instance);
133
- });
134
- }
135
- }
136
-
137
- // loop and bind
138
- for (const eventName in events) {
139
- if (Object.prototype.hasOwnProperty.call(events, eventName)) {
140
- _bindEvent(eventName, events[eventName]);
141
- }
142
- }
143
- }
144
-
145
- /**
146
- * render the echarts
147
- */
148
- private updateEChartsOption(): EChartsInstance {
149
- const {
150
- option,
151
- notMerge = false,
152
- lazyUpdate = false,
153
- showLoading,
154
- loadingOption = null,
155
- } = this.props;
156
- // 1. get or initial the echarts object
157
- const echartInstance = this.getEchartsInstance();
158
- // 2. set the echarts option
159
- try {
160
- // set option catch exception
161
- echartInstance.setOption(option, {
162
- notMerge: notMerge,
163
- lazyUpdate: lazyUpdate,
164
- silent: true,
165
- });
166
- } catch (e) {
167
- // FIXME: if don't dispose, setOption again will call cause bugs
168
- // https://github.com/apache/echarts/issues/16608
169
- this.dispose();
170
- log.warn("invalid echarts option:", e);
171
- }
172
- // 3. set loading mask
173
- if (showLoading) echartInstance.showLoading(loadingOption);
174
- else echartInstance.hideLoading();
175
-
176
- return echartInstance;
177
- }
178
-
179
- render(): JSX.Element {
180
- const { style, className = "" } = this.props;
181
- // default height = 300
182
- const newStyle = { height: 300, ...style };
183
-
184
- return (
185
- <div
186
- ref={(e: HTMLElement | null) => {
187
- this.ele = e;
188
- }}
189
- style={newStyle}
190
- className={`echarts-for-react ${className}`}
191
- />
192
- );
193
- }
194
- }