@evergis/charts 2.0.62 → 2.0.64

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,2 @@
1
+ import { BarChartProps } from "../types";
2
+ export declare const useSelection: (node: HTMLElement | null, props: BarChartProps) => void;
@@ -28,6 +28,9 @@ export declare const barChartClassNames: {
28
28
  barChartGridLineXZero: string;
29
29
  barChartGridLineYZero: string;
30
30
  barChartGridLineY: string;
31
+ barChartSelection: string;
31
32
  };
32
- export declare const SvgWrapper: import("styled-components").StyledComponent<"div", any, {}, never>;
33
+ export declare const SvgWrapper: import("styled-components").StyledComponent<"div", any, {
34
+ selectable?: boolean | undefined;
35
+ }, never>;
33
36
  export declare const TooltipStyles: import("styled-components").GlobalStyleComponent<{}, import("styled-components").DefaultTheme>;
@@ -110,6 +110,8 @@ export declare type BarChartProps = {
110
110
  xScaleItemWidth?: number;
111
111
  tooltipRoot?: Element;
112
112
  tooltipClassName?: string;
113
+ selectable?: boolean;
114
+ onSelect?: (value: [number, number]) => void;
113
115
  };
114
116
  export declare type BarChartMergedData = (BarChartMarshalledGroup & BarChartLineData & {
115
117
  groupName: string;
@@ -1984,11 +1984,149 @@ const barChartClassNames = /*#__PURE__*/_extends({
1984
1984
  barChartGridLineX: 'barChartGridLineX',
1985
1985
  barChartGridLineXZero: 'barChartGridLineXZero',
1986
1986
  barChartGridLineYZero: 'barChartGridLineYZero',
1987
- barChartGridLineY: 'barChartGridLineY'
1987
+ barChartGridLineY: 'barChartGridLineY',
1988
+ barChartSelection: 'barChartSelection'
1988
1989
  }, tooltipClassnames, labelClassnames, barChartLinesClassNames);
1989
- const SvgWrapper$4 = /*#__PURE__*/styled__default(Wrapper)(_templateObject$a || (_templateObject$a = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n line {\n stroke-width: 1px;\n shape-rendering: crispEdges;\n }\n .", ",\n .", " {\n stroke: rgba(48, 69, 79, 0.06);\n }\n .", " {\n }\n .", " {\n fill: none;\n pointer-events: all;\n }\n .", " {\n stroke-width: 1.5px;\n stroke-linejoin: round;\n stroke-linecap: round;\n }\n .", " {\n shape-rendering: auto;\n }\n .", " {\n fill-opacity: 0.24;\n }\n"])), barChartClassNames.barChartGridLineX, barChartClassNames.barChartGridLineY, barChartClassNames.barChartMouseRect, barChartClassNames.barChartMouseRect, barChartClassNames.barChartLinesGlobal, barChartClassNames.barChartLine, barChartClassNames.barChartArea);
1990
+ const SvgWrapper$4 = /*#__PURE__*/styled__default(Wrapper)(_templateObject$a || (_templateObject$a = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n display: ", ";\n user-select: ", ";\n cursor: ", ";\n width: ", ";\n \n line {\n stroke-width: 1px;\n shape-rendering: crispEdges;\n }\n \n .", ",\n .", " {\n stroke: rgba(48, 69, 79, 0.06);\n }\n \n .", " {\n }\n \n .", " {\n fill: none;\n pointer-events: all;\n }\n \n .", " {\n stroke-width: 1.5px;\n stroke-linejoin: round;\n stroke-linecap: round;\n }\n \n .", " {\n shape-rendering: auto;\n }\n \n .", " {\n fill-opacity: 0.24;\n }\n \n .", " {\n position: absolute;\n top: 0;\n width: 0;\n background: rgba(0, 170, 255, 0.06);\n box-shadow: 1px 0 0 #00AAFF, -1px 0 0 #00AAFF;\n pointer-events: none;\n }\n"])), _ref => {
1991
+ let {
1992
+ selectable
1993
+ } = _ref;
1994
+ return selectable && "inline-block";
1995
+ }, _ref2 => {
1996
+ let {
1997
+ selectable
1998
+ } = _ref2;
1999
+ return selectable && "none";
2000
+ }, _ref3 => {
2001
+ let {
2002
+ selectable
2003
+ } = _ref3;
2004
+ return selectable && "crosshair";
2005
+ }, _ref4 => {
2006
+ let {
2007
+ selectable
2008
+ } = _ref4;
2009
+ return selectable && "auto";
2010
+ }, barChartClassNames.barChartGridLineX, barChartClassNames.barChartGridLineY, barChartClassNames.barChartMouseRect, barChartClassNames.barChartMouseRect, barChartClassNames.barChartLinesGlobal, barChartClassNames.barChartLine, barChartClassNames.barChartArea, barChartClassNames.barChartSelection);
1990
2011
  const TooltipStyles$1 = /*#__PURE__*/styled.createGlobalStyle(_templateObject2$4 || (_templateObject2$4 = /*#__PURE__*/_taggedTemplateLiteralLoose(["\n .", " {\n z-index: 1;\n transition: all linear 144ms;\n\n .", " {\n margin-bottom: 4px;\n :last-of-type {\n margin-bottom: 0;\n }\n }\n }\n"])), barChartClassNames.barChartMouseTooltip, barChartClassNames.barChartTooltipItem);
1991
2012
 
2013
+ const useSelection = (node, props) => {
2014
+ const drawing = React.useRef(false);
2015
+ const startX = React.useRef(0);
2016
+ const selection = document.createElement("div");
2017
+ const onStartDrawing = React.useCallback(e => {
2018
+ const isTouch = Boolean(e.touches);
2019
+ e.stopPropagation();
2020
+
2021
+ if (node) {
2022
+ var _e$touches$;
2023
+
2024
+ drawing.current = true;
2025
+ startX.current = isTouch ? ((_e$touches$ = e.touches[0]) == null ? void 0 : _e$touches$.pageX) - node.firstChild.getBoundingClientRect().left : e.offsetX;
2026
+ selection.style.display = "block";
2027
+ selection.style.width = "0px";
2028
+ selection.style.left = startX.current + "px";
2029
+ }
2030
+ }, [node]);
2031
+ const onDraw = React.useCallback(e => {
2032
+ const isTouch = Boolean(e.touches);
2033
+ e.stopPropagation();
2034
+
2035
+ if (node && drawing.current) {
2036
+ var _e$touches$2;
2037
+
2038
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2039
+ const offsetX = isTouch ? ((_e$touches$2 = e.touches[0]) == null ? void 0 : _e$touches$2.pageX) - node.firstChild.getBoundingClientRect().left : e.offsetX;
2040
+ const newWidth = offsetX - startX.current;
2041
+
2042
+ if (offsetX >= 0) {
2043
+ if (newWidth > 0) {
2044
+ selection.style.marginLeft = "0px";
2045
+ selection.style.width = offsetX <= nodeWidth ? newWidth + "px" : nodeWidth - startX.current + "px";
2046
+ } else {
2047
+ selection.style.right = nodeWidth - startX.current + "px";
2048
+ selection.style.width = Math.abs(newWidth) + "px";
2049
+ selection.style.marginLeft = newWidth + "px";
2050
+ }
2051
+ }
2052
+
2053
+ if (isTouch) {
2054
+ if (node.offsetLeft > e.touches[0].pageX) {
2055
+ selection.style.right = nodeWidth - startX.current + "px";
2056
+ selection.style.width = startX.current + "px";
2057
+ selection.style.marginLeft = "-" + startX.current + "px";
2058
+ }
2059
+ }
2060
+ }
2061
+ }, [node]);
2062
+ const onMouseLeave = React.useCallback(e => {
2063
+ if (drawing.current && node) {
2064
+ if (node.offsetLeft > e.pageX) {
2065
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2066
+ selection.style.right = nodeWidth - startX.current + "px";
2067
+ selection.style.width = startX.current + "px";
2068
+ selection.style.marginLeft = "-" + startX.current + "px";
2069
+ }
2070
+
2071
+ if (node.offsetLeft + node.offsetWidth < e.pageX) {
2072
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2073
+ selection.style.left = startX.current + "px";
2074
+ selection.style.width = nodeWidth - startX.current + "px";
2075
+ selection.style.marginLeft = "0px";
2076
+ }
2077
+ }
2078
+ }, [node]);
2079
+ const onStopDrawing = React.useCallback(() => {
2080
+ if (node) {
2081
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2082
+ const selectionMinX = selection.offsetLeft >= 0 ? selection.offsetLeft <= nodeWidth ? selection.offsetLeft : nodeWidth : 0;
2083
+ const selectionWidth = selection.getBoundingClientRect().right - selection.getBoundingClientRect().left;
2084
+ const selectionMaxX = selection.offsetLeft + selectionWidth <= nodeWidth ? selection.offsetLeft + selectionWidth >= 0 ? selection.offsetLeft + selectionWidth : 0 : nodeWidth;
2085
+ const min = Math.round(selectionMinX);
2086
+ const max = Math.round(selectionMaxX);
2087
+
2088
+ if (max - min > 0 && props.onSelect) {
2089
+ props.onSelect([min, max]);
2090
+ }
2091
+
2092
+ drawing.current = false;
2093
+ selection.style.display = "none";
2094
+ }
2095
+ }, [node]);
2096
+ React.useEffect(() => {
2097
+ if (node) {
2098
+ var _props$margin$bottom, _props$margin;
2099
+
2100
+ selection.setAttribute("class", barChartClassNames.barChartSelection);
2101
+ selection.setAttribute("style", "height: calc(100% - " + ((_props$margin$bottom = (_props$margin = props.margin) == null ? void 0 : _props$margin.bottom) != null ? _props$margin$bottom : 0) + "px)");
2102
+ selection.style.display = "none";
2103
+ node.childNodes.forEach(child => {
2104
+ child.style.userSelect = "none";
2105
+ });
2106
+ node.appendChild(selection);
2107
+ node.addEventListener("mousedown", onStartDrawing);
2108
+ node.addEventListener("touchstart", onStartDrawing);
2109
+ node.addEventListener("mousemove", onDraw);
2110
+ node.addEventListener("touchmove", onDraw);
2111
+ node.addEventListener("mouseleave", onMouseLeave);
2112
+ node.addEventListener("mouseup", onStopDrawing);
2113
+ node.addEventListener("touchend", onStopDrawing);
2114
+ document.addEventListener("mouseup", onStopDrawing);
2115
+ }
2116
+
2117
+ return () => {
2118
+ node == null ? void 0 : node.removeEventListener("mousedown", onStartDrawing);
2119
+ node == null ? void 0 : node.removeEventListener("touchstart", onStartDrawing);
2120
+ node == null ? void 0 : node.removeEventListener("mousemove", onDraw);
2121
+ node == null ? void 0 : node.removeEventListener("touchmove", onDraw);
2122
+ node == null ? void 0 : node.removeEventListener("mouseleave", onMouseLeave);
2123
+ node == null ? void 0 : node.removeEventListener("mouseup", onStopDrawing);
2124
+ node == null ? void 0 : node.removeEventListener("touchend", onStopDrawing);
2125
+ document.removeEventListener("mouseup", onStopDrawing);
2126
+ };
2127
+ }, [node]);
2128
+ };
2129
+
1992
2130
  const drawGrid$1 = _ref => {
1993
2131
  let {
1994
2132
  svg,
@@ -2697,7 +2835,8 @@ const BarChart = props => {
2697
2835
  const {
2698
2836
  className,
2699
2837
  style,
2700
- children
2838
+ children,
2839
+ selectable
2701
2840
  } = props;
2702
2841
  const [ref, node] = useNode();
2703
2842
  React.useEffect(() => {
@@ -2707,11 +2846,13 @@ const BarChart = props => {
2707
2846
  const onDraw = () => draw$4(node, props);
2708
2847
 
2709
2848
  useResize(props.width, onDraw);
2849
+ useSelection(selectable ? node : null, props);
2710
2850
  return React__default.createElement("div", {
2711
2851
  className: className,
2712
2852
  style: style
2713
2853
  }, React__default.createElement(TooltipStyles$1, null), React__default.createElement(SvgWrapper$4, {
2714
- ref: ref
2854
+ ref: ref,
2855
+ selectable: selectable
2715
2856
  }, children));
2716
2857
  };
2717
2858
  BarChart.defaultProps = {