@evergis/charts 2.0.61 → 2.0.63

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,137 @@ 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 \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
+ }, barChartClassNames.barChartGridLineX, barChartClassNames.barChartGridLineY, barChartClassNames.barChartMouseRect, barChartClassNames.barChartMouseRect, barChartClassNames.barChartLinesGlobal, barChartClassNames.barChartLine, barChartClassNames.barChartArea, barChartClassNames.barChartSelection);
1990
2006
  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
2007
 
2008
+ const useSelection = (node, props) => {
2009
+ const drawing = React.useRef(false);
2010
+ const startX = React.useRef(0);
2011
+ const selection = document.createElement("div");
2012
+ const onStartDrawing = React.useCallback(e => {
2013
+ const isTouch = Boolean(e.touches);
2014
+ e.stopPropagation();
2015
+
2016
+ if (node) {
2017
+ var _e$touches$;
2018
+
2019
+ drawing.current = true;
2020
+ startX.current = isTouch ? ((_e$touches$ = e.touches[0]) == null ? void 0 : _e$touches$.pageX) - node.firstChild.getBoundingClientRect().left : e.offsetX;
2021
+ selection.style.display = "block";
2022
+ selection.style.width = "0px";
2023
+ selection.style.left = startX.current + "px";
2024
+ }
2025
+ }, [node]);
2026
+ const onDraw = React.useCallback(e => {
2027
+ const isTouch = Boolean(e.touches);
2028
+ e.stopPropagation();
2029
+
2030
+ if (node && drawing.current) {
2031
+ var _e$touches$2;
2032
+
2033
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2034
+ const offsetX = isTouch ? ((_e$touches$2 = e.touches[0]) == null ? void 0 : _e$touches$2.pageX) - node.firstChild.getBoundingClientRect().left : e.offsetX;
2035
+ const newWidth = offsetX - startX.current;
2036
+
2037
+ if (offsetX >= 0) {
2038
+ if (newWidth > 0) {
2039
+ selection.style.marginLeft = "0px";
2040
+ selection.style.width = offsetX <= nodeWidth ? newWidth + "px" : nodeWidth - startX.current + "px";
2041
+ } else {
2042
+ selection.style.right = nodeWidth - startX.current + "px";
2043
+ selection.style.width = Math.abs(newWidth) + "px";
2044
+ selection.style.marginLeft = newWidth + "px";
2045
+ }
2046
+ }
2047
+
2048
+ if (isTouch) {
2049
+ if (node.offsetLeft > e.touches[0].pageX) {
2050
+ selection.style.right = nodeWidth - startX.current + "px";
2051
+ selection.style.width = startX.current + "px";
2052
+ selection.style.marginLeft = "-" + startX.current + "px";
2053
+ }
2054
+ }
2055
+ }
2056
+ }, [node]);
2057
+ const onMouseMove = React.useCallback(e => {
2058
+ if (drawing.current && node) {
2059
+ if (node.offsetLeft > e.pageX) {
2060
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2061
+ selection.style.right = nodeWidth - startX.current + "px";
2062
+ selection.style.width = startX.current + "px";
2063
+ selection.style.marginLeft = "-" + startX.current + "px";
2064
+ }
2065
+ }
2066
+ }, [node]);
2067
+ const onStopDrawing = React.useCallback(e => {
2068
+ e.stopPropagation();
2069
+
2070
+ if (node) {
2071
+ const nodeWidth = node.firstChild.getBoundingClientRect().width;
2072
+ const selectionMinX = selection.offsetLeft >= 0 ? selection.offsetLeft <= nodeWidth ? selection.offsetLeft : nodeWidth : 0;
2073
+ const selectionWidth = selection.getBoundingClientRect().right - selection.getBoundingClientRect().left;
2074
+ const selectionMaxX = selection.offsetLeft + selectionWidth <= nodeWidth ? selection.offsetLeft + selectionWidth >= 0 ? selection.offsetLeft + selectionWidth : 0 : nodeWidth;
2075
+ const min = Math.round(selectionMinX);
2076
+ const max = Math.round(selectionMaxX);
2077
+
2078
+ if (max - min > 0 && props.onSelect) {
2079
+ props.onSelect([min, max]);
2080
+ }
2081
+
2082
+ drawing.current = false;
2083
+ selection.style.display = "none";
2084
+ }
2085
+ }, [node]);
2086
+ React.useEffect(() => {
2087
+ if (node) {
2088
+ var _props$margin$bottom, _props$margin;
2089
+
2090
+ selection.setAttribute("class", barChartClassNames.barChartSelection);
2091
+ 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)");
2092
+ selection.style.display = "none";
2093
+ node.childNodes.forEach(child => {
2094
+ child.style.userSelect = "none";
2095
+ });
2096
+ node.appendChild(selection);
2097
+ node.addEventListener("mousedown", onStartDrawing);
2098
+ node.addEventListener("touchstart", onStartDrawing);
2099
+ node.addEventListener("mousemove", onDraw);
2100
+ node.addEventListener("touchmove", onDraw);
2101
+ document.addEventListener("mousemove", onMouseMove);
2102
+ document.addEventListener("mouseup", onStopDrawing);
2103
+ document.addEventListener("touchend", onStopDrawing);
2104
+ }
2105
+
2106
+ return () => {
2107
+ node == null ? void 0 : node.removeEventListener("mousedown", onStartDrawing);
2108
+ node == null ? void 0 : node.removeEventListener("touchstart", onStartDrawing);
2109
+ node == null ? void 0 : node.removeEventListener("mousemove", onDraw);
2110
+ node == null ? void 0 : node.removeEventListener("touchmove", onDraw);
2111
+ document.removeEventListener("mousemove", onMouseMove);
2112
+ document.removeEventListener("mouseup", onStopDrawing);
2113
+ document.removeEventListener("touchend", onStopDrawing);
2114
+ };
2115
+ }, [node]);
2116
+ };
2117
+
1992
2118
  const drawGrid$1 = _ref => {
1993
2119
  let {
1994
2120
  svg,
@@ -2697,7 +2823,8 @@ const BarChart = props => {
2697
2823
  const {
2698
2824
  className,
2699
2825
  style,
2700
- children
2826
+ children,
2827
+ selectable
2701
2828
  } = props;
2702
2829
  const [ref, node] = useNode();
2703
2830
  React.useEffect(() => {
@@ -2707,11 +2834,13 @@ const BarChart = props => {
2707
2834
  const onDraw = () => draw$4(node, props);
2708
2835
 
2709
2836
  useResize(props.width, onDraw);
2837
+ useSelection(selectable ? node : null, props);
2710
2838
  return React__default.createElement("div", {
2711
2839
  className: className,
2712
2840
  style: style
2713
2841
  }, React__default.createElement(TooltipStyles$1, null), React__default.createElement(SvgWrapper$4, {
2714
- ref: ref
2842
+ ref: ref,
2843
+ selectable: selectable
2715
2844
  }, children));
2716
2845
  };
2717
2846
  BarChart.defaultProps = {