@redsift/charts 10.1.0-alpha.2 → 10.1.0-alpha.4

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 (4) hide show
  1. package/index.d.ts +79 -57
  2. package/index.js +309 -271
  3. package/index.js.map +1 -1
  4. package/package.json +2 -2
package/index.js CHANGED
@@ -1069,8 +1069,8 @@ const DEFAULT_PROPS$9 = {
1069
1069
  },
1070
1070
  width: 40
1071
1071
  };
1072
+ const getValue = data => data.cumulativeValue !== undefined ? data.cumulativeValue : data.value;
1072
1073
  const Bar = /*#__PURE__*/forwardRef((props, ref) => {
1073
- var _data;
1074
1074
  const {
1075
1075
  color,
1076
1076
  data,
@@ -1093,7 +1093,7 @@ const Bar = /*#__PURE__*/forwardRef((props, ref) => {
1093
1093
  } = props,
1094
1094
  forwardedProps = _objectWithoutProperties(props, _excluded$m);
1095
1095
  const theme = useTheme();
1096
- const interpolator = interpolate(((_data = previousData.data) === null || _data === void 0 ? void 0 : _data.value) || 0, data.data.value);
1096
+ const interpolator = interpolate(getValue(previousData.data) || 0, getValue(data.data));
1097
1097
  const text = labelDecorator ? labelDecorator(data, {
1098
1098
  index,
1099
1099
  isSelected: propsIsSelected,
@@ -1124,7 +1124,7 @@ const Bar = /*#__PURE__*/forwardRef((props, ref) => {
1124
1124
  fill: isDeselected ? 'var(--redsift-color-neutral-light-grey)' : color,
1125
1125
  width: config.immediate ? scale(data.data.value) : animatedProps.t.to(t => scale(interpolator(t)))
1126
1126
  }) : /*#__PURE__*/React__default.createElement(it.rect, {
1127
- height: config.immediate ? maxHeight - scale(data.data.value) : animatedProps.t.to(t => maxHeight - scale(interpolator(t))),
1127
+ height: config.immediate ? maxHeight - scale(getValue(data.data)) : animatedProps.t.to(t => Math.max(0, maxHeight - scale(interpolator(t)))),
1128
1128
  fill: isDeselected ? 'var(--redsift-color-neutral-light-grey)' : color,
1129
1129
  width: width
1130
1130
  }), isHorizontal ? /*#__PURE__*/React__default.createElement("text", {
@@ -1138,6 +1138,21 @@ Bar.className = CLASSNAME$9;
1138
1138
  Bar.defaultProps = DEFAULT_PROPS$9;
1139
1139
  Bar.displayName = COMPONENT_NAME$9;
1140
1140
 
1141
+ /**
1142
+ * Component's labels variant.
1143
+ */
1144
+ const BarChartLegendVariant = {
1145
+ none: 'none',
1146
+ externalLabel: 'externalLabel',
1147
+ externalLabelValue: 'externalLabelValue',
1148
+ externalLabelPercent: 'externalLabelPercent',
1149
+ custom: 'custom'
1150
+ };
1151
+
1152
+ /**
1153
+ * Component props.
1154
+ */
1155
+
1141
1156
  var reset$1 = "Reset";
1142
1157
  var enUS = {
1143
1158
  reset: reset$1
@@ -1603,7 +1618,222 @@ const RenderedOrdinalBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1603
1618
  }))));
1604
1619
  });
1605
1620
 
1606
- const _excluded$h = ["barProps", "className", "data", "id", "isBarSelected", "labelDecorator", "margins", "onBarClick", "size", "sortingMethod", "barRole", "colorTheme", "tooltipVariant", "xAxisVariant", "xAxisPlacement", "xAxisTickFormat", "xAxisTickPadding", "xAxisTickRotation", "xAxisTickSize", "xAxisTickValues", "xAxisMinValue", "xAxisMaxValue", "xAxisTickRemodelling", "yAxisVariant", "yAxisPlacement", "yAxisTickFormat", "yAxisTickPadding", "yAxisTickRotation", "yAxisTickSize", "yAxisTickValues", "yAxisMinValue", "yAxisMaxValue", "yAxisTickRemodelling", "dateParser"];
1621
+ /**
1622
+ * Component style.
1623
+ */
1624
+ const StyledLegend = styled.ul`
1625
+ display: flex;
1626
+ flex-direction: column;
1627
+
1628
+ ${baseContainer}
1629
+ `;
1630
+
1631
+ /**
1632
+ * Component style.
1633
+ */
1634
+ const StyledLegendItem = styled.li`
1635
+ display: flex;
1636
+ align-items: center;
1637
+ gap: 8px;
1638
+ font-size: var(--redsift-typography-caption-font-size);
1639
+
1640
+ ${_ref => {
1641
+ let {
1642
+ $theme
1643
+ } = _ref;
1644
+ return css`
1645
+ color: var(--redsift-color-neutral-${$theme === Theme.dark ? 'white' : 'x-dark-grey'});
1646
+ `;
1647
+ }}
1648
+
1649
+ ${_ref2 => {
1650
+ let {
1651
+ $variant,
1652
+ color
1653
+ } = _ref2;
1654
+ return $variant !== LegendVariant.custom ? css`
1655
+ > div {
1656
+ height: 16px;
1657
+ width: 16px;
1658
+ min-width: 16px;
1659
+ background-color: ${color};
1660
+ }
1661
+ ` : '';
1662
+ }}}
1663
+
1664
+ ${_ref3 => {
1665
+ let {
1666
+ $clickable
1667
+ } = _ref3;
1668
+ return $clickable ? css`
1669
+ cursor: pointer;
1670
+
1671
+ &:hover,
1672
+ &:focus {
1673
+ outline: none;
1674
+ > div {
1675
+ opacity: 0.7;
1676
+ }
1677
+ }
1678
+
1679
+ &:focus-visible {
1680
+ > div {
1681
+ outline: 4px solid var(--redsift-color-primary-n);
1682
+ }
1683
+ }
1684
+ ` : '';
1685
+ }}}
1686
+ `;
1687
+
1688
+ const _excluded$h = ["className", "color", "data", "id", "variant"];
1689
+ const COMPONENT_NAME$7 = 'LegendItem';
1690
+ const CLASSNAME$7 = 'redsift-legend-item';
1691
+ const DEFAULT_PROPS$7 = {
1692
+ color: monochrome,
1693
+ index: 0
1694
+ };
1695
+ const LegendItem = /*#__PURE__*/forwardRef((props, ref) => {
1696
+ const {
1697
+ index,
1698
+ isSelected: propsIsSelected,
1699
+ labelDecorator,
1700
+ legendDecorator,
1701
+ onClick,
1702
+ role
1703
+ } = props;
1704
+ const {
1705
+ className,
1706
+ color,
1707
+ data,
1708
+ id: propsId,
1709
+ variant
1710
+ } = props,
1711
+ forwardedProps = _objectWithoutProperties(props, _excluded$h);
1712
+ const theme = useTheme();
1713
+ const [_id] = useId();
1714
+ const id = propsId !== null && propsId !== void 0 ? propsId : _id;
1715
+ const isLegendCustom = variant === LegendVariant.custom && legendDecorator;
1716
+ const text = labelDecorator ? labelDecorator(data, {
1717
+ index,
1718
+ isSelected: propsIsSelected,
1719
+ color
1720
+ }) : data.data.key;
1721
+ const isEmpty = data.data.value === 0;
1722
+ const isSelectable = role === 'option';
1723
+ const isSelected = isSelectable && propsIsSelected === true;
1724
+ const isDeselected = isSelectable && propsIsSelected === false;
1725
+ const onKeyDown = event => {
1726
+ if (onClick) {
1727
+ event.stopPropagation();
1728
+ if (event.code === 'Enter' || event.code === 'Space') {
1729
+ event.preventDefault();
1730
+ onClick(data);
1731
+ }
1732
+ }
1733
+ };
1734
+ return /*#__PURE__*/React__default.createElement(StyledLegendItem, _extends({}, forwardedProps, {
1735
+ className: classNames(LegendItem.className, className, `_${index}`, {
1736
+ selected: isSelected,
1737
+ deselected: isDeselected
1738
+ }),
1739
+ color: isDeselected ? 'var(--redsift-color-neutral-light-grey)' : color,
1740
+ ref: ref,
1741
+ "aria-labelledby": !isEmpty ? `${id}-title` : undefined,
1742
+ "aria-selected": isSelected ? true : isDeselected ? false : undefined,
1743
+ id: id,
1744
+ onClick: onClick ? () => onClick(data) : undefined,
1745
+ onKeyDown: onClick ? e => onKeyDown(e) : undefined,
1746
+ role: role ? role : onClick ? 'button' : undefined,
1747
+ tabIndex: onClick ? 0 : undefined,
1748
+ $clickable: Boolean(onClick),
1749
+ $variant: variant,
1750
+ $theme: theme
1751
+ }), isLegendCustom ? legendDecorator(data, {
1752
+ index,
1753
+ isSelected: propsIsSelected,
1754
+ color: props.color
1755
+ }) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", null), typeof text === 'string' ? variant === LegendVariant.value ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Number$1, {
1756
+ as: "b",
1757
+ maximumFractionDigits: 2,
1758
+ value: data.data.value,
1759
+ variant: "inherit"
1760
+ }), /*#__PURE__*/React__default.createElement(Text, {
1761
+ variant: "caption"
1762
+ }, text)) : variant === LegendVariant.percent && data.data.percent ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Number$1, {
1763
+ as: "b",
1764
+ maximumFractionDigits: 2,
1765
+ type: "percent",
1766
+ value: data.data.percent,
1767
+ variant: "inherit"
1768
+ }), /*#__PURE__*/React__default.createElement(Text, {
1769
+ variant: "caption"
1770
+ }, text)) : /*#__PURE__*/React__default.createElement(Text, {
1771
+ variant: "caption"
1772
+ }, text) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, text)));
1773
+ });
1774
+ LegendItem.className = CLASSNAME$7;
1775
+ LegendItem.defaultProps = DEFAULT_PROPS$7;
1776
+ LegendItem.displayName = COMPONENT_NAME$7;
1777
+
1778
+ const _excluded$g = ["className", "data", "isLegendItemSelected", "labelDecorator", "legendDecorator", "legendItemRole", "onLegendItemClick", "variant", "width"],
1779
+ _excluded2 = ["color"];
1780
+ const COMPONENT_NAME$6 = 'Legend';
1781
+ const CLASSNAME$6 = 'redsift-chart-legend';
1782
+ const DEFAULT_PROPS$6 = {
1783
+ isLegendItemSelected: () => true,
1784
+ variant: LegendVariant.label
1785
+ };
1786
+ const Legend = /*#__PURE__*/forwardRef((props, ref) => {
1787
+ const {
1788
+ className,
1789
+ data,
1790
+ isLegendItemSelected,
1791
+ labelDecorator,
1792
+ legendDecorator,
1793
+ legendItemRole,
1794
+ onLegendItemClick,
1795
+ variant,
1796
+ width
1797
+ } = props,
1798
+ forwardedProps = _objectWithoutProperties(props, _excluded$g);
1799
+ const total = sum(data, d => d.value);
1800
+ return /*#__PURE__*/React__default.createElement(StyledLegend, _extends({}, forwardedProps, {
1801
+ className: classNames(Legend.className, className),
1802
+ ref: ref,
1803
+ $width: width
1804
+ }), data.map((_ref, index) => {
1805
+ var _datum$percent;
1806
+ let {
1807
+ color
1808
+ } = _ref,
1809
+ datum = _objectWithoutProperties(_ref, _excluded2);
1810
+ return /*#__PURE__*/React__default.createElement(LegendItem, {
1811
+ data: {
1812
+ data: _objectSpread2(_objectSpread2({}, datum), {}, {
1813
+ percent: (_datum$percent = datum.percent) !== null && _datum$percent !== void 0 ? _datum$percent : datum.value / total
1814
+ })
1815
+ },
1816
+ color: color,
1817
+ variant: variant,
1818
+ index: index,
1819
+ isSelected: Boolean(isLegendItemSelected({
1820
+ data: datum
1821
+ })),
1822
+ key: `legend-item _${index}`,
1823
+ labelDecorator: labelDecorator,
1824
+ legendDecorator: legendDecorator,
1825
+ onClick: onLegendItemClick,
1826
+ role: legendItemRole
1827
+ });
1828
+ }));
1829
+ });
1830
+ Legend.className = CLASSNAME$6;
1831
+ Legend.defaultProps = DEFAULT_PROPS$6;
1832
+ Legend.displayName = COMPONENT_NAME$6;
1833
+
1834
+ const _excluded$f = ["barProps", "className", "data", "id", "isBarSelected", "labelDecorator", "legendDecorator", "legendVariant", "legendProps", "margins", "onBarClick", "size", "sortingMethod", "barRole", "colorTheme", "tooltipVariant", "xAxisVariant", "xAxisPlacement", "xAxisTickFormat", "xAxisTickPadding", "xAxisTickRotation", "xAxisTickSize", "xAxisTickValues", "xAxisMinValue", "xAxisMaxValue", "xAxisTickRemodelling", "yAxisVariant", "yAxisPlacement", "yAxisTickFormat", "yAxisTickPadding", "yAxisTickRotation", "yAxisTickSize", "yAxisTickValues", "yAxisMinValue", "yAxisMaxValue", "yAxisTickRemodelling", "dateParser"];
1835
+ const getKey = datum => Array.isArray(datum.key) ? datum.key[0] : datum.key;
1836
+ const isSameKey = (key, previousKey, xScaleType) => xScaleType === 'Date' || xScaleType === 'dateString' ? key.getTime() === previousKey.getTime() : key === previousKey;
1607
1837
  const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1608
