@redsift/charts 10.8.2 → 10.9.0

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.d.ts CHANGED
@@ -319,6 +319,10 @@ declare const useColor: ({ data, colorTheme, category, theme }: UseColorProps) =
319
319
  type SortingMethod = 'none' | 'asc-key' | 'desc-key' | 'asc-value' | 'desc-value' | string[] | ((a: CategoryDatum, b: CategoryDatum) => 1 | -1);
320
320
  declare const getSortingMethod: (sortingMethod: SortingMethod) => (a: CategoryDatum, b: CategoryDatum) => 1 | -1;
321
321
  declare const isValidDate: (value: string) => boolean;
322
+ declare const mergeLegends: (arr1: Statistics, arr2: Statistics) => {
323
+ key: any;
324
+ value: any;
325
+ }[];
322
326
 
323
327
  interface UseFormatCategoricalDataProps {
324
328
  /** Dataset to use to generate the chart, should be a list of objects containing a key and a value. */
@@ -662,6 +666,8 @@ interface LocaleText$3 {
662
666
  }
663
667
  type BarChartDimensions = ChartDimensions & {
664
668
  fontSize: number;
669
+ minWidth?: number;
670
+ maxWidth?: number;
665
671
  };
666
672
  type XScaleType = 'number' | 'Date' | 'dateString' | undefined;
667
673
  /**
@@ -732,6 +738,16 @@ interface BarChartProps extends ChartContainerProps, ChartAxesProps {
732
738
  theme?: Theme;
733
739
  /** X-scale type override to skip x-scale type inferrence. */
734
740
  xScaleType?: XScaleType;
741
+ /**
742
+ * In case of a datetime x-axis, if the xAxisTickValues prop is passed with a d3.js method having an offset function (for instance `timeDay`), the following prop will be used as argument.
743
+ * For reference, the line of code using it is: xAxisTickValues.offset(new Date(domain[0]), xAxisStartDateOffset)
744
+ */
745
+ xAxisStartDateOffset?: number;
746
+ /**
747
+ * In case of a datetime x-axis, if the xAxisTickValues prop is passed with a d3.js method having an offset function (for instance `timeDay`), the following prop will be used as argument.
748
+ * For reference, the line of code using it is: xAxisTickValues.offset(new Date(domain[1]), xAxisEndDateOffset)
749
+ */
750
+ xAxisEndDateOffset?: number;
735
751
  }
736
752
  type StyledBarChartProps = BarChartProps & {};
737
753
 
@@ -1119,4 +1135,4 @@ declare const StyledScatterPlotEmptyText: styled_components.StyledComponent<"div
1119
1135
  $theme: Theme;
1120
1136
  }, never>;
1121
1137
 
