@mui/x-charts 6.0.0-alpha.7 → 6.0.0-alpha.9

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 (92) hide show
  1. package/BarChart/BarChart.d.ts +1 -0
  2. package/BarChart/BarChart.js +26 -13
  3. package/BarChart/BarPlot.js +25 -15
  4. package/BarChart/extremums.js +19 -3
  5. package/BarChart/formatter.js +3 -1
  6. package/CHANGELOG.md +212 -31
  7. package/ChartsAxisHighlight/ChartsAxisHighlight.d.ts +3 -2
  8. package/ChartsAxisHighlight/ChartsAxisHighlight.js +11 -3
  9. package/ChartsTooltip/ChartsAxisTooltipContent.js +13 -9
  10. package/LineChart/AreaElement.d.ts +1 -1
  11. package/LineChart/LineChart.d.ts +7 -2
  12. package/LineChart/LineChart.js +15 -3
  13. package/LineChart/LineElement.d.ts +1 -1
  14. package/LineChart/LineHighlightElement.d.ts +21 -0
  15. package/LineChart/LineHighlightElement.js +79 -0
  16. package/LineChart/LineHighlightPlot.d.ts +25 -0
  17. package/LineChart/LineHighlightPlot.js +99 -0
  18. package/LineChart/MarkElement.d.ts +1 -1
  19. package/LineChart/MarkElement.js +1 -4
  20. package/LineChart/MarkPlot.js +34 -15
  21. package/LineChart/index.d.ts +2 -0
  22. package/LineChart/index.js +20 -0
  23. package/PieChart/PieArc.d.ts +1 -1
  24. package/PieChart/PieArcLabel.d.ts +1 -1
  25. package/PieChart/PieChart.js +1 -1
  26. package/ScatterChart/ScatterChart.js +1 -1
  27. package/SparkLineChart/SparkLineChart.d.ts +3 -2
  28. package/SparkLineChart/SparkLineChart.js +7 -12
  29. package/context/CartesianContextProvider.js +4 -4
  30. package/esm/BarChart/BarChart.js +30 -17
  31. package/esm/BarChart/BarPlot.js +26 -16
  32. package/esm/BarChart/extremums.js +18 -2
  33. package/esm/BarChart/formatter.js +3 -1
  34. package/esm/ChartsAxisHighlight/ChartsAxisHighlight.js +11 -3
  35. package/esm/ChartsTooltip/ChartsAxisTooltipContent.js +13 -9
  36. package/esm/LineChart/LineChart.js +15 -3
  37. package/esm/LineChart/LineHighlightElement.js +68 -0
  38. package/esm/LineChart/LineHighlightPlot.js +92 -0
  39. package/esm/LineChart/MarkElement.js +1 -4
  40. package/esm/LineChart/MarkPlot.js +34 -15
  41. package/esm/LineChart/index.js +3 -1
  42. package/esm/PieChart/PieChart.js +1 -1
  43. package/esm/ScatterChart/ScatterChart.js +1 -1
  44. package/esm/SparkLineChart/SparkLineChart.js +8 -13
  45. package/esm/context/CartesianContextProvider.js +4 -4
  46. package/esm/hooks/useAxisEvents.js +21 -38
  47. package/esm/hooks/useTicks.js +2 -2
  48. package/hooks/useAxisEvents.js +21 -38
  49. package/hooks/useTicks.js +2 -2
  50. package/index.js +1 -1
  51. package/internals/defaultizeColor.d.ts +3 -0
  52. package/legacy/BarChart/BarChart.js +35 -20
  53. package/legacy/BarChart/BarPlot.js +26 -16
  54. package/legacy/BarChart/extremums.js +22 -2
  55. package/legacy/BarChart/formatter.js +3 -1
  56. package/legacy/ChartsAxisHighlight/ChartsAxisHighlight.js +11 -3
  57. package/legacy/ChartsTooltip/ChartsAxisTooltipContent.js +13 -9
  58. package/legacy/LineChart/LineChart.js +15 -3
  59. package/legacy/LineChart/LineHighlightElement.js +67 -0
  60. package/legacy/LineChart/LineHighlightPlot.js +85 -0
  61. package/legacy/LineChart/MarkElement.js +1 -4
  62. package/legacy/LineChart/MarkPlot.js +24 -6
  63. package/legacy/LineChart/index.js +3 -1
  64. package/legacy/PieChart/PieChart.js +1 -1
  65. package/legacy/ScatterChart/ScatterChart.js +1 -1
  66. package/legacy/SparkLineChart/SparkLineChart.js +8 -11
  67. package/legacy/context/CartesianContextProvider.js +4 -4
  68. package/legacy/hooks/useAxisEvents.js +21 -37
  69. package/legacy/hooks/useTicks.js +2 -2
  70. package/legacy/index.js +1 -1
  71. package/models/seriesType/bar.d.ts +6 -1
  72. package/models/seriesType/line.d.ts +33 -0
  73. package/modern/BarChart/BarChart.js +27 -14
  74. package/modern/BarChart/BarPlot.js +25 -15
  75. package/modern/BarChart/extremums.js +18 -2
  76. package/modern/BarChart/formatter.js +3 -1
  77. package/modern/ChartsAxisHighlight/ChartsAxisHighlight.js +11 -3
  78. package/modern/ChartsTooltip/ChartsAxisTooltipContent.js +13 -9
  79. package/modern/LineChart/LineChart.js +15 -3
  80. package/modern/LineChart/LineHighlightElement.js +68 -0
  81. package/modern/LineChart/LineHighlightPlot.js +91 -0
  82. package/modern/LineChart/MarkElement.js +1 -4
  83. package/modern/LineChart/MarkPlot.js +34 -15
  84. package/modern/LineChart/index.js +3 -1
  85. package/modern/PieChart/PieChart.js +1 -1
  86. package/modern/ScatterChart/ScatterChart.js +1 -1
  87. package/modern/SparkLineChart/SparkLineChart.js +8 -13
  88. package/modern/context/CartesianContextProvider.js +4 -4
  89. package/modern/hooks/useAxisEvents.js +21 -38
  90. package/modern/hooks/useTicks.js +2 -2
  91. package/modern/index.js +1 -1
  92. package/package.json +4 -4
