@fluentui/react-charts 9.2.0 → 9.2.2

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 (103) hide show
  1. package/CHANGELOG.md +38 -3
  2. package/dist/index.d.ts +84 -5
  3. package/lib/ChartTable.js +1 -0
  4. package/lib/ChartTable.js.map +1 -0
  5. package/lib/components/AreaChart/AreaChart.js.map +1 -1
  6. package/lib/components/ChartTable/ChartTable.js +57 -0
  7. package/lib/components/ChartTable/ChartTable.js.map +1 -0
  8. package/lib/components/ChartTable/ChartTable.types.js +1 -0
  9. package/lib/components/ChartTable/ChartTable.types.js.map +1 -0
  10. package/lib/components/ChartTable/index.js +3 -0
  11. package/lib/components/ChartTable/index.js.map +1 -0
  12. package/lib/components/ChartTable/useChartTableStyles.styles.js +93 -0
  13. package/lib/components/ChartTable/useChartTableStyles.styles.js.map +1 -0
  14. package/lib/components/ChartTable/useChartTableStyles.styles.raw.js +55 -0
  15. package/lib/components/ChartTable/useChartTableStyles.styles.raw.js.map +1 -0
  16. package/lib/components/CommonComponents/CartesianChart.js +110 -81
  17. package/lib/components/CommonComponents/CartesianChart.js.map +1 -1
  18. package/lib/components/CommonComponents/CartesianChart.types.js.map +1 -1
  19. package/lib/components/CommonComponents/ChartPopover.types.js.map +1 -1
  20. package/lib/components/DeclarativeChart/DeclarativeChart.js +20 -10
  21. package/lib/components/DeclarativeChart/DeclarativeChart.js.map +1 -1
  22. package/lib/components/DeclarativeChart/PlotlyColorAdapter.js +95 -0
  23. package/lib/components/DeclarativeChart/PlotlyColorAdapter.js.map +1 -0
  24. package/lib/components/DeclarativeChart/PlotlySchemaAdapter.js +204 -87
  25. package/lib/components/DeclarativeChart/PlotlySchemaAdapter.js.map +1 -1
  26. package/lib/components/DonutChart/DonutChart.js.map +1 -1
  27. package/lib/components/DonutChart/DonutChart.types.js.map +1 -1
  28. package/lib/components/DonutChart/Pie/Pie.js.map +1 -1
  29. package/lib/components/FunnelChart/FunnelChart.js.map +1 -1
  30. package/lib/components/GanttChart/GanttChart.js.map +1 -1
  31. package/lib/components/GroupedVerticalBarChart/GroupedVerticalBarChart.js +161 -141
  32. package/lib/components/GroupedVerticalBarChart/GroupedVerticalBarChart.js.map +1 -1
  33. package/lib/components/HeatMapChart/HeatMapChart.js.map +1 -1
  34. package/lib/components/HorizontalBarChart/HorizontalBarChart.js.map +1 -1
  35. package/lib/components/HorizontalBarChart/HorizontalBarChart.types.js.map +1 -1
  36. package/lib/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.js.map +1 -1
  37. package/lib/components/Legends/Legends.js.map +1 -1
  38. package/lib/components/Legends/OverflowMenu.js.map +1 -1
  39. package/lib/components/LineChart/LineChart.js.map +1 -1
  40. package/lib/components/ScatterChart/ScatterChart.js.map +1 -1
  41. package/lib/components/VerticalBarChart/VerticalBarChart.js +12 -11
  42. package/lib/components/VerticalBarChart/VerticalBarChart.js.map +1 -1
  43. package/lib/components/VerticalStackedBarChart/VerticalStackedBarChart.js +57 -51
  44. package/lib/components/VerticalStackedBarChart/VerticalStackedBarChart.js.map +1 -1
  45. package/lib/index.js +1 -0
  46. package/lib/index.js.map +1 -1
  47. package/lib/types/DataPoint.js.map +1 -1
  48. package/lib/utilities/FocusableTooltipText.js.map +1 -1
  49. package/lib/utilities/utilities.js +26 -37
  50. package/lib/utilities/utilities.js.map +1 -1
  51. package/lib/utilities/vbc-utils.js +24 -0
  52. package/lib/utilities/vbc-utils.js.map +1 -1
  53. package/lib-commonjs/ChartTable.js +6 -0
  54. package/lib-commonjs/ChartTable.js.map +1 -0
  55. package/lib-commonjs/components/AreaChart/AreaChart.js.map +1 -1
  56. package/lib-commonjs/components/ChartTable/ChartTable.js +68 -0
  57. package/lib-commonjs/components/ChartTable/ChartTable.js.map +1 -0
  58. package/lib-commonjs/components/ChartTable/ChartTable.types.js +6 -0
  59. package/lib-commonjs/components/ChartTable/ChartTable.types.js.map +1 -0
  60. package/lib-commonjs/components/ChartTable/index.js +8 -0
  61. package/lib-commonjs/components/ChartTable/index.js.map +1 -0
  62. package/lib-commonjs/components/ChartTable/useChartTableStyles.styles.js +196 -0
  63. package/lib-commonjs/components/ChartTable/useChartTableStyles.styles.js.map +1 -0
  64. package/lib-commonjs/components/ChartTable/useChartTableStyles.styles.raw.js +69 -0
  65. package/lib-commonjs/components/ChartTable/useChartTableStyles.styles.raw.js.map +1 -0
  66. package/lib-commonjs/components/CommonComponents/CartesianChart.js +109 -80
  67. package/lib-commonjs/components/CommonComponents/CartesianChart.js.map +1 -1
  68. package/lib-commonjs/components/CommonComponents/CartesianChart.types.js.map +1 -1
  69. package/lib-commonjs/components/CommonComponents/ChartPopover.types.js.map +1 -1
  70. package/lib-commonjs/components/DeclarativeChart/DeclarativeChart.js +19 -9
  71. package/lib-commonjs/components/DeclarativeChart/DeclarativeChart.js.map +1 -1
  72. package/lib-commonjs/components/DeclarativeChart/PlotlyColorAdapter.js +120 -0
  73. package/lib-commonjs/components/DeclarativeChart/PlotlyColorAdapter.js.map +1 -0
  74. package/lib-commonjs/components/DeclarativeChart/PlotlySchemaAdapter.js +204 -87
  75. package/lib-commonjs/components/DeclarativeChart/PlotlySchemaAdapter.js.map +1 -1
  76. package/lib-commonjs/components/DonutChart/DonutChart.js.map +1 -1
  77. package/lib-commonjs/components/DonutChart/DonutChart.types.js.map +1 -1
  78. package/lib-commonjs/components/DonutChart/Pie/Pie.js.map +1 -1
  79. package/lib-commonjs/components/FunnelChart/FunnelChart.js.map +1 -1
  80. package/lib-commonjs/components/GanttChart/GanttChart.js.map +1 -1
  81. package/lib-commonjs/components/GroupedVerticalBarChart/GroupedVerticalBarChart.js +159 -139
  82. package/lib-commonjs/components/GroupedVerticalBarChart/GroupedVerticalBarChart.js.map +1 -1
  83. package/lib-commonjs/components/HeatMapChart/HeatMapChart.js.map +1 -1
  84. package/lib-commonjs/components/HorizontalBarChart/HorizontalBarChart.js.map +1 -1
  85. package/lib-commonjs/components/HorizontalBarChart/HorizontalBarChart.types.js.map +1 -1
  86. package/lib-commonjs/components/HorizontalBarChartWithAxis/HorizontalBarChartWithAxis.js.map +1 -1
  87. package/lib-commonjs/components/Legends/Legends.js.map +1 -1
  88. package/lib-commonjs/components/Legends/OverflowMenu.js.map +1 -1
  89. package/lib-commonjs/components/LineChart/LineChart.js.map +1 -1
  90. package/lib-commonjs/components/ScatterChart/ScatterChart.js.map +1 -1
  91. package/lib-commonjs/components/VerticalBarChart/VerticalBarChart.js +11 -10
  92. package/lib-commonjs/components/VerticalBarChart/VerticalBarChart.js.map +1 -1
  93. package/lib-commonjs/components/VerticalStackedBarChart/VerticalStackedBarChart.js +56 -50
  94. package/lib-commonjs/components/VerticalStackedBarChart/VerticalStackedBarChart.js.map +1 -1
  95. package/lib-commonjs/index.js +1 -0
  96. package/lib-commonjs/index.js.map +1 -1
  97. package/lib-commonjs/types/DataPoint.js.map +1 -1
  98. package/lib-commonjs/utilities/FocusableTooltipText.js.map +1 -1
  99. package/lib-commonjs/utilities/utilities.js +29 -37
  100. package/lib-commonjs/utilities/utilities.js.map +1 -1
  101. package/lib-commonjs/utilities/vbc-utils.js +27 -0
  102. package/lib-commonjs/utilities/vbc-utils.js.map +1 -1
  103. package/package.json +9 -9