1838
  const {
1609
1839
  barProps,
@@ -1612,6 +1842,9 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1612
1842
  id,
1613
1843
  isBarSelected,
1614
1844
  labelDecorator,
1845
+ legendDecorator,
1846
+ legendVariant,
1847
+ legendProps,
1615
1848
  margins: propsMargins,
1616
1849
  onBarClick,
1617
1850
  size,
@@ -1641,11 +1874,11 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1641
1874
  yAxisTickRemodelling,
1642
1875
  dateParser
1643
1876
  } = props,
1644
- forwardedProps = _objectWithoutProperties(props, _excluded$h);
1877
+ forwardedProps = _objectWithoutProperties(props, _excluded$f);
1645
1878
  const cache = useRef();
1646
1879
  const theme = useTheme();
1647
- const xScaleType = propsData[0].key instanceof Date ? 'Date' : typeof propsData[0].key === 'number' ? 'number' : typeof propsData[0].key === 'string' && isValidDate(propsData[0].key) ? 'dateString' : undefined;
1648
- const data = xScaleType === 'number' ? propsData.filter(datum => typeof datum.key === 'number' && !Number.isNaN(datum.key)) : xScaleType === 'dateString' ? propsData.filter(datum => isValidDate(datum.key)) : xScaleType === 'Date' ? propsData.filter(datum => datum.key instanceof Date) : propsData;
1880
+ const xScaleType = getKey(propsData[0]) instanceof Date ? 'Date' : typeof getKey(propsData[0]) === 'number' ? 'number' : typeof getKey(propsData[0]) === 'string' && isValidDate(getKey(propsData[0])) ? 'dateString' : undefined;
1881
+ const data = xScaleType === 'number' ? propsData.filter(datum => typeof getKey(datum) === 'number' && !Number.isNaN(getKey(datum))) : xScaleType === 'dateString' ? propsData.filter(datum => isValidDate(getKey(datum))) : xScaleType === 'Date' ? propsData.filter(datum => getKey(datum) instanceof Date) : propsData;
1649
1882
  useEffect(() => {
1650
1883
  cache.current = data;
1651
1884
  });