@@ -1,4 +1,6 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
+ const _excluded = ["slots", "slotProps"];
2
4
  import * as React from 'react';
3
5
  import PropTypes from 'prop-types';
4
6
  import { SeriesContext } from '../context/SeriesContextProvider';
@@ -9,9 +11,10 @@ import { jsx as _jsx } from "react/jsx-runtime";
9
11
  function MarkPlot(props) {
10
12
  var _slots$mark;
11
13
  const {
12
- slots,
13
- slotProps
14
- } = props;
14
+ slots,
15
+ slotProps
16
+ } = props,
17
+ other = _objectWithoutPropertiesLoose(props, _excluded);
15
18
  const seriesData = React.useContext(SeriesContext).line;
16
19
  const axisData = React.useContext(CartesianContext);
17
20
  const Mark = (_slots$mark = slots == null ? void 0 : slots.mark) != null ? _slots$mark : MarkElement;
@@ -30,7 +33,7 @@ function MarkPlot(props) {
30
33
  } = axisData;
31
34
  const defaultXAxisId = xAxisIds[0];
32
35
  const defaultYAxisId = yAxisIds[0];
33
- return /*#__PURE__*/_jsx("g", _extends({}, props, {
36
+ return /*#__PURE__*/_jsx("g", _extends({}, other, {
34
37
  children: stackingGroups.flatMap(({
35
38
  ids: groupIds
36
39
  }) => {
@@ -38,8 +41,12 @@ function MarkPlot(props) {
38
41
  const {
39
42
  xAxisKey = defaultXAxisId,
40
43
  yAxisKey = defaultYAxisId,
41
- stackedData
44
+ stackedData,
45
+ showMark = true
42
46
  } = series[seriesId];
47
+ if (showMark === false) {
48
+ return null;
49
+ }
43
50
  const xScale = getValueToPositionMapper(xAxis[xAxisKey].scale);
44
51
  const yScale = yAxis[yAxisKey].scale;
45
52
  const xData = xAxis[xAxisKey].data;
@@ -65,21 +72,33 @@ function MarkPlot(props) {
65
72
  return {
66
73
  x: xScale(x),
67
74
  y: yScale(y),
75
+ position: x,
76
+ value: y,
68
77
  index
69
78
  };
70
79
  }).filter(isInRange).map(({
71
80
  x,
72
81
  y,
73
- index
74
- }) => /*#__PURE__*/_jsx(Mark, _extends({
75
- id: seriesId,
76
- dataIndex: index,
77
- shape: "circle",
78
- color: series[seriesId].color,
79
- x: x,
80
- y: y,
81
- highlightScope: series[seriesId].highlightScope
82
- }, slotProps == null ? void 0 : slotProps.mark), `${seriesId}-${index}`));
82
+ index,
83
+ position,
84
+ value
85
+ }) => {
86
+ return showMark === true || showMark({
87
+ x,
88
+ y,
89
+ index,
90
+ position,
91
+ value
92
+ }) ? /*#__PURE__*/_jsx(Mark, _extends({
93
+ id: seriesId,
94
+ dataIndex: index,
95
+ shape: "circle",
96
+ color: series[seriesId].color,
97
+ x: x,
98
+ y: y,
99
+ highlightScope: series[seriesId].highlightScope
100
+ }, slotProps == null ? void 0 : slotProps.mark), `${seriesId}-${index}`) : null;
101
+ });
83
102
  });
84
103
  })
85
104
  }));