@@ -20,9 +20,6 @@ const _index1 = require("../../index");
20
20
  const _imageexportutils = require("../../utilities/image-export-utils");
21
21
  const MIN_DOMAIN_MARGIN = 8;
22
22
  const X1_INNER_PADDING = 0.1;
23
- // x1_inner_padding = space_between_bars / (space_between_bars + bar_width)
24
- // => space_between_bars = (x1_inner_padding / (1 - x1_inner_padding)) * bar_width
25
- /** Rate at which the space between the bars in a group changes wrt the bar width */ const BAR_GAP_RATE = X1_INNER_PADDING / (1 - X1_INNER_PADDING);
26
23
  const VERTICAL_BAR_GAP = 1;
27
24
  const MIN_BAR_HEIGHT = 1;
28
25
  const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
@@ -33,7 +30,6 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
33
30
  const _emptyChartId = (0, _reactutilities.useId)('_GVBC_empty');
34
31
  const _useRtl = (0, _index.useRtl)();
35
32
  let _domainMargin = MIN_DOMAIN_MARGIN;
36
- let _keys = [];
37
33
  let _xAxisLabels = [];
38
34
  let _datasetForBars = [];
39
35
  let _margins = {
@@ -43,13 +39,14 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
43
39
  left: 0
44
40
  };