1122
- export { AnchorProps, AnyScale, Arc, ArcDatum, ArcProps, Arcs, ArcsProps, Axis, AxisPosition, AxisProps, AxisVariant, Bar, BarChart, BarChartDimensions, BarChartLegendVariant, BarChartProps, BarDatum, BarOrientation, BarProps, CategoricalOrLinearDim, CategoryData, CategoryDatum, CategoryDim, ChartAxesProps, ChartContainer, ChartContainerProps, ChartDimensions, ChartSize, ChartTheme, ColorTheme, Coordinates, CoordinatesCategoryData, CoordinatesCategoryDatum, CoordinatesCategoryDim, CustomColorTheme, DataPoint, DataPointProps, Datum, Dot, DotDatum, DotProps, DotVariant, EmptyDatum, JSONArray, JSONObject, JSONValue, LabelVariant, Legend, LegendItemDatum, LegendProps, LegendVariant, Line, LineChart, LineChartDimensions, LineChartLegendVariant, LineChartProps, LinePointDatum, LineProps, LinearData, LinearDatum, LinearDim, MarginProps, NumericValue, PieChart, PieChartDimensions, PieChartLegendVariant, PieChartProps, PieChartVariant, Scale, ScaleBand, ScaleLinear, ScaleLog, ScalePoint, ScaleSymlog, ScaleTime, ScaleTypeToScale, ScaleValue, ScaleWithBandwidth, ScatterPlot, ScatterPlotDimensions, ScatterPlotLegendVariant, ScatterPlotProps, ScatterPlotVariant, SortingMethod, Statistic, Statistics, StringValue, StyledArc, StyledArcProps, StyledArcs, StyledArcsProps, StyledAxis, StyledAxisProps, StyledBar, StyledBarChart, StyledBarChartEmptyText, StyledBarChartProps, StyledBarProps, StyledChartContainer, StyledChartContainerCaption, StyledChartContainerProps, StyledChartContainerTitle, StyledDataPoint, StyledDataPointProps, StyledDot, StyledDotProps, StyledLegend, StyledLegendProps, StyledLine, StyledLineChart, StyledLineChartEmptyText, StyledLineChartProps, StyledLineProps, StyledPieChart, StyledPieChartCenterText, StyledPieChartEmptyText, StyledPieChartProps, StyledScatterPlot, StyledScatterPlotEmptyText, StyledScatterPlotProps, SuccessDangerColorTheme, TicksSpec, TooltipVariant, TwoCategoryData, TwoCategoryDatum, TwoCategoryDim, UseBrushProps, UseColorProps, UseFormatCategoricalDataProps, UseZoomProps, XScaleType, empty, getColorScale, getSortingMethod, isValidDate, monochrome, scheme, successDangerScheme, useBrush, useColor, useFormatCategoricalData, useZoom };
1138
+ export { AnchorProps, AnyScale, Arc, ArcDatum, ArcProps, Arcs, ArcsProps, Axis, AxisPosition, AxisProps, AxisVariant, Bar, BarChart, BarChartDimensions, BarChartLegendVariant, BarChartProps, BarDatum, BarOrientation, BarProps, CategoricalOrLinearDim, CategoryData, CategoryDatum, CategoryDim, ChartAxesProps, ChartContainer, ChartContainerProps, ChartDimensions, ChartSize, ChartTheme, ColorTheme, Coordinates, CoordinatesCategoryData, CoordinatesCategoryDatum, CoordinatesCategoryDim, CustomColorTheme, DataPoint, DataPointProps, Datum, Dot, DotDatum, DotProps, DotVariant, EmptyDatum, JSONArray, JSONObject, JSONValue, LabelVariant, Legend, LegendItemDatum, LegendProps, LegendVariant, Line, LineChart, LineChartDimensions, LineChartLegendVariant, LineChartProps, LinePointDatum, LineProps, LinearData, LinearDatum, LinearDim, MarginProps, NumericValue, PieChart, PieChartDimensions, PieChartLegendVariant, PieChartProps, PieChartVariant, Scale, ScaleBand, ScaleLinear, ScaleLog, ScalePoint, ScaleSymlog, ScaleTime, ScaleTypeToScale, ScaleValue, ScaleWithBandwidth, ScatterPlot, ScatterPlotDimensions, ScatterPlotLegendVariant, ScatterPlotProps, ScatterPlotVariant, SortingMethod, Statistic, Statistics, StringValue, StyledArc, StyledArcProps, StyledArcs, StyledArcsProps, StyledAxis, StyledAxisProps, StyledBar, StyledBarChart, StyledBarChartEmptyText, StyledBarChartProps, StyledBarProps, StyledChartContainer, StyledChartContainerCaption, StyledChartContainerProps, StyledChartContainerTitle, StyledDataPoint, StyledDataPointProps, StyledDot, StyledDotProps, StyledLegend, StyledLegendProps, StyledLine, StyledLineChart, StyledLineChartEmptyText, StyledLineChartProps, StyledLineProps, StyledPieChart, StyledPieChartCenterText, StyledPieChartEmptyText, StyledPieChartProps, StyledScatterPlot, StyledScatterPlotEmptyText, StyledScatterPlotProps, SuccessDangerColorTheme, TicksSpec, TooltipVariant, TwoCategoryData, TwoCategoryDatum, TwoCategoryDim, UseBrushProps, UseColorProps, UseFormatCategoricalDataProps, UseZoomProps, XScaleType, empty, getColorScale, getSortingMethod, isValidDate, mergeLegends, monochrome, scheme, successDangerScheme, useBrush, useColor, useFormatCategoricalData, useZoom };
package/index.js CHANGED
@@ -226,6 +226,19 @@ const isValidDate = value => {
226
226
  const date = new Date(value);
227
227
  return !isNaN(date.getTime()) && date.toISOString().startsWith(value);
228
228
  };
