@publishfx/publish-chart 1.3.2 → 1.4.1

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