@mui/x-charts 6.0.0-alpha.14 → 6.0.0-alpha.15

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 (83) hide show
  1. package/BarChart/BarChart.d.ts +3 -0
  2. package/BarChart/BarChart.js +4 -8
  3. package/CHANGELOG.md +104 -0
  4. package/ChartsLegend/ChartsLegend.d.ts +43 -24
  5. package/ChartsLegend/ChartsLegend.js +185 -143
  6. package/ChartsLegend/utils.d.ts +1 -6
  7. package/ChartsSurface.js +3 -11
  8. package/ChartsXAxis/ChartsXAxis.js +48 -25
  9. package/ChartsYAxis/ChartsYAxis.js +47 -26
  10. package/LineChart/LineChart.d.ts +3 -0
  11. package/LineChart/LineChart.js +4 -8
  12. package/PieChart/PieChart.d.ts +3 -0
  13. package/PieChart/PieChart.js +4 -8
  14. package/ScatterChart/ScatterChart.d.ts +3 -0
  15. package/ScatterChart/ScatterChart.js +4 -8
  16. package/constants.js +1 -1
  17. package/context/CartesianContextProvider.js +10 -10
  18. package/context/DrawingProvider.d.ts +2 -0
  19. package/context/DrawingProvider.js +2 -0
  20. package/esm/BarChart/BarChart.js +4 -8
  21. package/esm/ChartsLegend/ChartsLegend.js +184 -142
  22. package/esm/ChartsSurface.js +3 -11
  23. package/esm/ChartsXAxis/ChartsXAxis.js +47 -24
  24. package/esm/ChartsYAxis/ChartsYAxis.js +46 -25
  25. package/esm/LineChart/LineChart.js +4 -8
  26. package/esm/PieChart/PieChart.js +4 -8
  27. package/esm/ScatterChart/ScatterChart.js +4 -8
  28. package/esm/constants.js +1 -1
  29. package/esm/context/CartesianContextProvider.js +11 -11
  30. package/esm/context/DrawingProvider.js +2 -0
  31. package/esm/hooks/useChartDimensions.js +2 -0
  32. package/esm/hooks/useTicks.js +5 -5
  33. package/esm/internals/components/AxisSharedComponents.js +15 -70
  34. package/esm/internals/components/ChartsText.js +71 -0
  35. package/esm/internals/domUtils.js +113 -0
  36. package/hooks/useChartDimensions.d.ts +2 -0
  37. package/hooks/useChartDimensions.js +2 -0
  38. package/hooks/useTicks.d.ts +2 -3
  39. package/hooks/useTicks.js +6 -6
  40. package/index.js +1 -1
  41. package/internals/components/AxisSharedComponents.d.ts +0 -4
  42. package/internals/components/AxisSharedComponents.js +16 -71
  43. package/internals/components/ChartsText.d.ts +32 -0
  44. package/internals/components/ChartsText.js +81 -0
  45. package/internals/domUtils.d.ts +14 -0
  46. package/internals/domUtils.js +122 -0
  47. package/legacy/BarChart/BarChart.js +4 -8
  48. package/legacy/ChartsLegend/ChartsLegend.js +196 -140
  49. package/legacy/ChartsSurface.js +2 -11
  50. package/legacy/ChartsXAxis/ChartsXAxis.js +47 -24
  51. package/legacy/ChartsYAxis/ChartsYAxis.js +46 -25
  52. package/legacy/LineChart/LineChart.js +4 -8
  53. package/legacy/PieChart/PieChart.js +4 -8
  54. package/legacy/ScatterChart/ScatterChart.js +4 -8
  55. package/legacy/constants.js +1 -1
  56. package/legacy/context/CartesianContextProvider.js +11 -11
  57. package/legacy/context/DrawingProvider.js +2 -0
  58. package/legacy/hooks/useChartDimensions.js +2 -0
  59. package/legacy/hooks/useTicks.js +5 -5
  60. package/legacy/index.js +1 -1
  61. package/legacy/internals/components/AxisSharedComponents.js +9 -63
  62. package/legacy/internals/components/ChartsText.js +77 -0
  63. package/legacy/internals/domUtils.js +121 -0
  64. package/models/axis.d.ts +6 -5
  65. package/models/layout.d.ts +7 -6
  66. package/modern/BarChart/BarChart.js +4 -8
  67. package/modern/ChartsLegend/ChartsLegend.js +184 -142
  68. package/modern/ChartsSurface.js +3 -11
  69. package/modern/ChartsXAxis/ChartsXAxis.js +47 -24
  70. package/modern/ChartsYAxis/ChartsYAxis.js +46 -25
  71. package/modern/LineChart/LineChart.js +4 -8
  72. package/modern/PieChart/PieChart.js +4 -8
  73. package/modern/ScatterChart/ScatterChart.js +4 -8
  74. package/modern/constants.js +1 -1
  75. package/modern/context/CartesianContextProvider.js +11 -11
  76. package/modern/context/DrawingProvider.js +2 -0
  77. package/modern/hooks/useChartDimensions.js +2 -0
  78. package/modern/hooks/useTicks.js +5 -5
  79. package/modern/index.js +1 -1
  80. package/modern/internals/components/AxisSharedComponents.js +15 -70
  81. package/modern/internals/components/ChartsText.js +71 -0
  82. package/modern/internals/domUtils.js +113 -0
  83. package/package.json +1 -1
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.ChartsTickLabel = exports.ChartsTick = exports.ChartsLine = exports.ChartsLabel = exports.AxisRoot = void 0;
7
+ exports.AxisRoot = void 0;
8
8
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
9
  var _styles = require("@mui/material/styles");