45
41
  let _groupedVerticalBarGraph = [];
46
- let _refArray = [];
47
42
  let _yMax = 0;
48
43
  let _calloutAnchorPoint = null;
49
44
  let _barWidth = 0;
50
45
  let _groupWidth = 0;
51
46
  let _xAxisInnerPadding = 0;
52
47
  let _xAxisOuterPadding = 0;
48
+ let _legends = [];
49
+ let _legendColorMap = {};
53
50
  const cartesianChartRef = _react.useRef(null);
54
51
  const Y_ORIGIN = 0;
55
52
  const _legendsRef = _react.useRef(null);
@@ -93,44 +90,53 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
93
90
  _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth);
94
91
  // x0_inner_padding = space_between_groups / (space_between_groups + group_width)
95
92
  // space_between_groups = 2 * bar_width
96
- // group_width = keys.length * bar_width + (keys.length - 1) * space_between_bars
97
- _xAxisInnerPadding = (0, _index.getScalePadding)(props.xAxisInnerPadding, undefined, 2 / (2 + keys.length + (keys.length - 1) * BAR_GAP_RATE));
93
+ // group_width = _legends.length * bar_width + (_legends.length - 1) * space_between_bars
94
+ _xAxisInnerPadding = (0, _index.getScalePadding)(props.xAxisInnerPadding, undefined, 2 / (2 + (0, _index.calcTotalBandUnits)(_legends.length, X1_INNER_PADDING)));
98
95
  _xAxisOuterPadding = (0, _index.getScalePadding)(props.xAxisOuterPadding);
99
96
  };
