@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.
- package/BarChart/BarChart.d.ts +3 -0
- package/BarChart/BarChart.js +4 -8
- package/CHANGELOG.md +104 -0
- package/ChartsLegend/ChartsLegend.d.ts +43 -24
- package/ChartsLegend/ChartsLegend.js +185 -143
- package/ChartsLegend/utils.d.ts +1 -6
- package/ChartsSurface.js +3 -11
- package/ChartsXAxis/ChartsXAxis.js +48 -25
- package/ChartsYAxis/ChartsYAxis.js +47 -26
- package/LineChart/LineChart.d.ts +3 -0
- package/LineChart/LineChart.js +4 -8
- package/PieChart/PieChart.d.ts +3 -0
- package/PieChart/PieChart.js +4 -8
- package/ScatterChart/ScatterChart.d.ts +3 -0
- package/ScatterChart/ScatterChart.js +4 -8
- package/constants.js +1 -1
- package/context/CartesianContextProvider.js +10 -10
- package/context/DrawingProvider.d.ts +2 -0
- package/context/DrawingProvider.js +2 -0
- package/esm/BarChart/BarChart.js +4 -8
- package/esm/ChartsLegend/ChartsLegend.js +184 -142
- package/esm/ChartsSurface.js +3 -11
- package/esm/ChartsXAxis/ChartsXAxis.js +47 -24
- package/esm/ChartsYAxis/ChartsYAxis.js +46 -25
- package/esm/LineChart/LineChart.js +4 -8
- package/esm/PieChart/PieChart.js +4 -8
- package/esm/ScatterChart/ScatterChart.js +4 -8
- package/esm/constants.js +1 -1
- package/esm/context/CartesianContextProvider.js +11 -11
- package/esm/context/DrawingProvider.js +2 -0
- package/esm/hooks/useChartDimensions.js +2 -0
- package/esm/hooks/useTicks.js +5 -5
- package/esm/internals/components/AxisSharedComponents.js +15 -70
- package/esm/internals/components/ChartsText.js +71 -0
- package/esm/internals/domUtils.js +113 -0
- package/hooks/useChartDimensions.d.ts +2 -0
- package/hooks/useChartDimensions.js +2 -0
- package/hooks/useTicks.d.ts +2 -3
- package/hooks/useTicks.js +6 -6
- package/index.js +1 -1
- package/internals/components/AxisSharedComponents.d.ts +0 -4
- package/internals/components/AxisSharedComponents.js +16 -71
- package/internals/components/ChartsText.d.ts +32 -0
- package/internals/components/ChartsText.js +81 -0
- package/internals/domUtils.d.ts +14 -0
- package/internals/domUtils.js +122 -0
- package/legacy/BarChart/BarChart.js +4 -8
- package/legacy/ChartsLegend/ChartsLegend.js +196 -140
- package/legacy/ChartsSurface.js +2 -11
- package/legacy/ChartsXAxis/ChartsXAxis.js +47 -24
- package/legacy/ChartsYAxis/ChartsYAxis.js +46 -25
- package/legacy/LineChart/LineChart.js +4 -8
- package/legacy/PieChart/PieChart.js +4 -8
- package/legacy/ScatterChart/ScatterChart.js +4 -8
- package/legacy/constants.js +1 -1
- package/legacy/context/CartesianContextProvider.js +11 -11
- package/legacy/context/DrawingProvider.js +2 -0
- package/legacy/hooks/useChartDimensions.js +2 -0
- package/legacy/hooks/useTicks.js +5 -5
- package/legacy/index.js +1 -1
- package/legacy/internals/components/AxisSharedComponents.js +9 -63
- package/legacy/internals/components/ChartsText.js +77 -0
- package/legacy/internals/domUtils.js +121 -0
- package/models/axis.d.ts +6 -5
- package/models/layout.d.ts +7 -6
- package/modern/BarChart/BarChart.js +4 -8
- package/modern/ChartsLegend/ChartsLegend.js +184 -142
- package/modern/ChartsSurface.js +3 -11
- package/modern/ChartsXAxis/ChartsXAxis.js +47 -24
- package/modern/ChartsYAxis/ChartsYAxis.js +46 -25
- package/modern/LineChart/LineChart.js +4 -8
- package/modern/PieChart/PieChart.js +4 -8
- package/modern/ScatterChart/ScatterChart.js +4 -8
- package/modern/constants.js +1 -1
- package/modern/context/CartesianContextProvider.js +11 -11
- package/modern/context/DrawingProvider.js +2 -0
- package/modern/hooks/useChartDimensions.js +2 -0
- package/modern/hooks/useTicks.js +5 -5
- package/modern/index.js +1 -1
- package/modern/internals/components/AxisSharedComponents.js +15 -70
- package/modern/internals/components/ChartsText.js +71 -0
- package/modern/internals/domUtils.js +113 -0
- package/package.json +1 -1
|
@@ -148,23 +148,19 @@ process.env.NODE_ENV !== "production" ? PieChart.propTypes = {
|
|
|
148
148
|
tickNumber: PropTypes.number,
|
|
149
149
|
tickSize: PropTypes.number
|
|
150
150
|
}), PropTypes.string]),
|
|
151
|
+
/**
|
|
152
|
+
* @deprecated Consider using `slotProps.legend` instead.
|
|
153
|
+
*/
|
|
151
154
|
legend: PropTypes.shape({
|
|
152
155
|
classes: PropTypes.object,
|
|
153
156
|
direction: PropTypes.oneOf(['column', 'row']),
|
|
154
157
|
hidden: PropTypes.bool,
|
|
155
|
-
itemWidth: PropTypes.number,
|
|
156
|
-
markSize: PropTypes.number,
|
|
157
|
-
offset: PropTypes.shape({
|
|
158
|
-
x: PropTypes.number,
|
|
159
|
-
y: PropTypes.number
|
|
160
|
-
}),
|
|
161
158
|
position: PropTypes.shape({
|
|
162
159
|
horizontal: PropTypes.oneOf(['left', 'middle', 'right']).isRequired,
|
|
163
160
|
vertical: PropTypes.oneOf(['bottom', 'middle', 'top']).isRequired
|
|
164
161
|
}),
|
|
165
162
|
slotProps: PropTypes.object,
|
|
166
|
-
slots: PropTypes.object
|
|
167
|
-
spacing: PropTypes.number
|
|
163
|
+
slots: PropTypes.object
|
|
168
164
|
}),
|
|
169
165
|
margin: PropTypes.shape({
|
|
170
166
|
bottom: PropTypes.number,
|
|
@@ -128,23 +128,19 @@ process.env.NODE_ENV !== "production" ? ScatterChart.propTypes = {
|
|
|
128
128
|
tickNumber: PropTypes.number,
|
|
129
129
|
tickSize: PropTypes.number
|
|
130
130
|
}), PropTypes.string]),
|
|
131
|
+
/**
|
|
132
|
+
* @deprecated Consider using `slotProps.legend` instead.
|
|
133
|
+
*/
|
|
131
134
|
legend: PropTypes.shape({
|
|
132
135
|
classes: PropTypes.object,
|
|
133
136
|
direction: PropTypes.oneOf(['column', 'row']),
|
|
134
137
|
hidden: PropTypes.bool,
|
|
135
|
-
itemWidth: PropTypes.number,
|
|
136
|
-
markSize: PropTypes.number,
|
|
137
|
-
offset: PropTypes.shape({
|
|
138
|
-
x: PropTypes.number,
|
|
139
|
-
y: PropTypes.number
|
|
140
|
-
}),
|
|
141
138
|
position: PropTypes.shape({
|
|
142
139
|
horizontal: PropTypes.oneOf(['left', 'middle', 'right']).isRequired,
|
|
143
140
|
vertical: PropTypes.oneOf(['bottom', 'middle', 'top']).isRequired
|
|
144
141
|
}),
|
|
145
142
|
slotProps: PropTypes.object,
|
|
146
|
-
slots: PropTypes.object
|
|
147
|
-
spacing: PropTypes.number
|
|
143
|
+
slots: PropTypes.object
|
|
148
144
|
}),
|
|
149
145
|
margin: PropTypes.shape({
|
|
150
146
|
bottom: PropTypes.number,
|
package/modern/constants.js
CHANGED
|
@@ -10,7 +10,7 @@ import { getScale } from '../internals/getScale';
|
|
|
10
10
|
import { DrawingContext } from './DrawingProvider';
|
|
11
11
|
import { SeriesContext } from './SeriesContextProvider';
|
|
12
12
|
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants';
|
|
13
|
-
import {
|
|
13
|
+
import { getTickNumber } from '../hooks/useTicks';
|
|
14
14
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
15
15
|
const DEFAULT_CATEGORY_GAP_RATIO = 0.2;
|
|
16
16
|
const DEFAULT_BAR_GAP_RATIO = 0.1;
|
|
@@ -109,13 +109,13 @@ function CartesianContextProvider({
|
|
|
109
109
|
barGapRatio
|
|
110
110
|
}, axis, {
|
|
111
111
|
scale: scaleBand(axis.data, range).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
|
|
112
|
-
|
|
112
|
+
tickNumber: axis.data.length
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
if (isPointScaleConfig(axis)) {
|
|
116
116
|
completedXAxis[axis.id] = _extends({}, axis, {
|
|
117
117
|
scale: scalePoint(axis.data, range),
|
|
118
|
-
|
|
118
|
+
tickNumber: axis.data.length
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
|
|
@@ -124,17 +124,17 @@ function CartesianContextProvider({
|
|
|
124
124
|
}
|
|
125
125
|
const scaleType = axis.scaleType ?? 'linear';
|
|
126
126
|
const extremums = [axis.min ?? minData, axis.max ?? maxData];
|
|
127
|
-
const
|
|
127
|
+
const tickNumber = getTickNumber(_extends({}, axis, {
|
|
128
128
|
range,
|
|
129
129
|
domain: extremums
|
|
130
130
|
}));
|
|
131
|
-
const niceScale = getScale(scaleType, extremums, range).nice(
|
|
131
|
+
const niceScale = getScale(scaleType, extremums, range).nice(tickNumber);
|
|
132
132
|
const niceDomain = niceScale.domain();
|
|
133
133
|
const domain = [axis.min ?? niceDomain[0], axis.max ?? niceDomain[1]];
|
|
134
134
|
completedXAxis[axis.id] = _extends({}, axis, {
|
|
135
135
|
scaleType,
|
|
136
136
|
scale: niceScale.domain(domain),
|
|
137
|
-
|
|
137
|
+
tickNumber
|
|
138
138
|
});
|
|
139
139
|
});
|
|
140
140
|
const allYAxis = [...(yAxis?.map((axis, index) => _extends({
|
|
@@ -157,13 +157,13 @@ function CartesianContextProvider({
|
|
|
157
157
|
barGapRatio: 0
|
|
158
158
|
}, axis, {
|
|
159
159
|
scale: scaleBand(axis.data, [range[1], range[0]]).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
|
|
160
|
-
|
|
160
|
+
tickNumber: axis.data.length
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
if (isPointScaleConfig(axis)) {
|
|
164
164
|
completedYAxis[axis.id] = _extends({}, axis, {
|
|
165
165
|
scale: scalePoint(axis.data, [range[1], range[0]]),
|
|
166
|
-
|
|
166
|
+
tickNumber: axis.data.length
|
|
167
167
|
});
|
|
168
168
|
}
|
|
169
169
|
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
|
|
@@ -172,17 +172,17 @@ function CartesianContextProvider({
|
|
|
172
172
|
}
|
|
173
173
|
const scaleType = axis.scaleType ?? 'linear';
|
|
174
174
|
const extremums = [axis.min ?? minData, axis.max ?? maxData];
|
|
175
|
-
const
|
|
175
|
+
const tickNumber = getTickNumber(_extends({}, axis, {
|
|
176
176
|
range,
|
|
177
177
|
domain: extremums
|
|
178
178
|
}));
|
|
179
|
-
const niceScale = getScale(scaleType, extremums, range).nice(
|
|
179
|
+
const niceScale = getScale(scaleType, extremums, range).nice(tickNumber);
|
|
180
180
|
const niceDomain = niceScale.domain();
|
|
181
181
|
const domain = [axis.min ?? niceDomain[0], axis.max ?? niceDomain[1]];
|
|
182
182
|
completedYAxis[axis.id] = _extends({}, axis, {
|
|
183
183
|
scaleType,
|
|
184
184
|
scale: niceScale.domain(domain),
|
|
185
|
-
|
|
185
|
+
tickNumber
|
|
186
186
|
});
|
|
187
187
|
});
|
|
188
188
|
return {
|
|
@@ -6,6 +6,8 @@ const useChartDimensions = (width, height, margin) => {
|
|
|
6
6
|
const drawingArea = React.useMemo(() => ({
|
|
7
7
|
left: defaultizedMargin.left,
|
|
8
8
|
top: defaultizedMargin.top,
|
|
9
|
+
right: defaultizedMargin.right,
|
|
10
|
+
bottom: defaultizedMargin.bottom,
|
|
9
11
|
width: Math.max(0, width - defaultizedMargin.left - defaultizedMargin.right),
|
|
10
12
|
height: Math.max(0, height - defaultizedMargin.top - defaultizedMargin.bottom)
|
|
11
13
|
}), [width, height, defaultizedMargin.top, defaultizedMargin.bottom, defaultizedMargin.left, defaultizedMargin.right]);
|
package/modern/hooks/useTicks.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { isBandScale } from '../internals/isBandScale';
|
|
3
|
-
export function
|
|
3
|
+
export function getTickNumber(params) {
|
|
4
4
|
const {
|
|
5
5
|
tickMaxStep,
|
|
6
6
|
tickMinStep,
|
|
@@ -16,7 +16,7 @@ export function getTicksNumber(params) {
|
|
|
16
16
|
function useTicks(options) {
|
|
17
17
|
const {
|
|
18
18
|
scale,
|
|
19
|
-
|
|
19
|
+
tickNumber,
|
|
20
20
|
valueFormatter
|
|
21
21
|
} = options;
|
|
22
22
|
return React.useMemo(() => {
|
|
@@ -43,11 +43,11 @@ function useTicks(options) {
|
|
|
43
43
|
labelOffset: 0
|
|
44
44
|
}));
|
|
45
45
|
}
|
|
46
|
-
return scale.ticks(
|
|
47
|
-
formattedValue: valueFormatter?.(value) ?? scale.tickFormat(
|
|
46
|
+
return scale.ticks(tickNumber).map(value => ({
|
|
47
|
+
formattedValue: valueFormatter?.(value) ?? scale.tickFormat(tickNumber)(value),
|
|
48
48
|
offset: scale(value),
|
|
49
49
|
labelOffset: 0
|
|
50
50
|
}));
|
|
51
|
-
}, [
|
|
51
|
+
}, [tickNumber, scale, valueFormatter]);
|
|
52
52
|
}
|
|
53
53
|
export default useTicks;
|
package/modern/index.js
CHANGED
|
@@ -5,77 +5,22 @@ export const AxisRoot = styled('g', {
|
|
|
5
5
|
name: 'MuiChartsAxis',
|
|
6
6
|
slot: 'Root',
|
|
7
7
|
overridesResolver: (props, styles) => styles.root
|
|
8
|
-
})({
|
|
9
|
-
[`&.${axisClasses.directionY}`]: {
|
|
10
|
-
[`.${axisClasses.tickLabel}`]: {
|
|
11
|
-
dominantBaseline: 'middle'
|
|
12
|
-
},
|
|
13
|
-
[`.${axisClasses.label}`]: {
|
|
14
|
-
dominantBaseline: 'auto',
|
|
15
|
-
textAnchor: 'middle'
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
[`&.${axisClasses.left}`]: {
|
|
19
|
-
[`.${axisClasses.tickLabel}`]: {
|
|
20
|
-
dominantBaseline: 'central',
|
|
21
|
-
textAnchor: 'end'
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
[`&.${axisClasses.right}`]: {
|
|
25
|
-
[`.${axisClasses.tickLabel}`]: {
|
|
26
|
-
dominantBaseline: 'central',
|
|
27
|
-
textAnchor: 'start'
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
[`&.${axisClasses.bottom}`]: {
|
|
31
|
-
[`.${axisClasses.tickLabel}, .${axisClasses.label}`]: {
|
|
32
|
-
dominantBaseline: 'hanging',
|
|
33
|
-
textAnchor: 'middle'
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
[`&.${axisClasses.top}`]: {
|
|
37
|
-
[`.${axisClasses.tickLabel}, .${axisClasses.label}`]: {
|
|
38
|
-
dominantBaseline: 'baseline',
|
|
39
|
-
textAnchor: 'middle'
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
export const ChartsLine = styled('line', {
|
|
44
|
-
name: 'MuiChartsAxis',
|
|
45
|
-
slot: 'Line',
|
|
46
|
-
overridesResolver: (props, styles) => styles.line
|
|
47
|
-
})(({
|
|
48
|
-
theme
|
|
49
|
-
}) => ({
|
|
50
|
-
stroke: (theme.vars || theme).palette.text.primary,
|
|
51
|
-
shapeRendering: 'crispEdges',
|
|
52
|
-
strokeWidth: 1
|
|
53
|
-
}));
|
|
54
|
-
export const ChartsTick = styled('line', {
|
|
55
|
-
name: 'MuiChartsAxis',
|
|
56
|
-
slot: 'Tick',
|
|
57
|
-
overridesResolver: (props, styles) => styles.tick
|
|
58
8
|
})(({
|
|
59
9
|
theme
|
|
60
10
|
}) => ({
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
overridesResolver: (props, styles) => styles.label
|
|
77
|
-
})(({
|
|
78
|
-
theme
|
|
79
|
-
}) => _extends({}, theme.typography.body1, {
|
|
80
|
-
fill: (theme.vars || theme).palette.text.primary
|
|
11
|
+
[`& .${axisClasses.tickLabel}`]: _extends({}, theme.typography.caption, {
|
|
12
|
+
fill: (theme.vars || theme).palette.text.primary
|
|
13
|
+
}),
|
|
14
|
+
[`& .${axisClasses.label}`]: _extends({}, theme.typography.body1, {
|
|
15
|
+
fill: (theme.vars || theme).palette.text.primary
|
|
16
|
+
}),
|
|
17
|
+
[`& .${axisClasses.line}`]: {
|
|
18
|
+
stroke: (theme.vars || theme).palette.text.primary,
|
|
19
|
+
shapeRendering: 'crispEdges',
|
|
20
|
+
strokeWidth: 1
|
|
21
|
+
},
|
|
22
|
+
[`& .${axisClasses.tick}`]: {
|
|
23
|
+
stroke: (theme.vars || theme).palette.text.primary,
|
|
24
|
+
shapeRendering: 'crispEdges'
|
|
25
|
+
}
|
|
81
26
|
}));
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
2
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
3
|
+
const _excluded = ["x", "y", "textAnchor", "dominantBaseline", "style", "text", "ownerState"];
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import { getStringSize } from '../domUtils';
|
|
6
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
7
|
+
export function getWordsByLines({
|
|
8
|
+
style,
|
|
9
|
+
needsComputation,
|
|
10
|
+
text
|
|
11
|
+
}) {
|
|
12
|
+
return text.split('\n').map(subText => _extends({
|
|
13
|
+
text: subText
|
|
14
|
+
}, needsComputation ? getStringSize(subText, style) : {
|
|
15
|
+
width: 0,
|
|
16
|
+
height: 0
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
export function ChartsText(props) {
|
|
20
|
+
const {
|
|
21
|
+
x,
|
|
22
|
+
y,
|
|
23
|
+
textAnchor = 'start',
|
|
24
|
+
dominantBaseline = 'central',
|
|
25
|
+
style,
|
|
26
|
+
text
|
|
27
|
+
} = props,
|
|
28
|
+
textProps = _objectWithoutPropertiesLoose(props, _excluded);
|
|
29
|
+
const wordsByLines = React.useMemo(() => getWordsByLines({
|
|
30
|
+
style,
|
|
31
|
+
needsComputation: text.includes('\n'),
|
|
32
|
+
text
|
|
33
|
+
}), [style, text]);
|
|
34
|
+
let startDy;
|
|
35
|
+
switch (dominantBaseline) {
|
|
36
|
+
case 'hanging':
|
|
37
|
+
startDy = 0;
|
|
38
|
+
break;
|
|
39
|
+
case 'central':
|
|
40
|
+
startDy = (wordsByLines.length - 1) / 2 * -wordsByLines[0].height;
|
|
41
|
+
break;
|
|
42
|
+
default:
|
|
43
|
+
startDy = (wordsByLines.length - 1) * -wordsByLines[0].height;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// const transforms = [];
|
|
48
|
+
// if (scaleToFit) {
|
|
49
|
+
// const lineWidth = wordsByLines[0].width;
|
|
50
|
+
// transforms.push(`scale(${(isNumber(width as number) ? (width as number) / lineWidth : 1) / lineWidth})`);
|
|
51
|
+
// }
|
|
52
|
+
// if (angle) {
|
|
53
|
+
// transforms.push(`rotate(${angle}, ${x}, ${y})`);
|
|
54
|
+
// }
|
|
55
|
+
// if (transforms.length) {
|
|
56
|
+
// textProps.transform = transforms.join(' ');
|
|
57
|
+
// }
|
|
58
|
+
|
|
59
|
+
return /*#__PURE__*/_jsx("text", _extends({}, textProps, {
|
|
60
|
+
x: x,
|
|
61
|
+
y: y,
|
|
62
|
+
textAnchor: textAnchor,
|
|
63
|
+
dominantBaseline: dominantBaseline,
|
|
64
|
+
style: style,
|
|
65
|
+
children: wordsByLines.map((line, index) => /*#__PURE__*/_jsx("tspan", {
|
|
66
|
+
x: x,
|
|
67
|
+
dy: `${index === 0 ? startDy : wordsByLines[0].height}px`,
|
|
68
|
+
children: line.text
|
|
69
|
+
}, index))
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
// DOM utils taken from
|
|
3
|
+
// https://github.com/recharts/recharts/blob/master/src/util/DOMUtils.ts
|
|
4
|
+
|
|
5
|
+
const isSsr = () => !(typeof window !== 'undefined' && window.document && window.setTimeout);
|
|
6
|
+
const stringCache = {
|
|
7
|
+
widthCache: {},
|
|
8
|
+
cacheCount: 0
|
|
9
|
+
};
|
|
10
|
+
const MAX_CACHE_NUM = 2000;
|
|
11
|
+
const SPAN_STYLE = {
|
|
12
|
+
position: 'absolute',
|
|
13
|
+
top: '-20000px',
|
|
14
|
+
left: 0,
|
|
15
|
+
padding: 0,
|
|
16
|
+
margin: 0,
|
|
17
|
+
border: 'none',
|
|
18
|
+
whiteSpace: 'pre'
|
|
19
|
+
};
|
|
20
|
+
const STYLE_LIST = ['minWidth', 'maxWidth', 'width', 'minHeight', 'maxHeight', 'height', 'top', 'left', 'fontSize', 'padding', 'margin', 'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom', 'marginLeft', 'marginRight', 'marginTop', 'marginBottom'];
|
|
21
|
+
const MEASUREMENT_SPAN_ID = 'mui_measurement_span';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param name CSS property name
|
|
26
|
+
* @param value
|
|
27
|
+
* @returns add 'px' for distance properties
|
|
28
|
+
*/
|
|
29
|
+
function autoCompleteStyle(name, value) {
|
|
30
|
+
if (STYLE_LIST.indexOf(name) >= 0 && value === +value) {
|
|
31
|
+
return `${value}px`;
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @param text camelcase css property
|
|
39
|
+
* @returns css property
|
|
40
|
+
*/
|
|
41
|
+
function camelToMiddleLine(text) {
|
|
42
|
+
const strs = text.split('');
|
|
43
|
+
const formatStrs = strs.reduce((result, entry) => {
|
|
44
|
+
if (entry === entry.toUpperCase()) {
|
|
45
|
+
return [...result, '-', entry.toLowerCase()];
|
|
46
|
+
}
|
|
47
|
+
return [...result, entry];
|
|
48
|
+
}, []);
|
|
49
|
+
return formatStrs.join('');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param style React style object
|
|
55
|
+
* @returns CSS styling string
|
|
56
|
+
*/
|
|
57
|
+
export const getStyleString = style => Object.keys(style).sort().reduce((result, s) => `${result}${camelToMiddleLine(s)}:${autoCompleteStyle(s, style[s])};`, '');
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* @param text The string to estimate
|
|
62
|
+
* @param style The style applied
|
|
63
|
+
* @returns width and height of the text
|
|
64
|
+
*/
|
|
65
|
+
export const getStringSize = (text, style = {}) => {
|
|
66
|
+
if (text === undefined || text === null || isSsr()) {
|
|
67
|
+
return {
|
|
68
|
+
width: 0,
|
|
69
|
+
height: 0
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const str = `${text}`;
|
|
73
|
+
const styleString = getStyleString(style);
|
|
74
|
+
const cacheKey = `${str}-${styleString}`;
|
|
75
|
+
if (stringCache.widthCache[cacheKey]) {
|
|
76
|
+
return stringCache.widthCache[cacheKey];
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
let measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
|
|
80
|
+
if (measurementSpan === null) {
|
|
81
|
+
measurementSpan = document.createElement('span');
|
|
82
|
+
measurementSpan.setAttribute('id', MEASUREMENT_SPAN_ID);
|
|
83
|
+
measurementSpan.setAttribute('aria-hidden', 'true');
|
|
84
|
+
document.body.appendChild(measurementSpan);
|
|
85
|
+
}
|
|
86
|
+
// Need to use CSS Object Model (CSSOM) to be able to comply with Content Security Policy (CSP)
|
|
87
|
+
// https://en.wikipedia.org/wiki/Content_Security_Policy
|
|
88
|
+
const measurementSpanStyle = _extends({}, SPAN_STYLE, style);
|
|
89
|
+
Object.keys(measurementSpanStyle).map(styleKey => {
|
|
90
|
+
measurementSpan.style[camelToMiddleLine(styleKey)] = autoCompleteStyle(styleKey, measurementSpanStyle[styleKey]);
|
|
91
|
+
return styleKey;
|
|
92
|
+
});
|
|
93
|
+
measurementSpan.textContent = str;
|
|
94
|
+
const rect = measurementSpan.getBoundingClientRect();
|
|
95
|
+
const result = {
|
|
96
|
+
width: rect.width,
|
|
97
|
+
height: rect.height
|
|
98
|
+
};
|
|
99
|
+
stringCache.widthCache[cacheKey] = result;
|
|
100
|
+
if (stringCache.cacheCount + 1 > MAX_CACHE_NUM) {
|
|
101
|
+
stringCache.cacheCount = 0;
|
|
102
|
+
stringCache.widthCache = {};
|
|
103
|
+
} else {
|
|
104
|
+
stringCache.cacheCount += 1;
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
} catch (e) {
|
|
108
|
+
return {
|
|
109
|
+
width: 0,
|
|
110
|
+
height: 0
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
};
|