10
10
  var _axisClasses = require("../../ChartsAxis/axisClasses");
@@ -12,77 +12,22 @@ const AxisRoot = exports.AxisRoot = (0, _styles.styled)('g', {
12
12
  name: 'MuiChartsAxis',
13
13
  slot: 'Root',
14
14
  overridesResolver: (props, styles) => styles.root
15
- })({
16
- [`&.${_axisClasses.axisClasses.directionY}`]: {
17
- [`.${_axisClasses.axisClasses.tickLabel}`]: {
18
- dominantBaseline: 'middle'
19
- },
20
- [`.${_axisClasses.axisClasses.label}`]: {
21
- dominantBaseline: 'auto',
22
- textAnchor: 'middle'
23
- }
24
- },
25
- [`&.${_axisClasses.axisClasses.left}`]: {
26
- [`.${_axisClasses.axisClasses.tickLabel}`]: {
27
- dominantBaseline: 'central',
28
- textAnchor: 'end'
29
- }
30
- },
31
- [`&.${_axisClasses.axisClasses.right}`]: {
32
- [`.${_axisClasses.axisClasses.tickLabel}`]: {
33
- dominantBaseline: 'central',
34
- textAnchor: 'start'
35
- }
36
- },
37
- [`&.${_axisClasses.axisClasses.bottom}`]: {
38
- [`.${_axisClasses.axisClasses.tickLabel}, .${_axisClasses.axisClasses.label}`]: {
39
- dominantBaseline: 'hanging',
40
- textAnchor: 'middle'
41
- }
42
- },
43
- [`&.${_axisClasses.axisClasses.top}`]: {
44
- [`.${_axisClasses.axisClasses.tickLabel}, .${_axisClasses.axisClasses.label}`]: {
45
- dominantBaseline: 'baseline',
46
- textAnchor: 'middle'
47
- }
48
- }
49
- });
50
- const ChartsLine = exports.ChartsLine = (0, _styles.styled)('line', {
51
- name: 'MuiChartsAxis',
52
- slot: 'Line',
53
- overridesResolver: (props, styles) => styles.line
54
15
  })(({
55
16
  theme
56
17
  }) => ({
57
- stroke: (theme.vars || theme).palette.text.primary,
58
- shapeRendering: 'crispEdges',
59
- strokeWidth: 1
60
- }));
61
- const ChartsTick = exports.ChartsTick = (0, _styles.styled)('line', {
62
- name: 'MuiChartsAxis',
63
- slot: 'Tick',
64
- overridesResolver: (props, styles) => styles.tick
65
- })(({
66
- theme
67
- }) => ({
68
- stroke: (theme.vars || theme).palette.text.primary,
69
- shapeRendering: 'crispEdges'
70
- }));
71
- const ChartsTickLabel = exports.ChartsTickLabel = (0, _styles.styled)('text', {
72
- name: 'MuiChartsAxis',
73
- slot: 'TickLabel',
74
- overridesResolver: (props, styles) => styles.tickLabel
75
- })(({
76
- theme
77
- }) => (0, _extends2.default)({}, theme.typography.caption, {
78
- fill: (theme.vars || theme).palette.text.primary
79
- }));
80
- const ChartsLabel = exports.ChartsLabel = (0, _styles.styled)('text', {
81
- name: 'MuiChartsAxis',
82
- slot: 'Label',
83
- overridesResolver: (props, styles) => styles.label
84
- })(({
85
- theme
86
- }) => (0, _extends2.default)({}, theme.typography.body1, {
87
- fill: (theme.vars || theme).palette.text.primary
18
+ [`& .${_axisClasses.axisClasses.tickLabel}`]: (0, _extends2.default)({}, theme.typography.caption, {
19
+ fill: (theme.vars || theme).palette.text.primary
20
+ }),
21
+ [`& .${_axisClasses.axisClasses.label}`]: (0, _extends2.default)({}, theme.typography.body1, {
22
+ fill: (theme.vars || theme).palette.text.primary
23
+ }),
24
+ [`& .${_axisClasses.axisClasses.line}`]: {
25
+ stroke: (theme.vars || theme).palette.text.primary,
26
+ shapeRendering: 'crispEdges',
27
+ strokeWidth: 1
28
+ },
29
+ [`& .${_axisClasses.axisClasses.tick}`]: {
30
+ stroke: (theme.vars || theme).palette.text.primary,
31
+ shapeRendering: 'crispEdges'
32
+ }
88
33
  }));