229
+ const mergeLegends = (arr1, arr2) => {
230
+ const map = new Map();
231
+ [...arr1, ...arr2].forEach(item => {
232
+ map.set(item.key, item.value);
233
+ });
234
+ return Array.from(map, _ref => {
235
+ let [key, value] = _ref;
236
+ return {
237
+ key,
238
+ value
239
+ };
240
+ });
241
+ };
229
242
 
230
243
  const useFormatCategoricalData = _ref => {
231
244
  let {
@@ -1978,7 +1991,7 @@ const Legend = /*#__PURE__*/forwardRef((props, ref) => {
1978
1991
  Legend.className = CLASSNAME$6;
1979
1992
  Legend.displayName = COMPONENT_NAME$6;
1980
1993
 
1981
- const _excluded$f = ["barProps", "className", "data", "disableAnimations", "id", "getBarAnchorProps", "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", "xScaleType", "dateParser"],
1994
+ const _excluded$f = ["barProps", "className", "data", "disableAnimations", "id", "getBarAnchorProps", "isBarSelected", "labelDecorator", "legendDecorator", "legendVariant", "legendProps", "margins", "onBarClick", "size", "sortingMethod", "barRole", "colorTheme", "tooltipVariant", "xAxisVariant", "xAxisPlacement", "xAxisTickFormat", "xAxisTickPadding", "xAxisTickRotation", "xAxisTickSize", "xAxisTickValues", "xAxisMinValue", "xAxisMaxValue", "xAxisTickRemodelling", "xAxisStartDateOffset", "xAxisEndDateOffset", "yAxisVariant", "yAxisPlacement", "yAxisTickFormat", "yAxisTickPadding", "yAxisTickRotation", "yAxisTickSize", "yAxisTickValues", "yAxisMinValue", "yAxisMaxValue", "yAxisTickRemodelling", "xScaleType", "dateParser"],
1982
1995
  _excluded2$3 = ["extraLegendItems"];
1983
1996
  const getKey = datum => Array.isArray(datum.key) ? datum.key[0] : datum.key;
1984
1997
  const filterData = (hasCategory, data, filterFn) => {
@@ -1995,6 +2008,7 @@ const reduceData = (hasCategory, data, reduceFn, initialValue) => {
1995
2008
  };
1996
2009
  const isSameKey = (key, previousKey, xScaleType) => xScaleType === 'Date' || xScaleType === 'dateString' ? key.getTime() === previousKey.getTime() : key === previousKey;
1997
2010
  const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
2011
+ var _chartDimensions$maxW, _chartDimensions$minW;
1998
2012
  const {
1999
2013
  barProps,
2000
2014
  className,
@@ -2024,6 +2038,8 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
2024
2038
  xAxisMinValue,
2025
2039
  xAxisMaxValue,
2026
2040
  xAxisTickRemodelling,
2041
+ xAxisStartDateOffset = -1,
2042
+ xAxisEndDateOffset = 1,
2027
2043
  yAxisVariant: propsYAxisVariant,
2028
2044
  yAxisPlacement,
2029
2045
  yAxisTickFormat,
@@ -2051,7 +2067,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
2051
2067
  const xAxisVariant = propsXAxisVariant ? propsXAxisVariant : AxisVariant.tickValue;
2052
2068
  const yAxisVariant = propsYAxisVariant ? propsYAxisVariant : AxisVariant.default;
2053
2069
  const chartDimensions = sizeToDimension$3(size);
2054
- const width = chartDimensions.width;
2070
+ const width = Math.max(Math.min(chartDimensions.width, (_chartDimensions$maxW = chartDimensions.maxWidth) !== null && _chartDimensions$maxW !== void 0 ? _chartDimensions$maxW : Infinity), (_chartDimensions$minW = chartDimensions.minWidth) !== null && _chartDimensions$minW !== void 0 ? _chartDimensions$minW : -Infinity);
2055
2071
  const height = chartDimensions.height;
2056
2072
  const margins = _objectSpread2({
2057
2073
  top: xAxisVariant === AxisVariant.default || xAxisVariant === AxisVariant.tickValue ? 24 : 8,
@@ -2079,9 +2095,9 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
2079
2095
  const domain = xScaleType === 'dateString' ? extent(data, d => Date.parse(getKey(d))) : extent(data, d => getKey(d).valueOf());
2080
2096
  domain[0] = (_ref3 = xAxisMinValue) !== null && _ref3 !== void 0 ? _ref3 : domain[0];
2081
2097
  domain[1] = (_ref4 = xAxisMaxValue) !== null && _ref4 !== void 0 ? _ref4 : domain[1];
2082
- const startDate = xAxisTickValues && typeof xAxisTickValues === 'function' && typeof xAxisTickValues.offset === 'function' ? xAxisTickValues.offset(new Date(domain[0]), -1) : new Date(domain[0]);
2083
- const endDate = xAxisTickValues && typeof xAxisTickValues === 'function' && typeof xAxisTickValues.offset === 'function' ? xAxisTickValues.offset(new Date(domain[1]), 1) : new Date(domain[1]);
2084
- return scaleTime().domain([startDate, endDate]).range([0, chartWidth]).nice();
2098
+ const startDate = xAxisTickValues && typeof xAxisTickValues === 'function' && typeof xAxisTickValues.offset === 'function' ? xAxisTickValues.offset(new Date(domain[0]), xAxisStartDateOffset) : new Date(domain[0]);
2099
+ const endDate = xAxisTickValues && typeof xAxisTickValues === 'function' && typeof xAxisTickValues.offset === 'function' ? xAxisTickValues.offset(new Date(domain[1]), xAxisEndDateOffset) : new Date(domain[1]);
2100
+ return barProps !== null && barProps !== void 0 && barProps.width && typeof barProps.width === 'number' ? scaleTime().domain([startDate, endDate]).range([barProps.width / 2 + 10, chartWidth - (barProps.width / 2 + 5)]) : scaleTime().domain([startDate, endDate]).range([barWidth, chartWidth - barWidth]).nice();
2085
2101
  } else {
2086
2102
  var _ref5, _ref6;
2087
2103
  const domain = xAxisTickValues && Array.isArray(xAxisTickValues) ? xAxisTickValues : data.map(d => getKey(d));
@@ -2342,7 +2358,7 @@ const RenderedLinearBarChart = /*#__PURE__*/forwardRef((props, ref) => {
2342
2358
  tickRemodelling: yAxisTickRemodelling,
2343
2359
  variant: yAxisVariant
2344
2360
  }) : null))), hasCategory && legendVariant !== BarChartLegendVariant.none ? /*#__PURE__*/React__default.createElement(Legend, _extends({
2345
- data: (extraLegendItems ? [...statsByCategory, ...extraLegendItems] : statsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
2361
+ data: (extraLegendItems ? mergeLegends(extraLegendItems, statsByCategory).sort(getSortingMethod(sortingMethod)) : statsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
2346
2362
  color: colorScale === null || colorScale === void 0 ? void 0 : colorScale(d.key)
2347
2363
  })),
2348
2364
  variant: legendVariant === BarChartLegendVariant.externalLabelValue ? LegendVariant.value : legendVariant === BarChartLegendVariant.externalLabelPercent ? LegendVariant.percent : legendVariant === BarChartLegendVariant.externalLabel ? LegendVariant.label : LegendVariant.custom,
@@ -3169,7 +3185,7 @@ const RenderedLineChart = /*#__PURE__*/forwardRef((props, ref) => {
3169
3185
  tickRemodelling: yAxisTickRemodelling,
3170
3186
  variant: yAxisVariant
3171
3187
  }) : null))), hasCategory && legendVariant !== LineChartLegendVariant.none ? /*#__PURE__*/React__default.createElement(Legend, _extends({
3172
- data: (extraLegendItems ? [...statsByCategory, ...extraLegendItems] : statsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
3188
+ data: (extraLegendItems ? mergeLegends(extraLegendItems, statsByCategory).sort(getSortingMethod(sortingMethod)) : statsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
3173
3189
  color: colorScale === null || colorScale === void 0 ? void 0 : colorScale(d.key)
3174
3190
  })),
3175
3191
  variant: legendVariant === LineChartLegendVariant.externalLabelValue ? LegendVariant.value : legendVariant === LineChartLegendVariant.externalLabelPercent ? LegendVariant.percent : legendVariant === LineChartLegendVariant.externalLabel ? LegendVariant.label : LegendVariant.custom,
@@ -3741,7 +3757,7 @@ const RenderedPieChart = /*#__PURE__*/forwardRef((props, ref) => {
3741
3757
  tooltipVariant: tooltipVariant,
3742
3758
  transform: `translate(${width / 2} ${isHalf ? height : height / 2})`
3743
3759
  }))), legendVariant !== PieChartLegendVariant.none && legendVariant !== PieChartLegendVariant.internal ? /*#__PURE__*/React__default.createElement(Legend, _extends({
3744
- data: (extraLegendItems ? [...data, ...extraLegendItems] : data).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
3760
+ data: (extraLegendItems ? mergeLegends(extraLegendItems, data).sort(getSortingMethod(sortingMethod)) : data).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
3745
3761
  color: colorScale === null || colorScale === void 0 ? void 0 : colorScale(d.key)
3746
3762
  })),
3747
3763
  variant: legendVariant === PieChartLegendVariant.externalLabelValue ? LegendVariant.value : legendVariant === PieChartLegendVariant.externalLabelPercent ? LegendVariant.percent : legendVariant === PieChartLegendVariant.externalLabel ? LegendVariant.label : LegendVariant.custom,
@@ -4385,7 +4401,7 @@ const RenderedScatterPlot = /*#__PURE__*/forwardRef((props, ref) => {
4385
4401
  tickFormat: yAxisTickFormat,
4386
4402
  variant: yAxisVariant
4387
4403
  }) : null))), hasCategory && legendVariant !== ScatterPlotLegendVariant.none ? /*#__PURE__*/React__default.createElement(Legend, _extends({
4388
- data: (extraLegendItems ? [...countsByCategory, ...extraLegendItems] : countsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
4404
+ data: (extraLegendItems ? mergeLegends(extraLegendItems, countsByCategory).sort(getSortingMethod(sortingMethod)) : countsByCategory).map(d => _objectSpread2(_objectSpread2({}, d), {}, {
4389
4405
  color: colorScale === null || colorScale === void 0 ? void 0 : colorScale(d.key)
4390
4406
  })),
4391
4407
  variant: legendVariant === ScatterPlotLegendVariant.externalLabelValue ? LegendVariant.value : legendVariant === ScatterPlotLegendVariant.externalLabelPercent ? LegendVariant.percent : legendVariant === ScatterPlotLegendVariant.externalLabel ? LegendVariant.label : LegendVariant.custom,
@@ -4484,5 +4500,5 @@ const ScatterPlot = /*#__PURE__*/forwardRef((props, ref) => {
4484
4500
  ScatterPlot.className = CLASSNAME;
4485
4501
  ScatterPlot.displayName = COMPONENT_NAME;
4486
4502
 
4487
- 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 };
4503
+ 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, mergeLegends, monochrome, scheme, successDangerScheme, useBrush, useColor, useFormatCategoricalData, useZoom };
4488
4504
  //# sourceMappingURL=index.js.map