@trackunit/react-chart-components 0.0.6 → 0.0.7

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 (2) hide show
  1. package/index.js +160 -0
  2. package/package.json +5 -4
package/index.js ADDED
@@ -0,0 +1,160 @@
1
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
+ import { Spinner, Text } from '@trackunit/react-components';
3
+ import { tw, tws } from '@trackunit/tailwind-styled-components';
4
+ import EChart from 'echarts-for-react';
5
+ import * as React from 'react';
6
+
7
+ /**
8
+ * The Chart component is a wrapper component for ECharts.
9
+ *
10
+ * Handles events and click functionality.
11
+ *
12
+ * For more information see the [eCharts documentation](https://github.com/hustcc/echarts-for-react).
13
+ *
14
+ * @param {ChartProps} props - The props for the Chart component
15
+ * @returns {JSX.Element} Chart component
16
+ */
17
+ const Chart = ({ options, showLoading, className, style, onChartReady, onClick, onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart, merge = true, loadingOption, timezoneLabel, renderer = "canvas", dataTestId, }) => {
18
+ var _a, _b;
19
+ const echartsRef = React.useRef(null);
20
+ const eventHandlers = React.useMemo(() => {
21
+ const values = {};
22
+ if (onDataZoom !== undefined) {
23
+ values.dataZoom = onDataZoom;
24
+ }
25
+ if (onBrush !== undefined) {
26
+ values.brush = onBrush;
27
+ }
28
+ if (onBrushSelected !== undefined) {
29
+ values.brushSelected = onBrushSelected;
30
+ }
31
+ if (mouseLeaveChart !== undefined) {
32
+ values.globalout = mouseLeaveChart;
33
+ }
34
+ if (mouseMoveChart !== undefined) {
35
+ values.mouseMove = (event) => {
36
+ var _a;
37
+ mouseMoveChart(event, (_a = echartsRef === null || echartsRef === void 0 ? void 0 : echartsRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance());
38
+ };
39
+ }
40
+ return values;
41
+ }, [onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart]);
42
+ React.useEffect(() => {
43
+ var _a;
44
+ const instance = (_a = echartsRef === null || echartsRef === void 0 ? void 0 : echartsRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance();
45
+ if (instance && onClick) {
46
+ instance.on("click", onClick);
47
+ }
48
+ return () => {
49
+ if (instance && !instance.isDisposed()) {
50
+ instance.off("click", onClick);
51
+ }
52
+ };
53
+ }, [onClick, echartsRef]);
54
+ return (jsx(ChartContainer, Object.assign({ style: { height: (_a = style === null || style === void 0 ? void 0 : style.height) !== null && _a !== void 0 ? _a : "300px", width: (_b = style === null || style === void 0 ? void 0 : style.width) !== null && _b !== void 0 ? _b : "100%" }, "data-testid": dataTestId }, { children: showLoading ? (jsxs(SpinnerContainer, { children: [jsx(Spinner, { centering: "centered" }), (loadingOption === null || loadingOption === void 0 ? void 0 : loadingOption.text) && (jsx(Text, Object.assign({ align: "center", subtle: true }, { children: loadingOption.text })))] })) : (jsxs(Fragment, { children: [jsx(EChart, { ref: echartsRef, option: options, opts: { width: "auto", height: "auto", renderer: renderer }, notMerge: !merge, onEvents: eventHandlers, className: className, style: style, onChartReady: onChartReady }), timezoneLabel] })) })));
55
+ };
56
+ const SpinnerContainer = tw.div `
57
+ tu-chart-spinner-container
58
+ `;
59
+ const ChartContainer = tw.div `
60
+ tu-chart-container
61
+ `;
62
+
63
+ /**
64
+ * The LegendItem component is used to show form legends.
65
+ *
66
+ * @param {ILegendItem} props - The props for the LegendItem component
67
+ * @returns {JSX.Element} LegendItem component
68
+ */
69
+ const LegendItem = ({ className, label, disabled, selected, onClick, color, dataTestId }) => {
70
+ const handleOnClick = onClick && !disabled ? () => onClick() : undefined;
71
+ return (jsxs(Root, Object.assign({ "data-testid": dataTestId, "$disabled": disabled, className: className, "$selected": selected, onClick: handleOnClick }, { children: [jsx(Indicator, { style: { backgroundColor: color }, "$selected": selected }), label && jsx("p", { children: label })] })));
72
+ };
73
+ const Root = tw.div `
74
+ inline-flex
75
+ items-center
76
+ transition-all
77
+ ease-in-out
78
+ duration-200
79
+ text-neutral-500
80
+ hover:text-neutral-700
81
+ text-sm
82
+ gap-2
83
+ ${p => (p.$disabled ? tws `text-neutral-300` : p.$selected ? tws `text-black` : "")}
84
+ ${p => (p.$selected ? tws `font-medium` : null)}
85
+ ${p => (p.onClick ? tws `cursor-pointer` : tws `cursor-default`)}
86
+ `;
87
+ const Indicator = tw.div `
88
+ w-3
89
+ h-3
90
+ rounded-[50%]
91
+ ${p => (p.$selected ? tws `border border-black border-opacity-50` : null)}
92
+ `;
93
+
94
+ /**
95
+ * Create a PieChart with legends based on our current Chart component
96
+ *
97
+ * @param {PieChartProps} props - The props for the Chart component
98
+ * @returns {JSX.Element} Chart component
99
+ */
100
+ const PieChart = ({ data, size = "full", loading, onClick, className, dataTestId, }) => (jsxs(ChartRoot, Object.assign({ className: className, "data-testid": dataTestId }, { children: [jsx("div", { children: size === "full" && (jsx("div", Object.assign({ className: "flex flex-col", "data-testid": "legend" }, { children: data === null || data === void 0 ? void 0 : data.map(item => (jsx(LegendItem, { dataTestId: `legend-${item.id}`, label: (item === null || item === void 0 ? void 0 : item.count) !== undefined ? `${item.name} (${item.count})` : item.name, onClick: onClick ? () => onClick(item) : undefined, selected: item.selected, color: item.color }, item.id))) }))) }), jsx(Chart, { showLoading: loading, style: size === "full" ? { height: 110, width: 110 } : { height: 60, width: 60 }, onClick: evt => {
101
+ const clickedEntry = data === null || data === void 0 ? void 0 : data.find(x => x.id === evt.data.id);
102
+ if (onClick && clickedEntry) {
103
+ onClick(clickedEntry);
104
+ }
105
+ }, options: {
106
+ legend: {
107
+ show: false,
108
+ },
109
+ tooltip: {
110
+ show: false,
111
+ },
112
+ series: [
113
+ {
114
+ type: "pie",
115
+ radius: ["50%", "75%"],
116
+ center: ["50%", "50%"],
117
+ minAngle: 10,
118
+ selectedOffset: 8,
119
+ itemStyle: {
120
+ borderColor: "#ffffff",
121
+ borderWidth: 2,
122
+ },
123
+ avoidLabelOverlap: false,
124
+ label: {
125
+ show: false,
126
+ position: "center",
127
+ fontSize: "18",
128
+ fontWeight: "bold",
129
+ },
130
+ emphasis: {
131
+ label: {
132
+ show: true,
133
+ formatter: "{c}",
134
+ },
135
+ },
136
+ labelLine: {
137
+ show: false,
138
+ },
139
+ data: data === null || data === void 0 ? void 0 : data.map(item => {
140
+ var _a;
141
+ return ({
142
+ id: item.id,
143
+ name: item.name,
144
+ value: (_a = item === null || item === void 0 ? void 0 : item.count) !== null && _a !== void 0 ? _a : 0,
145
+ itemStyle: { color: item.color },
146
+ });
147
+ }),
148
+ },
149
+ ],
150
+ } })] })));
151
+ const ChartRoot = tw.div `
152
+ flex
153
+ flex-direction-col
154
+ flex-wrap-reverse
155
+ justify-between
156
+ items-center
157
+ h-full
158
+ `;
159
+
160
+ export { Chart, LegendItem, PieChart };
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@trackunit/react-chart-components",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
7
- "@trackunit/react-components": "0.1.65",
8
- "@trackunit/ui-design-tokens": "0.0.60",
7
+ "@trackunit/react-components": "0.1.66",
8
+ "@trackunit/ui-design-tokens": "0.0.61",
9
9
  "@trackunit/tailwind-styled-components": "0.0.54",
10
10
  "echarts": "5.4.0",
11
11
  "echarts-for-react": "3.0.2",
12
12
  "react": "18.2.0"
13
13
  },
14
+ "module": "./index.js",
14
15
  "main": "./index.cjs",
15
- "type": "commonjs",
16
+ "type": "module",
16
17
  "types": "./src/index.d.ts",
17
18
  "peerDependencies": {}
18
19
  }