@mui/x-charts-pro 9.0.1 → 9.0.3
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/BarChartPro/BarChartPro.js +20 -1
- package/BarChartPro/BarChartPro.mjs +20 -1
- package/CHANGELOG.md +257 -0
- package/ChartsDataProviderPro/ChartsDataProviderPro.js +2 -2
- package/ChartsDataProviderPro/ChartsDataProviderPro.mjs +2 -2
- package/ChartsRadialDataProvider/index.d.mts +1 -0
- package/ChartsRadialDataProvider/index.d.ts +1 -0
- package/ChartsRadialDataProvider/index.js +16 -0
- package/ChartsRadialDataProvider/index.mjs +2 -0
- package/ChartsRadialGrid/index.d.mts +1 -0
- package/ChartsRadialGrid/index.d.ts +1 -0
- package/ChartsRadialGrid/index.js +16 -0
- package/ChartsRadialGrid/index.mjs +2 -0
- package/ChartsRadiusAxis/index.d.mts +1 -0
- package/ChartsRadiusAxis/index.d.ts +1 -0
- package/ChartsRadiusAxis/index.js +16 -0
- package/ChartsRadiusAxis/index.mjs +2 -0
- package/ChartsRotationAxis/index.d.mts +1 -0
- package/ChartsRotationAxis/index.d.ts +1 -0
- package/ChartsRotationAxis/index.js +16 -0
- package/ChartsRotationAxis/index.mjs +2 -0
- package/ChartsToolbarPro/ChartsToolbarImageExportTrigger.js +1 -0
- package/ChartsToolbarPro/ChartsToolbarImageExportTrigger.mjs +1 -0
- package/ChartsToolbarPro/ChartsToolbarPrintExportTrigger.js +1 -0
- package/ChartsToolbarPro/ChartsToolbarPrintExportTrigger.mjs +1 -0
- package/ChartsToolbarPro/ChartsToolbarPro.d.mts +30 -0
- package/ChartsToolbarPro/ChartsToolbarPro.d.ts +30 -0
- package/ChartsToolbarPro/ChartsToolbarPro.js +66 -17
- package/ChartsToolbarPro/ChartsToolbarPro.mjs +66 -17
- package/ChartsToolbarPro/ChartsToolbarRangeButtonTrigger.d.mts +41 -0
- package/ChartsToolbarPro/ChartsToolbarRangeButtonTrigger.d.ts +41 -0
- package/ChartsToolbarPro/ChartsToolbarRangeButtonTrigger.js +156 -0
- package/ChartsToolbarPro/ChartsToolbarRangeButtonTrigger.mjs +150 -0
- package/ChartsToolbarPro/ChartsToolbarZoomInTrigger.js +1 -1
- package/ChartsToolbarPro/ChartsToolbarZoomInTrigger.mjs +1 -1
- package/ChartsToolbarPro/ChartsToolbarZoomOutTrigger.js +1 -1
- package/ChartsToolbarPro/ChartsToolbarZoomOutTrigger.mjs +1 -1
- package/ChartsToolbarPro/index.d.mts +1 -0
- package/ChartsToolbarPro/index.d.ts +1 -0
- package/ChartsToolbarPro/index.js +11 -0
- package/ChartsToolbarPro/index.mjs +1 -0
- package/ChartsToolbarPro/rangeButtonValueToZoom.d.mts +66 -0
- package/ChartsToolbarPro/rangeButtonValueToZoom.d.ts +66 -0
- package/ChartsToolbarPro/rangeButtonValueToZoom.js +217 -0
- package/ChartsToolbarPro/rangeButtonValueToZoom.mjs +212 -0
- package/Heatmap/Heatmap.js +4 -1
- package/Heatmap/Heatmap.mjs +4 -1
- package/LineChartPro/LineChartPro.js +20 -1
- package/LineChartPro/LineChartPro.mjs +20 -1
- package/ScatterChartPro/ScatterChartPro.js +20 -1
- package/ScatterChartPro/ScatterChartPro.mjs +20 -1
- package/index.d.mts +4 -0
- package/index.d.ts +4 -0
- package/index.js +45 -1
- package/index.mjs +5 -1
- package/internals/plugins/useChartProZoom/gestureHooks/useZoom.utils.js +3 -3
- package/internals/plugins/useChartProZoom/gestureHooks/useZoom.utils.mjs +3 -3
- package/internals/plugins/useChartProZoom/useChartProZoom.js +13 -4
- package/internals/plugins/useChartProZoom/useChartProZoom.mjs +13 -4
- package/internals/plugins/useChartProZoom/useChartProZoom.selectors.d.mts +3 -0
- package/internals/plugins/useChartProZoom/useChartProZoom.selectors.d.ts +3 -0
- package/internals/plugins/useChartProZoom/useChartProZoom.selectors.js +4 -3
- package/internals/plugins/useChartProZoom/useChartProZoom.selectors.mjs +3 -2
- package/internals/plugins/useChartProZoom/useChartProZoom.types.d.mts +10 -0
- package/internals/plugins/useChartProZoom/useChartProZoom.types.d.ts +10 -0
- package/models/seriesType/heatmap.d.mts +9 -1
- package/models/seriesType/heatmap.d.ts +9 -1
- package/package.json +62 -6
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
4
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
5
|
+
const _excluded = ["render", "value", "axisId", "label", "size"];
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import PropTypes from 'prop-types';
|
|
8
|
+
import { useChartsContext, useChartsSlots, selectorChartRawXAxis, selectorChartXAxisWithDomains, selectorChartZoomOptionsLookup } from '@mui/x-charts/internals';
|
|
9
|
+
import { useComponentRenderer } from '@mui/x-internals/useComponentRenderer';
|
|
10
|
+
import { selectorChartActiveRangeButtonKey, selectorChartCanZoomOut } from "../internals/plugins/useChartProZoom/index.mjs";
|
|
11
|
+
import { rangeButtonValueToZoom } from "./rangeButtonValueToZoom.mjs";
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
/**
|
|
14
|
+
* A button that sets the chart zoom to a predefined range.
|
|
15
|
+
* It renders the `baseToggleButton` slot.
|
|
16
|
+
*/
|
|
17
|
+
const ChartsToolbarRangeButtonTrigger = /*#__PURE__*/React.forwardRef(function ChartsToolbarRangeButtonTrigger(_ref, ref) {
|
|
18
|
+
let {
|
|
19
|
+
render,
|
|
20
|
+
value,
|
|
21
|
+
axisId: axisIdProp,
|
|
22
|
+
label,
|
|
23
|
+
size = 'small'
|
|
24
|
+
} = _ref,
|
|
25
|
+
other = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
26
|
+
const {
|
|
27
|
+
slots,
|
|
28
|
+
slotProps
|
|
29
|
+
} = useChartsSlots();
|
|
30
|
+
const {
|
|
31
|
+
instance,
|
|
32
|
+
store
|
|
33
|
+
} = useChartsContext();
|
|
34
|
+
const activeRangeButtonKey = store.use(selectorChartActiveRangeButtonKey);
|
|
35
|
+
const canZoomOut = store.use(selectorChartCanZoomOut);
|
|
36
|
+
const zoomOptionsLookup = store.use(selectorChartZoomOptionsLookup);
|
|
37
|
+
const rawXAxes = store.use(selectorChartRawXAxis);
|
|
38
|
+
const {
|
|
39
|
+
domains
|
|
40
|
+
} = store.use(selectorChartXAxisWithDomains);
|
|
41
|
+
|
|
42
|
+
// Resolve the target axis ID: use the prop, or find the first zoomable x-axis.
|
|
43
|
+
const resolvedAxisId = React.useMemo(() => {
|
|
44
|
+
if (axisIdProp !== undefined) {
|
|
45
|
+
return axisIdProp;
|
|
46
|
+
}
|
|
47
|
+
if (!rawXAxes) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return rawXAxes.find(axis => zoomOptionsLookup[axis.id] !== undefined)?.id;
|
|
51
|
+
}, [axisIdProp, rawXAxes, zoomOptionsLookup]);
|
|
52
|
+
|
|
53
|
+
// Determine if the resolved axis is ordinal (band/point) to use index-based domain.
|
|
54
|
+
const resolvedAxis = React.useMemo(() => rawXAxes?.find(axis => axis.id === resolvedAxisId), [rawXAxes, resolvedAxisId]);
|
|
55
|
+
const isOrdinal = resolvedAxis?.scaleType === 'band' || resolvedAxis?.scaleType === 'point';
|
|
56
|
+
|
|
57
|
+
// Get the full domain for the target axis, ignoring the current zoom.
|
|
58
|
+
// For ordinal axes (band/point), use index-based range since domain values are categories.
|
|
59
|
+
const axisDomain = React.useMemo(() => {
|
|
60
|
+
if (resolvedAxisId === undefined) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const domainDef = domains[resolvedAxisId];
|
|
64
|
+
if (!domainDef || domainDef.domain.length < 2) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
if (isOrdinal) {
|
|
68
|
+
return {
|
|
69
|
+
min: 0,
|
|
70
|
+
max: domainDef.domain.length - 1
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const min = domainDef.domain[0];
|
|
74
|
+
const max = domainDef.domain[domainDef.domain.length - 1];
|
|
75
|
+
return {
|
|
76
|
+
min: Number(min),
|
|
77
|
+
max: Number(max)
|
|
78
|
+
};
|
|
79
|
+
}, [resolvedAxisId, domains, isOrdinal]);
|
|
80
|
+
const handleClick = React.useCallback(() => {
|
|
81
|
+
if (resolvedAxisId === undefined || !axisDomain) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const zoom = rangeButtonValueToZoom(value, {
|
|
85
|
+
scaleType: resolvedAxis?.scaleType ?? 'linear',
|
|
86
|
+
data: resolvedAxis?.data,
|
|
87
|
+
domain: axisDomain
|
|
88
|
+
});
|
|
89
|
+
instance.setAxisZoomData(resolvedAxisId, {
|
|
90
|
+
axisId: resolvedAxisId,
|
|
91
|
+
start: zoom.start,
|
|
92
|
+
end: zoom.end
|
|
93
|
+
});
|
|
94
|
+
instance.setActiveRangeButtonKey(label);
|
|
95
|
+
}, [resolvedAxisId, resolvedAxis, axisDomain, value, instance, label]);
|
|
96
|
+
|
|
97
|
+
// Determine if this button is selected.
|
|
98
|
+
// When explicitly clicked, activeRangeButtonKey matches the label.
|
|
99
|
+
// When no button has been clicked and zoom is at full range, the null-value button is active.
|
|
100
|
+
const isActive = activeRangeButtonKey === label || activeRangeButtonKey === null && value === null && !canZoomOut;
|
|
101
|
+
const element = useComponentRenderer(slots.baseToggleButton, render, _extends({}, slotProps.baseToggleButton, {
|
|
102
|
+
onClick: handleClick,
|
|
103
|
+
selected: isActive,
|
|
104
|
+
value: label,
|
|
105
|
+
size
|
|
106
|
+
}, other, {
|
|
107
|
+
ref
|
|
108
|
+
}));
|
|
109
|
+
return /*#__PURE__*/_jsx(React.Fragment, {
|
|
110
|
+
children: element
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
if (process.env.NODE_ENV !== "production") ChartsToolbarRangeButtonTrigger.displayName = "ChartsToolbarRangeButtonTrigger";
|
|
114
|
+
process.env.NODE_ENV !== "production" ? ChartsToolbarRangeButtonTrigger.propTypes = {
|
|
115
|
+
// ----------------------------- Warning --------------------------------
|
|
116
|
+
// | These PropTypes are generated from the TypeScript type definitions |
|
|
117
|
+
// | To update them edit the TypeScript types and run "pnpm proptypes" |
|
|
118
|
+
// ----------------------------------------------------------------------
|
|
119
|
+
/**
|
|
120
|
+
* The axis ID to apply the range to.
|
|
121
|
+
* Defaults to the first x-axis with zoom enabled.
|
|
122
|
+
*/
|
|
123
|
+
axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
124
|
+
/**
|
|
125
|
+
* The label used to identify the button for active-state tracking.
|
|
126
|
+
*/
|
|
127
|
+
label: PropTypes.string.isRequired,
|
|
128
|
+
/**
|
|
129
|
+
* A function to customize the rendering of the component.
|
|
130
|
+
*/
|
|
131
|
+
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
|
|
132
|
+
/**
|
|
133
|
+
* The size of the button.
|
|
134
|
+
* @default 'small'
|
|
135
|
+
*/
|
|
136
|
+
size: PropTypes.oneOf(['large', 'medium', 'small']),
|
|
137
|
+
/**
|
|
138
|
+
* The range value. Specifies how far back from the end of the data to zoom.
|
|
139
|
+
*
|
|
140
|
+
* - `{ unit, step }` — A calendar interval from the end of the data (e.g., `{ unit: 'month', step: 3 }` for 3 months).
|
|
141
|
+
* - `[start, end]` — An absolute date range.
|
|
142
|
+
* - `(params) => { start, end }` — A function that receives axis context (`scaleType`, `data`, `domain`) and returns zoom percentages (0-100).
|
|
143
|
+
* - `null` — Resets zoom to show all data.
|
|
144
|
+
*/
|
|
145
|
+
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.instanceOf(Date).isRequired), PropTypes.func, PropTypes.shape({
|
|
146
|
+
step: PropTypes.number,
|
|
147
|
+
unit: PropTypes.oneOf(['day', 'hour', 'microsecond', 'millisecond', 'minute', 'month', 'second', 'week', 'year']).isRequired
|
|
148
|
+
})])
|
|
149
|
+
} : void 0;
|
|
150
|
+
export { ChartsToolbarRangeButtonTrigger };
|
|
@@ -33,7 +33,7 @@ const ChartsToolbarZoomInTrigger = exports.ChartsToolbarZoomInTrigger = /*#__PUR
|
|
|
33
33
|
instance,
|
|
34
34
|
store
|
|
35
35
|
} = (0, _internals.useChartsContext)();
|
|
36
|
-
const disabled = store.use(_useChartProZoom.selectorChartCanZoomIn);
|
|
36
|
+
const disabled = !store.use(_useChartProZoom.selectorChartCanZoomIn);
|
|
37
37
|
const element = (0, _useComponentRenderer.useComponentRenderer)(slots.baseButton, render, (0, _extends2.default)({}, slotProps.baseButton, {
|
|
38
38
|
onClick: () => instance.zoomIn(),
|
|
39
39
|
disabled
|
|
@@ -26,7 +26,7 @@ const ChartsToolbarZoomInTrigger = /*#__PURE__*/React.forwardRef(function Charts
|
|
|
26
26
|
instance,
|
|
27
27
|
store
|
|
28
28
|
} = useChartsContext();
|
|
29
|
-
const disabled = store.use(selectorChartCanZoomIn);
|
|
29
|
+
const disabled = !store.use(selectorChartCanZoomIn);
|
|
30
30
|
const element = useComponentRenderer(slots.baseButton, render, _extends({}, slotProps.baseButton, {
|
|
31
31
|
onClick: () => instance.zoomIn(),
|
|
32
32
|
disabled
|
|
@@ -33,7 +33,7 @@ const ChartsToolbarZoomOutTrigger = exports.ChartsToolbarZoomOutTrigger = /*#__P
|
|
|
33
33
|
instance,
|
|
34
34
|
store
|
|
35
35
|
} = (0, _internals.useChartsContext)();
|
|
36
|
-
const disabled = store.use(_useChartProZoom.selectorChartCanZoomOut);
|
|
36
|
+
const disabled = !store.use(_useChartProZoom.selectorChartCanZoomOut);
|
|
37
37
|
const element = (0, _useComponentRenderer.useComponentRenderer)(slots.baseButton, render, (0, _extends2.default)({}, slotProps.baseButton, {
|
|
38
38
|
onClick: () => instance.zoomOut(),
|
|
39
39
|
disabled
|
|
@@ -26,7 +26,7 @@ const ChartsToolbarZoomOutTrigger = /*#__PURE__*/React.forwardRef(function Chart
|
|
|
26
26
|
instance,
|
|
27
27
|
store
|
|
28
28
|
} = useChartsContext();
|
|
29
|
-
const disabled = store.use(selectorChartCanZoomOut);
|
|
29
|
+
const disabled = !store.use(selectorChartCanZoomOut);
|
|
30
30
|
const element = useComponentRenderer(slots.baseButton, render, _extends({}, slotProps.baseButton, {
|
|
31
31
|
onClick: () => instance.zoomOut(),
|
|
32
32
|
disabled
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./ChartsToolbarPro.mjs";
|
|
2
2
|
export * from "./ChartsToolbarZoomInTrigger.mjs";
|
|
3
3
|
export * from "./ChartsToolbarZoomOutTrigger.mjs";
|
|
4
|
+
export * from "./ChartsToolbarRangeButtonTrigger.mjs";
|
|
4
5
|
export * from "./ChartsToolbarPrintExportTrigger.mjs";
|
|
5
6
|
export * from "./ChartsToolbarImageExportTrigger.mjs";
|
|
6
7
|
export type { ChartsToolbarProSlots, ChartsToolbarProSlotProps } from "./Toolbar.types.mjs";
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./ChartsToolbarPro.js";
|
|
2
2
|
export * from "./ChartsToolbarZoomInTrigger.js";
|
|
3
3
|
export * from "./ChartsToolbarZoomOutTrigger.js";
|
|
4
|
+
export * from "./ChartsToolbarRangeButtonTrigger.js";
|
|
4
5
|
export * from "./ChartsToolbarPrintExportTrigger.js";
|
|
5
6
|
export * from "./ChartsToolbarImageExportTrigger.js";
|
|
6
7
|
export type { ChartsToolbarProSlots, ChartsToolbarProSlotProps } from "./Toolbar.types.js";
|
|
@@ -36,6 +36,17 @@ Object.keys(_ChartsToolbarZoomOutTrigger).forEach(function (key) {
|
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
|
+
var _ChartsToolbarRangeButtonTrigger = require("./ChartsToolbarRangeButtonTrigger");
|
|
40
|
+
Object.keys(_ChartsToolbarRangeButtonTrigger).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _ChartsToolbarRangeButtonTrigger[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _ChartsToolbarRangeButtonTrigger[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
39
50
|
var _ChartsToolbarPrintExportTrigger = require("./ChartsToolbarPrintExportTrigger");
|
|
40
51
|
Object.keys(_ChartsToolbarPrintExportTrigger).forEach(function (key) {
|
|
41
52
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./ChartsToolbarPro.mjs";
|
|
2
2
|
export * from "./ChartsToolbarZoomInTrigger.mjs";
|
|
3
3
|
export * from "./ChartsToolbarZoomOutTrigger.mjs";
|
|
4
|
+
export * from "./ChartsToolbarRangeButtonTrigger.mjs";
|
|
4
5
|
export * from "./ChartsToolbarPrintExportTrigger.mjs";
|
|
5
6
|
export * from "./ChartsToolbarImageExportTrigger.mjs";
|
|
6
7
|
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A calendar interval unit for range buttons.
|
|
3
|
+
*/
|
|
4
|
+
export type RangeButtonIntervalUnit = 'microsecond' | 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
|
|
5
|
+
/**
|
|
6
|
+
* Parameters passed to a range button function value.
|
|
7
|
+
*/
|
|
8
|
+
export interface RangeButtonFunctionParams {
|
|
9
|
+
/**
|
|
10
|
+
* The scale type of the axis (e.g., `'time'`, `'band'`, `'linear'`).
|
|
11
|
+
*/
|
|
12
|
+
scaleType: string;
|
|
13
|
+
/**
|
|
14
|
+
* The axis data values. Available for ordinal (band/point) axes.
|
|
15
|
+
*/
|
|
16
|
+
data: readonly unknown[] | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The full domain bounds, ignoring any current zoom.
|
|
19
|
+
* For time axes, these are timestamps. For ordinal axes, these are indices.
|
|
20
|
+
*/
|
|
21
|
+
domain: {
|
|
22
|
+
min: number;
|
|
23
|
+
max: number;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Defines the value of a range button.
|
|
28
|
+
*
|
|
29
|
+
* - `{ unit, step }` — A calendar interval from the end of the data.
|
|
30
|
+
* @example { unit: 'month', step: 3 } // Last 3 months
|
|
31
|
+
* @example { unit: 'year' } // Last year (step defaults to 1)
|
|
32
|
+
* - `[start, end]` — An absolute date range.
|
|
33
|
+
* @example [new Date(2024, 0, 1), new Date(2024, 6, 1)] // Jan–Jul 2024
|
|
34
|
+
* - `(params) => { start, end }` — A function that receives axis context (`scaleType`, `data`, `domain`) and returns zoom percentages (0-100).
|
|
35
|
+
* @example ({ domain }) => ({ start: 0, end: 50 }) // First half of data
|
|
36
|
+
* @example ({ data }) => {
|
|
37
|
+
* const lastFive = Math.max(0, data.length - 5);
|
|
38
|
+
* return { start: (lastFive / (data.length - 1)) * 100, end: 100 };
|
|
39
|
+
* } // Last 5 items on an ordinal axis
|
|
40
|
+
* - `null` — Resets zoom to show all data.
|
|
41
|
+
* @example null // Show all data
|
|
42
|
+
*/
|
|
43
|
+
export type RangeButtonValue = {
|
|
44
|
+
unit: RangeButtonIntervalUnit;
|
|
45
|
+
step?: number;
|
|
46
|
+
} | [Date, Date] | ((params: RangeButtonFunctionParams) => {
|
|
47
|
+
start: number;
|
|
48
|
+
end: number;
|
|
49
|
+
}) | null;
|
|
50
|
+
/**
|
|
51
|
+
* Converts a range button value to zoom start/end percentages.
|
|
52
|
+
*
|
|
53
|
+
* The range is calculated from the end of the axis domain.
|
|
54
|
+
* For example, `{ unit: 'month', step: 3 }` will zoom to show the last 3 months of data.
|
|
55
|
+
*
|
|
56
|
+
* @param value The range button value.
|
|
57
|
+
* @param params The axis context passed to function values.
|
|
58
|
+
* @param params.scaleType The scale type of the axis.
|
|
59
|
+
* @param params.data The axis data values (for ordinal axes).
|
|
60
|
+
* @param params.domain The full domain bounds.
|
|
61
|
+
* @returns The zoom start and end percentages (0-100).
|
|
62
|
+
*/
|
|
63
|
+
export declare function rangeButtonValueToZoom(value: RangeButtonValue, params: RangeButtonFunctionParams): {
|
|
64
|
+
start: number;
|
|
65
|
+
end: number;
|
|
66
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A calendar interval unit for range buttons.
|
|
3
|
+
*/
|
|
4
|
+
export type RangeButtonIntervalUnit = 'microsecond' | 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
|
|
5
|
+
/**
|
|
6
|
+
* Parameters passed to a range button function value.
|
|
7
|
+
*/
|
|
8
|
+
export interface RangeButtonFunctionParams {
|
|
9
|
+
/**
|
|
10
|
+
* The scale type of the axis (e.g., `'time'`, `'band'`, `'linear'`).
|
|
11
|
+
*/
|
|
12
|
+
scaleType: string;
|
|
13
|
+
/**
|
|
14
|
+
* The axis data values. Available for ordinal (band/point) axes.
|
|
15
|
+
*/
|
|
16
|
+
data: readonly unknown[] | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The full domain bounds, ignoring any current zoom.
|
|
19
|
+
* For time axes, these are timestamps. For ordinal axes, these are indices.
|
|
20
|
+
*/
|
|
21
|
+
domain: {
|
|
22
|
+
min: number;
|
|
23
|
+
max: number;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Defines the value of a range button.
|
|
28
|
+
*
|
|
29
|
+
* - `{ unit, step }` — A calendar interval from the end of the data.
|
|
30
|
+
* @example { unit: 'month', step: 3 } // Last 3 months
|
|
31
|
+
* @example { unit: 'year' } // Last year (step defaults to 1)
|
|
32
|
+
* - `[start, end]` — An absolute date range.
|
|
33
|
+
* @example [new Date(2024, 0, 1), new Date(2024, 6, 1)] // Jan–Jul 2024
|
|
34
|
+
* - `(params) => { start, end }` — A function that receives axis context (`scaleType`, `data`, `domain`) and returns zoom percentages (0-100).
|
|
35
|
+
* @example ({ domain }) => ({ start: 0, end: 50 }) // First half of data
|
|
36
|
+
* @example ({ data }) => {
|
|
37
|
+
* const lastFive = Math.max(0, data.length - 5);
|
|
38
|
+
* return { start: (lastFive / (data.length - 1)) * 100, end: 100 };
|
|
39
|
+
* } // Last 5 items on an ordinal axis
|
|
40
|
+
* - `null` — Resets zoom to show all data.
|
|
41
|
+
* @example null // Show all data
|
|
42
|
+
*/
|
|
43
|
+
export type RangeButtonValue = {
|
|
44
|
+
unit: RangeButtonIntervalUnit;
|
|
45
|
+
step?: number;
|
|
46
|
+
} | [Date, Date] | ((params: RangeButtonFunctionParams) => {
|
|
47
|
+
start: number;
|
|
48
|
+
end: number;
|
|
49
|
+
}) | null;
|
|
50
|
+
/**
|
|
51
|
+
* Converts a range button value to zoom start/end percentages.
|
|
52
|
+
*
|
|
53
|
+
* The range is calculated from the end of the axis domain.
|
|
54
|
+
* For example, `{ unit: 'month', step: 3 }` will zoom to show the last 3 months of data.
|
|
55
|
+
*
|
|
56
|
+
* @param value The range button value.
|
|
57
|
+
* @param params The axis context passed to function values.
|
|
58
|
+
* @param params.scaleType The scale type of the axis.
|
|
59
|
+
* @param params.data The axis data values (for ordinal axes).
|
|
60
|
+
* @param params.domain The full domain bounds.
|
|
61
|
+
* @returns The zoom start and end percentages (0-100).
|
|
62
|
+
*/
|
|
63
|
+
export declare function rangeButtonValueToZoom(value: RangeButtonValue, params: RangeButtonFunctionParams): {
|
|
64
|
+
start: number;
|
|
65
|
+
end: number;
|
|
66
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.rangeButtonValueToZoom = rangeButtonValueToZoom;
|
|
7
|
+
var _warning = require("@mui/x-internals/warning");
|
|
8
|
+
/**
|
|
9
|
+
* A calendar interval unit for range buttons.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Parameters passed to a range button function value.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Defines the value of a range button.
|
|
18
|
+
*
|
|
19
|
+
* - `{ unit, step }` — A calendar interval from the end of the data.
|
|
20
|
+
* @example { unit: 'month', step: 3 } // Last 3 months
|
|
21
|
+
* @example { unit: 'year' } // Last year (step defaults to 1)
|
|
22
|
+
* - `[start, end]` — An absolute date range.
|
|
23
|
+
* @example [new Date(2024, 0, 1), new Date(2024, 6, 1)] // Jan–Jul 2024
|
|
24
|
+
* - `(params) => { start, end }` — A function that receives axis context (`scaleType`, `data`, `domain`) and returns zoom percentages (0-100).
|
|
25
|
+
* @example ({ domain }) => ({ start: 0, end: 50 }) // First half of data
|
|
26
|
+
* @example ({ data }) => {
|
|
27
|
+
* const lastFive = Math.max(0, data.length - 5);
|
|
28
|
+
* return { start: (lastFive / (data.length - 1)) * 100, end: 100 };
|
|
29
|
+
* } // Last 5 items on an ordinal axis
|
|
30
|
+
* - `null` — Resets zoom to show all data.
|
|
31
|
+
* @example null // Show all data
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Attempts to convert a value to a timestamp.
|
|
36
|
+
* Handles Date objects, numeric timestamps, and date-like strings.
|
|
37
|
+
*
|
|
38
|
+
* @returns The timestamp in milliseconds, or `undefined` if the value is not date-like.
|
|
39
|
+
*/
|
|
40
|
+
function toTimestamp(val) {
|
|
41
|
+
if (val instanceof Date) {
|
|
42
|
+
return val.getTime();
|
|
43
|
+
}
|
|
44
|
+
if (typeof val === 'number') {
|
|
45
|
+
return val;
|
|
46
|
+
}
|
|
47
|
+
if (typeof val === 'string') {
|
|
48
|
+
const parsed = Date.parse(val);
|
|
49
|
+
if (!Number.isNaN(parsed)) {
|
|
50
|
+
return parsed;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Converts ordinal data to timestamps.
|
|
58
|
+
* Items that cannot be converted are kept as `NaN` so their index is preserved.
|
|
59
|
+
* Returns `undefined` if fewer than 2 items could be converted.
|
|
60
|
+
*/
|
|
61
|
+
function toTimestampArray(data) {
|
|
62
|
+
const timestamps = new Array(data.length);
|
|
63
|
+
let convertedItems = 0;
|
|
64
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
65
|
+
const ts = toTimestamp(data[i]);
|
|
66
|
+
if (ts !== undefined) {
|
|
67
|
+
convertedItems += 1;
|
|
68
|
+
timestamps[i] = ts;
|
|
69
|
+
} else {
|
|
70
|
+
timestamps[i] = Number.NaN;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return convertedItems >= 2 ? timestamps : undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Converts a range button value to zoom start/end percentages.
|
|
78
|
+
*
|
|
79
|
+
* The range is calculated from the end of the axis domain.
|
|
80
|
+
* For example, `{ unit: 'month', step: 3 }` will zoom to show the last 3 months of data.
|
|
81
|
+
*
|
|
82
|
+
* @param value The range button value.
|
|
83
|
+
* @param params The axis context passed to function values.
|
|
84
|
+
* @param params.scaleType The scale type of the axis.
|
|
85
|
+
* @param params.data The axis data values (for ordinal axes).
|
|
86
|
+
* @param params.domain The full domain bounds.
|
|
87
|
+
* @returns The zoom start and end percentages (0-100).
|
|
88
|
+
*/
|
|
89
|
+
function rangeButtonValueToZoom(value, params) {
|
|
90
|
+
if (value === null) {
|
|
91
|
+
return {
|
|
92
|
+
start: 0,
|
|
93
|
+
end: 100
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (typeof value === 'function') {
|
|
97
|
+
const result = value(params);
|
|
98
|
+
if (process.env.NODE_ENV !== 'production' && result.end < result.start) {
|
|
99
|
+
(0, _warning.warnOnce)([`MUI X Charts: Range button function returned an end value (${result.end}) lower than the start value (${result.start}).`, 'This likely produces an unexpected zoom range.']);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
start: result.start,
|
|
103
|
+
end: result.end
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const {
|
|
107
|
+
domain: {
|
|
108
|
+
min: domainMin,
|
|
109
|
+
max: domainMax
|
|
110
|
+
},
|
|
111
|
+
data: ordinalData
|
|
112
|
+
} = params;
|
|
113
|
+
|
|
114
|
+
// For ordinal axes with date-like data, resolve date ranges and intervals to matching indices.
|
|
115
|
+
const timestamps = ordinalData ? toTimestampArray(ordinalData) : undefined;
|
|
116
|
+
if (timestamps) {
|
|
117
|
+
const maxIndex = timestamps.length - 1;
|
|
118
|
+
if (Array.isArray(value)) {
|
|
119
|
+
const startTarget = value[0].getTime();
|
|
120
|
+
const endTarget = value[1].getTime();
|
|
121
|
+
if (process.env.NODE_ENV !== 'production' && endTarget < startTarget) {
|
|
122
|
+
(0, _warning.warnOnce)(['MUI X Charts: Range button received a date range whose end is before its start.', 'This produces an empty zoom range.']);
|
|
123
|
+
}
|
|
124
|
+
const firstGte = timestamps.findIndex(ts => !Number.isNaN(ts) && ts >= startTarget);
|
|
125
|
+
const lastLte = timestamps.findLastIndex(ts => !Number.isNaN(ts) && ts <= endTarget);
|
|
126
|
+
const startIndex = firstGte === -1 ? maxIndex : firstGte;
|
|
127
|
+
const endIndex = lastLte === -1 ? 0 : lastLte;
|
|
128
|
+
return {
|
|
129
|
+
start: startIndex / maxIndex * 100,
|
|
130
|
+
end: endIndex / maxIndex * 100
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Interval — compute target date from the last valid data point's timestamp.
|
|
135
|
+
const lastValidIndex = timestamps.findLastIndex(ts => !Number.isNaN(ts));
|
|
136
|
+
const lastTimestamp = timestamps[lastValidIndex];
|
|
137
|
+
const targetStartMs = computeIntervalStart(value, lastTimestamp);
|
|
138
|
+
const firstGte = timestamps.findIndex(ts => !Number.isNaN(ts) && ts >= targetStartMs);
|
|
139
|
+
const startIndex = firstGte === -1 ? maxIndex : firstGte;
|
|
140
|
+
return {
|
|
141
|
+
start: startIndex / maxIndex * 100,
|
|
142
|
+
end: 100
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (process.env.NODE_ENV !== 'production' && ordinalData !== undefined) {
|
|
146
|
+
(0, _warning.warnOnce)(['MUI X Charts: Range button received a date value for an ordinal axis whose data could not be parsed as dates.', 'The zoom range may not match the intended selection. Provide date-like axis data or use a function value.']);
|
|
147
|
+
}
|
|
148
|
+
const domainRange = domainMax - domainMin;
|
|
149
|
+
if (domainRange <= 0) {
|
|
150
|
+
return {
|
|
151
|
+
start: 0,
|
|
152
|
+
end: 100
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Absolute date range
|
|
157
|
+
if (Array.isArray(value)) {
|
|
158
|
+
const [rangeStart, rangeEnd] = value;
|
|
159
|
+
if (process.env.NODE_ENV !== 'production' && rangeEnd.getTime() < rangeStart.getTime()) {
|
|
160
|
+
(0, _warning.warnOnce)(['MUI X Charts: Range button received a date range whose end is before its start.', 'This produces an empty zoom range.']);
|
|
161
|
+
}
|
|
162
|
+
const startPercent = (rangeStart.getTime() - domainMin) / domainRange * 100;
|
|
163
|
+
const endPercent = (rangeEnd.getTime() - domainMin) / domainRange * 100;
|
|
164
|
+
return {
|
|
165
|
+
start: startPercent,
|
|
166
|
+
end: endPercent
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Interval — subtract from the end of the domain
|
|
171
|
+
const targetStartMs = computeIntervalStart(value, domainMax);
|
|
172
|
+
const startPercent = (targetStartMs - domainMin) / domainRange * 100;
|
|
173
|
+
return {
|
|
174
|
+
start: startPercent,
|
|
175
|
+
end: 100
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Computes the start timestamp for a calendar interval subtracted from a reference point.
|
|
181
|
+
*/
|
|
182
|
+
function computeIntervalStart(interval, referenceMs) {
|
|
183
|
+
const {
|
|
184
|
+
unit,
|
|
185
|
+
step = 1
|
|
186
|
+
} = interval;
|
|
187
|
+
switch (unit) {
|
|
188
|
+
case 'year':
|
|
189
|
+
{
|
|
190
|
+
const d = new Date(referenceMs);
|
|
191
|
+
d.setFullYear(d.getFullYear() - step);
|
|
192
|
+
return d.getTime();
|
|
193
|
+
}
|
|
194
|
+
case 'month':
|
|
195
|
+
{
|
|
196
|
+
const d = new Date(referenceMs);
|
|
197
|
+
d.setMonth(d.getMonth() - step);
|
|
198
|
+
return d.getTime();
|
|
199
|
+
}
|
|
200
|
+
case 'week':
|
|
201
|
+
return referenceMs - step * 7 * 24 * 60 * 60 * 1000;
|
|
202
|
+
case 'day':
|
|
203
|
+
return referenceMs - step * 24 * 60 * 60 * 1000;
|
|
204
|
+
case 'hour':
|
|
205
|
+
return referenceMs - step * 60 * 60 * 1000;
|
|
206
|
+
case 'minute':
|
|
207
|
+
return referenceMs - step * 60 * 1000;
|
|
208
|
+
case 'second':
|
|
209
|
+
return referenceMs - step * 1000;
|
|
210
|
+
case 'millisecond':
|
|
211
|
+
return referenceMs - step;
|
|
212
|
+
case 'microsecond':
|
|
213
|
+
return referenceMs - step / 1000;
|
|
214
|
+
default:
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
}
|