lowcoder-comps 0.0.19 → 0.0.20

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/01102044.js +940 -0
  2. package/011ccb4a.js +46688 -0
  3. package/0d5d7030.js +212 -0
  4. package/0e8dfd52.js +793 -0
  5. package/0f25771b.js +1118 -0
  6. package/2470e1d8.js +91 -0
  7. package/256b619e.js +92 -0
  8. package/2ff2c7a6.js +6 -0
  9. package/3196b84f.js +798 -0
  10. package/31c6e136.js +34 -0
  11. package/32432dc3.js +86 -0
  12. package/36552e14.js +849 -0
  13. package/384b70d4.js +24 -0
  14. package/3ad35722.js +365 -0
  15. package/3f4db197.js +943 -0
  16. package/4791bdd6.js +117321 -0
  17. package/5a7c40d7.js +18619 -0
  18. package/5fd0f607.js +447 -0
  19. package/71827e37.js +1032 -0
  20. package/7a200c6a.js +607 -0
  21. package/804e952b.js +451 -0
  22. package/872f2918.js +7 -0
  23. package/8d714103.js +1602 -0
  24. package/93b98f3e.js +832 -0
  25. package/a06ced05.js +326 -0
  26. package/a1f987b7.js +236 -0
  27. package/a42a2ba9.js +2103 -0
  28. package/af0489f3.js +2456 -0
  29. package/b56f953d.js +985 -0
  30. package/b89c6a6a.js +159 -0
  31. package/bc7030d3.js +70 -0
  32. package/c739ba3e.js +2679 -0
  33. package/d306d59c.js +2827 -0
  34. package/d8adcd1e.js +823 -0
  35. package/e1466a22.js +275 -0
  36. package/ef845d7f.js +1246 -0
  37. package/f9637058.js +16 -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,356 +0,0 @@
