Skip to content

Commit 5bec3d8

Browse files
Add margin
1 parent 121e8ca commit 5bec3d8

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

packages/x-charts-pro/src/ChartZoomSlider/internals/ChartAxisZoomSlider.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { styled } from '@mui/material/styles';
1616
import { useXAxes, useYAxes } from '@mui/x-charts/hooks';
1717
import { rafThrottle } from '@mui/x-internals/rafThrottle';
18-
import { ZOOM_SLIDER_SIZE } from '@mui/x-charts/constants';
18+
import { ZOOM_SLIDER_MARGIN } from '@mui/x-charts/constants';
1919
import {
2020
selectorChartAxisZoomData,
2121
UseChartProZoomSignature,
@@ -54,8 +54,14 @@ interface ChartZoomSliderProps {
5454

5555
const ZOOM_SLIDER_BACKGROUND_SIZE = 8;
5656
const ZOOM_SLIDER_FOREGROUND_SIZE = 10;
57-
const ZOOM_SLIDER_HANDLE_HEIGHT = ZOOM_SLIDER_SIZE;
57+
const ZOOM_SLIDER_HANDLE_HEIGHT = 20;
5858
const ZOOM_SLIDER_HANDLE_WIDTH = 10;
59+
const ZOOM_SLIDER_SIZE = Math.max(
60+
ZOOM_SLIDER_BACKGROUND_SIZE,
61+
ZOOM_SLIDER_FOREGROUND_SIZE,
62+
ZOOM_SLIDER_HANDLE_HEIGHT,
63+
ZOOM_SLIDER_HANDLE_WIDTH,
64+
);
5965

6066
/**
6167
* Renders the zoom slider for a specific axis.
@@ -88,8 +94,8 @@ export function ChartAxisZoomSlider({ axisDirection, axisId }: ChartZoomSliderPr
8894
x = drawingArea.left;
8995
y =
9096
axis.position === 'bottom'
91-
? drawingArea.top + drawingArea.height + axis.offset + axisSize
92-
: drawingArea.top - axis.offset - axisSize - ZOOM_SLIDER_SIZE;
97+
? drawingArea.top + drawingArea.height + axis.offset + axisSize + ZOOM_SLIDER_MARGIN
98+
: drawingArea.top - axis.offset - axisSize - ZOOM_SLIDER_SIZE - ZOOM_SLIDER_MARGIN;
9399
reverse = axis.reverse ?? false;
94100
} else {
95101
const axis = yAxis[axisId];
@@ -102,13 +108,13 @@ export function ChartAxisZoomSlider({ axisDirection, axisId }: ChartZoomSliderPr
102108

103109
x =
104110
axis.position === 'right'
105-
? drawingArea.left + drawingArea.width + axis.offset + axisSize
106-
: drawingArea.left - axis.offset - axisSize - ZOOM_SLIDER_SIZE;
111+
? drawingArea.left + drawingArea.width + axis.offset + axisSize + ZOOM_SLIDER_MARGIN
112+
: drawingArea.left - axis.offset - axisSize - ZOOM_SLIDER_SIZE - ZOOM_SLIDER_MARGIN;
107113
y = drawingArea.top;
108114
reverse = axis.reverse ?? false;
109115
}
110116

111-
const backgroundRectOffset = (ZOOM_SLIDER_FOREGROUND_SIZE - ZOOM_SLIDER_BACKGROUND_SIZE) / 2;
117+
const backgroundRectOffset = (ZOOM_SLIDER_SIZE - ZOOM_SLIDER_BACKGROUND_SIZE) / 2;
112118

113119
return (
114120
<g transform={`translate(${x} ${y})`}>
@@ -338,9 +344,9 @@ function ChartAxisZoomSliderSpan({
338344
previewHeight = size;
339345

340346
startHandleX = (zoomData.start / 100) * drawingArea.width;
341-
startHandleY = (size - previewHandleHeight) / 2;
347+
startHandleY = 0;
342348
endHandleX = (zoomData.end / 100) * drawingArea.width;
343-
endHandleY = (size - previewHandleHeight) / 2;
349+
endHandleY = 0;
344350

345351
if (reverse) {
346352
previewX = drawingArea.width - previewX - previewWidth;
@@ -357,9 +363,9 @@ function ChartAxisZoomSliderSpan({
357363
previewWidth = size;
358364
previewHeight = (drawingArea.height * (zoomData.end - zoomData.start)) / 100;
359365

360-
startHandleX = (size - previewHandleWidth) / 2;
366+
startHandleX = 0;
361367
startHandleY = drawingArea.height - (zoomData.start / 100) * drawingArea.height;
362-
endHandleX = (size - previewHandleWidth) / 2;
368+
endHandleX = 0;
363369
endHandleY = drawingArea.height - (zoomData.end / 100) * drawingArea.height;
364370

365371
if (reverse) {
@@ -373,12 +379,14 @@ function ChartAxisZoomSliderSpan({
373379
endHandleY -= previewHandleHeight / 2;
374380
}
375381

382+
const previewOffset = (ZOOM_SLIDER_HANDLE_HEIGHT - ZOOM_SLIDER_FOREGROUND_SIZE) / 2;
383+
376384
return (
377385
<React.Fragment>
378386
<ZoomRangePreviewRect
379387
ref={activePreviewRectRef}
380-
x={previewX}
381-
y={previewY}
388+
x={previewX + (axisDirection === 'x' ? 0 : previewOffset)}
389+
y={previewY + (axisDirection === 'x' ? previewOffset : 0)}
382390
width={previewWidth}
383391
height={previewHeight}
384392
/>

packages/x-charts/src/constants/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export const DEFAULT_MARGINS = {
1111
export const DEFAULT_AXIS_SIZE_WIDTH = 45;
1212
export const DEFAULT_AXIS_SIZE_HEIGHT = 25;
1313

14-
export const ZOOM_SLIDER_SIZE = 20;
14+
/** Margin in the opposite direction of the axis, i.e., horizontal if the axis is vertical and vice versa. */
15+
export const ZOOM_SLIDER_MARGIN = 4;
16+
17+
/** Size reserved for the zoom slider. The actual size of the slider might be smaller. */
18+
export const ZOOM_SLIDER_SIZE = 20 + 2 * ZOOM_SLIDER_MARGIN;
1519

1620
// How many pixels to add to the default axis size if that axis has a label
1721
export const AXIS_LABEL_DEFAULT_HEIGHT = 20;

scripts/x-charts-pro.exports.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@
462462
{ "name": "yellowPalette", "kind": "Variable" },
463463
{ "name": "yellowPaletteDark", "kind": "Variable" },
464464
{ "name": "yellowPaletteLight", "kind": "Variable" },
465+
{ "name": "ZOOM_SLIDER_MARGIN", "kind": "Variable" },
465466
{ "name": "ZOOM_SLIDER_SIZE", "kind": "Variable" },
466467
{ "name": "ZoomData", "kind": "TypeAlias" },
467468
{ "name": "ZoomFilterMode", "kind": "TypeAlias" },

scripts/x-charts.exports.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,5 +421,6 @@
421421
{ "name": "yellowPalette", "kind": "Variable" },
422422
{ "name": "yellowPaletteDark", "kind": "Variable" },
423423
{ "name": "yellowPaletteLight", "kind": "Variable" },
424+
{ "name": "ZOOM_SLIDER_MARGIN", "kind": "Variable" },
424425
{ "name": "ZOOM_SLIDER_SIZE", "kind": "Variable" }
425426
]

0 commit comments

Comments
 (0)