@@ -1669,7 +1902,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1669
1902
  const scaleX = (() => {
1670
1903
  if (xScaleType === 'number') {
1671
1904
  var _ref, _ref2;
1672
- const domain = extent(data, d => d.key);
1905
+ const domain = extent(data, d => getKey(d));
1673
1906
  domain[0] = (_ref = xAxisMinValue) !== null && _ref !== void 0 ? _ref : domain[0];
1674
1907
  domain[1] = (_ref2 = xAxisMaxValue) !== null && _ref2 !== void 0 ? _ref2 : domain[1];
1675
1908
  const gap = domain[1] - domain[0];
@@ -1678,7 +1911,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1678
1911
  return scaleLinear().domain(domain).range([0, chartWidth]).nice();
1679
1912
  } else if (xScaleType === 'dateString' || xScaleType === 'Date') {
1680
1913
  var _ref3, _ref4;
1681
- const domain = xScaleType === 'dateString' ? extent(data, d => Date.parse(d.key)) : extent(data, d => d.key.valueOf());
1914
+ const domain = xScaleType === 'dateString' ? extent(data, d => Date.parse(getKey(d))) : extent(data, d => getKey(d).valueOf());
1682
1915
  domain[0] = (_ref3 = xAxisMinValue) !== null && _ref3 !== void 0 ? _ref3 : domain[0];
1683
1916
  domain[1] = (_ref4 = xAxisMaxValue) !== null && _ref4 !== void 0 ? _ref4 : domain[1];
1684
1917
  const startDate = xAxisTickValues && typeof xAxisTickValues.offset === 'function' ? xAxisTickValues.offset(new Date(domain[0]), -1) : new Date(domain[0]);
@@ -1686,23 +1919,14 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1686
1919
  return scaleTime().domain([startDate, endDate]).range([0, chartWidth]).nice();
1687
1920
  } else {
1688
1921
  var _ref5, _ref6;
1689
- const domain = xAxisTickValues && Array.isArray(xAxisTickValues) ? xAxisTickValues : data.map(d => d.key);
1922
+ const domain = xAxisTickValues && Array.isArray(xAxisTickValues) ? xAxisTickValues : data.map(d => getKey(d));
1690
1923
  domain[0] = (_ref5 = xAxisMinValue) !== null && _ref5 !== void 0 ? _ref5 : domain[0];
1691
1924
  domain[1] = (_ref6 = xAxisMaxValue) !== null && _ref6 !== void 0 ? _ref6 : domain[1];
1692
1925
  return scalePoint().domain(domain).range([0, chartWidth]);
1693
1926
  }
1694
1927
  })();