@@ -0,0 +1,32 @@
1
+ import * as React from 'react';
2
+ interface GetWordsByLinesParams {
3
+ /**
4
+ * Text displayed.
5
+ */
6
+ text: string;
7
+ /**
8
+ * Style applied to text elements.
9
+ */
10
+ style?: React.SVGAttributes<'text'>['style'];
11
+ /**
12
+ * If true, the line width is computed.
13
+ * @default false
14
+ */
15
+ needsComputation?: boolean;
16
+ }
17
+ export type ChartsTextBaseline = 'hanging' | 'central' | 'auto';
18
+ export interface ChartsTextProps extends Omit<React.SVGTextElementAttributes<SVGTextElement>, 'width' | 'ref'>, GetWordsByLinesParams {
19
+ /**
20
+ * Height of a text line (in `em`).
21
+ */
22
+ lineHeight?: number;
23
+ /**
24
+ * The text baseline
25
+ * @default 'central'
26
+ */
27
+ dominantBaseline?: ChartsTextBaseline;
28
+ ownerState?: any;
29
+ }
30
+ export declare function getWordsByLines({ style, needsComputation, text }: GetWordsByLinesParams): any[];
31
+ export declare function ChartsText(props: ChartsTextProps): React.JSX.Element;
32
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.ChartsText = ChartsText;
8
+ exports.getWordsByLines = getWordsByLines;
9
+ var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
10
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
+ var React = _interopRequireWildcard(require("react"));
12
+ var _domUtils = require("../domUtils");
13
+ var _jsxRuntime = require("react/jsx-runtime");
14
+ const _excluded = ["x", "y", "textAnchor", "dominantBaseline", "style", "text", "ownerState"];
15
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+ function getWordsByLines({
18
+ style,
19
+ needsComputation,
20
+ text
21
+ }) {
22
+ return text.split('\n').map(subText => (0, _extends2.default)({
23
+ text: subText
24
+ }, needsComputation ? (0, _domUtils.getStringSize)(subText, style) : {
25
+ width: 0,
26
+ height: 0
27
+ }));
28
+ }
29
+ function ChartsText(props) {
30
+ const {
31
+ x,
32
+ y,
33
+ textAnchor = 'start',
34
+ dominantBaseline = 'central',
35
+ style,
36
+ text
37
+ } = props,
38
+ textProps = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
39
+ const wordsByLines = React.useMemo(() => getWordsByLines({
40
+ style,
41
+ needsComputation: text.includes('\n'),
42
+ text
43
+ }), [style, text]);
44
+ let startDy;
45
+ switch (dominantBaseline) {
46
+ case 'hanging':
47
+ startDy = 0;
48
+ break;
49
+ case 'central':
50
+ startDy = (wordsByLines.length - 1) / 2 * -wordsByLines[0].height;
51
+ break;
52
+ default:
53
+ startDy = (wordsByLines.length - 1) * -wordsByLines[0].height;
54
+ break;
55
+ }
56
+
57
+ // const transforms = [];
58
+ // if (scaleToFit) {
59
+ // const lineWidth = wordsByLines[0].width;
60
+ // transforms.push(`scale(${(isNumber(width as number) ? (width as number) / lineWidth : 1) / lineWidth})`);
61
+ // }
62
+ // if (angle) {
63
+ // transforms.push(`rotate(${angle}, ${x}, ${y})`);
64
+ // }
65
+ // if (transforms.length) {
66
+ // textProps.transform = transforms.join(' ');
67
+ // }
68
+
69
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("text", (0, _extends2.default)({}, textProps, {
70
+ x: x,
71
+ y: y,
72
+ textAnchor: textAnchor,
73
+ dominantBaseline: dominantBaseline,
74
+ style: style,
75
+ children: wordsByLines.map((line, index) => /*#__PURE__*/(0, _jsxRuntime.jsx)("tspan", {
76
+ x: x,
77
+ dy: `${index === 0 ? startDy : wordsByLines[0].height}px`,
78
+ children: line.text
79
+ }, index))
80
+ }));
81
+ }
@@ -0,0 +1,14 @@
1
+ import * as React from 'react';
2
+ /**
3
+ *
4
+ * @param style React style object
5
+ * @returns CSS styling string
6
+ */
7
+ export declare const getStyleString: (style: React.CSSProperties) => string;
8
+ /**
9
+ *
10
+ * @param text The string to estimate
11
+ * @param style The style applied
12
+ * @returns width and height of the text
13
+ */
14
+ export declare const getStringSize: (text: string | number, style?: React.CSSProperties) => any;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.getStyleString = exports.getStringSize = void 0;
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+ // DOM utils taken from
10
+ // https://github.com/recharts/recharts/blob/master/src/util/DOMUtils.ts
11
+
12
+ const isSsr = () => !(typeof window !== 'undefined' && window.document && window.setTimeout);
13
+ const stringCache = {
14
+ widthCache: {},
15
+ cacheCount: 0
16
+ };
17
+ const MAX_CACHE_NUM = 2000;
18
+ const SPAN_STYLE = {
19
+ position: 'absolute',
20
+ top: '-20000px',
21
+ left: 0,
22
+ padding: 0,
23
+ margin: 0,
24
+ border: 'none',
25
+ whiteSpace: 'pre'
26
+ };
27
+ const STYLE_LIST = ['minWidth', 'maxWidth', 'width', 'minHeight', 'maxHeight', 'height', 'top', 'left', 'fontSize', 'padding', 'margin', 'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom', 'marginLeft', 'marginRight', 'marginTop', 'marginBottom'];
28
+ const MEASUREMENT_SPAN_ID = 'mui_measurement_span';
29
+
30
+ /**
31
+ *
32
+ * @param name CSS property name
33
+ * @param value
34
+ * @returns add 'px' for distance properties
35
+ */
36
+ function autoCompleteStyle(name, value) {
37
+ if (STYLE_LIST.indexOf(name) >= 0 && value === +value) {
38
+ return `${value}px`;
39
+ }
40
+ return value;
41
+ }
42
+
43
+ /**
44
+ *
45
+ * @param text camelcase css property
46
+ * @returns css property
47
+ */
48
+ function camelToMiddleLine(text) {
49
+ const strs = text.split('');
50
+ const formatStrs = strs.reduce((result, entry) => {
51
+ if (entry === entry.toUpperCase()) {
52
+ return [...result, '-', entry.toLowerCase()];
53
+ }
54
+ return [...result, entry];
55
+ }, []);
56
+ return formatStrs.join('');
57
+ }
58
+
59
+ /**
60
+ *
61
+ * @param style React style object
62
+ * @returns CSS styling string
63
+ */
64
+ const getStyleString = style => Object.keys(style).sort().reduce((result, s) => `${result}${camelToMiddleLine(s)}:${autoCompleteStyle(s, style[s])};`, '');
65
+
66
+ /**
67
+ *
68
+ * @param text The string to estimate
69
+ * @param style The style applied
70
+ * @returns width and height of the text
71
+ */
72
+ exports.getStyleString = getStyleString;
73
+ const getStringSize = (text, style = {}) => {
74
+ if (text === undefined || text === null || isSsr()) {
75
+ return {
76
+ width: 0,
77
+ height: 0
78
+ };
79
+ }
80
+ const str = `${text}`;
81
+ const styleString = getStyleString(style);
82
+ const cacheKey = `${str}-${styleString}`;
83
+ if (stringCache.widthCache[cacheKey]) {
84
+ return stringCache.widthCache[cacheKey];
85
+ }
86
+ try {
87
+ let measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
88
+ if (measurementSpan === null) {
89
+ measurementSpan = document.createElement('span');
90
+ measurementSpan.setAttribute('id', MEASUREMENT_SPAN_ID);
91
+ measurementSpan.setAttribute('aria-hidden', 'true');
92
+ document.body.appendChild(measurementSpan);
93
+ }
94
+ // Need to use CSS Object Model (CSSOM) to be able to comply with Content Security Policy (CSP)
95
+ // https://en.wikipedia.org/wiki/Content_Security_Policy
96
+ const measurementSpanStyle = (0, _extends2.default)({}, SPAN_STYLE, style);
97
+ Object.keys(measurementSpanStyle).map(styleKey => {
98
+ measurementSpan.style[camelToMiddleLine(styleKey)] = autoCompleteStyle(styleKey, measurementSpanStyle[styleKey]);
99
+ return styleKey;
100
+ });
101
+ measurementSpan.textContent = str;
102
+ const rect = measurementSpan.getBoundingClientRect();
103
+ const result = {
104
+ width: rect.width,
105
+ height: rect.height
106
+ };
107
+ stringCache.widthCache[cacheKey] = result;
108
+ if (stringCache.cacheCount + 1 > MAX_CACHE_NUM) {
109
+ stringCache.cacheCount = 0;
110
+ stringCache.widthCache = {};
111
+ } else {
112
+ stringCache.cacheCount += 1;
113
+ }
114
+ return result;
115
+ } catch (e) {
116
+ return {
117
+ width: 0,
118
+ height: 0
119
+ };
120
+ }
121
+ };
122
+ exports.getStringSize = getStringSize;
@@ -167,23 +167,19 @@ process.env.NODE_ENV !== "production" ? BarChart.propTypes = {
167
167
  tickNumber: PropTypes.number,
168
168
  tickSize: PropTypes.number
169
169
  }), PropTypes.string]),
170
+ /**
171
+ * @deprecated Consider using `slotProps.legend` instead.
172
+ */
170
173
  legend: PropTypes.shape({
171
174
  classes: PropTypes.object,
172
175
  direction: PropTypes.oneOf(['column', 'row']),
173
176
  hidden: PropTypes.bool,
174
- itemWidth: PropTypes.number,
175
- markSize: PropTypes.number,
176
- offset: PropTypes.shape({
177
- x: PropTypes.number,
178
- y: PropTypes.number
179
- }),
180
177
  position: PropTypes.shape({
181
178
  horizontal: PropTypes.oneOf(['left', 'middle', 'right']).isRequired,
182
179
  vertical: PropTypes.oneOf(['bottom', 'middle', 'top']).isRequired
183
180
  }),
184
181
  slotProps: PropTypes.object,
185
- slots: PropTypes.object,
186
- spacing: PropTypes.number
182
+ slots: PropTypes.object
187
183
  }),
188
184
  margin: PropTypes.shape({
189
185
  bottom: PropTypes.number,