@@ -1,7 +1,9 @@
1
1
  export { LinePlot } from './LinePlot';
2
2
  export { AreaPlot } from './AreaPlot';
3
3
  export { MarkPlot } from './MarkPlot';
4
+ export { LineHighlightPlot } from './LineHighlightPlot';
4
5
  export { LineChart } from './LineChart';
5
6
  export * from './AreaElement';
6
7
  export * from './LineElement';
7
- export * from './MarkElement';
8
+ export * from './MarkElement';
9
+ export * from './LineHighlightElement';
@@ -88,7 +88,7 @@ process.env.NODE_ENV !== "production" ? PieChart.propTypes = {
88
88
  // ----------------------------------------------------------------------
89
89
  axisHighlight: PropTypes.shape({
90
90
  x: PropTypes.oneOf(['band', 'line', 'none']),
91
- y: PropTypes.oneOf(['line', 'none'])
91
+ y: PropTypes.oneOf(['band', 'line', 'none'])
92
92
  }),
93
93
  /**
94
94
  * Indicate which axis to display the bottom of the charts.
@@ -70,7 +70,7 @@ process.env.NODE_ENV !== "production" ? ScatterChart.propTypes = {
70
70
  // ----------------------------------------------------------------------
71
71
  axisHighlight: PropTypes.shape({
72
72
  x: PropTypes.oneOf(['band', 'line', 'none']),
73
- y: PropTypes.oneOf(['line', 'none'])
73
+ y: PropTypes.oneOf(['band', 'line', 'none'])
74
74
  }),
75
75
  /**
76
76
  * Indicate which axis to display the bottom of the charts.
@@ -1,23 +1,14 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import * as React from 'react';
3
3
  import PropTypes from 'prop-types';
4
- import { styled } from '@mui/material/styles';
5
4
  import { BarPlot } from '../BarChart';
6
- import { LinePlot, MarkPlot, AreaPlot, markElementClasses } from '../LineChart';
5
+ import { LinePlot, AreaPlot, LineHighlightPlot } from '../LineChart';
7
6
  import { ResponsiveChartContainer } from '../ResponsiveChartContainer';
8
7
  import { DEFAULT_X_AXIS_KEY } from '../constants';
9
8
  import { ChartsTooltip } from '../ChartsTooltip';
10
9
  import { ChartsAxisHighlight } from '../ChartsAxisHighlight';
11
10
  import { jsx as _jsx } from "react/jsx-runtime";
12
11
  import { jsxs as _jsxs } from "react/jsx-runtime";
13
- const SparkLineMarkPlot = styled(MarkPlot)({
14
- [`& .${markElementClasses.root}`]: {
15
- display: 'none',
16
- [`&.${markElementClasses.highlighted}`]: {
17
- display: 'inherit'
18
- }
19
- }
20
- });
21
12
  const SPARKLINE_DEFAULT_MARGIN = {
22
13
  top: 5,
23
14
  bottom: 5,
@@ -59,7 +50,8 @@ const SparkLineChart = /*#__PURE__*/React.forwardRef(function SparkLineChart(pro
59
50
  valueFormatter
60
51
  }, plotType === 'bar' ? {} : {
61
52
  area,
62
- curve
53
+ curve,
54
+ disableHighlight: !showHighlight
63
55
  })],
64
56
  width: width,
65
57
  height: height,
@@ -85,7 +77,10 @@ const SparkLineChart = /*#__PURE__*/React.forwardRef(function SparkLineChart(pro
85
77
  }), /*#__PURE__*/_jsx(LinePlot, {
86
78
  slots: slots,
87
79
  slotProps: slotProps
88
- }), showHighlight && /*#__PURE__*/_jsx(SparkLineMarkPlot, {})]
80
+ }), /*#__PURE__*/_jsx(LineHighlightPlot, {
81
+ slots: slots,
82
+ slotProps: slotProps
83
+ })]
89
84
  }), /*#__PURE__*/_jsx(ChartsAxisHighlight, _extends({}, axisHighlight)), showTooltip && /*#__PURE__*/_jsx(ChartsTooltip, _extends({}, tooltip)), children]
90
85
  });
91
86
  });