1695
- const scaleY = (() => {
1696
- var _ref7, _ref8;
1697
- const domain = extent(data, d => d.value);
1698
- domain[0] = (_ref7 = yAxisMinValue) !== null && _ref7 !== void 0 ? _ref7 : domain[0];
1699
- domain[1] = (_ref8 = yAxisMaxValue) !== null && _ref8 !== void 0 ? _ref8 : domain[1];
1700
- if (domain[1] === 0 && domain[0] === 0) {
1701
- domain[1] = 1;
1702
- }
1703
- return scaleLinear().domain(domain).range([chartHeight, 0]).nice();
1704
- })();
1705
1928
  const xAxisTickFormat = propsXAxisTickFormat !== null && propsXAxisTickFormat !== void 0 ? propsXAxisTickFormat : xScaleType === 'Date' || xScaleType === 'dateString' ? scaleX.tickFormat() : undefined;
1929
+ const hasCategory = data[0] && Array.isArray(data[0].key) && data[0].key.length >= 2 && data[0].key[1] !== null && data[0].key[1] !== undefined;
1706
1930
  const statsByCategory = statsBy$1(data, sortingMethod);
1707
1931
  const colorScale = useColor({
1708
1932
  data: statsByCategory,
@@ -1711,37 +1935,54 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1711
1935
  theme
1712
1936
  });
