@trackunit/react-chart-components 1.3.117 → 1.3.118

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/index.cjs.js CHANGED
@@ -7,6 +7,8 @@ var sharedUtils = require('@trackunit/shared-utils');
7
7
  var echarts = require('echarts');
8
8
  var react = require('react');
9
9
  var uiDesignTokens = require('@trackunit/ui-design-tokens');
10
+ var dateAndTimeUtils = require('@trackunit/date-and-time-utils');
11
+ var reactDateAndTimeHooks = require('@trackunit/react-date-and-time-hooks');
10
12
 
11
13
  function _interopNamespaceDefault(e) {
12
14
  var n = Object.create(null);
@@ -404,9 +406,9 @@ const DonutChart = ({ data, size = "full", loading, onClick, className, dataTest
404
406
  ],
405
407
  };
406
408
  }, [size, currentValue, showOthers, limitedData, limitedDataWithoutOthers, chartColor, hoveringItem?.id]);
407
- return (jsxRuntime.jsxs("div", { className: cvaChartRoot({ className, legendPosition }), "data-testid": dataTestId, ref: containerRef, children: [jsxRuntime.jsx("div", { className: cvaChartContainer({ legendPosition }), children: jsxRuntime.jsx(Chart, { className: cvaChart({ legendPosition, size }), dataTestId: "pie-chart", onChartReady: handleChartReady, onClick: handleChartClick, onEvents: handleChartEvents, options: chartOptions, showLoading: loading, style: { width: "100%", height: "100%" } }) }), size === "full" && (jsxRuntime.jsx("div", { className: cvaLegend({ legendPosition }), "data-testid": "legend", children: limitedData.map((item, index) => (jsxRuntime.jsx(LegendItem, { className: "p-1.5 py-0.5", color: item.color ?? chartColor(index), count: item.value, dataTestId: `legend-${item.id}`, disabled: (item.value ?? 0) === 0, label: item.name, onClick: onClick ? () => onClick(item) : undefined, onMouseEnter: () => handleLegendMouseEnter(item), onMouseLeave: handleLegendMouseLeave, selected: item.selected }, item.id))) }))] }));
409
+ return (jsxRuntime.jsxs("div", { className: cvaChartRoot$1({ className, legendPosition }), "data-testid": dataTestId, ref: containerRef, children: [jsxRuntime.jsx("div", { className: cvaChartContainer({ legendPosition }), children: jsxRuntime.jsx(Chart, { className: cvaChart({ legendPosition, size }), dataTestId: "pie-chart", onChartReady: handleChartReady, onClick: handleChartClick, onEvents: handleChartEvents, options: chartOptions, showLoading: loading, style: { width: "100%", height: "100%" } }) }), size === "full" && (jsxRuntime.jsx("div", { className: cvaLegend({ legendPosition }), "data-testid": "legend", children: limitedData.map((item, index) => (jsxRuntime.jsx(LegendItem, { className: "p-1.5 py-0.5", color: item.color ?? chartColor(index), count: item.value, dataTestId: `legend-${item.id}`, disabled: (item.value ?? 0) === 0, label: item.name, onClick: onClick ? () => onClick(item) : undefined, onMouseEnter: () => handleLegendMouseEnter(item), onMouseLeave: handleLegendMouseLeave, selected: item.selected }, item.id))) }))] }));
408
410
  };
