@trackunit/react-chart-components 0.0.2

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.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ > **⚠️ Beta**
2
+ >
3
+ > This is a beta version and subject to change without notice.
4
+
5
+ # Trackunit React Chart Components
6
+ The `@trackunit/react-chart-components` package is the home of Trackunits public UI library.
7
+
8
+ This library is exposed publicly for use in the the Trackunit [Iris App SDK](https://www.npmjs.com/package/@trackunit/iris-app).
9
+
10
+ To browse all avaliable components visit our [Public Storybook](https://developers.trackunit.com/page/ui-components).
11
+
12
+ For more info and a full guide on Iris App SDK Development, please visit our [Developer Hub](https://developers.trackunit.com/).
13
+
14
+ ## Development
15
+ At this point this library is only developed by Trackunit Employees.
16
+ For development related information see the [development readme](https://github.com/Trackunit/manager/blob/master/libs/react/components/DEVELOPMENT.md).
17
+
18
+ ## Trackunit
19
+ This package was developed by Trackunit ApS.
20
+ Trackunit is the leading SaaS-based IoT solution for the construction industry, offering an ecosystem of hardware, fleet management software & telematics.
21
+
22
+ ![The Trackunit logo](https://trackunit.com/wp-content/uploads/2022/03/top-logo.svg)
package/index.cjs ADDED
@@ -0,0 +1,189 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var reactComponents = require('@trackunit/react-components');
7
+ var tailwindStyledComponents = require('@trackunit/tailwind-styled-components');
8
+ var EChart = require('echarts-for-react');
9
+ var React = require('react');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ function _interopNamespace(e) {
14
+ if (e && e.__esModule) return e;
15
+ var n = Object.create(null);
16
+ if (e) {
17
+ Object.keys(e).forEach(function (k) {
18
+ if (k !== 'default') {
19
+ var d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: function () { return e[k]; }
23
+ });
24
+ }
25
+ });
26
+ }
27
+ n["default"] = e;
28
+ return Object.freeze(n);
29
+ }
30
+
31
+ var EChart__default = /*#__PURE__*/_interopDefaultLegacy(EChart);
32
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
33
+
34
+ /**
35
+ * The Chart component is a wrapper component for ECharts.
36
+ *
37
+ * Handles events and click functionality.
38
+ *
39
+ * For more information see the [eCharts documentation](https://github.com/hustcc/echarts-for-react).
40
+ *
41
+ * @param {ChartProps} props - The props for the Chart component
42
+ * @returns {JSX.Element} Chart component
43
+ */
44
+ const Chart = ({ options, showLoading, className, style, onChartReady, onClick, onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart, merge = true, loadingOption, timezoneLabel, renderer = "canvas", dataTestId, }) => {
45
+ var _a, _b;
46
+ const echartsRef = React__namespace.useRef(null);
47
+ const eventHandlers = React__namespace.useMemo(() => {
48
+ const values = {};
49
+ if (onDataZoom !== undefined) {
50
+ values.dataZoom = onDataZoom;
51
+ }
52
+ if (onBrush !== undefined) {
53
+ values.brush = onBrush;
54
+ }
55
+ if (onBrushSelected !== undefined) {
56
+ values.brushSelected = onBrushSelected;
57
+ }
58
+ if (mouseLeaveChart !== undefined) {
59
+ values.globalout = mouseLeaveChart;
60
+ }
61
+ if (mouseMoveChart !== undefined) {
62
+ values.mouseMove = (event) => {
63
+ var _a;
64
+ mouseMoveChart(event, (_a = echartsRef === null || echartsRef === void 0 ? void 0 : echartsRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance());
65
+ };
66
+ }
67
+ return values;
68
+ }, [onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart]);
69
+ React__namespace.useEffect(() => {
70
+ var _a;
71
+ const instance = (_a = echartsRef === null || echartsRef === void 0 ? void 0 : echartsRef.current) === null || _a === void 0 ? void 0 : _a.getEchartsInstance();
72
+ if (instance && onClick) {
73
+ instance.on("click", onClick);
74
+ }
75
+ return () => {
76
+ if (instance && !instance.isDisposed()) {
77
+ instance.off("click", onClick);
78
+ }
79
+ };
80
+ }, [onClick, echartsRef]);
81
+ return (jsxRuntime.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 ? (jsxRuntime.jsxs(SpinnerContainer, { children: [jsxRuntime.jsx(reactComponents.Spinner, { centering: "centered" }), (loadingOption === null || loadingOption === void 0 ? void 0 : loadingOption.text) && (jsxRuntime.jsx(reactComponents.Text, Object.assign({ align: "center", subtle: true }, { children: loadingOption.text })))] })) : (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(EChart__default["default"], { ref: echartsRef, option: options, opts: { width: "auto", height: "auto", renderer: renderer }, notMerge: !merge, onEvents: eventHandlers, className: className, style: style, onChartReady: onChartReady }), timezoneLabel] })) })));
82
+ };
83
+ const SpinnerContainer = tailwindStyledComponents.tw.div `
84
+ tu-chart-spinner-container
85
+ `;
86
+ const ChartContainer = tailwindStyledComponents.tw.div `
87
+ tu-chart-container
88
+ `;
89
+
90
+ /**
91
+ * The LegendItem component is used to show form legends.
92
+ *
93
+ * @param {ILegendItem} props - The props for the LegendItem component
94
+ * @returns {JSX.Element} LegendItem component
95
+ */
96
+ const LegendItem = ({ className, label, disabled, selected, onClick, color, dataTestId }) => {
97
+ const handleOnClick = onClick && !disabled ? () => onClick() : undefined;
98
+ return (jsxRuntime.jsxs(Root, Object.assign({ "data-testid": dataTestId, "$disabled": disabled, className: className, "$selected": selected, onClick: handleOnClick }, { children: [jsxRuntime.jsx(Indicator, { style: { backgroundColor: color }, "$selected": selected }), label && jsxRuntime.jsx("p", { children: label })] })));
99
+ };
100
+ const Root = tailwindStyledComponents.tw.div `
101
+ inline-flex
102
+ items-center
103
+ transition-all
104
+ ease-in-out
105
+ duration-200
106
+ text-neutral-500
107
+ hover:text-neutral-700
108
+ text-sm
109
+ gap-2
110
+ ${p => (p.$disabled ? tailwindStyledComponents.tws `text-neutral-300` : p.$selected ? tailwindStyledComponents.tws `text-black` : "")}
111
+ ${p => (p.$selected ? tailwindStyledComponents.tws `font-medium` : null)}
112
+ ${p => (p.onClick ? tailwindStyledComponents.tws `cursor-pointer` : tailwindStyledComponents.tws `cursor-default`)}
113
+ `;
114
+ const Indicator = tailwindStyledComponents.tw.div `
115
+ w-3
116
+ h-3
117
+ rounded-[50%]
118
+ ${p => (p.$selected ? tailwindStyledComponents.tws `border border-black border-opacity-50` : null)}
119
+ `;
120
+
121
+ /**
122
+ * Create a PieChart with legends based on our current Chart component
123
+ *
124
+ * @param {PieChartProps} props - The props for the Chart component
125
+ * @returns {JSX.Element} Chart component
126
+ */
127
+ const PieChart = ({ data, size = "full", loading, onClick, className, dataTestId, }) => (jsxRuntime.jsxs(ChartRoot, Object.assign({ className: className, "data-testid": dataTestId }, { children: [jsxRuntime.jsx("div", { children: size === "full" && (jsxRuntime.jsx("div", Object.assign({ className: "flex flex-col", "data-testid": "legend" }, { children: data === null || data === void 0 ? void 0 : data.map(item => (jsxRuntime.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))) }))) }), jsxRuntime.jsx(Chart, { showLoading: loading, style: size === "full" ? { height: 110, width: 110 } : { height: 60, width: 60 }, onClick: evt => {
128
+ const clickedEntry = data === null || data === void 0 ? void 0 : data.find(x => x.id === evt.data.id);
129
+ if (onClick && clickedEntry) {
130
+ onClick(clickedEntry);
131
+ }
132
+ }, options: {
133
+ legend: {
134
+ show: false,
135
+ },
136
+ tooltip: {
137
+ show: false,
138
+ },
139
+ series: [
140
+ {
141
+ type: "pie",
142
+ radius: ["50%", "75%"],
143
+ center: ["50%", "50%"],
144
+ minAngle: 10,
145
+ selectedOffset: 8,
146
+ itemStyle: {
147
+ borderColor: "#ffffff",
148
+ borderWidth: 2,
149
+ },
150
+ avoidLabelOverlap: false,
151
+ label: {
152
+ show: false,
153
+ position: "center",
154
+ fontSize: "18",
155
+ fontWeight: "bold",
156
+ },
157
+ emphasis: {
158
+ label: {
159
+ show: true,
160
+ formatter: "{c}",
161
+ },
162
+ },
163
+ labelLine: {
164
+ show: false,
165
+ },
166
+ data: data === null || data === void 0 ? void 0 : data.map(item => {
167
+ var _a;
168
+ return ({
169
+ id: item.id,
170
+ name: item.name,
171
+ value: (_a = item === null || item === void 0 ? void 0 : item.count) !== null && _a !== void 0 ? _a : 0,
172
+ itemStyle: { color: item.color },
173
+ });
174
+ }),
175
+ },
176
+ ],
177
+ } })] })));
178
+ const ChartRoot = tailwindStyledComponents.tw.div `
179
+ flex
180
+ flex-direction-col
181
+ flex-wrap-reverse
182
+ justify-between
183
+ items-center
184
+ h-full
185
+ `;
186
+
187
+ exports.Chart = Chart;
188
+ exports.LegendItem = LegendItem;
189
+ exports.PieChart = PieChart;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@trackunit/react-chart-components",
3
+ "version": "0.0.2",
4
+ "repository": "https://github.com/Trackunit/manager",
5
+ "license": "SEE LICENSE IN LICENSE.txt",
6
+ "dependencies": {
7
+ "@trackunit/react-components": "0.1.61",
8
+ "@trackunit/ui-design-tokens": "0.0.60",
9
+ "@trackunit/tailwind-styled-components": "0.0.54",
10
+ "echarts": "5.4.0",
11
+ "echarts-for-react": "3.0.2",
12
+ "react": "18.2.0"
13
+ },
14
+ "main": "./index.cjs",
15
+ "type": "commonjs",
16
+ "types": "./src/index.d.ts",
17
+ "peerDependencies": {}
18
+ }
@@ -0,0 +1,81 @@
1
+ import { CommonProps } from "@trackunit/react-components";
2
+ import { EChartsType } from "echarts";
3
+ import * as React from "react";
4
+ export interface IOptionsType {
5
+ [key: string]: any;
6
+ }
7
+ export interface ChartProps extends CommonProps {
8
+ /**
9
+ * Option values for EChart.
10
+ */
11
+ options: IOptionsType;
12
+ /**
13
+ * Whether or not to low the preloader.
14
+ */
15
+ showLoading?: boolean;
16
+ /**
17
+ * onClick event.
18
+ */
19
+ onClick?: (event: any) => void;
20
+ /**
21
+ * onDataZoom event.
22
+ */
23
+ onDataZoom?: (event: any) => void;
24
+ /**
25
+ * onBrush event.
26
+ */
27
+ onBrush?: (event: any) => void;
28
+ /**
29
+ * Event fired when brush is selected.
30
+ */
31
+ onBrushSelected?: (event: any) => void;
32
+ /**
33
+ * Event fired when mouse leaves the chart.
34
+ */
35
+ mouseLeaveChart?: () => void;
36
+ /**
37
+ * Event fired when mouse is moving over the chart.
38
+ */
39
+ mouseMoveChart?: (event: any, chart: EChartsType | undefined) => void;
40
+ /**
41
+ * CSS styles for the chart.
42
+ */
43
+ style?: Record<string, string | number>;
44
+ /**
45
+ * To denote of chart is ready or not.
46
+ */
47
+ onChartReady?: (chart: any) => void;
48
+ /**
49
+ * Whether to merge with previous option.
50
+ */
51
+ merge?: boolean;
52
+ /**
53
+ * Values for the loading component.
54
+ */
55
+ loadingOption?: {
56
+ text: string;
57
+ };
58
+ /**
59
+ * Time-zone label.
60
+ */
61
+ timezoneLabel?: React.ReactNode;
62
+ /**
63
+ * Type of rendered used. Either "svg" or "canvas".
64
+ */
65
+ renderer?: "svg" | "canvas";
66
+ /**
67
+ * Value used to find element by `data-testid` attribute.
68
+ */
69
+ dataTestId?: string;
70
+ }
71
+ /**
72
+ * The Chart component is a wrapper component for ECharts.
73
+ *
74
+ * Handles events and click functionality.
75
+ *
76
+ * For more information see the [eCharts documentation](https://github.com/hustcc/echarts-for-react).
77
+ *
78
+ * @param {ChartProps} props - The props for the Chart component
79
+ * @returns {JSX.Element} Chart component
80
+ */
81
+ export declare const Chart: ({ options, showLoading, className, style, onChartReady, onClick, onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart, merge, loadingOption, timezoneLabel, renderer, dataTestId, }: ChartProps) => JSX.Element;
@@ -0,0 +1,9 @@
1
+ /// <reference types="react" />
2
+ import { ComponentMeta, Story } from "@storybook/react";
3
+ import { ChartProps } from "./Chart";
4
+ import "./Chart.css";
5
+ declare const _default: ComponentMeta<({ options, showLoading, className, style, onChartReady, onClick, onDataZoom, onBrush, onBrushSelected, mouseLeaveChart, mouseMoveChart, merge, loadingOption, timezoneLabel, renderer, dataTestId, }: ChartProps) => JSX.Element>;
6
+ export default _default;
7
+ export declare const Default: Story<ChartProps>;
8
+ export declare const Usage: () => JSX.Element;
9
+ export declare const Examples: () => JSX.Element;
@@ -0,0 +1 @@
1
+ export * from "./Chart";
@@ -0,0 +1,17 @@
1
+ /// <reference types="react" />
2
+ export interface ILegendItem {
3
+ label?: string;
4
+ selected?: boolean;
5
+ disabled?: boolean;
6
+ onClick?: () => void;
7
+ color: string;
8
+ className?: string;
9
+ dataTestId?: string;
10
+ }
11
+ /**
12
+ * The LegendItem component is used to show form legends.
13
+ *
14
+ * @param {ILegendItem} props - The props for the LegendItem component
15
+ * @returns {JSX.Element} LegendItem component
16
+ */
17
+ export declare const LegendItem: ({ className, label, disabled, selected, onClick, color, dataTestId }: ILegendItem) => JSX.Element;
@@ -0,0 +1 @@
1
+ export * from "./LegendItem";
@@ -0,0 +1,55 @@
1
+ /// <reference types="react" />
2
+ import { CommonProps } from "@trackunit/react-components";
3
+ type Size = "compact" | "full";
4
+ export interface ChartData<T extends object = object> {
5
+ /**
6
+ * Unique ID for this entry. Not visible for the user
7
+ */
8
+ id: string;
9
+ /**
10
+ * The text to show for to the user
11
+ */
12
+ name: string;
13
+ /**
14
+ * Number of elements with wrapped under this item
15
+ */
16
+ count?: number;
17
+ /**
18
+ * The color to use in the chart and legends for this item
19
+ */
20
+ color: string;
21
+ /**
22
+ * If selected, it'll be highlighted
23
+ */
24
+ selected?: boolean;
25
+ /**
26
+ * Supply the original object that this chart data item was constructed from. It'll be available on callbacks
27
+ */
28
+ original?: T;
29
+ }
30
+ export interface PieChartProps<T extends object> extends CommonProps {
31
+ /**
32
+ * Array of data points to show
33
+ */
34
+ data: ChartData<T>[] | undefined;
35
+ /**
36
+ * onClick handler which includes the data object clicked on
37
+ */
38
+ onClick?: (data: ChartData<T>) => void;
39
+ /**
40
+ * Show chart as loading
41
+ */
42
+ loading?: boolean;
43
+ /**
44
+ * When compact, the chart is smaller and legends are hidden.
45
+ */
46
+ size?: Size;
47
+ }
48
+ /**
49
+ * Create a PieChart with legends based on our current Chart component
50
+ *
51
+ * @param {PieChartProps} props - The props for the Chart component
52
+ * @returns {JSX.Element} Chart component
53
+ */
54
+ export declare const PieChart: <T extends object>({ data, size, loading, onClick, className, dataTestId, }: PieChartProps<T>) => JSX.Element;
55
+ export {};
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ import { ComponentMeta, Story } from "@storybook/react";
3
+ import { PieChartProps } from "./PieChart";
4
+ declare const _default: ComponentMeta<(<T extends object>({ data, size, loading, onClick, className, dataTestId, }: PieChartProps<T>) => JSX.Element)>;
5
+ export default _default;
6
+ export declare const Default: Story<PieChartProps<object>>;
7
+ export declare const HiddenCompactVariant: () => JSX.Element;
8
+ export declare const HiddenFullVariant: () => JSX.Element;
9
+ export declare const Usage: () => JSX.Element;
10
+ export declare const Variants: () => JSX.Element;
11
+ export declare const Examples: () => JSX.Element;
@@ -0,0 +1 @@
1
+ export * from "./PieChart";
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./Chart";
2
+ export * from "./LegendItem";
3
+ export * from "./PieChart";