1713
1937
  const bars = data.sort((a, b) => {
1714
- const currentKeyA = a.key;
1715
- const currentKeyB = b.key;
1938
+ const currentKeyA = hasCategory ? a.key[0] : a.key;
1939
+ const currentKeyB = hasCategory ? b.key[0] : b.key;
1716
1940
  if (xScaleType === 'number') {
1717
1941
  if (currentKeyA < currentKeyB) return -1;
1718
1942
  if (currentKeyA > currentKeyB) return 1;
1719
- return 0;
1943
+ return hasCategory ? a.key[1] < b.key[1] ? 1 : -1 : 0;
1720
1944
  } else if (xScaleType === 'dateString') {
1721
1945
  if (Date.parse(currentKeyA) < Date.parse(currentKeyB)) return -1;
1722
1946
  if (Date.parse(currentKeyA) > Date.parse(currentKeyB)) return 1;
1723
- return 0;
1947
+ return hasCategory ? a.key[1] < b.key[1] ? 1 : -1 : 0;
1724
1948
  } else if (xScaleType === 'Date') {
1725
1949
  if (currentKeyA.valueOf() < currentKeyB.valueOf()) return -1;
1726
1950
  if (currentKeyA.valueOf() > currentKeyB.valueOf()) return 1;
1727
- return 0;
1951
+ return hasCategory ? a.key[1] < b.key[1] ? 1 : -1 : 0;
1728
1952
  } else if (xScaleType === 'string' && xAxisTickValues && Array.isArray(xAxisTickValues)) {
1729
1953
  if (xAxisTickValues.indexOf(currentKeyA) < xAxisTickValues.indexOf(currentKeyB)) return -1;
1730
1954
  if (xAxisTickValues.indexOf(currentKeyA) > xAxisTickValues.indexOf(currentKeyB)) return 1;
1731
- return 0;
1955
+ return hasCategory ? a.key[1] < b.key[1] ? 1 : -1 : 0;
1732
1956
  }
1733
- return 0;
1734
- }).filter(d => d.value !== null).map(d => {
1735
- const currentKeyD = d.key;
1736
- return {
1737
- category: 'default',
1738
- key: xScaleType === 'dateString' ? dateParser ? dateParser(currentKeyD) : new Date(Date.parse(currentKeyD)) : currentKeyD,
1739
- value: d.value
1740
- };
1741
- });
1957
+ return hasCategory ? a.key[1] < b.key[1] ? 1 : -1 : 0;
1958
+ }).filter(d => d.value !== null).reduce((previousValue, currentValue, currentIndex) => {
1959
+ var _key$;
1960
+ const currentKeyD = hasCategory ? currentValue.key[0] : currentValue.key;
1961
+ const formattedKeyD = xScaleType === 'dateString' ? dateParser ? dateParser(currentKeyD) : new Date(Date.parse(currentKeyD)) : currentKeyD;
1962
+ previousValue.push({
1963
+ category: hasCategory ? (_key$ = currentValue.key[1]) !== null && _key$ !== void 0 ? _key$ : 'default' : 'default',
1964
+ key: formattedKeyD,
1965
+ value: currentValue.value,
1966
+ cumulativeValue: currentValue.value
1967
+ });
1968
+ if (hasCategory && currentIndex > 0 && isSameKey(formattedKeyD, previousValue[currentIndex - 1].key, xScaleType)) {
1969
+ previousValue[currentIndex].cumulativeValue = previousValue[currentIndex].cumulativeValue + previousValue[currentIndex - 1].cumulativeValue;
1970
+ }
1971
+ return previousValue;
1972
+ }, []);
1973
+ const scaleY = (() => {
1974
+ var _ref7, _ref8;
1975
+ const domain = extent(bars, d => d.cumulativeValue);
1976
+ domain[0] = (_ref7 = yAxisMinValue) !== null && _ref7 !== void 0 ? _ref7 : domain[0];
1977
+ domain[1] = (_ref8 = yAxisMaxValue) !== null && _ref8 !== void 0 ? _ref8 : domain[1];
1978
+ if (domain[1] === 0 && domain[0] === 0) {
1979
+ domain[1] = 1;
1980
+ }
1981
+ return scaleLinear().domain(domain).range([chartHeight, 0]).nice();
1982
+ })();
1742
1983
  if (xScaleType) {
1743
1984
  const minimalGap = Math.min(...bars.map((bar, i) => i > 0 ? Math.abs(bar.key - bars[i - 1].key) : undefined).filter(k => k)) * 0.9;
1744
- barWidth = Math.min(barWidth, chartWidth / ((scaleX.domain()[1] - scaleX.domain()[0]) / minimalGap), chartWidth / (xAxisTickValues ? xAxisTickValues.range(scaleX.domain()[0], scaleX.domain()[1]).length : 1));
1985
+ barWidth = Math.min(barWidth, chartWidth / ((scaleX.domain()[1] - scaleX.domain()[0]) / minimalGap), chartWidth / (xAxisTickValues ? typeof xAxisTickValues === 'number' ? xAxisTickValues : xAxisTickValues.range(scaleX.domain()[0], scaleX.domain()[1]).length : 1));
1745
1986
  }
1746
1987
  return /*#__PURE__*/React__default.createElement(StyledBarChart, _extends({}, forwardedProps, {
1747
1988
  id: id,
@@ -1759,7 +2000,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1759
2000
  transform: `translate(${margins.left},${margins.top})`
1760
2001
  }, bars.map((bar, index) => {
1761
2002
  const to = {
1762
- data: _objectSpread2({}, bar)
2003
+ data: bar
1763
2004
  };
1764
2005
  const from = cache.current ? {
1765
2006
  data: cache.current[index]
@@ -1774,7 +2015,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1774
2015
  isSelected: Boolean(isBarSelected(to)),
1775
2016
  key: `bar _${index}`,
1776
2017
  labelDecorator: labelDecorator,
1777
- maxHeight: chartHeight,
2018
+ maxHeight: scaleY(bar.cumulativeValue - bar.value),
1778
2019
  onClick: onBarClick,
1779
2020
  orientation: BarOrientation.vertical,
1780
2021
  previousData: from,
@@ -1836,13 +2077,19 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
1836
2077
  tickFormat: yAxisTickFormat,
1837
2078
  tickRemodelling: yAxisTickRemodelling,
1838
2079
  variant: yAxisVariant
1839
- }) : null))));
2080
+ }) : null))), hasCategory && legendVariant !== BarChartLegendVariant.none ? /*#__PURE__*/React__default.createElement(Legend, _extends({
2081
+ data: statsByCategory.map(d => _objectSpread2(_objectSpread2({}, d), {}, {
2082
+ color: colorScale === null || colorScale === void 0 ? void 0 : colorScale(d.key)
2083
+ })),
2084
+ variant: legendVariant === BarChartLegendVariant.externalLabelValue ? LegendVariant.value : legendVariant === BarChartLegendVariant.externalLabelPercent ? LegendVariant.percent : legendVariant === BarChartLegendVariant.externalLabel ? LegendVariant.label : LegendVariant.custom,
2085
+ legendDecorator: legendDecorator ? (datum, props) => legendDecorator(datum, props) : undefined
2086
+ }, legendProps)) : null);
1840
2087
  });
1841
2088
 