409
- const cvaChartRoot = cssClassVarianceUtilities.cvaMerge(["flex", "w-full", "h-full", "gap-4"], {
411
+ const cvaChartRoot$1 = cssClassVarianceUtilities.cvaMerge(["flex", "w-full", "h-full", "gap-4"], {
410
412
  variants: {
411
413
  legendPosition: {
412
414
  Right: ["items-center", "justify-items-stretch", "flex-row"],
@@ -449,7 +451,99 @@ const cvaLegend = cssClassVarianceUtilities.cvaMerge(["flex", "overflow-auto"],
449
451
  },
450
452
  });
451
453
 
454
+ /**
455
+ * Create a BarChart with legends. Based on Chart component
456
+ *
457
+ * @param {BarChartProps} props - The props for the Chart component
458
+ * @returns {ReactElement} Chart component
459
+ */
460
+ const BarChart = ({ series, loading, onClick, className, dataTestId, units, showDataZoom = false, }) => {
461
+ const { formatDate } = reactDateAndTimeHooks.useDateAndTime();
462
+ const { chartColor } = useChartColor();
463
+ const seriesData = react.useMemo(() => {
464
+ const seriesAsArray = Array.isArray(series) ? series : series !== undefined ? [series] : [];
465
+ return seriesAsArray.map((s, i) => ({
466
+ ...s,
467
+ color: s.color || chartColor(i),
468
+ type: "bar",
469
+ cursor: "arrow",
470
+ barGap: "0",
471
+ data: s.data.map(d => ({
472
+ ...d,
473
+ })),
474
+ }));
475
+ }, [series, chartColor]);
476
+ const isDateData = (data) => !!data && "date" in data;
477
+ return (jsxRuntime.jsxs("div", { className: cvaChartRoot({ className }), "data-testid": dataTestId, children: [jsxRuntime.jsx("div", { className: "flex-0 flex flex-row gap-2 place-self-end pr-8", "data-testid": "legend", children: seriesData.map((item, index) => (jsxRuntime.jsx(LegendItem, { color: item.color || chartColor(index), dataTestId: `legend-${item.name}`, label: item.name }, item.name))) }), jsxRuntime.jsx("div", { className: "flex min-h-[260px] w-full flex-1", children: jsxRuntime.jsx(Chart, { dataTestId: "bar-chart", onClick: onClick
478
+ ? (e) => {
479
+ onClick(e);
480
+ }
481
+ : undefined, options: {
482
+ tooltip: {
483
+ trigger: "item",
484
+ confine: true,
485
+ valueFormatter: value => `${value} ${units ?? ""}`,
486
+ },
487
+ grid: {
488
+ left: "2%",
489
+ right: "2%",
490
+ bottom: "10%",
491
+ top: "10%",
492
+ containLabel: true,
493
+ },
494
+ xAxis: [
495
+ {
496
+ type: "category",
497
+ axisLabel: {
498
+ formatter: value => {
499
+ if (isDateData(seriesData[0]?.data[0])) {
500
+ const date = formatDate(dateAndTimeUtils.toDateUtil(value), {
501
+ selectFormat: "dateOnly",
502
+ dateFormat: "medium",
503
+ });
504
+ return date.replace(",", ",\n");
505
+ }
506
+ return value;
507
+ },
508
+ fontSize: 10,
509
+ fontFamily: uiDesignTokens.fontFamily,
510
+ rotate: 45,
511
+ overflow: "truncate",
512
+ },
513
+ data: seriesData[0]?.data
514
+ ?.filter(({ value }) => value)
515
+ .map(data => (isDateData(data) ? data.date : data.key)),
516
+ },
517
+ ],
518
+ dataZoom: showDataZoom
519
+ ? [
520
+ {
521
+ type: "slider",
522
+ id: "insideX",
523
+ xAxisIndex: 0,
524
+ startValue: 0,
525
+ endValue: (seriesData[0]?.data?.length || 0) > 10 ? 10 : seriesData[0]?.data?.length || 0,
526
+ showDetail: false,
527
+ },
528
+ ]
529
+ : [],
530
+ yAxis: [
531
+ {
532
+ type: "value",
533
+ axisLabel: {
534
+ fontSize: 10,
535
+ fontFamily: uiDesignTokens.fontFamily,
536
+ },
537
+ },
538
+ ],
539
+ series: seriesData,
540
+ }, showLoading: loading, style: { height: "360px", width: "100%" } }) })] }));
541
+ };
542
+ const cvaChartRoot = cssClassVarianceUtilities.cvaMerge(["flex", "flex-col", "items-center", "h-full"]);
543
+
544
+ exports.BarChart = BarChart;
452
545
  exports.Chart = Chart;
453
546
  exports.DonutChart = DonutChart;
547
+ exports.EChart = EChart;
454
548
  exports.LegendItem = LegendItem;
455
549
  exports.useChartColor = useChartColor;
package/index.esm.js CHANGED
@@ -4,7 +4,9 @@ import { cvaMerge } from '@trackunit/css-class-variance-utilities';
4
4
  import { objectKeys } from '@trackunit/shared-utils';
5
5
  import * as echarts from 'echarts';
6
6
  import { useRef, useCallback, useEffect, useMemo, useState } from 'react';
7
- import { DEFAULT_CHART_COLORS, CHART_STATUS_COLORS, DEFAULT_CHART_OTHER } from '@trackunit/ui-design-tokens';
7
+ import { DEFAULT_CHART_COLORS, CHART_STATUS_COLORS, DEFAULT_CHART_OTHER, fontFamily } from '@trackunit/ui-design-tokens';
8
+ import { toDateUtil } from '@trackunit/date-and-time-utils';
9
+ import { useDateAndTime } from '@trackunit/react-date-and-time-hooks';
8
10
 
9
11
  function isECElementEvent(value) {
10
12
  return (typeof value === "object" &&
@@ -383,9 +385,9 @@ const DonutChart = ({ data, size = "full", loading, onClick, className, dataTest
383
385
  ],
384
386
  };
385
387
  }, [size, currentValue, showOthers, limitedData, limitedDataWithoutOthers, chartColor, hoveringItem?.id]);
386
- return (jsxs("div", { className: cvaChartRoot({ className, legendPosition }), "data-testid": dataTestId, ref: containerRef, children: [jsx("div", { className: cvaChartContainer({ legendPosition }), children: jsx(Chart, { className: cvaChart({ legendPosition, size }), dataTestId: "pie-chart", onChartReady: handleChartReady, onClick: handleChartClick, onEvents: handleChartEvents, options: chartOptions, showLoading: loading, style: { width: "100%", height: "100%" } }) }), size === "full" && (jsx("div", { className: cvaLegend({ legendPosition }), "data-testid": "legend", children: limitedData.map((item, index) => (jsx(LegendItem, { className: "p-1.5 py-0.5", color: item.color ?? chartColor(index), count: item.value, dataTestId: `legend-${item.id}`, disabled: (item.value ?? 0) === 0, label: item.name, onClick: onClick ? () => onClick(item) : undefined, onMouseEnter: () => handleLegendMouseEnter(item), onMouseLeave: handleLegendMouseLeave, selected: item.selected }, item.id))) }))] }));
388
+ return (jsxs("div", { className: cvaChartRoot$1({ className, legendPosition }), "data-testid": dataTestId, ref: containerRef, children: [jsx("div", { className: cvaChartContainer({ legendPosition }), children: jsx(Chart, { className: cvaChart({ legendPosition, size }), dataTestId: "pie-chart", onChartReady: handleChartReady, onClick: handleChartClick, onEvents: handleChartEvents, options: chartOptions, showLoading: loading, style: { width: "100%", height: "100%" } }) }), size === "full" && (jsx("div", { className: cvaLegend({ legendPosition }), "data-testid": "legend", children: limitedData.map((item, index) => (jsx(LegendItem, { className: "p-1.5 py-0.5", color: item.color ?? chartColor(index), count: item.value, dataTestId: `legend-${item.id}`, disabled: (item.value ?? 0) === 0, label: item.name, onClick: onClick ? () => onClick(item) : undefined, onMouseEnter: () => handleLegendMouseEnter(item), onMouseLeave: handleLegendMouseLeave, selected: item.selected }, item.id))) }))] }));
387
389
  };
388
- const cvaChartRoot = cvaMerge(["flex", "w-full", "h-full", "gap-4"], {
390
+ const cvaChartRoot$1 = cvaMerge(["flex", "w-full", "h-full", "gap-4"], {
389
391
  variants: {
390
392
  legendPosition: {
391
393
  Right: ["items-center", "justify-items-stretch", "flex-row"],
@@ -428,4 +430,94 @@ const cvaLegend = cvaMerge(["flex", "overflow-auto"], {
428
430
  },
429
431
  });
430
432
 
431
- export { Chart, DonutChart, LegendItem, useChartColor };
433
+ /**
434
+ * Create a BarChart with legends. Based on Chart component
435
+ *
436
+ * @param {BarChartProps} props - The props for the Chart component
437
+ * @returns {ReactElement} Chart component
438
+ */
439
+ const BarChart = ({ series, loading, onClick, className, dataTestId, units, showDataZoom = false, }) => {
440
+ const { formatDate } = useDateAndTime();
441
+ const { chartColor } = useChartColor();
442
+ const seriesData = useMemo(() => {
443
+ const seriesAsArray = Array.isArray(series) ? series : series !== undefined ? [series] : [];
444
+ return seriesAsArray.map((s, i) => ({
445
+ ...s,
446
+ color: s.color || chartColor(i),
447
+ type: "bar",
448
+ cursor: "arrow",
449
+ barGap: "0",
450
+ data: s.data.map(d => ({
451
+ ...d,
452
+ })),
453
+ }));
454
+ }, [series, chartColor]);
455
+ const isDateData = (data) => !!data && "date" in data;
456
+ return (jsxs("div", { className: cvaChartRoot({ className }), "data-testid": dataTestId, children: [jsx("div", { className: "flex-0 flex flex-row gap-2 place-self-end pr-8", "data-testid": "legend", children: seriesData.map((item, index) => (jsx(LegendItem, { color: item.color || chartColor(index), dataTestId: `legend-${item.name}`, label: item.name }, item.name))) }), jsx("div", { className: "flex min-h-[260px] w-full flex-1", children: jsx(Chart, { dataTestId: "bar-chart", onClick: onClick
457
+ ? (e) => {
458
+ onClick(e);
459
+ }
460
+ : undefined, options: {
461
+ tooltip: {
462
+ trigger: "item",
463
+ confine: true,
464
+ valueFormatter: value => `${value} ${units ?? ""}`,
465
+ },
466
+ grid: {
467
+ left: "2%",
468
+ right: "2%",
469
+ bottom: "10%",
470
+ top: "10%",
471
+ containLabel: true,
472
+ },
473
+ xAxis: [
474
+ {
475
+ type: "category",
476
+ axisLabel: {
477
+ formatter: value => {
478
+ if (isDateData(seriesData[0]?.data[0])) {
479
+ const date = formatDate(toDateUtil(value), {
480
+ selectFormat: "dateOnly",
481
+ dateFormat: "medium",
482
+ });
483
+ return date.replace(",", ",\n");
484
+ }
485
+ return value;
486
+ },
487
+ fontSize: 10,
488
+ fontFamily,
489
+ rotate: 45,
490
+ overflow: "truncate",
491
+ },
492
+ data: seriesData[0]?.data
493
+ ?.filter(({ value }) => value)
494
+ .map(data => (isDateData(data) ? data.date : data.key)),
495
+ },
496
+ ],
497
+ dataZoom: showDataZoom
498
+ ? [
499
+ {
500
+ type: "slider",
501
+ id: "insideX",
502
+ xAxisIndex: 0,
503
+ startValue: 0,
504
+ endValue: (seriesData[0]?.data?.length || 0) > 10 ? 10 : seriesData[0]?.data?.length || 0,
505
+ showDetail: false,
506
+ },
507
+ ]
508
+ : [],
509
+ yAxis: [
510
+ {
511
+ type: "value",
512
+ axisLabel: {
513
+ fontSize: 10,
514
+ fontFamily,
515
+ },
516
+ },
517
+ ],
518
+ series: seriesData,
519
+ }, showLoading: loading, style: { height: "360px", width: "100%" } }) })] }));
520
+ };
521
+ const cvaChartRoot = cvaMerge(["flex", "flex-col", "items-center", "h-full"]);
522
+
523
+ export { BarChart, Chart, DonutChart, EChart, LegendItem, useChartColor };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-chart-components",
3
- "version": "1.3.117",
3
+ "version": "1.3.118",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -1,10 +1,7 @@
1
1
  import { CommonProps } from "@trackunit/react-components";
2
+ import { ECElementEvent } from "echarts";
2
3
  import { ReactElement } from "react";
3
- export interface ChartData<TProps extends object = object> {
4
- /**
5
- * Unique ID for this entry. Not visible for the user
6
- */
7
- date: string;
4
+ export interface BarChartData<TProps extends object = object> {
8
5
  /**
9
6
  * The value to show for to the user
10
7
  */
@@ -18,10 +15,22 @@ export interface ChartData<TProps extends object = object> {
18
15
  */
19
16
  original?: TProps;
20
17
  }
18
+ export interface BarChartGenericData<TProps extends object = object> extends BarChartData<TProps> {
19
+ /**
20
+ * The value to display on the x-axis
21
+ */
22
+ key: string;
23
+ }
24
+ export interface BarChartDateData<TProps extends object = object> extends BarChartData<TProps> {
25
+ /**
26
+ * The date value to display on the x-axis
27
+ */
28
+ date: string;
29
+ }
21
30
  export interface SeriesData<TProps extends object> {
22
31
  name: string;
23
32
  color?: string;
24
- data: ChartData<TProps>[];
33
+ data: BarChartGenericData<TProps>[] | BarChartDateData<TProps>[];
25
34
  }
26
35
  export interface BarChartProps<TProps extends object> extends CommonProps {
27
36
  /**
@@ -31,7 +40,7 @@ export interface BarChartProps<TProps extends object> extends CommonProps {
31
40
  /**
32
41
  * onClick handler which includes the data object clicked on
33
42
  */
34
- onClick?: (data: ChartData<TProps>) => void;
43
+ onClick?: (event: ECElementEvent) => void;
35
44
  /**
36
45
  * Show chart as loading
37
46
  */
package/src/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from "./Chart/Chart";
2
2
  export * from "./DonutChart/DonutChart";
3
+ export * from "./BarChart/BarChart";
3
4
  export * from "./LegendItem/LegendItem";
4
5
  export * from "./utils/useChartColor";
6
+ export * from "./EChart/EChart";
@@ -1 +0,0 @@
1
- export * from "./BarChart";