@@ -102,7 +97,7 @@ process.env.NODE_ENV !== "production" ? SparkLineChart.propTypes = {
102
97
  area: PropTypes.bool,
103
98
  axisHighlight: PropTypes.shape({
104
99
  x: PropTypes.oneOf(['band', 'line', 'none']),
105
- y: PropTypes.oneOf(['line', 'none'])
100
+ y: PropTypes.oneOf(['band', 'line', 'none'])
106
101
  }),
107
102
  children: PropTypes.node,
108
103
  className: PropTypes.string,
@@ -156,17 +156,17 @@ function CartesianContextProvider({
156
156
  if (isBandScaleConfig(axis)) {
157
157
  var _axis$categoryGapRati2;
158
158
  const categoryGapRatio = (_axis$categoryGapRati2 = axis.categoryGapRatio) != null ? _axis$categoryGapRati2 : DEFAULT_CATEGORY_GAP_RATIO;
159
- completedXAxis[axis.id] = _extends({
159
+ completedYAxis[axis.id] = _extends({
160
160
  categoryGapRatio,
161
161
  barGapRatio: 0
162
162
  }, axis, {
163
- scale: scaleBand(axis.data, range).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
163
+ scale: scaleBand(axis.data, [range[1], range[0]]).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
164
164
  ticksNumber: axis.data.length
165
165
  });
166
166
  }
167
167
  if (isPointScaleConfig(axis)) {
168
- completedXAxis[axis.id] = _extends({}, axis, {
169
- scale: scalePoint(axis.data, range),
168
+ completedYAxis[axis.id] = _extends({}, axis, {
169
+ scale: scalePoint(axis.data, [range[1], range[0]]),
170
170
  ticksNumber: axis.data.length
171
171
  });
172
172
  }
@@ -33,66 +33,49 @@ export const useAxisEvents = disableAxisListener => {
33
33
  if (element === null || disableAxisListener) {
34
34
  return () => {};
35
35
  }
36
- const getUpdateY = y => {
37
- if (usedYAxis === null) {
38
- return null;
39
- }
40
- const {
41
- scale: yScale,
42
- data: yAxisData
43
- } = yAxis[usedYAxis];
44
- if (!isBandScale(yScale)) {
45
- return {
46
- value: yScale.invert(y)
47
- };
48
- }
49
- const dataIndex = Math.floor((y - yScale.range()[0]) / yScale.step());
50
- if (dataIndex < 0 || dataIndex >= yAxisData.length) {
51
- return null;
52
- }
53
- return {
54
- index: dataIndex,
55
- value: yAxisData[dataIndex]
56
- };
57
- };
58
- const getUpdateX = x => {
36
+ const getUpdate = (axisConfig, mouseValue) => {
59
37
  if (usedXAxis === null) {
60
38
  return null;
61
39
  }
62
40
  const {
63
- scale: xScale,
64
- data: xAxisData
65
- } = xAxis[usedXAxis];
66
- if (!isBandScale(xScale)) {
67
- const value = xScale.invert(x);
68
- const closestIndex = xAxisData == null ? void 0 : xAxisData.findIndex((v, index) => {
41
+ scale,
42
+ data: axisData
43
+ } = axisConfig;
44
+ if (!isBandScale(scale)) {
45
+ const value = scale.invert(mouseValue);
46
+ if (axisData === undefined) {
47
+ return {
48
+ value
49
+ };
50
+ }
51
+ const closestIndex = axisData == null ? void 0 : axisData.findIndex((v, index) => {
69
52
  if (v > value) {
70
53
  // @ts-ignore
71
- if (index === 0 || Math.abs(value - v) <= Math.abs(value - xAxisData[index - 1])) {
54
+ if (index === 0 || Math.abs(value - v) <= Math.abs(value - axisData[index - 1])) {
72
55
  return true;
73
56
  }
74
57
  }
75
58
  if (v <= value) {
76
- if (index === xAxisData.length - 1 ||
59
+ if (index === axisData.length - 1 ||
77
60
  // @ts-ignore
78
- Math.abs(value - v) < Math.abs(value - xAxisData[index + 1])) {
61
+ Math.abs(value - v) < Math.abs(value - axisData[index + 1])) {
79
62
  return true;
80
63
  }
81
64
  }
82
65
  return false;
83
66
  });
84
67
  return {
85
- value: closestIndex !== undefined && closestIndex >= 0 ? xAxisData[closestIndex] : value,
68
+ value: closestIndex !== undefined && closestIndex >= 0 ? axisData[closestIndex] : value,
86
69
  index: closestIndex
87
70
  };
88
71
  }
89
- const dataIndex = xScale.bandwidth() === 0 ? Math.floor((x - xScale.range()[0] + xScale.step() / 2) / xScale.step()) : Math.floor((x - xScale.range()[0]) / xScale.step());
90
- if (dataIndex < 0 || dataIndex >= xAxisData.length) {
72
+ const dataIndex = scale.bandwidth() === 0 ? Math.floor((mouseValue - Math.min(...scale.range()) + scale.step() / 2) / scale.step()) : Math.floor((mouseValue - Math.min(...scale.range())) / scale.step());
73
+ if (dataIndex < 0 || dataIndex >= axisData.length) {
91
74
  return null;
92
75
  }
93
76
  return {
94
77
  index: dataIndex,
95
- value: xAxisData[dataIndex]
78
+ value: axisData[dataIndex]
96
79
  };
97
80
  };
98
81
  const handleMouseOut = () => {
@@ -130,8 +113,8 @@ export const useAxisEvents = disableAxisListener => {
130
113
  });
131
114
  return;
132
115
  }
133
- const newStateX = getUpdateX(svgPt.x);
134
- const newStateY = getUpdateY(svgPt.y);
116
+ const newStateX = getUpdate(xAxis[usedXAxis], svgPt.x);
117
+ const newStateY = getUpdate(yAxis[usedYAxis], svgPt.y);
135
118
  dispatch({
136
119
  type: 'updateAxis',
137
120
  data: {
@@ -25,11 +25,11 @@ function useTicks(options) {
25
25
  const domain = scale.domain();
26
26
  if (scale.bandwidth() > 0) {
27
27
  // scale type = 'band'
28
- return [...domain.map((value, index) => {
28
+ return [...domain.map(value => {
29
29
  var _valueFormatter;
30
30
  return {
31
31
  formattedValue: (_valueFormatter = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter : value,
32
- offset: index === 0 ? scale.range()[0] : scale(value) - (scale.step() - scale.bandwidth()) / 2,
32
+ offset: scale(value) - (scale.step() - scale.bandwidth()) / 2,
33
33
  labelOffset: scale.step() / 2
34
34
  };
35
35
  }), {
@@ -41,66 +41,49 @@ const useAxisEvents = disableAxisListener => {
41
41
  if (element === null || disableAxisListener) {
42
42
  return () => {};
43
43
  }
44
- const getUpdateY = y => {
45
- if (usedYAxis === null) {
46
- return null;
47
- }
48
- const {
49
- scale: yScale,
50
- data: yAxisData
51
- } = yAxis[usedYAxis];
52
- if (!(0, _isBandScale.isBandScale)(yScale)) {
53
- return {
54
- value: yScale.invert(y)
55
- };
56
- }
57
- const dataIndex = Math.floor((y - yScale.range()[0]) / yScale.step());
58
- if (dataIndex < 0 || dataIndex >= yAxisData.length) {
59
- return null;
60
- }
61
- return {
62
- index: dataIndex,
63
- value: yAxisData[dataIndex]
64
- };
65
- };
66
- const getUpdateX = x => {
44
+ const getUpdate = (axisConfig, mouseValue) => {
67
45
  if (usedXAxis === null) {
68
46
  return null;
69
47
  }
70
48
  const {
71
- scale: xScale,
72
- data: xAxisData
73
- } = xAxis[usedXAxis];
74
- if (!(0, _isBandScale.isBandScale)(xScale)) {
75
- const value = xScale.invert(x);
76
- const closestIndex = xAxisData?.findIndex((v, index) => {
49
+ scale,
50
+ data: axisData
51
+ } = axisConfig;
52
+ if (!(0, _isBandScale.isBandScale)(scale)) {
53
+ const value = scale.invert(mouseValue);
54
+ if (axisData === undefined) {
55
+ return {
56
+ value
57
+ };
58
+ }
59
+ const closestIndex = axisData?.findIndex((v, index) => {
77
60
  if (v > value) {
78
61
  // @ts-ignore
79
- if (index === 0 || Math.abs(value - v) <= Math.abs(value - xAxisData[index - 1])) {
62
+ if (index === 0 || Math.abs(value - v) <= Math.abs(value - axisData[index - 1])) {
80
63
  return true;
81
64
  }
82
65
  }
83
66
  if (v <= value) {
84
- if (index === xAxisData.length - 1 ||
67
+ if (index === axisData.length - 1 ||
85
68
  // @ts-ignore
86
- Math.abs(value - v) < Math.abs(value - xAxisData[index + 1])) {
69
+ Math.abs(value - v) < Math.abs(value - axisData[index + 1])) {
87
70
  return true;
88
71
  }
89
72
  }
90
73
  return false;
91
74
  });
92
75
  return {
93
- value: closestIndex !== undefined && closestIndex >= 0 ? xAxisData[closestIndex] : value,
76
+ value: closestIndex !== undefined && closestIndex >= 0 ? axisData[closestIndex] : value,
94
77
  index: closestIndex
95
78
  };
96
79
  }
97
- const dataIndex = xScale.bandwidth() === 0 ? Math.floor((x - xScale.range()[0] + xScale.step() / 2) / xScale.step()) : Math.floor((x - xScale.range()[0]) / xScale.step());
98
- if (dataIndex < 0 || dataIndex >= xAxisData.length) {
80
+ const dataIndex = scale.bandwidth() === 0 ? Math.floor((mouseValue - Math.min(...scale.range()) + scale.step() / 2) / scale.step()) : Math.floor((mouseValue - Math.min(...scale.range())) / scale.step());
81
+ if (dataIndex < 0 || dataIndex >= axisData.length) {
99
82
  return null;
100
83
  }
101
84
  return {
102
85
  index: dataIndex,
103
- value: xAxisData[dataIndex]
86
+ value: axisData[dataIndex]
104
87
  };
105
88
  };
106
89
  const handleMouseOut = () => {
@@ -138,8 +121,8 @@ const useAxisEvents = disableAxisListener => {
138
121
  });
139
122
  return;
140
123
  }
141
- const newStateX = getUpdateX(svgPt.x);
142
- const newStateY = getUpdateY(svgPt.y);
124
+ const newStateX = getUpdate(xAxis[usedXAxis], svgPt.x);
125
+ const newStateY = getUpdate(yAxis[usedYAxis], svgPt.y);
143
126
  dispatch({
144
127
  type: 'updateAxis',
145
128
  data: {
package/hooks/useTicks.js CHANGED
@@ -34,9 +34,9 @@ function useTicks(options) {
34
34
  const domain = scale.domain();
35
35
  if (scale.bandwidth() > 0) {
36
36
  // scale type = 'band'
37
- return [...domain.map((value, index) => ({
37
+ return [...domain.map(value => ({
38
38
  formattedValue: valueFormatter?.(value) ?? value,
39
- offset: index === 0 ? scale.range()[0] : scale(value) - (scale.step() - scale.bandwidth()) / 2,
39
+ offset: scale(value) - (scale.step() - scale.bandwidth()) / 2,
40
40
  labelOffset: scale.step() / 2
41
41
  })), {
42
42
  formattedValue: undefined,
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @mui/x-charts v6.0.0-alpha.7
2
+ * @mui/x-charts v6.0.0-alpha.9
3
3
  *
4
4
  * @license MIT
5
5
  * This source code is licensed under the MIT license found in the
@@ -39,6 +39,7 @@ export declare function defaultizeColor(series: AllSeriesType, seriesIndex: numb
39
39
  data?: number[] | undefined;
40
40
  dataKey?: string | undefined;
41
41
  label?: string | undefined;
42
+ layout?: "horizontal" | "vertical" | undefined;
42
43
  id?: string | undefined;
43
44
  color: string;
44
45
  valueFormatter?: ((value: number) => string) | undefined;
@@ -56,6 +57,8 @@ export declare function defaultizeColor(series: AllSeriesType, seriesIndex: numb
56
57
  area?: boolean | undefined;
57
58
  label?: string | undefined;
58
59
  curve?: import("../models/seriesType").CurveType | undefined;
60
+ showMark?: boolean | ((params: import("../models/seriesType").ShowMarkParams) => boolean) | undefined;
61
+ disableHighlight?: boolean | undefined;
59
62
  id?: string | undefined;
60
63
  color: string;
61
64
  valueFormatter?: ((value: number) => string) | undefined;
@@ -1,12 +1,12 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3
3
  import * as React from 'react';
4
4
  import useId from '@mui/utils/useId';
5
5
  import PropTypes from 'prop-types';
6
6
  import { BarPlot } from './BarPlot';
7
7
  import { ResponsiveChartContainer } from '../ResponsiveChartContainer';
8
8
  import { ChartsAxis } from '../ChartsAxis';
9
- import { DEFAULT_X_AXIS_KEY } from '../constants';
9
+ import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants';
10
10
  import { ChartsTooltip } from '../ChartsTooltip';
11
11
  import { ChartsLegend } from '../ChartsLegend';
12
12
  import { ChartsAxisHighlight } from '../ChartsAxisHighlight';
@@ -23,6 +23,7 @@ var BarChart = /*#__PURE__*/React.forwardRef(function BarChart(props, ref) {
23
23
  colors = props.colors,
24
24
  dataset = props.dataset,
25
25
  sx = props.sx,
26
+ layout = props.layout,
26
27
  tooltip = props.tooltip,
27
28
  axisHighlight = props.axisHighlight,
28
29
  legend = props.legend,
@@ -35,29 +36,43 @@ var BarChart = /*#__PURE__*/React.forwardRef(function BarChart(props, ref) {
35
36
  slotProps = props.slotProps;
36
37
  var id = useId();
37
38
  var clipPathId = "".concat(id, "-clip-path");
39
+ var hasHorizontalSeries = layout === 'horizontal' || layout === undefined && series.some(function (item) {
40
+ return item.layout === 'horizontal';
41
+ });
42
+ var defaultAxisConfig = {
43
+ scaleType: 'band',
44
+ data: Array.from({
45
+ length: Math.max.apply(Math, _toConsumableArray(series.map(function (s) {
46
+ var _ref, _s$data;
47
+ return ((_ref = (_s$data = s.data) != null ? _s$data : dataset) != null ? _ref : []).length;
48
+ })))
49
+ }, function (_, index) {
50
+ return index;
51
+ })
52
+ };
53
+ var defaultizedAxisHighlight = _extends({}, hasHorizontalSeries ? {
54
+ y: 'band'
55
+ } : {
56
+ x: 'band'
57
+ }, axisHighlight);
38
58
  return /*#__PURE__*/_jsxs(ResponsiveChartContainer, {
39
59
  ref: ref,
40
60
  series: series.map(function (s) {
41
61
  return _extends({
42
62
  type: 'bar'
43
- }, s);
63
+ }, s, {
64
+ layout: hasHorizontalSeries ? 'horizontal' : 'vertical'
65
+ });
44
66
  }),
45
67
  width: width,
46
68
  height: height,
47
69
  margin: margin,
48
- xAxis: xAxis != null ? xAxis : [{
49
- id: DEFAULT_X_AXIS_KEY,
50
- scaleType: 'band',
51
- data: Array.from({
52
- length: Math.max.apply(Math, _toConsumableArray(series.map(function (s) {
53
- var _ref, _s$data;
54
- return ((_ref = (_s$data = s.data) != null ? _s$data : dataset) != null ? _ref : []).length;
55
- })))
56
- }, function (_, index) {
57
- return index;
58
- })
59
- }],
60
- yAxis: yAxis,
70
+ xAxis: xAxis != null ? xAxis : hasHorizontalSeries ? undefined : [_extends({
71
+ id: DEFAULT_X_AXIS_KEY
72
+ }, defaultAxisConfig)],
73
+ yAxis: yAxis != null ? yAxis : hasHorizontalSeries ? [_extends({
74
+ id: DEFAULT_Y_AXIS_KEY
75
+ }, defaultAxisConfig)] : undefined,
61
76
  colors: colors,
62
77
  dataset: dataset,
63
78
  sx: sx,
@@ -78,9 +93,7 @@ var BarChart = /*#__PURE__*/React.forwardRef(function BarChart(props, ref) {
78
93
  }), /*#__PURE__*/_jsx(ChartsLegend, _extends({}, legend, {
79
94
  slots: slots,
80
95
  slotProps: slotProps
81
- })), /*#__PURE__*/_jsx(ChartsAxisHighlight, _extends({
82
- x: "band"
83
- }, axisHighlight)), /*#__PURE__*/_jsx(ChartsTooltip, _extends({}, tooltip, {
96
+ })), /*#__PURE__*/_jsx(ChartsAxisHighlight, _extends({}, defaultizedAxisHighlight)), /*#__PURE__*/_jsx(ChartsTooltip, _extends({}, tooltip, {
84
97
  slots: slots,
85
98
  slotProps: slotProps
86
99
  })), /*#__PURE__*/_jsx(ChartsClipPath, {
@@ -95,7 +108,7 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
95
108
  // ----------------------------------------------------------------------
96
109
  axisHighlight: PropTypes.shape({
97
110
  x: PropTypes.oneOf(['band', 'line', 'none']),
98
- y: PropTypes.oneOf(['line', 'none'])
111
+ y: PropTypes.oneOf(['band', 'line', 'none'])
99
112
  }),
100
113
  /**
101
114
  * Indicate which axis to display the bottom of the charts.
@@ -130,6 +143,7 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
130
143
  desc: PropTypes.string,
131
144
  disableAxisListener: PropTypes.bool,
132
145
  height: PropTypes.number,
146
+ layout: PropTypes.oneOf(['horizontal', 'vertical']),
133
147
  /**
134
148
  * Indicate which axis to display the left of the charts.
135
149
  * Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
@@ -210,6 +224,7 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
210
224
  }),
211
225
  id: PropTypes.string,
212
226
  label: PropTypes.string,
227
+ layout: PropTypes.oneOf(['horizontal', 'vertical']),
213
228
  stack: PropTypes.string,
214
229
  stackOffset: PropTypes.oneOf(['diverging', 'expand', 'none', 'silhouette', 'wiggle']),
215
230
  stackOrder: PropTypes.oneOf(['appearance', 'ascending', 'descending', 'insideOut', 'none', 'reverse']),
@@ -57,40 +57,50 @@ function BarPlot(props) {
57
57
  var yAxisKey = (_series$seriesId$yAxi = series[seriesId].yAxisKey) != null ? _series$seriesId$yAxi : defaultYAxisId;
58
58
  var xAxisConfig = xAxis[xAxisKey];
59
59
  var yAxisConfig = yAxis[yAxisKey];
60
- if (!isBandScaleConfig(xAxisConfig)) {
61
- throw new Error("Axis with id \"".concat(xAxisKey, "\" shoud be of type \"band\" to display the bar series of id \"").concat(seriesId, "\""));
62
- }
63
- if (xAxis[xAxisKey].data === undefined) {
64
- throw new Error("Axis with id \"".concat(xAxisKey, "\" shoud have data property"));
60
+ var verticalLayout = series[seriesId].layout === 'vertical';
61
+ var baseScaleConfig;
62
+ if (verticalLayout) {
63
+ if (!isBandScaleConfig(xAxisConfig)) {
64
+ throw new Error("Axis with id \"".concat(xAxisKey, "\" shoud be of type \"band\" to display the bar series of id \"").concat(seriesId, "\""));
65
+ }
66
+ if (xAxis[xAxisKey].data === undefined) {
67
+ throw new Error("Axis with id \"".concat(xAxisKey, "\" shoud have data property"));
68
+ }
69
+ baseScaleConfig = xAxisConfig;
70
+ } else {
71
+ if (!isBandScaleConfig(yAxisConfig)) {
72
+ throw new Error("Axis with id \"".concat(yAxisKey, "\" shoud be of type \"band\" to display the bar series of id \"").concat(seriesId, "\""));
73
+ }
74
+ if (yAxis[yAxisKey].data === undefined) {
75
+ throw new Error("Axis with id \"".concat(xAxisKey, "\" shoud have data property"));
76
+ }
77
+ baseScaleConfig = yAxisConfig;
65
78
  }
66
79
  var xScale = xAxisConfig.scale;
67
80
  var yScale = yAxisConfig.scale;
68
-
69
- // Currently assuming all bars are vertical
70
- var bandWidth = xScale.bandwidth();
81
+ var bandWidth = baseScaleConfig.scale.bandwidth();
71
82
  var _getBandSize = getBandSize({
72
83
  bandWidth: bandWidth,
73
84
  numberOfGroups: stackingGroups.length,
74
- gapRatio: xAxisConfig.barGapRatio
85
+ gapRatio: baseScaleConfig.barGapRatio
75
86
  }),
76
87
  barWidth = _getBandSize.barWidth,
77
88
  offset = _getBandSize.offset;
78
-
79
- // @ts-ignore TODO: fix when adding a correct API for customisation
89
+ var barOffset = groupIndex * (barWidth + offset);
80
90
  var _series$seriesId = series[seriesId],
81
91
  stackedData = _series$seriesId.stackedData,
82
92
  color = _series$seriesId.color;
83
93
  return stackedData.map(function (values, dataIndex) {
84
- var _xAxis$xAxisKey$data;
94
+ var _xAxis$xAxisKey$data, _yAxis$yAxisKey$data;
85
95
  var baseline = Math.min.apply(Math, _toConsumableArray(values));
86
96
  var value = Math.max.apply(Math, _toConsumableArray(values));
87
97
  return /*#__PURE__*/_jsx(BarElement, _extends({
88
98
  id: seriesId,
89
99
  dataIndex: dataIndex,
90
- x: xScale((_xAxis$xAxisKey$data = xAxis[xAxisKey].data) == null ? void 0 : _xAxis$xAxisKey$data[dataIndex]) + groupIndex * (barWidth + offset),
91
- y: yScale(value),
92
- height: yScale(baseline) - yScale(value),
93
- width: barWidth,
100
+ x: verticalLayout ? xScale((_xAxis$xAxisKey$data = xAxis[xAxisKey].data) == null ? void 0 : _xAxis$xAxisKey$data[dataIndex]) + barOffset : xScale(baseline),
101
+ y: verticalLayout ? yScale(value) : yScale((_yAxis$yAxisKey$data = yAxis[yAxisKey].data) == null ? void 0 : _yAxis$yAxisKey$data[dataIndex]) + barOffset,
102
+ height: verticalLayout ? Math.abs(yScale(baseline) - yScale(value)) : barWidth,
103
+ width: verticalLayout ? barWidth : Math.abs(xScale(baseline) - xScale(value)),
94
104
  color: color,
95
105
  highlightScope: series[seriesId].highlightScope
96
106
  }, props), "".concat(seriesId, "-").concat(dataIndex));