100
97
  const _createDataset = (points)=>{
101
98
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
99
  const datasetForBars = [];
103
- const dataset = [];
104
100
  points.forEach((point, index)=>{
105
- const singleDatasetPoint = {};
106
101
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
102
  const singleDatasetPointForBars = {};
108
- const singleDataSeries = [];
109
- point.series.forEach((seriesPoint, seriesIndex)=>{
110
- singleDatasetPoint[seriesPoint.key] = seriesPoint.data;
111
- singleDatasetPointForBars[seriesPoint.key] = {
112
- ...seriesPoint
113
- };
114
- singleDataSeries.push(seriesPoint);
103
+ const legendToBarPoint = {};
104
+ point.series.forEach((seriesPoint)=>{
105
+ if (!singleDatasetPointForBars[seriesPoint.legend]) {
106
+ singleDatasetPointForBars[seriesPoint.legend] = [
107
+ {
108
+ ...seriesPoint
109
+ }
110
+ ];
111
+ legendToBarPoint[seriesPoint.legend] = {
112
+ ...seriesPoint
113
+ };
114
+ } else {
115
+ singleDatasetPointForBars[seriesPoint.legend].push({
116
+ ...seriesPoint
117
+ });
118
+ legendToBarPoint[seriesPoint.legend].data += seriesPoint.data;
119
+ }
115
120
  });
116
121
  singleDatasetPointForBars.xAxisPoint = point.name;
117
122
  singleDatasetPointForBars.indexNum = index;
118
- singleDatasetPointForBars.groupSeries = singleDataSeries;
123
+ singleDatasetPointForBars.groupSeries = Object.values(legendToBarPoint);
119
124
  singleDatasetPointForBars.stackCallOutAccessibilityData = point.stackCallOutAccessibilityData;
120
125
  datasetForBars.push(singleDatasetPointForBars);
121
- dataset.push(singleDatasetPoint);
122
126
  });
123
127
  return datasetForBars;
124
128
  };
125
129
  const _createDataSetOfGVBC = (points)=>{
126
- const keys = [];
130
+ const legends = new Set();
127
131
  const xAxisLabels = points.map((singlePoint)=>singlePoint.name);
128
- points[0].series.forEach((singleKey)=>{
129
- keys.push(singleKey.key);
132
+ points.forEach((point)=>{
133
+ point.series.forEach((seriesPoint)=>{
134
+ legends.add(seriesPoint.legend);
135
+ });
130
136
  });
131
137
  const datasetForBars = _createDataset(points);
132
138
  return {
133
- keys,
139
+ legends: Array.from(legends),
134
140
  xAxisLabels,
135
141
  datasetForBars
136
142
  };
@@ -146,36 +152,22 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
146
152
  props.legendProps.onChange(selectedLegends, event, currentLegend);
147
153
  }
148
154
  };
155
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
149
156
  const _getLegendData = (points)=>{
150
- const data = points;
151
- const defaultPalette = [
152
- (0, _index1.getColorFromToken)(_index1.DataVizPalette.color1),
153
- (0, _index1.getColorFromToken)(_index1.DataVizPalette.color2),
154
- (0, _index1.getColorFromToken)(_index1.DataVizPalette.color3),
155
- (0, _index1.getColorFromToken)(_index1.DataVizPalette.color4),
156
- (0, _index1.getColorFromToken)(_index1.DataVizPalette.color5)
157
- ];
158
157
  const actions = [];
159
- data.forEach((singleChartData)=>{
160
- singleChartData.series.forEach((point)=>{
161
- let color = point.color ? point.color : defaultPalette[Math.floor(Math.random() * 4 + 1)];
162
- const checkSimilarLegends = actions.filter((leg)=>leg.title === point.legend && leg.color === color);
163
- if (checkSimilarLegends.length > 0) {
164
- return;
158
+ _legends.forEach((legendTitle)=>{
159
+ const legend = {
160
+ title: legendTitle,
161
+ color: _legendColorMap[legendTitle][0],
162
+ hoverAction: ()=>{
163
+ _handleChartMouseLeave();
164
+ _onLegendHover(legendTitle);
165
+ },
166
+ onMouseOutAction: ()=>{
167
+ _onLegendLeave();
165
168
  }
166
- const legend = {
167
- title: point.legend,
168
- color,
169
- hoverAction: ()=>{
170
- _handleChartMouseLeave();
171
- _onLegendHover(point.legend);
172
- },
173
- onMouseOutAction: ()=>{
174
- _onLegendLeave();
175
- }
176
- };
177
- actions.push(legend);
178
- });
169
+ };
170
+ actions.push(legend);
179
171
  });
180
172
  return /*#__PURE__*/ _react.createElement(_index1.Legends, {
181
173
  legends: actions,
@@ -186,13 +178,40 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
186
178
  legendRef: _legendsRef
187
179
  });
188
180
  };
189
- const points = props.data;
190
- const { keys, xAxisLabels, datasetForBars } = _createDataSetOfGVBC(points);
191
- _keys = keys;
181
+ const _addDefaultColors = (data)=>{
182
+ _legendColorMap = {};
183
+ let colorIndex = 0;
184
+ var _data_map;
185
+ return (_data_map = data === null || data === void 0 ? void 0 : data.map((point)=>{
186
+ var _point_series;
187
+ var _point_series_map;
188
+ return {
189
+ ...point,
190
+ series: (_point_series_map = (_point_series = point.series) === null || _point_series === void 0 ? void 0 : _point_series.map((seriesPoint)=>{
191
+ if (!_legendColorMap[seriesPoint.legend]) {
192
+ let startColor = seriesPoint.color ? seriesPoint.color : (0, _index.getNextColor)(colorIndex, 0);
193
+ let endColor = startColor;
194
+ _legendColorMap[seriesPoint.legend] = [
195
+ startColor,
196
+ endColor
197
+ ];
198
+ colorIndex += 1;
199
+ }
200
+ return {
201
+ ...seriesPoint,
202
+ color: _legendColorMap[seriesPoint.legend][0]
203
+ };
204
+ })) !== null && _point_series_map !== void 0 ? _point_series_map : []
205
+ };
206
+ })) !== null && _data_map !== void 0 ? _data_map : [];
207
+ };
208
+ const points = _addDefaultColors(props.data);
209
+ const { legends, xAxisLabels, datasetForBars } = _createDataSetOfGVBC(points);
210
+ _legends = legends;
192
211
  _xAxisLabels = xAxisLabels;
193
212
  _datasetForBars = datasetForBars;
194
213
  const _xAxisType = (0, _index.getTypeOfAxis)(points[0].name, true);
195
- const legends = _getLegendData(points);
214
+ const legendBars = _getLegendData(points);
196
215
  _adjustProps();
197
216
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Add commentMore actions
198
217
  function _getMinMaxOfYAxis(datasetForBars, yAxisType, useSecondaryYScale) {
@@ -252,8 +271,8 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
252
271
  const xScale0 = _createX0Scale(containerWidth);
253
272
  // Setting the bar width here is safe because there are no dependencies earlier in the code
254
273
  // that rely on the width of bars in vertical bar charts with string x-axis.
255
- _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth, xScale0.bandwidth() / (_keys.length + (_keys.length - 1) * BAR_GAP_RATE));
256
- _groupWidth = (_keys.length + (_keys.length - 1) * BAR_GAP_RATE) * _barWidth;
274
+ _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth, (0, _index.calcBandwidth)(xScale0.bandwidth(), _legends.length, X1_INNER_PADDING));
275
+ _groupWidth = (0, _index.calcRequiredWidth)(_barWidth, _legends.length, X1_INNER_PADDING);
257
276
  const xScale1 = _createX1Scale();
258
277
  const allGroupsBars = [];
259
278
  _datasetForBars.forEach((singleSet)=>{
@@ -264,10 +283,6 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
264
283
  const _getMargins = (margins)=>{
265
284
  _margins = margins;
266
285
  };
267
- const _getOpacity = (legendTitle)=>{
268
- const opacity = _legendHighlighted(legendTitle) || _noLegendHighlighted() ? '' : '0.1';
269
- return opacity;
270
- };
271
286
  function updatePosition(newX, newY) {
272
287
  const threshold = 1; // Set a threshold for movement
273
288
  const { x, y } = clickPosition;
@@ -304,93 +319,99 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
304
319
  _calloutAnchorPoint = null;
305
320
  setPopoverOpen(false);
306
321
  };
307
- const onBarFocus = (event, pointData, groupData, refArrayIndexNumber)=>{
322
+ const onBarFocus = (event, pointData, groupData)=>{
308
323
  let x = 0;
309
324
  let y = 0;
310
325
  const targetRect = event.target.getBoundingClientRect();
311
326
  x = targetRect.left + targetRect.width / 2;
312
327
  y = targetRect.top + targetRect.height / 2;
313
328
  updatePosition(x, y);
314
- _refArray.forEach((obj, index)=>{
315
- if (obj.index === pointData.legend && refArrayIndexNumber === index) {
316
- setPopoverOpen(_noLegendHighlighted() || _legendHighlighted(pointData.legend));
317
- setCalloutLegend(pointData.legend);
318
- setDataForHoverCard(pointData.data);
319
- setColor(pointData.color);
320
- setXCalloutValue(pointData.xAxisCalloutData);
321
- setYCalloutValue(pointData.yAxisCalloutData);
322
- setCallOutAccessibilityData(props.isCalloutForStack ? groupData.stackCallOutAccessibilityData : pointData.callOutAccessibilityData);
323
- setYValueHover(groupData.groupSeries);
324
- setHoverXValue(pointData.xAxisCalloutData);
325
- }
326
- });
327
- };
328
- const _refCallback = (element, legendTitle, refIndexNumber)=>{
329
- _refArray[refIndexNumber] = {
330
- index: legendTitle,
331
- refElement: element
332
- };
329
+ setPopoverOpen(_noLegendHighlighted() || _legendHighlighted(pointData.legend));
330
+ setCalloutLegend(pointData.legend);
331
+ setDataForHoverCard(pointData.data);
332
+ setColor(pointData.color);
333
+ setXCalloutValue(pointData.xAxisCalloutData);
334
+ setYCalloutValue(pointData.yAxisCalloutData);
335
+ setCallOutAccessibilityData(props.isCalloutForStack ? groupData.stackCallOutAccessibilityData : pointData.callOutAccessibilityData);
336
+ setYValueHover(groupData.groupSeries);
337
+ setHoverXValue(pointData.xAxisCalloutData);
333
338
  };
334
339
  const _buildGraph = (singleSet, xScale0, xScale1, yScalePrimary, yScaleSecondary, containerHeight, xElement)=>{
340
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
335
341
  const singleGroup = [];
342
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
336
343
  const barLabelsForGroup = [];
337
- const tempDataSet = Object.keys(datasetForBars[0]).splice(0, keys.length);
338
- tempDataSet.forEach((datasetKey, index)=>{
339
- const refIndexNumber = singleSet.indexNum * tempDataSet.length + index;
340
- const pointData = singleSet[datasetKey];
341
- const yBarScale = pointData.useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary;
342
- if (pointData) {
343
- // To align the centers of the generated bandwidth and the calculated one when they differ,
344
- // use the following addend.
345
- const xPoint = xScale1(datasetKey) + (xScale1.bandwidth() - _barWidth) / 2;
346
- const startColor = pointData.color ? pointData.color : (0, _index.getNextColor)(index, 0);
344
+ // Get the actual legends present at this x-axis point
345
+ const presentLegends = Object.keys(singleSet).filter((key)=>key in _legendColorMap);
346
+ const effectiveGroupWidth = (0, _index.calcRequiredWidth)(_barWidth, presentLegends.length, X1_INNER_PADDING);
347
+ // For stacked bars, center the single bar group in the available space
348
+ // Instead of using the global legend position, use the local position within present legends
349
+ const localScale = (0, _d3scale.scaleBand)().domain(presentLegends).range(_useRtl ? [
350
+ effectiveGroupWidth,
351
+ 0
352
+ ] : [
353
+ 0,
354
+ effectiveGroupWidth
355
+ ]).paddingInner(X1_INNER_PADDING);
356
+ _legends.forEach((legendTitle, legendIndex)=>{
357
+ const barPoints = singleSet[legendTitle];
358
+ if (barPoints) {
359
+ const yBarScale = barPoints[0].useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary;
360
+ var _localScale;
361
+ const xPoint = ((_localScale = localScale(legendTitle)) !== null && _localScale !== void 0 ? _localScale : 0) + (localScale.bandwidth() - _barWidth) / 2;
362
+ const isLegendActive = _legendHighlighted(legendTitle) || _noLegendHighlighted();
363
+ const barOpacity = isLegendActive ? '' : '0.1';
364
+ let barTotalValue = 0;
347
365
  const yBaseline = yBarScale(Y_ORIGIN);
348
366
  let yPositiveStart = yBaseline;
349
367
  let yNegativeStart = yBaseline;
350
368
  let yPoint = Y_ORIGIN;
351
- const barGap = VERTICAL_BAR_GAP / 2 * (index > 0 ? 2 : 0);
352
- const height = Math.max(yBarScale(Y_ORIGIN) - yBarScale(Math.abs(pointData.data)), MIN_BAR_HEIGHT);
353
- if (pointData.data >= Y_ORIGIN) {
354
- yPositiveStart -= height + barGap;
355
- yPoint = yPositiveStart;
356
- } else {
357
- yPoint = yNegativeStart + barGap;
358
- yNegativeStart = yPoint + height;
359
- }
360
- // Not rendering data with 0.
361
- pointData.data && singleGroup.push(/*#__PURE__*/ _react.createElement(_react.Fragment, {
362
- key: `${singleSet.indexNum}-${index}`
363
- }, /*#__PURE__*/ _react.createElement("rect", {
364
- className: classes.opacityChangeOnHover,
365
- height: height,
366
- width: _barWidth,
367
- x: xPoint,
368
- y: yPoint,
369
- opacity: _getOpacity(pointData.legend),
370
- ref: (e)=>{
371
- _refCallback(e, pointData.legend, refIndexNumber);
372
- },
373
- fill: startColor,
374
- rx: 0,
375
- onMouseOver: (event)=>onBarHover(pointData, singleSet, event),
376
- onMouseMove: (event)=>onBarHover(pointData, singleSet, event),
377
- onMouseOut: _onBarLeave,
378
- onFocus: (event)=>onBarFocus(event, pointData, singleSet, refIndexNumber),
379
- onBlur: _onBarLeave,
380
- onClick: pointData.onClick,
381
- "aria-label": getAriaLabel(pointData, singleSet.xAxisPoint),
382
- tabIndex: _legendHighlighted(pointData.legend) || _noLegendHighlighted() ? 0 : undefined,
383
- role: "img"
384
- })));
385
- if (pointData.data && !props.hideLabels && _barWidth >= 16 && (_legendHighlighted(pointData.legend) || _noLegendHighlighted())) {
369
+ barPoints.forEach((pointData, pointIndex)=>{
370
+ if (!pointData.data) {
371
+ // Not rendering data with 0.
372
+ return;
373
+ }
374
+ const barGap = VERTICAL_BAR_GAP / 2 * (pointIndex > 0 ? 2 : 0);
375
+ const height = Math.max(yBarScale(Y_ORIGIN) - yBarScale(Math.abs(pointData.data)), MIN_BAR_HEIGHT);
376
+ const pointColor = pointData.color; // Use the color of the current point
377
+ if (pointData.data >= Y_ORIGIN) {
378
+ yPositiveStart -= height + barGap;
379
+ yPoint = yPositiveStart;
380
+ } else {
381
+ yPoint = yNegativeStart + barGap;
382
+ yNegativeStart = yPoint + height;
383
+ }
384
+ singleGroup.push(/*#__PURE__*/ _react.createElement("rect", {
385
+ key: `${singleSet.indexNum}-${legendIndex}-${pointIndex}`,
386
+ className: classes.opacityChangeOnHover,
387
+ height: height,
388
+ width: _barWidth,
389
+ x: xPoint,
390
+ y: yPoint,
391
+ opacity: barOpacity,
392
+ fill: pointColor,
393
+ rx: 0,
394
+ onMouseOver: (event)=>onBarHover(pointData, singleSet, event),
395
+ onMouseMove: (event)=>onBarHover(pointData, singleSet, event),
396
+ onMouseOut: _onBarLeave,
397
+ onFocus: (event)=>onBarFocus(event, pointData, singleSet),
398
+ onBlur: _onBarLeave,
399
+ onClick: pointData.onClick,
400
+ "aria-label": getAriaLabel(pointData, singleSet.xAxisPoint),
401
+ tabIndex: _legendHighlighted(pointData.legend) || _noLegendHighlighted() ? 0 : undefined,
402
+ role: "img"
403
+ }));
404
+ barTotalValue += pointData.data;
405
+ });
406
+ if (barTotalValue !== null && !props.hideLabels && _barWidth >= 16 && isLegendActive) {
386
407
  barLabelsForGroup.push(/*#__PURE__*/ _react.createElement("text", {
387
- key: `${singleSet.indexNum}-${index}`,
408
+ key: `${singleSet.indexNum}-${legendIndex}`,
388
409
  x: xPoint + _barWidth / 2,
389
- y: pointData.data >= Y_ORIGIN ? yPositiveStart - 6 : yNegativeStart + 12,
410
+ y: barTotalValue >= Y_ORIGIN ? yPositiveStart - 6 : yNegativeStart + 12,
390
411
  textAnchor: "middle",
391
412
  className: classes.barLabel,
392
413
  "aria-hidden": true
393
- }, typeof props.yAxisTickFormat === 'function' ? props.yAxisTickFormat(pointData.data) : (0, _index.formatScientificLimitWidth)(pointData.data)));
414
+ }, typeof props.yAxisTickFormat === 'function' ? props.yAxisTickFormat(barTotalValue) : (0, _index.formatScientificLimitWidth)(barTotalValue)));
394
415
  }
395
416
  }
396
417
  });
@@ -410,7 +431,7 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
410
431
  }
411
432
  return /*#__PURE__*/ _react.createElement("g", {
412
433
  key: singleSet.indexNum,
413
- transform: `translate(${xScale0(singleSet.xAxisPoint) + (xScale0.bandwidth() - _groupWidth) / 2}, 0)`
434
+ transform: `translate(${xScale0(singleSet.xAxisPoint) + (xScale0.bandwidth() - effectiveGroupWidth) / 2}, 0)`
414
435
  }, singleGroup, barLabelsForGroup);
415
436
  };
416
437
  // For grouped vertical bar chart, First need to define total scale (from start to end)
@@ -427,7 +448,7 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
427
448
  };
428
449
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
429
450
  const _createX1Scale = ()=>{
430
- return (0, _d3scale.scaleBand)().domain(keys) // When there is only one group, xScale0 adds padding around it,
451
+ return (0, _d3scale.scaleBand)().domain(_legends) // When there is only one group, xScale0 adds padding around it,
431
452
  // causing the bandwidth to become smaller than the actual group width.
432
453
  // So to render bars in the group correctly, use groupWidth instead of the generated scale bandwidth.
433
454
  .range(_useRtl ? [
@@ -478,8 +499,7 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
478
499
  return ((_point_callOutAccessibilityData = point.callOutAccessibilityData) === null || _point_callOutAccessibilityData === void 0 ? void 0 : _point_callOutAccessibilityData.ariaLabel) || `${xValue}. ${legend}, ${yValue}.`;
479
500
  };
480
501
  const _getDomainMargins = (containerWidth)=>{
481
- /** Total width available to render the bars */ const totalWidth = containerWidth - (_margins.left + MIN_DOMAIN_MARGIN) - (_margins.right + MIN_DOMAIN_MARGIN);
482
- /** Rate at which the space between the groups changes wrt the group width */ const groupGapRate = _xAxisInnerPadding / (1 - _xAxisInnerPadding);
502
+ /** Total width available to render the bars */ const totalWidth = (0, _index.calcTotalWidth)(containerWidth, _margins, MIN_DOMAIN_MARGIN);
483
503
  if (_xAxisType === _index.XAxisTypes.StringAxis) {
484
504
  if ((0, _index.isScalePaddingDefined)(props.xAxisOuterPadding)) {
485
505
  // Setting the domain margin for string x-axis to 0 because the xAxisOuterPadding prop is now available
@@ -489,19 +509,19 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
489
509
  // Update the bar width so that when CartesianChart rerenders,
490
510
  // the following calculations don't use the previous bar width.
491
511
  _barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth);
492
- const groupWidth = (keys.length + (keys.length - 1) * BAR_GAP_RATE) * _barWidth;
493
- /** Total width required to render the groups. Directly proportional to group width */ const reqWidth = (xAxisLabels.length + (xAxisLabels.length - 1) * groupGapRate) * groupWidth;
512
+ const groupWidth = (0, _index.calcRequiredWidth)(_barWidth, _legends.length, X1_INNER_PADDING);
513
+ /** Total width required to render the groups. Directly proportional to group width */ const reqWidth = (0, _index.calcRequiredWidth)(groupWidth, _xAxisLabels.length, _xAxisInnerPadding);
494
514
  if (totalWidth >= reqWidth) {
495
515
  // Center align the chart by setting equal left and right margins for domain
496
516
  _domainMargin = MIN_DOMAIN_MARGIN + (totalWidth - reqWidth) / 2;
497
517
  }
498
518
  } else if (props.mode === 'plotly' && xAxisLabels.length > 1) {
499
519
  // Calculate the remaining width after rendering groups at their maximum allowable width
500
- const groupBandwidth = totalWidth / (xAxisLabels.length + (xAxisLabels.length - 1) * groupGapRate);
501
- const barBandwidth = groupBandwidth / (keys.length + (keys.length - 1) * BAR_GAP_RATE);
520
+ const groupBandwidth = (0, _index.calcBandwidth)(totalWidth, _xAxisLabels.length, _xAxisInnerPadding);
521
+ const barBandwidth = (0, _index.calcBandwidth)(groupBandwidth, _legends.length, X1_INNER_PADDING);
502
522
  const barWidth = (0, _index.getBarWidth)(props.barWidth, props.maxBarWidth, barBandwidth);
503
- const groupWidth = (keys.length + (keys.length - 1) * BAR_GAP_RATE) * barWidth;
504
- let reqWidth = (xAxisLabels.length + (xAxisLabels.length - 1) * groupGapRate) * groupWidth;
523
+ const groupWidth = (0, _index.calcRequiredWidth)(barWidth, _legends.length, X1_INNER_PADDING);
524
+ let reqWidth = (0, _index.calcRequiredWidth)(groupWidth, _xAxisLabels.length, _xAxisInnerPadding);
505
525
  const margin1 = (totalWidth - reqWidth) / 2;
506
526
  let margin2 = Number.POSITIVE_INFINITY;
507
527
  if (!props.hideTickOverlap) {
@@ -523,7 +543,7 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
523
543
  return !(props.data && props.data.length > 0 && props.data.filter((item)=>item.series.length).length > 0);
524
544
  };
525
545
  const _getChartTitle = ()=>{
526
- return (props.chartTitle ? `${props.chartTitle}. ` : '') + `Vertical bar chart with ${_xAxisLabels.length} groups of ${_keys.length} bars each. `;
546
+ return (props.chartTitle ? `${props.chartTitle}. ` : '') + `Vertical bar chart with ${_xAxisLabels.length} groups of ${_legends.length} bars each. `;
527
547
  };
528
548
  return !_isChartEmpty() ? /*#__PURE__*/ _react.createElement(_index1.CartesianChart, {
529
549
  ...props,
@@ -534,7 +554,7 @@ const GroupedVerticalBarChart = /*#__PURE__*/ _react.forwardRef((props = {
534
554
  getMinMaxOfYAxis: _getMinMaxOfYAxis,
535
555
  createStringYAxis: _index.createStringYAxis,
536
556
  calloutProps: calloutProps,
537
- legendBars: legends,
557
+ legendBars: legendBars,
538
558
  xAxisType: _xAxisType,
539
559
  createYAxis: _index.createNumericYAxis,
540
560
  datasetForXAxisDomain: _xAxisLabels,