1
- import {
2
- changeChildAction,
3
- changeValueAction,
4
- CompAction,
5
- CompActionTypes,
6
- wrapChildAction,
7
- } from "lowcoder-core";
8
- import { AxisFormatterComp, EchartsAxisType } from "./chartConfigs/cartesianAxisConfig";
9
- import { chartChildrenMap, ChartSize, getDataKeys } from "./chartConstants";
10
- import { chartPropertyView } from "./chartPropertyView";
11
- import _ from "lodash";
12
- import { useContext, useEffect, useMemo, useRef, useState } from "react";
13
- import ReactResizeDetector from "react-resize-detector";
14
- import ReactECharts from "./reactEcharts";
15
- import {
16
- childrenToProps,
17
- depsConfig,
18
- genRandomKey,
19
- JSONObject,
20
- JSONValue,
21
- NameConfig,
22
- ToViewReturn,
23
- UICompBuilder,
24
- withDefault,
25
- withExposingConfigs,
26
- withMethodExposing,
27
- withViewFn,
28
- ThemeContext,
29
- chartColorPalette,
30
- } from "lowcoder-sdk";
31
- import { getEchartsLocale, trans } from "i18n/comps";
32
- import { ItemColorComp } from "comps/chartComp/chartConfigs/lineChartConfig";
33
- import {
34
- echartsConfigOmitChildren,
35
- getEchartsConfig,
36
- getSelectedPoints,
37
- loadGoogleMapsScript,
38
- } from "comps/chartComp/chartUtils";
39
- import 'echarts-extension-gmap';
40
- import log from "loglevel";
41
-
42
- let ChartTmpComp = (function () {
43
- return new UICompBuilder(chartChildrenMap, () => null)
44
- .setPropertyViewFn(chartPropertyView)
45
- .build();
46
- })();
47
-
48
- ChartTmpComp = withViewFn(ChartTmpComp, (comp) => {
49
- const apiKey = comp.children.mapApiKey.getView();
50
- const mode = comp.children.mode.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();
58
-
59
- const echartsCompRef = useRef<ReactECharts | null>();
60
- const [chartSize, setChartSize] = useState<ChartSize>();
61
- const [mapScriptLoaded, setMapScriptLoaded] = useState(false);
62
- const firstResize = useRef(true);
63
- const theme = useContext(ThemeContext);
64
- const defaultChartTheme = {
65
- color: chartColorPalette,
66
- backgroundColor: "#fff",
67
- };
68
-
69
- let themeConfig = defaultChartTheme;
70
- try {
71
- themeConfig = theme?.theme.chart ? JSON.parse(theme?.theme.chart) : defaultChartTheme;
72
- } catch (error) {
73
- log.error('theme chart error: ', error);
74
- }
75
-
76
- useEffect(() => {
77
- if(mode !== 'ui') return;
78
-
79
- // bind events
80
- const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
81
- if (!echartsCompInstance) {
82
- return _.noop;
83
- }
84
- echartsCompInstance?.on("selectchanged", (param: any) => {
85
- const option: any = echartsCompInstance?.getOption();
86
- //log.log("chart select change", param);
87
- if (param.fromAction === "select") {
88
- comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option)));
89
- onUIEvent("select");
90
- } else if (param.fromAction === "unselect") {
91
- comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option)));
92
- onUIEvent("unselect");
93
- }
94
- });
95
- // unbind
96
- return () => echartsCompInstance?.off("selectchanged");
97
- }, [mode, onUIEvent]);
98
-
99
- const echartsConfigChildren = _.omit(comp.children, echartsConfigOmitChildren);
100
- const option = useMemo(() => {
101
- return getEchartsConfig(
102
- childrenToProps(echartsConfigChildren) as ToViewReturn<typeof echartsConfigChildren>,
103
- chartSize
104
- );
105
- }, [chartSize, ...Object.values(echartsConfigChildren)]);
106
-
107
- const isMapScriptLoaded = useMemo(() => {
108
- return mapScriptLoaded || window?.google;
109
- }, [mapScriptLoaded])
110
-
111
- const loadGoogleMapData = () => {
112
- const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
113
- if (!echartsCompInstance) {
114
- return _.noop;
115
- }
116
-
117
- comp.children.mapInstance.dispatch(changeValueAction(echartsCompInstance))
118
- onMapEvent('mapReady')
119
- }
120
-
121
- const handleOnMapScriptLoad = () => {
122
- setMapScriptLoaded(true);
123
- loadGoogleMapData();
124
- }
125
-
126
- useEffect(() => {
127
- if( mode !== 'map') {
128
- comp.children.mapInstance.dispatch(changeValueAction(undefined))
129
- return;
130
- }
131
-
132
- const gMapScript = loadGoogleMapsScript(apiKey);
133
- if(isMapScriptLoaded) {
134
- handleOnMapScriptLoad();
135
- return;
136
- }
137
- gMapScript.addEventListener('load', handleOnMapScriptLoad);
138
- return () => {
139
- gMapScript.removeEventListener('load', handleOnMapScriptLoad);
140
- }
141
- }, [mode, apiKey, option])
142
-
143
- useEffect(() => {
144
- if(mode !== 'map') return;
145
- onMapEvent('centerPositionChange');
146
- }, [mode, mapCenterPosition.lat, mapCenterPosition.lng])
147
-
148
- useEffect(() => {
149
- if(mode !== 'map') return;
150
- onMapEvent('zoomLevelChange');
151
- }, [mode, mapZoomlevel])
152
-
153
- return (
154
- <ReactResizeDetector
155
- onResize={(w, h) => {
156
- if (w && h) {
157
- setChartSize({ w: w, h: h });
158
- }
159
- if (!firstResize.current) {
160
- // ignore the first resize, which will impact the loading animation
161
- echartsCompRef.current?.getEchartsInstance().resize();
162
- } else {
163
- firstResize.current = false;
164
- }
165
- }}
166
- >
167
- {(mode !== 'map' || (mode === 'map' && isMapScriptLoaded)) && (
168
- <ReactECharts
169
- ref={(e) => (echartsCompRef.current = e)}
170
- style={{ height: "100%" }}
171
- notMerge
172
- lazyUpdate
173
- opts={{ locale: getEchartsLocale() }}
174
- option={option}
175
- theme={mode !== 'map' ? themeConfig : undefined}
176
- mode={mode}
177
- />
178
- )}
179
- </ReactResizeDetector>
180
- );
181
- });
182
-
183
- function getYAxisFormatContextValue(
184
- data: Array<JSONObject>,
185
- yAxisType: EchartsAxisType,
186
- yAxisName?: string
187
- ) {
188
- const dataSample = yAxisName && data.length > 0 && data[0][yAxisName];
189
- let contextValue = dataSample;
190
- if (yAxisType === "time") {
191
- // to timestamp
192
- const time =
193
- typeof dataSample === "number" || typeof dataSample === "string"
194
- ? new Date(dataSample).getTime()
195
- : null;
196
- if (time) contextValue = time;
197
- }
198
- return contextValue;
199
- }
200
-
201
- ChartTmpComp = class extends ChartTmpComp {
202
- private lastYAxisFormatContextVal?: JSONValue;
203
- private lastColorContext?: JSONObject;
204
-
205
- updateContext(comp: this) {
206
- // the context value of axis format
207
- let resultComp = comp;
208
- const data = comp.children.data.getView();
209
- const sampleSeries = comp.children.series.getView().find((s) => !s.getView().hide);
210
- const yAxisContextValue = getYAxisFormatContextValue(
211
- data,
212
- comp.children.yConfig.children.yAxisType.getView(),
213
- sampleSeries?.children.columnName.getView()
214
- );
215
- if (yAxisContextValue !== comp.lastYAxisFormatContextVal) {
216
- comp.lastYAxisFormatContextVal = yAxisContextValue;
217
- resultComp = comp.setChild(
218
- "yConfig",
219
- comp.children.yConfig.reduce(
220
- wrapChildAction(
221
- "formatter",
222
- AxisFormatterComp.changeContextDataAction({ value: yAxisContextValue })
223
- )
224
- )
225
- );
226
- }
227
- // item color context
228
- const colorContextVal = {
229
- seriesName: sampleSeries?.children.seriesName.getView(),
230
- value: yAxisContextValue,
231
- };
232
- if (
233
- comp.children.chartConfig.children.comp.children.hasOwnProperty("itemColor") &&
234
- !_.isEqual(colorContextVal, comp.lastColorContext)
235
- ) {
236
- comp.lastColorContext = colorContextVal;
237
- resultComp = resultComp.setChild(
238
- "chartConfig",
239
- comp.children.chartConfig.reduce(
240
- wrapChildAction(
241
- "comp",
242
- wrapChildAction("itemColor", ItemColorComp.changeContextDataAction(colorContextVal))
243
- )
244
- )
245
- );
246
- }
247
- return resultComp;
248
- }
249
-
250
- override reduce(action: CompAction): this {
251
- const comp = super.reduce(action);
252
- if (action.type === CompActionTypes.UPDATE_NODES_V2) {
253
- const newData = comp.children.data.getView();
254
- // data changes
255
- if (comp.children.data !== this.children.data) {
256
- setTimeout(() => {
257
- // update x-axis value
258
- const keys = getDataKeys(newData);
259
- if (keys.length > 0 && !keys.includes(comp.children.xAxisKey.getView())) {
260
- comp.children.xAxisKey.dispatch(changeValueAction(keys[0] || ""));
261
- }
262
- // pass to child series comp
263
- comp.children.series.dispatchDataChanged(newData);
264
- }, 0);
265
- }
266
- return this.updateContext(comp);
267
- }
268
- return comp;
269
- }
270
-
271
- override autoHeight(): boolean {
272
- return false;
273
- }
274
- };
275
-
276
- let ChartComp = withExposingConfigs(ChartTmpComp, [
277
- depsConfig({
278
- name: "selectedPoints",
279
- desc: trans("chart.selectedPointsDesc"),
280
- depKeys: ["selectedPoints"],
281
- func: (input) => {
282
- return input.selectedPoints;
283
- },
284
- }),
285
- depsConfig({
286
- name: "data",
287
- desc: trans("chart.dataDesc"),
288
- depKeys: ["data", "mode"],
289
- func: (input) => {
290
- if (input.mode === "ui") {
291
- return input.data;
292
- } else {
293
- // no data in json mode
294
- return [];
295
- }
296
- },
297
- }),
298
- new NameConfig("title", trans("chart.titleDesc")),
299
- ]);
300
-
301
- ChartComp = withMethodExposing(ChartComp, [
302
- {
303
- method: {
304
- name: "getMapInstance",
305
- },
306
- execute: (comp) => {
307
- return new Promise(resolve => {
308
- let intervalCount = 0;
309
- const mapInstanceInterval = setInterval(() => {
310
- const instance = comp.children.mapInstance.getView();
311
- const mapInstance = instance?.getModel()?.getComponent("gmap")?.getGoogleMap()
312
- if(mapInstance || intervalCount === 10) {
313
- clearInterval(mapInstanceInterval)
314
- resolve(mapInstance)
315
- }
316
- intervalCount++;
317
- }, 1000);
318
- })
319
- }
320
- },
321
- {
322
- method: {
323
- name: "getMapZoomLevel",
324
- },
325
- execute: (comp) => {
326
- return comp.children.mapZoomLevel.getView();
327
- }
328
- },
329
- {
330
- method: {
331
- name: "getMapCenterPosition",
332
- },
333
- execute: (comp) => {
334
- return Promise.resolve({
335
- lng: comp.children.mapCenterLng.getView(),
336
- lat: comp.children.mapCenterLat.getView(),
337
- });
338
- }
339
- },
340
- ])
341
-
342
- export const ChartCompWithDefault = withDefault(ChartComp, {
343
- xAxisKey: "date",
344
- series: [
345
- {
346
- dataIndex: genRandomKey(),
347
- seriesName: trans("chart.spending"),
348
- columnName: "spending",
349
- },
350
- {
351
- dataIndex: genRandomKey(),
352
- seriesName: trans("chart.budget"),
353
- columnName: "budget",
354
- },
355
- ],
356
- });
@@ -1,51 +0,0 @@
1
- import {
2
- BoolControl,
3
- dropdownControl,
4
- MultiCompBuilder,
5
- showLabelPropertyView,
6
- } from "lowcoder-sdk";
7
- import { BarSeriesOption } from "echarts";
8
- import { trans } from "i18n/comps";
9
-
10
- const BarTypeOptions = [
11
- {
12
- label: trans("chart.basicBar"),
13
- value: "basicBar",
14
- },
15
- {
16
- label: trans("chart.stackedBar"),
17
- value: "stackedBar",
18
- },
19
- ] as const;
20
-
21
- export const BarChartConfig = (function () {
22
- return new MultiCompBuilder(
23
- {
24
- showLabel: BoolControl,
25
- type: dropdownControl(BarTypeOptions, "basicBar"),
26
- },
27
- (props): BarSeriesOption => {
28
- const config: BarSeriesOption = {
29
- type: "bar",
30
- label: {
31
- show: props.showLabel,
32
- position: "top",
33
- },
34
- };
35
- if (props.type === "stackedBar") {
36
- config.stack = "stackValue";
37
- }
38
- return config;
39
- }
40
- )
41
- .setPropertyViewFn((children) => (
42
- <>
43
- {showLabelPropertyView(children)}
44
- {children.type.propertyView({
45
- label: trans("chart.barType"),
46
- radioButton: true,
47
- })}
48
- </>
49
- ))
50
- .build();
51
- })();
@@ -1,307 +0,0 @@
1
- import { XAXisComponentOption, YAXisComponentOption } from "echarts";
2
- import { ChartSize, XAxisDirectionType } from "../chartConstants";
3
- import { i18n } from "lowcoder-core";
4
- import {
5
- MultiCompBuilder,
6
- withContext,
7
- NumberControl,
8
- StringControl,
9
- dropdownControl,
10
- JSONValue,
11
- isNumeric,
12
- } from "lowcoder-sdk";
13
- import { i18nObjs, trans } from "i18n/comps";
14
- import _, { isNil } from "lodash";
15
- import { xAxisTypeUrl } from "./chartUrls";
16
-
17
- const XAxisTypeOptions = [
18
- {
19
- label: trans("chart.auto"),
20
- value: "default",
21
- },
22
- {
23
- label: trans("chart.categoryAxis"),
24
- value: "category",
25
- },
26
- {
27
- label: trans("chart.valueAxis"),
28
- value: "value",
29
- },
30
- {
31
- label: trans("chart.timeAxis"),
32
- value: "time",
33
- },
34
- {
35
- label: trans("chart.logAxis"),
36
- value: "log",
37
- },
38
- ] as const;
39
-
40
- const YAxisTypeOptions = [
41
- {
42
- label: trans("chart.valueAxis"),
43
- value: "value",
44
- },
45
- {
46
- label: trans("chart.categoryAxis"),
47
- value: "category",
48
- },
49
- {
50
- label: trans("chart.timeAxis"),
51
- value: "time",
52
- },
53
- {
54
- label: trans("chart.logAxis"),
55
- value: "log",
56
- },
57
- ] as const;
58
-
59
- export type EchartsAxisType = "category" | "value" | "time" | "log";
60
-
61
- const axisCommonMap = {
62
- axisName: StringControl,
63
- logBase: NumberControl,
64
- };
65
-
66
- export const AxisFormatterComp = withContext(
67
- new MultiCompBuilder({ value: StringControl }, (props) => props.value)
68
- .setPropertyViewFn((children) =>
69
- children.value.propertyView({
70
- label: trans("chart.yAxisDataFormat"),
71
- placeholder: "{{value}}",
72
- tooltip: trans("chart.yAxisDataFormatTooltip"),
73
- })
74
- )
75
- .build(),
76
- ["value"] as const
77
- );
78
-
79
- export const XAxisConfig = (function () {
80
- return new MultiCompBuilder(
81
- {
82
- ...axisCommonMap,
83
- type: dropdownControl(XAxisTypeOptions, "default"),
84
- },
85
- (props): XAXisComponentOption => {
86
- const config: XAXisComponentOption = {
87
- name: props.axisName,
88
- nameGap: 22,
89
- // @ts-ignore
90
- nameLocation: "middle",
91
- };
92
- if (props.type !== "default") {
93
- // don't assign value for default value, compute it in the end
94
- config.type = props.type;
95
- }
96
- return config;
97
- }
98
- )
99
- .setPropertyViewFn((children) => (
100
- <>
101
- {children.axisName.propertyView({
102
- label: trans("chart.xAxisName"),
103
- })}
104
- {children.type.propertyView({
105
- label: trans("chart.xAxisType"),
106
- tooltip: (
107
- <>
108
- {trans("chart.xAxisTypeTooltip")}
109
- <a href={xAxisTypeUrl} target="_blank" rel="noreferrer">
110
- {trans("chart.xAxisType")}
111
- </a>
112
- </>
113
- ),
114
- })}
115
- {children.type.getView() === "log" &&
116
- children.logBase.propertyView({
117
- label: trans("chart.logBase"),
118
- })}
119
- </>
120
- ))
121
- .build();
122
- })();
123
-
124
- export const YAxisConfig = (function () {
125
- return new MultiCompBuilder(
126
- {
127
- ...axisCommonMap,
128
- // the old data has "type" field with default value "category". change field name to "yAxisType" for compatibility
129
- yAxisType: dropdownControl(YAxisTypeOptions, "value"),
130
- formatter: AxisFormatterComp,
131
- },
132
- (props) => () => {
133
- const config: YAXisComponentOption = {
134
- name: props.axisName,
135
- type: props.yAxisType,
136
- nameTextStyle: {
137
- align: "left",
138
- },
139
- };
140
- const numberFormat = new Intl.NumberFormat(i18n.locales, {
141
- notation: "compact",
142
- });
143
- (config.axisLabel as any) = {
144
- formatter: (value: string | number) => {
145
- const res = (props.formatter as any)({ value: value });
146
- if (!isNil(res) && res !== "") {
147
- return res;
148
- }
149
- if (
150
- (props.yAxisType === "value" || props.yAxisType === "log") &&
151
- typeof value === "number"
152
- ) {
153
- return numberFormat.format(value);
154
- }
155
- return value + "";
156
- },
157
- };
158
- if (props.yAxisType === "log") {
159
- (config as any).logBase = props.logBase || 10;
160
- }
161
- return config;
162
- }
163
- )
164
- .setPropertyViewFn((children) => (
165
- <>
166
- {children.axisName.propertyView({
167
- label: trans("chart.yAxisName"),
168
- })}
169
- {children.yAxisType.propertyView({
170
- label: trans("chart.yAxisType"),
171
- })}
172
- {children.yAxisType.getView() === "log" &&
173
- children.logBase.propertyView({
174
- label: trans("chart.logBase"),
175
- })}
176
- {children.formatter.getPropertyView()}
177
- </>
178
- ))
179
- .build();
180
- })();
181
-
182
- function calcXAxisType(xAxisData: Array<JSONValue | undefined>): EchartsAxisType {
183
- if (!xAxisData || xAxisData.length <= 0) {
184
- return "category";
185
- }
186
- const sampleData = xAxisData[0];
187
- if (!sampleData) {
188
- return "category";
189
- }
190
- if (isNumeric(sampleData)) {
191
- return "value";
192
- } else if (!isNaN(new Date(sampleData.toString()).getDate())) {
193
- return "time";
194
- } else {
195
- return "category";
196
- }
197
- }
198
-
199
- const dateInterval = {
200
- year: 3600 * 24 * 1000 * 365,
201
- month: 3600 * 24 * 1000 * 28,
202
- day: 3600 * 24 * 1000,
203
- };
204
-
205
- function calcTimeInterval(xAxisData: Array<JSONValue | undefined>) {
206
- const minIntervals = xAxisData.map((data) => {
207
- if (!data) {
208
- // 1 is echarts default value, to make sure axis tick is integer
209
- return 1;
210
- }
211
- const dataLen = data.toString().length;
212
- if (dataLen === 4) {
213
- // year 2022
214
- return dateInterval.year;
215
- } else if (dataLen === 6 || dataLen === 7) {
216
- // month 2022-01 222201
217
- return dateInterval.month;
218
- } else if (dataLen === 10 || dataLen === 8) {
219
- // day 2022-01-01 20220101
220
- return dateInterval.day;
221
- } else {
222
- return 1;
223
- }
224
- });
225
- return _.min(minIntervals);
226
- }
227
-
228
- let measureCanvas: HTMLCanvasElement;
229
-
230
- // calculate x-axis text width
231
- function getXAxisDataLength(xAxisData: Array<JSONValue | undefined>) {
232
- const canvas = measureCanvas || (measureCanvas = document.createElement("canvas"));
233
- const context = canvas.getContext("2d");
234
- if (!context) {
235
- return [];
236
- }
237
- // echarts default font
238
- context.font = "normal 12px sans-serif";
239
- return xAxisData.map((d) => (d ? context.measureText(d.toString()).width + 2 : 0));
240
- }
241
-
242
- export function calcXYConfig(
243
- xConfig: XAXisComponentOption,
244
- yConfig: YAXisComponentOption,
245
- xAxisDirection: XAxisDirectionType,
246
- xAxisData: Array<JSONValue | undefined>,
247
- chartSize?: ChartSize & { right: number }
248
- ) {
249
- const resXConfig = { ...xConfig };
250
- const resYConfig = { ...yConfig };
251
-
252
- if (!resXConfig.type) {
253
- // simple calculate x-axis type
254
- resXConfig.type = calcXAxisType(xAxisData);
255
- }
256
- // x-axis label style adaptive
257
- if (resXConfig.type === "category" && chartSize) {
258
- const xAxisDataLenList = getXAxisDataLength(xAxisData);
259
- // get x-axis single data's max width
260
- const maxDataWidth = _.max(xAxisDataLenList);
261
- const lastDataWidth = xAxisDataLenList[xAxisDataLenList.length - 1];
262
- // grid width
263
- let eachDataWidth = chartSize.w / xAxisData.length;
264
- let rotate = 0;
265
- let labelWidth = maxDataWidth;
266
- // rotate when width is not enough
267
- if (maxDataWidth && eachDataWidth < maxDataWidth && xAxisDirection === "horizontal") {
268
- labelWidth = Math.min(maxDataWidth, 150);
269
- // vertical rotate 0.87 => sin(60) when exceeding the right boundary
270
- const verticalRotate =
271
- lastDataWidth && lastDataWidth * 0.87 > eachDataWidth / 2 + chartSize.right;
272
- rotate = verticalRotate ? 270 : 330;
273
- // to keep x-axis name under label, nameGap is related to label rotation angle
274
- resXConfig.nameGap = verticalRotate ? labelWidth + 5 : labelWidth / 2 + 10;
275
- } else if (xAxisDirection === "vertical" && maxDataWidth) {
276
- // vertical direction
277
- resXConfig.nameGap = maxDataWidth + 10;
278
- }
279
- resXConfig.axisLabel = {
280
- interval: 0,
281
- width: labelWidth,
282
- // @ts-ignore
283
- overflow: "truncate",
284
- rotate: rotate,
285
- };
286
- } else if (resXConfig.type === "time") {
287
- (resXConfig as any).minInterval = calcTimeInterval(xAxisData);
288
- const timeXAxisLabel = i18nObjs.timeXAxisLabel;
289
- if (timeXAxisLabel) {
290
- resXConfig.axisLabel = timeXAxisLabel;
291
- }
292
- }
293
- if (xAxisDirection === "vertical") {
294
- resYConfig.nameLocation = "middle";
295
- resYConfig.nameGap = 25;
296
- }
297
-
298
- return xAxisDirection === "horizontal"
299
- ? {
300
- xConfig: resXConfig,
301
- yConfig: resYConfig,
302
- }
303
- : {
304
- xConfig: resYConfig,
305
- yConfig: resXConfig,
306
- };
307
- }