@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
package/esm/PieChart/PieChart.js
CHANGED
|
@@ -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/esm/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;
|
|
@@ -113,13 +113,13 @@ function CartesianContextProvider({
|
|
|
113
113
|
barGapRatio
|
|
114
114
|
}, axis, {
|
|
115
115
|
scale: scaleBand(axis.data, range).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
|
|
116
|
-
|
|
116
|
+
tickNumber: axis.data.length
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
119
|
if (isPointScaleConfig(axis)) {
|
|
120
120
|
completedXAxis[axis.id] = _extends({}, axis, {
|
|
121
121
|
scale: scalePoint(axis.data, range),
|
|
122
|
-
|
|
122
|
+
tickNumber: axis.data.length
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
125
|
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
|
|
@@ -128,17 +128,17 @@ function CartesianContextProvider({
|
|
|
128
128
|
}
|
|
129
129
|
const scaleType = (_axis$scaleType = axis.scaleType) != null ? _axis$scaleType : 'linear';
|
|
130
130
|
const extremums = [(_axis$min = axis.min) != null ? _axis$min : minData, (_axis$max = axis.max) != null ? _axis$max : maxData];
|
|
131
|
-
const
|
|
131
|
+
const tickNumber = getTickNumber(_extends({}, axis, {
|
|
132
132
|
range,
|
|
133
133
|
domain: extremums
|
|
134
134
|
}));
|
|
135
|
-
const niceScale = getScale(scaleType, extremums, range).nice(
|
|
135
|
+
const niceScale = getScale(scaleType, extremums, range).nice(tickNumber);
|
|
136
136
|
const niceDomain = niceScale.domain();
|
|
137
137
|
const domain = [(_axis$min2 = axis.min) != null ? _axis$min2 : niceDomain[0], (_axis$max2 = axis.max) != null ? _axis$max2 : niceDomain[1]];
|
|
138
138
|
completedXAxis[axis.id] = _extends({}, axis, {
|
|
139
139
|
scaleType,
|
|
140
140
|
scale: niceScale.domain(domain),
|
|
141
|
-
|
|
141
|
+
tickNumber
|
|
142
142
|
});
|
|
143
143
|
});
|
|
144
144
|
const allYAxis = [...((_yAxis$map = yAxis == null ? void 0 : yAxis.map((axis, index) => _extends({
|
|
@@ -163,13 +163,13 @@ function CartesianContextProvider({
|
|
|
163
163
|
barGapRatio: 0
|
|
164
164
|
}, axis, {
|
|
165
165
|
scale: scaleBand(axis.data, [range[1], range[0]]).paddingInner(categoryGapRatio).paddingOuter(categoryGapRatio / 2),
|
|
166
|
-
|
|
166
|
+
tickNumber: axis.data.length
|
|
167
167
|
});
|
|
168
168
|
}
|
|
169
169
|
if (isPointScaleConfig(axis)) {
|
|
170
170
|
completedYAxis[axis.id] = _extends({}, axis, {
|
|
171
171
|
scale: scalePoint(axis.data, [range[1], range[0]]),
|
|
172
|
-
|
|
172
|
+
tickNumber: axis.data.length
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
175
|
if (axis.scaleType === 'band' || axis.scaleType === 'point') {
|
|
@@ -178,17 +178,17 @@ function CartesianContextProvider({
|
|
|
178
178
|
}
|
|
179
179
|
const scaleType = (_axis$scaleType2 = axis.scaleType) != null ? _axis$scaleType2 : 'linear';
|
|
180
180
|
const extremums = [(_axis$min3 = axis.min) != null ? _axis$min3 : minData, (_axis$max3 = axis.max) != null ? _axis$max3 : maxData];
|
|
181
|
-
const
|
|
181
|
+
const tickNumber = getTickNumber(_extends({}, axis, {
|
|
182
182
|
range,
|
|
183
183
|
domain: extremums
|
|
184
184
|
}));
|
|
185
|
-
const niceScale = getScale(scaleType, extremums, range).nice(
|
|
185
|
+
const niceScale = getScale(scaleType, extremums, range).nice(tickNumber);
|
|
186
186
|
const niceDomain = niceScale.domain();
|
|
187
187
|
const domain = [(_axis$min4 = axis.min) != null ? _axis$min4 : niceDomain[0], (_axis$max4 = axis.max) != null ? _axis$max4 : niceDomain[1]];
|
|
188
188
|
completedYAxis[axis.id] = _extends({}, axis, {
|
|
189
189
|
scaleType,
|
|
190
190
|
scale: niceScale.domain(domain),
|
|
191
|
-
|
|
191
|
+
tickNumber
|
|
192
192
|
});
|
|
193
193
|
});
|
|
194
194
|
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/esm/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(() => {
|
|
@@ -49,14 +49,14 @@ function useTicks(options) {
|
|
|
49
49
|
};
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
|
-
return scale.ticks(
|
|
52
|
+
return scale.ticks(tickNumber).map(value => {
|
|
53
53
|
var _valueFormatter3;
|
|
54
54
|
return {
|
|
55
|
-
formattedValue: (_valueFormatter3 = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter3 : scale.tickFormat(
|
|
55
|
+
formattedValue: (_valueFormatter3 = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter3 : scale.tickFormat(tickNumber)(value),
|
|
56
56
|
offset: scale(value),
|
|
57
57
|
labelOffset: 0
|
|
58
58
|
};
|
|
59
59
|
});
|
|
60
|
-
}, [
|
|
60
|
+
}, [tickNumber, scale, valueFormatter]);
|
|
61
61
|
}
|
|
62
62
|
export default useTicks;
|
|
@@ -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
|
+
};
|
|
@@ -15,6 +15,8 @@ const useChartDimensions = (width, height, margin) => {
|
|
|
15
15
|
const drawingArea = React.useMemo(() => ({
|
|
16
16
|
left: defaultizedMargin.left,
|
|
17
17
|
top: defaultizedMargin.top,
|
|
18
|
+
right: defaultizedMargin.right,
|
|
19
|
+
bottom: defaultizedMargin.bottom,
|
|
18
20
|
width: Math.max(0, width - defaultizedMargin.left - defaultizedMargin.right),
|
|
19
21
|
height: Math.max(0, height - defaultizedMargin.top - defaultizedMargin.bottom)
|
|
20
22
|
}), [width, height, defaultizedMargin.top, defaultizedMargin.bottom, defaultizedMargin.left, defaultizedMargin.right]);
|
package/hooks/useTicks.d.ts
CHANGED
|
@@ -18,15 +18,14 @@ export interface TickParams {
|
|
|
18
18
|
*/
|
|
19
19
|
tickNumber?: number;
|
|
20
20
|
}
|
|
21
|
-
export declare function
|
|
21
|
+
export declare function getTickNumber(params: TickParams & {
|
|
22
22
|
range: any[];
|
|
23
23
|
domain: any[];
|
|
24
24
|
}): number;
|
|
25
25
|
declare function useTicks(options: {
|
|
26
26
|
scale: D3Scale;
|
|
27
|
-
ticksNumber?: number;
|
|
28
27
|
valueFormatter?: (value: any) => string;
|
|
29
|
-
}): {
|
|
28
|
+
} & Pick<TickParams, 'tickNumber'>): {
|
|
30
29
|
formattedValue: any;
|
|
31
30
|
offset: number;
|
|
32
31
|
labelOffset: number;
|
package/hooks/useTicks.js
CHANGED
|
@@ -4,12 +4,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
exports.
|
|
7
|
+
exports.getTickNumber = getTickNumber;
|
|
8
8
|
var React = _interopRequireWildcard(require("react"));
|
|
9
9
|
var _isBandScale = require("../internals/isBandScale");
|
|
10
10
|
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); }
|
|
11
11
|
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; }
|
|
12
|
-
function
|
|
12
|
+
function getTickNumber(params) {
|
|
13
13
|
const {
|
|
14
14
|
tickMaxStep,
|
|
15
15
|
tickMinStep,
|
|
@@ -25,7 +25,7 @@ function getTicksNumber(params) {
|
|
|
25
25
|
function useTicks(options) {
|
|
26
26
|
const {
|
|
27
27
|
scale,
|
|
28
|
-
|
|
28
|
+
tickNumber,
|
|
29
29
|
valueFormatter
|
|
30
30
|
} = options;
|
|
31
31
|
return React.useMemo(() => {
|
|
@@ -52,11 +52,11 @@ function useTicks(options) {
|
|
|
52
52
|
labelOffset: 0
|
|
53
53
|
}));
|
|
54
54
|
}
|
|
55
|
-
return scale.ticks(
|
|
56
|
-
formattedValue: valueFormatter?.(value) ?? scale.tickFormat(
|
|
55
|
+
return scale.ticks(tickNumber).map(value => ({
|
|
56
|
+
formattedValue: valueFormatter?.(value) ?? scale.tickFormat(tickNumber)(value),
|
|
57
57
|
offset: scale(value),
|
|
58
58
|
labelOffset: 0
|
|
59
59
|
}));
|
|
60
|
-
}, [
|
|
60
|
+
}, [tickNumber, scale, valueFormatter]);
|
|
61
61
|
}
|
|
62
62
|
var _default = exports.default = useTicks;
|
package/index.js
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
export declare const AxisRoot: import("@emotion/styled").StyledComponent<import("@mui/system").MUIStyledCommonProps<import("@mui/material/styles").Theme>, Pick<import("react").SVGProps<SVGGElement>, keyof import("react").SVGProps<SVGGElement>>, {}>;
|
|
3
|
-
export declare const ChartsLine: import("@emotion/styled").StyledComponent<import("@mui/system").MUIStyledCommonProps<import("@mui/material/styles").Theme>, Pick<import("react").SVGLineElementAttributes<SVGLineElement>, keyof import("react").SVGLineElementAttributes<SVGLineElement>>, {}>;
|
|
4
|
-
export declare const ChartsTick: import("@emotion/styled").StyledComponent<import("@mui/system").MUIStyledCommonProps<import("@mui/material/styles").Theme>, Pick<import("react").SVGLineElementAttributes<SVGLineElement>, keyof import("react").SVGLineElementAttributes<SVGLineElement>>, {}>;
|
|
5
|
-
export declare const ChartsTickLabel: import("@emotion/styled").StyledComponent<import("@mui/system").MUIStyledCommonProps<import("@mui/material/styles").Theme>, Pick<import("react").SVGTextElementAttributes<SVGTextElement>, keyof import("react").SVGTextElementAttributes<SVGTextElement>>, {}>;
|
|
6
|
-
export declare const ChartsLabel: import("@emotion/styled").StyledComponent<import("@mui/system").MUIStyledCommonProps<import("@mui/material/styles").Theme>, Pick<import("react").SVGTextElementAttributes<SVGTextElement>, keyof import("react").SVGTextElementAttributes<SVGTextElement>>, {}>;
|