@publishfx/publish-chart 1.3.1 → 1.4.0

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.
@@ -0,0 +1,3 @@
1
+ export declare const newThemeColors: string[];
2
+ declare const _default: import("react").MemoExoticComponent<({ height, data, x, y, legend, indicatorMap, config, auxiliaryLineData, nodeSetting, highlightDate, }: any) => JSX.Element>;
3
+ export default _default;
@@ -0,0 +1,421 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { memo, useEffect, useMemo, useState } from "react";
3
+ import { Axis, Chart, Coordinate, Interval, Legend, Tooltip, View } from "bizcharts";
4
+ import lib from "@antv/data-set/lib";
5
+ import { formatIndicatorV2 } from "../../utils/formatters.js";
6
+ import { calTextWidth, calculateBarWidth, getAxisFormat, getAxisHorPaddingByText, truncateText } from "../../utils/chartHelpers.js";
7
+ import { defaultChartConfig, nodeMap } from "../../core/ChartConfig.js";
8
+ import AuxiliaryLine from "../shared/AuxiliaryLine.js";
9
+ import { NodeGeom } from "../shared/NodeGeom.js";
10
+ import XAxisBackground from "../shared/XAxisBackground.js";
11
+ const newThemeColors = [
12
+ '#5B8FF9',
13
+ '#5AD8A6',
14
+ '#5D7092',
15
+ '#F6BD16',
16
+ '#6F5EF9',
17
+ '#6DC8EC',
18
+ '#945FB9',
19
+ '#FF9845',
20
+ '#1E9493',
21
+ '#FF99C3',
22
+ '#8CD85A',
23
+ '#FE765E',
24
+ '#67A1FF',
25
+ '#D85ACB',
26
+ '#F95B8F'
27
+ ];
28
+ const GroupBar = ({ height, data, x = 'groupName', y = '', legend = 'groupType', indicatorMap, config, auxiliaryLineData, nodeSetting = {
29
+ showType: 0,
30
+ type: []
31
+ }, highlightDate })=>{
32
+ const yAxis = 'groupValue';
33
+ const { isDataTag = true, isLegend = true, isDescend = false, isHorizontal = false, isHighlight = true } = config || {};
34
+ const ds = new lib();
35
+ const dv = ds.createView().source(data);
36
+ const tdv = ds.createView().source(data);
37
+ const [chartInsWidth, setChartInsWidth] = useState(0);
38
+ const [axisHorPadding, setAxisHorPadding] = useState(0);
39
+ const [padding, setPadding] = useState([
40
+ 20,
41
+ 10,
42
+ 10,
43
+ 0
44
+ ]);
45
+ const [mainBarSize, setMainBarSize] = useState(void 0);
46
+ if ('' !== y) dv.transform({
47
+ type: 'map',
48
+ callback (row) {
49
+ row[yAxis] = '' !== row[yAxis] ? +row[yAxis] : '-';
50
+ return row;
51
+ }
52
+ });
53
+ tdv.transform({
54
+ type: 'map',
55
+ callback (row) {
56
+ row[yAxis] = '' !== row[yAxis] ? +row[yAxis] : '-';
57
+ return row;
58
+ }
59
+ });
60
+ tdv.transform({
61
+ type: 'filter',
62
+ callback (row) {
63
+ return !row.groupValue || '-' !== row.groupValue;
64
+ }
65
+ });
66
+ tdv.transform({
67
+ type: 'aggregate',
68
+ fields: [
69
+ yAxis
70
+ ],
71
+ groupBy: [
72
+ x
73
+ ],
74
+ operations: [
75
+ 'sum'
76
+ ],
77
+ as: [
78
+ 'total'
79
+ ]
80
+ });
81
+ tdv.transform({
82
+ type: 'sort',
83
+ callback (a, b) {
84
+ return b.total - a.total;
85
+ }
86
+ });
87
+ const sortData = tdv.rows.map((item)=>item[x]);
88
+ if ('' !== y) {
89
+ if (isDescend) dv.transform({
90
+ type: 'sort',
91
+ callback (a, b) {
92
+ if (-1 === sortData.indexOf(a[x])) return isHorizontal ? -1 : 1;
93
+ if (-1 === sortData.indexOf(b[x])) return isHorizontal ? 1 : -1;
94
+ return isHorizontal ? sortData.indexOf(b[x]) - sortData.indexOf(a[x]) : sortData.indexOf(a[x]) - sortData.indexOf(b[x]);
95
+ }
96
+ });
97
+ }
98
+ const transformData = dv.rows;
99
+ let maxY = Math.max(...dv.rows.filter((item)=>'-' !== item['total']).map((item)=>item['total']));
100
+ if (auxiliaryLineData?.length) {
101
+ const maxAuxiliaryLine = Math.max(...auxiliaryLineData.map((item)=>+item.value));
102
+ if (maxAuxiliaryLine > maxY) maxY = maxAuxiliaryLine;
103
+ }
104
+ const horPaddingRight = useMemo(()=>calTextWidth(formatIndicatorV2(maxY, indicatorMap[y]), 12) - 10, [
105
+ maxY
106
+ ]);
107
+ useEffect(()=>{
108
+ maxY > Math.max(...dv.rows.filter((item)=>'-' !== item['total']).map((item)=>item['total'])) ? setPadding((pre)=>[
109
+ isHorizontal ? 20 : 25,
110
+ isHorizontal ? isDataTag ? horPaddingRight : defaultChartConfig.layout.horBarPaddingRight || 50 : pre[1],
111
+ pre[2],
112
+ pre[3]
113
+ ]) : setPadding((pre)=>[
114
+ pre[0],
115
+ isHorizontal && isDataTag ? horPaddingRight : defaultChartConfig.layout.barPaddingRight || 10,
116
+ pre[2],
117
+ pre[3]
118
+ ]);
119
+ }, [
120
+ maxY,
121
+ data,
122
+ isHorizontal,
123
+ isDataTag,
124
+ horPaddingRight
125
+ ]);
126
+ const scale = {
127
+ [yAxis]: {
128
+ min: 0,
129
+ max: maxY,
130
+ type: 'linear-strict',
131
+ nice: true
132
+ }
133
+ };
134
+ const topLabel = {};
135
+ dv.rows.forEach((item)=>{
136
+ const infos = [
137
+ ...item?.nodeInfos?.info || [],
138
+ ...item?.nodeInfos?.infosCompare || []
139
+ ];
140
+ const len = infos.length || 0;
141
+ item.nodeLen = len;
142
+ if (0 === len) return;
143
+ item.node = 0;
144
+ item.color = 1 == len ? nodeMap.get(infos[0].type)?.color : 'l (0) 0:#32C5FF 0.44:#B620E0 0.99:#F7B500';
145
+ });
146
+ const [pointP, setPointP] = useState({
147
+ x: 0,
148
+ y: 0
149
+ });
150
+ const [pointData, setPointData] = useState({
151
+ info: [],
152
+ infosCompare: []
153
+ });
154
+ useEffect(()=>{
155
+ setPointP({
156
+ x: 0,
157
+ y: 0
158
+ });
159
+ setPointData({
160
+ info: [],
161
+ infosCompare: []
162
+ });
163
+ }, [
164
+ data
165
+ ]);
166
+ const filterFn = useMemo(()=>(item)=>highlightDate?.includes(item.groupName) ?? false, [
167
+ highlightDate
168
+ ]);
169
+ const nodeViewData = useMemo(()=>{
170
+ const uniqueMap = new Map();
171
+ transformData.forEach((item)=>{
172
+ const hasNode = (item?.nodeInfos?.info?.length || 0) > 0 || (item?.nodeInfos?.infosCompare?.length || 0) > 0;
173
+ if (hasNode && !uniqueMap.has(item.groupName)) uniqueMap.set(item.groupName, {
174
+ groupName: item.groupName,
175
+ node: 0,
176
+ nodeInfos: item.nodeInfos,
177
+ color: item.color,
178
+ [legend]: 'NodeEvent'
179
+ });
180
+ });
181
+ return Array.from(uniqueMap.values());
182
+ }, [
183
+ transformData
184
+ ]);
185
+ return /*#__PURE__*/ jsxs(Chart, {
186
+ height: height || 300,
187
+ data: transformData,
188
+ ...defaultChartConfig.baseChartConfig,
189
+ appendPadding: padding,
190
+ onGetG2Instance: (c)=>{
191
+ setChartInsWidth(c.width);
192
+ },
193
+ scale: scale,
194
+ onPointMouseover: (e)=>{
195
+ if (1 == nodeSetting.showType) {
196
+ setPointP({
197
+ x: e.data.x,
198
+ y: e.data.y
199
+ });
200
+ setPointData(e.data.data.nodeInfos);
201
+ }
202
+ },
203
+ onAfterrender: (chart)=>{
204
+ const estimatedBarWidth = calculateBarWidth(chart, transformData, x, 0.5);
205
+ if (void 0 !== estimatedBarWidth) setMainBarSize(estimatedBarWidth);
206
+ },
207
+ children: [
208
+ /*#__PURE__*/ jsx(XAxisBackground, {
209
+ x: x,
210
+ data: transformData,
211
+ isHighlight: isHighlight,
212
+ filterFn: filterFn,
213
+ dataLength: transformData.length,
214
+ mainBarSize: mainBarSize,
215
+ isHorizontal: isHorizontal
216
+ }),
217
+ /*#__PURE__*/ jsx(Coordinate, {
218
+ transpose: isHorizontal
219
+ }),
220
+ /*#__PURE__*/ jsx(Interval, {
221
+ adjust: [
222
+ {
223
+ type: 'stack',
224
+ reverseOrder: true
225
+ }
226
+ ],
227
+ color: [
228
+ legend,
229
+ newThemeColors
230
+ ],
231
+ position: `${x}*${yAxis}`,
232
+ style: {
233
+ cursor: 'pointer'
234
+ },
235
+ label: [
236
+ `${legend}*total*${x}`,
237
+ (_v, total, x)=>{
238
+ let renderFlag = false;
239
+ if (!topLabel[x]) {
240
+ renderFlag = true;
241
+ topLabel[x] = true;
242
+ }
243
+ return {
244
+ content: isDataTag && renderFlag ? formatIndicatorV2(total, indicatorMap[y]) : '',
245
+ position: isHorizontal ? 'right' : 'top',
246
+ offset: 5,
247
+ style: {
248
+ fontWeight: 'bold'
249
+ }
250
+ };
251
+ }
252
+ ]
253
+ }),
254
+ /*#__PURE__*/ jsx(Tooltip, {
255
+ shared: true,
256
+ showCrosshairs: true,
257
+ children: (title, items)=>{
258
+ items?.sort((a, b)=>{
259
+ if ('-' === a.value || '-' === b.value) return '-' === a.value ? 1 : -1;
260
+ return b.value - a.value;
261
+ });
262
+ return /*#__PURE__*/ jsxs("div", {
263
+ style: {
264
+ padding: '10px 0'
265
+ },
266
+ children: [
267
+ title,
268
+ items && /*#__PURE__*/ jsxs(Fragment, {
269
+ children: [
270
+ /*#__PURE__*/ jsx("div", {
271
+ children: items.map((it, index)=>/*#__PURE__*/ jsxs("div", {
272
+ style: {
273
+ display: 'flex',
274
+ justifyContent: 'space-between',
275
+ marginTop: '10px'
276
+ },
277
+ children: [
278
+ /*#__PURE__*/ jsxs("span", {
279
+ children: [
280
+ /*#__PURE__*/ jsx("span", {
281
+ style: {
282
+ width: '8px',
283
+ height: '8px',
284
+ backgroundColor: it.color,
285
+ display: 'inline-block',
286
+ borderRadius: '50%',
287
+ marginRight: '8px'
288
+ }
289
+ }),
290
+ it.name,
291
+ ":"
292
+ ]
293
+ }),
294
+ /*#__PURE__*/ jsxs("span", {
295
+ style: {
296
+ fontWeight: 'bold',
297
+ paddingLeft: '10px'
298
+ },
299
+ children: [
300
+ formatIndicatorV2(it.value, indicatorMap[y]),
301
+ "(",
302
+ (100 * Number(it.data.percent)).toFixed(2),
303
+ "%)"
304
+ ]
305
+ })
306
+ ]
307
+ }, index))
308
+ }),
309
+ auxiliaryLineData && auxiliaryLineData.map((item, index)=>/*#__PURE__*/ jsxs("div", {
310
+ style: {
311
+ marginLeft: '16px',
312
+ marginTop: '10px',
313
+ display: 'flex',
314
+ justifyContent: 'space-between'
315
+ },
316
+ children: [
317
+ /*#__PURE__*/ jsxs("div", {
318
+ children: [
319
+ item.name,
320
+ ":"
321
+ ]
322
+ }),
323
+ /*#__PURE__*/ jsx("div", {
324
+ children: formatIndicatorV2(item.value, indicatorMap[y])
325
+ })
326
+ ]
327
+ }, index))
328
+ ]
329
+ })
330
+ ]
331
+ });
332
+ }
333
+ }),
334
+ auxiliaryLineData?.length > 0 && /*#__PURE__*/ jsxs(View, {
335
+ data: transformData,
336
+ scale: scale,
337
+ padding: [
338
+ 0,
339
+ 0,
340
+ 0,
341
+ isHorizontal ? 0 : -axisHorPadding
342
+ ],
343
+ children: [
344
+ /*#__PURE__*/ jsx(Axis, {
345
+ name: yAxis,
346
+ visible: false
347
+ }),
348
+ /*#__PURE__*/ jsx(Axis, {
349
+ name: x,
350
+ visible: false
351
+ }),
352
+ /*#__PURE__*/ jsx(Interval, {
353
+ position: `${x}*${yAxis}`,
354
+ visible: false
355
+ }),
356
+ auxiliaryLineData.map((item, index)=>/*#__PURE__*/ jsx(AuxiliaryLine, {
357
+ name: item.name,
358
+ value: item.value,
359
+ isHorizontal: isHorizontal
360
+ }, index))
361
+ ]
362
+ }, `${axisHorPadding}${isHorizontal}`),
363
+ /*#__PURE__*/ jsx(Legend, {
364
+ position: "bottom-left",
365
+ maxItemWidth: 1,
366
+ visible: isLegend,
367
+ itemHeight: 14,
368
+ name: legend
369
+ }),
370
+ /*#__PURE__*/ jsx(Legend, {
371
+ name: "color",
372
+ visible: false
373
+ }),
374
+ /*#__PURE__*/ jsx(Axis, {
375
+ name: yAxis,
376
+ label: {
377
+ formatter (val) {
378
+ setAxisHorPadding(getAxisHorPaddingByText(val, indicatorMap, y) - 25);
379
+ return getAxisFormat(val, indicatorMap, y);
380
+ }
381
+ }
382
+ }),
383
+ /*#__PURE__*/ jsx(Axis, {
384
+ name: x,
385
+ label: {
386
+ autoEllipsis: true,
387
+ formatter: (text)=>isHorizontal ? truncateText(text, chartInsWidth / 5) : text
388
+ }
389
+ }),
390
+ nodeViewData.length > 0 && 1 == nodeSetting.showType && /*#__PURE__*/ jsxs(View, {
391
+ data: nodeViewData,
392
+ scale: {
393
+ node: {
394
+ min: 0,
395
+ max: 1
396
+ },
397
+ groupName: {
398
+ type: 'cat'
399
+ }
400
+ },
401
+ children: [
402
+ /*#__PURE__*/ jsx(Axis, {
403
+ name: "node",
404
+ visible: false
405
+ }),
406
+ /*#__PURE__*/ jsx(Legend, {
407
+ name: "color",
408
+ visible: false
409
+ }),
410
+ /*#__PURE__*/ jsx(NodeGeom, {
411
+ pointData: pointData,
412
+ pointP: pointP,
413
+ isLegend: isLegend
414
+ })
415
+ ]
416
+ })
417
+ ]
418
+ }, nodeSetting?.showType + JSON.stringify(config));
419
+ };
420
+ const composite_GroupBar = /*#__PURE__*/ memo(GroupBar);
421
+ export { composite_GroupBar as default, newThemeColors };
@@ -0,0 +1,2 @@
1
+ declare const _default: import("react").MemoExoticComponent<({ height, data, x, y, legend, indicatorMap, config, auxiliaryLineData, nodeSetting, highlightDate, }: any) => JSX.Element>;
2
+ export default _default;