-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathradarChartUtils.ts
397 lines (385 loc) · 12.2 KB
/
radarChartUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import {
CharOptionCompType,
ChartCompPropsType,
ChartSize,
noDataAxisConfig,
noDataPieChartConfig,
} from "comps/chartComp/chartConstants";
import { getPieRadiusAndCenter } from "comps/chartComp/chartConfigs/pieChartConfig";
import { EChartsOptionWithMap } from "../chartComp/reactEcharts/types";
import _ from "lodash";
import { chartColorPalette, isNumeric, JSONObject, loadScript } from "lowcoder-sdk";
import { calcXYConfig } from "comps/chartComp/chartConfigs/cartesianAxisConfig";
import Big from "big.js";
import { googleMapsApiUrl } from "../chartComp/chartConfigs/chartUrls";
import {chartStyleWrapper, styleWrapper} from "../../util/styleWrapper";
import parseBackground from "../../util/gradientBackgroundColor";
export function transformData(
originData: JSONObject[],
xAxis: string,
seriesColumnNames: string[]
) {
// aggregate data by x-axis
const transformedData: JSONObject[] = [];
originData.reduce((prev, cur) => {
if (cur === null || cur === undefined) {
return prev;
}
const groupValue = cur[xAxis] as string;
if (!prev[groupValue]) {
// init as 0
const initValue: any = {};
seriesColumnNames.forEach((name) => {
initValue[name] = 0;
});
prev[groupValue] = initValue;
transformedData.push(prev[groupValue]);
}
// remain the x-axis data
prev[groupValue][xAxis] = groupValue;
seriesColumnNames.forEach((key) => {
if (key === xAxis) {
return;
} else if (isNumeric(cur[key])) {
const bigNum = Big(cur[key]);
prev[groupValue][key] = bigNum.add(prev[groupValue][key]).toNumber();
} else {
prev[groupValue][key] += 1;
}
});
return prev;
}, {} as any);
return transformedData;
}
const notAxisChartSet: Set<CharOptionCompType> = new Set(["pie"] as const);
export const echartsConfigOmitChildren = [
"hidden",
"selectedPoints",
"onUIEvent",
"mapInstance"
] as const;
type EchartsConfigProps = Omit<ChartCompPropsType, typeof echartsConfigOmitChildren[number]>;
export function isAxisChart(type: CharOptionCompType) {
return !notAxisChartSet.has(type);
}
export function getSeriesConfig(props: EchartsConfigProps) {
const visibleSeries = props.series.filter((s) => !s.getView().hide);
const seriesLength = visibleSeries.length;
return visibleSeries.map((s, index) => {
if (isAxisChart(props.chartConfig.type)) {
let encodeX: string, encodeY: string;
const horizontalX = props.xAxisDirection === "horizontal";
let itemStyle = props.chartConfig.itemStyle;
// FIXME: need refactor... chartConfig returns a function with paramters
if (props.chartConfig.type === "bar") {
// barChart's border radius, depend on x-axis direction and stack state
const borderRadius = horizontalX ? [2, 2, 0, 0] : [0, 2, 2, 0];
if (props.chartConfig.stack && index === visibleSeries.length - 1) {
itemStyle = { ...itemStyle, borderRadius: borderRadius };
} else if (!props.chartConfig.stack) {
itemStyle = { ...itemStyle, borderRadius: borderRadius };
}
}
if (horizontalX) {
encodeX = props.xAxisKey;
encodeY = s.getView().columnName;
} else {
encodeX = s.getView().columnName;
encodeY = props.xAxisKey;
}
return {
name: s.getView().seriesName,
selectedMode: "single",
select: {
itemStyle: {
borderColor: "#000",
},
},
encode: {
x: encodeX,
y: encodeY,
},
// each type of chart's config
...props.chartConfig,
itemStyle: itemStyle,
label: {
...props.chartConfig.label,
...(!horizontalX && { position: "outside" }),
},
};
} else {
// pie
const radiusAndCenter = getPieRadiusAndCenter(seriesLength, index, props.chartConfig);
return {
...props.chartConfig,
radius: radiusAndCenter.radius,
center: radiusAndCenter.center,
name: s.getView().seriesName,
selectedMode: "single",
encode: {
itemName: props.xAxisKey,
value: s.getView().columnName,
},
};
}
});
}
// https://echarts.apache.org/en/option.html
export function getEchartsConfig(
props: EchartsConfigProps,
chartSize?: ChartSize,
theme?: any,
): EChartsOptionWithMap {
if (props.mode === "json") {
let opt={
title: {
text: props?.echartsTitle,
top: props?.echartsTitleVerticalConfig.top,
left: props?.echartsTitleConfig.top,
textStyle: {
...styleWrapper(props?.titleStyle, theme?.titleStyle)
}
},
legend: props.legendVisibility && {
top: props.echartsLegendConfig.top,
left: props.echartsLegendAlignConfig.left,
orient: props.echartsLegendOrientConfig.orient,
textStyle: {
...styleWrapper(props?.legendStyle, theme?.legendStyle, 15),
}
},
backgroundColor: parseBackground(
props?.chartStyle?.background || theme?.chartStyle?.backgroundColor || "#FFFFFF"
),
color: props.echartsData.data?.map(data => data.color) || props.echartsOption.data?.map(data => data.color),
tooltip: {
trigger: "axis",
formatter: function (params) {
let tooltipText = params[0].name + '<br/>';
params.forEach(function (item) {
tooltipText += item.seriesName + ': ' + item.value + '<br/>';
});
return tooltipText;
}
},
radar: [
{
indicator: (props.echartsIndicators.length !== 0 && props.echartsIndicators) || props.echartsOption.indicator,
center: [`${props?.position_x}%`, `${props?.position_y}%`],
startAngle: props?.startAngle,
endAngle: props?.endAngle,
splitNumber: props?.splitNumber,
radius: `${props.radius}%`,
shape: props?.areaFlag ? 'circle' : 'line',
axisName: {
...styleWrapper(props?.detailStyle, theme?.detailStyle, 13),
show: props?.indicatorVisibility,
},
splitArea: {
areaStyle: {
color: props?.echartsData?.color || props?.echartsOption?.color,
...chartStyleWrapper(props?.chartStyle, theme?.chartStyle),
}
},
}
],
series:
props?.echartsData.length !== 0 ?
{
data: props?.echartsData && [
...props?.echartsData.map(item => ({
...item,
areaStyle: item.areaColor && {
...chartStyleWrapper(props?.chartStyle, theme?.chartStyle),
color: item.areaColor
},
lineStyle: {
...chartStyleWrapper(props?.chartStyle, theme?.chartStyle),
color: item.lineColor,
width: item.lineWidth,
},
symbolSize: item.pointSize,
itemStyle: {
color: item.pointColor
}
}))
],
type: "radar"
}
:
props?.echartsOption && {
data: props?.echartsOption?.series && [
...props?.echartsOption?.series.map(item => ({
...item,
areaStyle: item.areaColor && {
...chartStyleWrapper(props?.chartStyle, theme?.chartStyle),
color: item.areaColor
},
lineStyle: {
...chartStyleWrapper(props?.chartStyle, theme?.chartStyle),
color: item.lineColor,
width: item.lineWidth,
},
symbolSize: item.pointSize,
itemStyle: {
color: item.pointColor
}
}))
],
type: "radar"
}
}
return props.echartsData || props.echartsOption ? opt : {};
}
if(props.mode === "map") {
const {
mapZoomLevel,
mapCenterLat,
mapCenterLng,
mapOptions,
showCharts,
} = props;
const echartsOption = mapOptions && showCharts ? mapOptions : {};
return {
gmap: {
center: [mapCenterLng, mapCenterLat],
zoom: mapZoomLevel,
renderOnMoving: true,
echartsLayerZIndex: showCharts ? 2019 : 0,
roam: true
},
...echartsOption,
}
}
// axisChart
const axisChart = isAxisChart(props.chartConfig.type);
const gridPos = {
left: 20,
right: props.legendConfig.left === "right" ? "10%" : 20,
top: 50,
bottom: 35,
};
let config: EChartsOptionWithMap = {
title: { text: props.title, left: "center" },
tooltip: {
confine: true,
trigger: axisChart ? "axis" : "item",
},
legend: props.legendConfig,
grid: {
...gridPos,
containLabel: true,
},
};
if (props.data.length <= 0) {
// no data
return {
...config,
...(axisChart ? noDataAxisConfig : noDataPieChartConfig),
};
}
const yAxisConfig = props.yConfig();
const seriesColumnNames = props.series
.filter((s) => !s.getView().hide)
.map((s) => s.getView().columnName);
// y-axis is category and time, data doesn't need to aggregate
const transformedData =
yAxisConfig.type === "category" || yAxisConfig.type === "time"
? props.data
: transformData(props.data, props.xAxisKey, seriesColumnNames);
config = {
...config,
dataset: [
{
source: transformedData,
sourceHeader: false,
},
],
series: getSeriesConfig(props),
};
if (axisChart) {
// pure chart's size except the margin around
let chartRealSize;
if (chartSize) {
const rightSize =
typeof gridPos.right === "number"
? gridPos.right
: (chartSize.w * parseFloat(gridPos.right)) / 100.0;
chartRealSize = {
// actually it's self-adaptive with the x-axis label on the left, not that accurate but work
w: chartSize.w - gridPos.left - rightSize,
// also self-adaptive on the bottom
h: chartSize.h - gridPos.top - gridPos.bottom,
right: rightSize,
};
}
const finalXyConfig = calcXYConfig(
props.xConfig,
yAxisConfig,
props.xAxisDirection,
transformedData.map((d) => d[props.xAxisKey]),
chartRealSize
);
config = {
...config,
// @ts-ignore
xAxis: finalXyConfig.xConfig,
// @ts-ignore
yAxis: finalXyConfig.yConfig,
};
}
// log.log("Echarts transformedData and config", transformedData, config);
return config;
}
export function getSelectedPoints(param: any, option: any) {
const series = option.series;
const dataSource = _.isArray(option.dataset) && option.dataset[0]?.source;
if (series && dataSource) {
return param.selected.flatMap((selectInfo: any) => {
const seriesInfo = series[selectInfo.seriesIndex];
if (!seriesInfo || !seriesInfo.encode) {
return [];
}
return selectInfo.dataIndex.map((index: any) => {
const commonResult = {
seriesName: seriesInfo.name,
};
if (seriesInfo.encode.itemName && seriesInfo.encode.value) {
return {
...commonResult,
itemName: dataSource[index][seriesInfo.encode.itemName],
value: dataSource[index][seriesInfo.encode.value],
};
} else {
return {
...commonResult,
x: dataSource[index][seriesInfo.encode.x],
y: dataSource[index][seriesInfo.encode.y],
};
}
});
});
}
return [];
}
export function loadGoogleMapsScript(apiKey: string) {
const mapsUrl = `${googleMapsApiUrl}?key=${apiKey}`;
const scripts = document.getElementsByTagName('script');
// is script already loaded
let scriptIndex = _.findIndex(scripts, (script) => script.src.endsWith(mapsUrl));
if(scriptIndex > -1) {
return scripts[scriptIndex];
}
// is script loaded with diff api_key, remove the script and load again
scriptIndex = _.findIndex(scripts, (script) => script.src.startsWith(googleMapsApiUrl));
if(scriptIndex > -1) {
scripts[scriptIndex].remove();
}
const script = document.createElement("script");
script.type = "text/javascript";
script.src = mapsUrl;
script.async = true;
script.defer = true;
window.document.body.appendChild(script);
return script;
}