1842
- const _excluded$g = ["caping", "chartRef", "className", "data", "emptyComponent", "id", "isBarSelected", "labelDecorator", "localeText", "onBarClick", "orientation", "others", "size", "barRole", "theme", "tooltipDecorator", "colorTheme", "tooltipVariant"];
1843
- const COMPONENT_NAME$7 = 'BarChart';
1844
- const CLASSNAME$7 = 'redsift-barchart';
1845
- const DEFAULT_PROPS$7 = {
2089
+ const _excluded$e = ["caping", "chartRef", "className", "data", "emptyComponent", "id", "isBarSelected", "labelDecorator", "legendDecorator", "legendVariant", "localeText", "onBarClick", "orientation", "others", "size", "barRole", "theme", "tooltipDecorator", "colorTheme", "tooltipVariant"];
2090
+ const COMPONENT_NAME$5 = 'BarChart';
2091
+ const CLASSNAME$5 = 'redsift-barchart';
2092
+ const DEFAULT_PROPS$5 = {
1846
2093
  isBarSelected: () => true,
1847
2094
  orientation: BarOrientation.horizontal,
1848
2095
  others: true,
@@ -1866,6 +2113,8 @@ const BarChart = /*#__PURE__*/forwardRef((props, ref) => {
1866
2113
  id: propsId,
1867
2114
  isBarSelected,
1868
2115
  labelDecorator,
2116
+ legendDecorator,
2117
+ legendVariant: propsLegendVariant,
1869
2118
  localeText,
1870
2119
  onBarClick,
1871
2120
  orientation,
@@ -1877,9 +2126,10 @@ const BarChart = /*#__PURE__*/forwardRef((props, ref) => {
1877
2126
  colorTheme,
1878
2127
  tooltipVariant
1879
2128
  } = props,
1880
- forwardedProps = _objectWithoutProperties(props, _excluded$g);
2129
+ forwardedProps = _objectWithoutProperties(props, _excluded$e);
1881
2130
  const [_id] = useId();
1882
2131
  const id = propsId !== null && propsId !== void 0 ? propsId : _id;
2132
+ const legendVariant = propsLegendVariant !== null && propsLegendVariant !== void 0 ? propsLegendVariant : BarChartLegendVariant.externalLabel;
1883
2133
  if (propsData === undefined || propsData === null) {
1884
2134
  return /*#__PURE__*/React__default.createElement(LoadingBarChart, _extends({
1885
2135
  id: id
@@ -1920,7 +2170,7 @@ const BarChart = /*#__PURE__*/forwardRef((props, ref) => {
1920
2170
  }, forwardedProps, {
1921
2171
  ref: ref
1922
2172
  }));
1923
- } else if (dataType === 'LinearData') {
2173
+ } else if (dataType === 'LinearData' || dataType === 'TwoCategoryData') {
1924
2174
  return /*#__PURE__*/React__default.createElement(RenderedLinearBarChart, _extends({
1925
2175
  barRole: barRole,
1926
2176
  chartRef: chartRef,
@@ -1929,6 +2179,8 @@ const BarChart = /*#__PURE__*/forwardRef((props, ref) => {
1929
2179
  id: id,
1930
2180
  isBarSelected: isBarSelected,
1931
2181
  labelDecorator: labelDecorator,
2182
+ legendDecorator: legendDecorator,
2183
+ legendVariant: legendVariant,
1932
2184
  onBarClick: onBarClick,
1933
2185
  size: size,
1934
2186
  theme: theme,
@@ -1941,9 +2193,9 @@ const BarChart = /*#__PURE__*/forwardRef((props, ref) => {
1941
2193
  }
1942
2194
  return null;
1943
2195
  });
1944
- BarChart.className = CLASSNAME$7;
1945
- BarChart.defaultProps = DEFAULT_PROPS$7;
1946
- BarChart.displayName = COMPONENT_NAME$7;
2196
+ BarChart.className = CLASSNAME$5;
2197
+ BarChart.defaultProps = DEFAULT_PROPS$5;
2198
+ BarChart.displayName = COMPONENT_NAME$5;
1947
2199
 
1948
2200
  /**
1949
2201
  * Component's variant.
@@ -1996,10 +2248,10 @@ const StyledDot = styled(DataPoint)`
1996
2248
  }}}
1997
2249
  `;
1998
2250
 
1999
- const _excluded$f = ["className", "scaleX", "scaleY", "variant"];
2000
- const COMPONENT_NAME$6 = 'Dot';
2001
- const CLASSNAME$6 = 'redsift-dot';
2002
- const DEFAULT_PROPS$6 = {
2251
+ const _excluded$d = ["className", "scaleX", "scaleY", "variant"];
2252
+ const COMPONENT_NAME$4 = 'Dot';
2253
+ const CLASSNAME$4 = 'redsift-dot';
2254
+ const DEFAULT_PROPS$4 = {
2003
2255
  color: monochrome,
2004
2256
  index: 0,
2005
2257
  labelDecorator: datum => {
@@ -2021,7 +2273,7 @@ const Dot = /*#__PURE__*/forwardRef((props, ref) => {
2021
2273
  scaleY,
2022
2274
  variant
2023
2275
  } = props,
2024
- forwardedProps = _objectWithoutProperties(props, _excluded$f);
2276
+ forwardedProps = _objectWithoutProperties(props, _excluded$d);
2025
2277
  const theme = useTheme();
2026
2278
  const isSelectable = role === 'option';
2027
2279
  const isDeselected = isSelectable && propsIsSelected === false;
@@ -2040,222 +2292,9 @@ const Dot = /*#__PURE__*/forwardRef((props, ref) => {
2040
2292
  strokeWidth: variant === DotVariant.hollow ? '2' : '0'
2041
2293
  }));
2042
2294
  });
2043
- Dot.className = CLASSNAME$6;
2044
- Dot.defaultProps = DEFAULT_PROPS$6;
2045
- Dot.displayName = COMPONENT_NAME$6;
2046
-
2047
- /**
2048
- * Component style.
2049
- */
2050
- const StyledLegend = styled.ul`
2051
- display: flex;
2052
- flex-direction: column;
2053
-
2054
- ${baseContainer}
2055
- `;
2056
-
2057
- /**
2058
- * Component style.
2059
- */
2060
- const StyledLegendItem = styled.li`
2061
- display: flex;
2062
- align-items: center;
2063
- gap: 8px;
2064
- font-size: var(--redsift-typography-caption-font-size);
2065
-
2066
- ${_ref => {
2067
- let {
2068
- $theme
2069
- } = _ref;
2070
- return css`
2071
- color: var(--redsift-color-neutral-${$theme === Theme.dark ? 'white' : 'x-dark-grey'});
2072
- `;
2073
- }}
2074
-
2075
- ${_ref2 => {
2076
- let {
2077
- $variant,
2078
- color
2079
- } = _ref2;
2080
- return $variant !== LegendVariant.custom ? css`
2081
- > div {
2082
- height: 16px;
2083
- width: 16px;
2084
- min-width: 16px;
2085
- background-color: ${color};
2086
- }
2087
- ` : '';
2088
- }}}
2089
-
2090
- ${_ref3 => {
2091
- let {
2092
- $clickable
2093
- } = _ref3;
2094
- return $clickable ? css`
2095
- cursor: pointer;
2096
-
2097
- &:hover,
2098
- &:focus {
2099
- outline: none;
2100
- > div {
2101
- opacity: 0.7;
2102
- }
2103
- }
2104
-
2105
- &:focus-visible {
2106
- > div {
2107
- outline: 4px solid var(--redsift-color-primary-n);
2108
- }
2109
- }
2110
- ` : '';
2111
- }}}
2112
- `;
2113
-
2114
- const _excluded$e = ["className", "color", "data", "id", "variant"];
2115
- const COMPONENT_NAME$5 = 'LegendItem';
2116
- const CLASSNAME$5 = 'redsift-legend-item';
2117
- const DEFAULT_PROPS$5 = {
2118
- color: monochrome,
2119
- index: 0
2120
- };
2121
- const LegendItem = /*#__PURE__*/forwardRef((props, ref) => {
2122
- const {
2123
- index,
2124
- isSelected: propsIsSelected,
2125
- labelDecorator,
2126
- legendDecorator,
2127
- onClick,
2128
- role
2129
- } = props;
2130
- const {
2131
- className,
2132
- color,
2133
- data,
2134
- id: propsId,
2135
- variant
2136
- } = props,
2137
- forwardedProps = _objectWithoutProperties(props, _excluded$e);
2138
- const theme = useTheme();
2139
- const [_id] = useId();
2140
- const id = propsId !== null && propsId !== void 0 ? propsId : _id;
2141
- const isLegendCustom = variant === LegendVariant.custom && legendDecorator;
2142
- const text = labelDecorator ? labelDecorator(data, {
2143
- index,
2144
- isSelected: propsIsSelected,
2145
- color
2146
- }) : data.data.key;
2147
- const isEmpty = data.data.value === 0;
2148
- const isSelectable = role === 'option';
2149
- const isSelected = isSelectable && propsIsSelected === true;
2150
- const isDeselected = isSelectable && propsIsSelected === false;
2151
- const onKeyDown = event => {
2152
- if (onClick) {
2153
- event.stopPropagation();
2154
- if (event.code === 'Enter' || event.code === 'Space') {
2155
- event.preventDefault();
2156
- onClick(data);
2157
- }
2158
- }
2159
- };
2160
- return /*#__PURE__*/React__default.createElement(StyledLegendItem, _extends({}, forwardedProps, {
2161
- className: classNames(LegendItem.className, className, `_${index}`, {
2162
- selected: isSelected,
2163
- deselected: isDeselected
2164
- }),
2165
- color: isDeselected ? 'var(--redsift-color-neutral-light-grey)' : color,
2166
- ref: ref,
2167
- "aria-labelledby": !isEmpty ? `${id}-title` : undefined,
2168
- "aria-selected": isSelected ? true : isDeselected ? false : undefined,
2169
- id: id,
2170
- onClick: onClick ? () => onClick(data) : undefined,
2171
- onKeyDown: onClick ? e => onKeyDown(e) : undefined,
2172
- role: role ? role : onClick ? 'button' : undefined,
2173
- tabIndex: onClick ? 0 : undefined,
2174
- $clickable: Boolean(onClick),
2175
- $variant: variant,
2176
- $theme: theme
2177
- }), isLegendCustom ? legendDecorator(data, {
2178
- index,
2179
- isSelected: propsIsSelected,
2180
- color: props.color
2181
- }) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", null), typeof text === 'string' ? variant === LegendVariant.value ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Number$1, {
2182
- as: "b",
2183
- maximumFractionDigits: 2,
2184
- value: data.data.value,
2185
- variant: "inherit"
2186
- }), /*#__PURE__*/React__default.createElement(Text, {
2187
- variant: "caption"
2188
- }, text)) : variant === LegendVariant.percent && data.data.percent ? /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(Number$1, {
2189
- as: "b",
2190
- maximumFractionDigits: 2,
2191
- type: "percent",
2192
- value: data.data.percent,
2193
- variant: "inherit"
2194
- }), /*#__PURE__*/React__default.createElement(Text, {
2195
- variant: "caption"
2196
- }, text)) : /*#__PURE__*/React__default.createElement(Text, {
2197
- variant: "caption"
2198
- }, text) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, text)));
2199
- });
2200
- LegendItem.className = CLASSNAME$5;
2201
- LegendItem.defaultProps = DEFAULT_PROPS$5;
2202
- LegendItem.displayName = COMPONENT_NAME$5;
2203
-
2204
- const _excluded$d = ["className", "data", "isLegendItemSelected", "labelDecorator", "legendDecorator", "legendItemRole", "onLegendItemClick", "variant", "width"],
2205
- _excluded2 = ["color"];
2206
- const COMPONENT_NAME$4 = 'Legend';
2207
- const CLASSNAME$4 = 'redsift-chart-legend';
2208
- const DEFAULT_PROPS$4 = {
2209
- isLegendItemSelected: () => true,
2210
- variant: LegendVariant.label
2211
- };
2212
- const Legend = /*#__PURE__*/forwardRef((props, ref) => {
2213
- const {
2214
- className,
2215
- data,
2216
- isLegendItemSelected,
2217
- labelDecorator,
2218
- legendDecorator,
2219
- legendItemRole,
2220
- onLegendItemClick,
2221
- variant,
2222
- width
2223
- } = props,
2224
- forwardedProps = _objectWithoutProperties(props, _excluded$d);
2225
- const total = sum(data, d => d.value);
2226
- return /*#__PURE__*/React__default.createElement(StyledLegend, _extends({}, forwardedProps, {
2227
- className: classNames(Legend.className, className),
2228
- ref: ref,
2229
- $width: width
2230
- }), data.map((_ref, index) => {
2231
- var _datum$percent;
2232
- let {
2233
- color
2234
- } = _ref,
2235
- datum = _objectWithoutProperties(_ref, _excluded2);
2236
- return /*#__PURE__*/React__default.createElement(LegendItem, {
2237
- data: {
2238
- data: _objectSpread2(_objectSpread2({}, datum), {}, {
2239
- percent: (_datum$percent = datum.percent) !== null && _datum$percent !== void 0 ? _datum$percent : datum.value / total
2240
- })
2241
- },
2242
- color: color,
2243
- variant: variant,
2244
- index: index,
2245
- isSelected: Boolean(isLegendItemSelected({
2246
- data: datum
2247
- })),
2248
- key: `legend-item _${index}`,
2249
- labelDecorator: labelDecorator,
2250
- legendDecorator: legendDecorator,
2251
- onClick: onLegendItemClick,
2252
- role: legendItemRole
2253
- });
2254
- }));
2255
- });
2256
- Legend.className = CLASSNAME$4;
2257
- Legend.defaultProps = DEFAULT_PROPS$4;
2258
- Legend.displayName = COMPONENT_NAME$4;
2295
+ Dot.className = CLASSNAME$4;
2296
+ Dot.defaultProps = DEFAULT_PROPS$4;
2297
+ Dot.displayName = COMPONENT_NAME$4;
2259
2298
 
2260
2299
  /**
2261
2300
  * Component style.
@@ -2748,7 +2787,6 @@ const _excluded$8 = ["className", "data", "dotRole", "emptyComponent", "id", "is
2748
2787
  const COMPONENT_NAME$2 = 'LineChart';
2749
2788
  const CLASSNAME$2 = 'redsift-line-chart';
2750
2789
  const DEFAULT_PROPS$2 = {
2751
- // legendVariant: LineChartLegendVariant.externalLabel,
2752
2790
  localeText: {
2753
2791
  emptyChartText: 'No Data'
2754
2792
  },
@@ -3914,5 +3952,5 @@ ScatterPlot.className = CLASSNAME;
3914
3952
  ScatterPlot.defaultProps = DEFAULT_PROPS;
3915
3953
  ScatterPlot.displayName = COMPONENT_NAME;
3916
3954
 
3917
- export { Arc, Arcs, Axis, AxisPosition, AxisVariant, Bar, BarChart, BarOrientation, ChartContainer, ChartSize, ColorTheme, DataPoint, Dot, DotVariant, LabelVariant, Legend, LegendVariant, Line, LineChart, LineChartLegendVariant, PieChart, PieChartLegendVariant, PieChartVariant, ScatterPlot, ScatterPlotLegendVariant, ScatterPlotVariant, StyledArc, StyledArcs, StyledAxis, StyledBar, StyledBarChart, StyledBarChartEmptyText, StyledChartContainer, StyledChartContainerCaption, StyledChartContainerTitle, StyledDataPoint, StyledDot, StyledLegend, StyledLine, StyledLineChart, StyledLineChartEmptyText, StyledPieChart, StyledPieChartCenterText, StyledPieChartEmptyText, StyledScatterPlot, StyledScatterPlotEmptyText, TooltipVariant, empty, getColorScale, getSortingMethod, isValidDate, monochrome, scheme, successDangerScheme, useBrush, useColor, useFormatCategoricalData, useZoom };
3955
+ export { Arc, Arcs, Axis, AxisPosition, AxisVariant, Bar, BarChart, BarChartLegendVariant, BarOrientation, ChartContainer, ChartSize, ColorTheme, DataPoint, Dot, DotVariant, LabelVariant, Legend, LegendVariant, Line, LineChart, LineChartLegendVariant, PieChart, PieChartLegendVariant, PieChartVariant, ScatterPlot, ScatterPlotLegendVariant, ScatterPlotVariant, StyledArc, StyledArcs, StyledAxis, StyledBar, StyledBarChart, StyledBarChartEmptyText, StyledChartContainer, StyledChartContainerCaption, StyledChartContainerTitle, StyledDataPoint, StyledDot, StyledLegend, StyledLine, StyledLineChart, StyledLineChartEmptyText, StyledPieChart, StyledPieChartCenterText, StyledPieChartEmptyText, StyledScatterPlot, StyledScatterPlotEmptyText, TooltipVariant, empty, getColorScale, getSortingMethod, isValidDate, monochrome, scheme, successDangerScheme, useBrush, useColor, useFormatCategoricalData, useZoom };
3918
3956
  //# sourceMappingURL=index.js.map