Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Fix themeAugmentation #14372

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/pages/x/api/charts/charts-tooltip.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"name": "enum",
"description": "'axis'<br>&#124;&nbsp;'item'<br>&#124;&nbsp;'none'"
},
"default": "'item'"
"default": "'axis'"
}
},
"name": "ChartsTooltip",
Expand Down Expand Up @@ -76,6 +76,12 @@
"description": "Styles applied to the markCell element.",
"isGlobal": false
},
{
"key": "paper",
"className": "MuiChartsTooltip-paper",
"description": "Styles applied to the paper element.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiChartsTooltip-root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"description": "Styles applied to the markCell element.",
"isGlobal": false
},
{
"key": "paper",
"className": "MuiDefaultChartsAxisTooltipContent-paper",
"description": "Styles applied to the paper element.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiDefaultChartsAxisTooltipContent-root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"description": "Styles applied to the markCell element.",
"isGlobal": false
},
{
"key": "paper",
"className": "MuiDefaultChartsItemTooltipContent-paper",
"description": "Styles applied to the paper element.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiDefaultChartsItemTooltipContent-root",
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/x/api/charts/default-heatmap-tooltip.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"description": "Styles applied to the markCell element.",
"isGlobal": false
},
{
"key": "paper",
"className": "MuiDefaultHeatmapTooltip-paper",
"description": "Styles applied to the paper element.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiDefaultHeatmapTooltip-root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the markCell element"
},
"paper": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the paper element" },
"root": { "description": "Styles applied to the root element." },
"row": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the row element" },
"table": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the table element" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the markCell element"
},
"paper": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the paper element" },
"root": { "description": "Styles applied to the root element." },
"row": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the row element" },
"table": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the table element" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the markCell element"
},
"paper": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the paper element" },
"root": { "description": "Styles applied to the root element." },
"row": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the row element" },
"table": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the table element" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the markCell element"
},
"paper": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the paper element" },
"root": { "description": "Styles applied to the root element." },
"row": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the row element" },
"table": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the table element" },
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts-pro/src/BarChartPro/BarChartPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const BarChartPro = React.forwardRef(function BarChartPro(inProps: BarChartProPr
onZoomChange={onZoomChange}
>
{props.onAxisClick && <ChartsOnAxisClickHandler {...axisClickHandlerProps} />}
{props.grid && <ChartsGrid {...gridProps} />}
<ChartsGrid {...gridProps} />
<g {...clipPathGroupProps}>
<BarChartPlotZoom {...barPlotProps} />
<ChartsOverlay {...overlayProps} />
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts-pro/src/LineChartPro/LineChartPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const LineChartPro = React.forwardRef(function LineChartPro(inProps: LineChartPr
onZoomChange={onZoomChange}
>
{props.onAxisClick && <ChartsOnAxisClickHandler {...axisClickHandlerProps} />}
{props.grid && <ChartsGrid {...gridProps} />}
<ChartsGrid {...gridProps} />
<g {...clipPathGroupProps}>
<AreaPlotZoom {...areaPlotProps} />
<LinePlotZoom {...linePlotProps} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const ScatterChartPro = React.forwardRef(function ScatterChartPro(
<ZAxisContextProvider {...zAxisProps}>
{!props.disableVoronoi && <ChartsVoronoiHandler {...voronoiHandlerProps} />}
<ChartsAxis {...chartsAxisProps} />
{props.grid && <ChartsGrid {...gridProps} />}
<ChartsGrid {...gridProps} />
<g data-drawing-container>
{/* The `data-drawing-container` indicates that children are part of the drawing area. Ref: https://github.com/mui/mui-x/issues/13659 */}
<ScatterPlot {...scatterPlotProps} />
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const BarChart = React.forwardRef(function BarChart(inProps: BarChartProps, ref)
return (
<ResponsiveChartContainer ref={ref} {...chartContainerProps}>
{props.onAxisClick && <ChartsOnAxisClickHandler {...axisClickHandlerProps} />}
{props.grid && <ChartsGrid {...gridProps} />}
<ChartsGrid {...gridProps} />
<g {...clipPathGroupProps}>
<BarPlot {...barPlotProps} />
<ChartsOverlay {...overlayProps} />
Expand Down
81 changes: 14 additions & 67 deletions packages/x-charts/src/ChartsGrid/ChartsGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import composeClasses from '@mui/utils/composeClasses';
import { styled, useThemeProps } from '@mui/material/styles';

import { useThemeProps } from '@mui/material/styles';
import { useCartesianContext } from '../context/CartesianProvider';
import { useTicks } from '../hooks/useTicks';
import {
ChartsGridClasses,
getChartsGridUtilityClass,
chartsGridClasses,
} from './chartsGridClasses';
import { ChartsGridClasses, getChartsGridUtilityClass } from './chartsGridClasses';
import { useDrawingArea } from '../hooks/useDrawingArea';

const GridRoot = styled('g', {
name: 'MuiChartsGrid',
slot: 'Root',
overridesResolver: (props, styles) => [
{ [`&.${chartsGridClasses.verticalLine}`]: styles.verticalLine },
{ [`&.${chartsGridClasses.horizontalLine}`]: styles.horizontalLine },
styles.root,
],
})({});

const GridLine = styled('line', {
name: 'MuiChartsGrid',
slot: 'Line',
overridesResolver: (props, styles) => styles.line,
})(({ theme }) => ({
stroke: (theme.vars || theme).palette.divider,
shapeRendering: 'crispEdges',
strokeWidth: 1,
}));
import { GridRoot } from './styledCommonents';
import { ChartsGridVertical } from './ChartsVerticalGrid';
import { ChartsGridHorizontal } from './ChartsHorizontalGrid';

const useUtilityClasses = ({ classes }: ChartsGridProps) => {
const slots = {
Expand Down Expand Up @@ -75,48 +52,18 @@ function ChartsGrid(inProps: ChartsGridProps) {

const classes = useUtilityClasses(props);

const horizontalAxisId = yAxisIds[0];
const verticalAxisId = xAxisIds[0];

const {
scale: xScale,
tickNumber: xTickNumber,
tickInterval: xTickInterval,
} = xAxis[verticalAxisId];

const {
scale: yScale,
tickNumber: yTickNumber,
tickInterval: yTickInterval,
} = yAxis[horizontalAxisId];

const xTicks = useTicks({ scale: xScale, tickNumber: xTickNumber, tickInterval: xTickInterval });
const yTicks = useTicks({ scale: yScale, tickNumber: yTickNumber, tickInterval: yTickInterval });
const horizontalAxis = yAxis[yAxisIds[0]];
const verticalAxis = xAxis[xAxisIds[0]];

return (
<GridRoot {...other} className={classes.root}>
{vertical &&
xTicks.map(({ formattedValue, offset }) => (
<GridLine
key={`vertical-${formattedValue}`}
y1={drawingArea.top}
y2={drawingArea.top + drawingArea.height}
x1={offset}
x2={offset}
className={classes.verticalLine}
/>
))}
{horizontal &&
yTicks.map(({ formattedValue, offset }) => (
<GridLine
key={`horizontal-${formattedValue}`}
y1={offset}
y2={offset}
x1={drawingArea.left}
x2={drawingArea.left + drawingArea.width}
className={classes.horizontalLine}
/>
))}
{vertical && (
<ChartsGridVertical axis={verticalAxis} drawingArea={drawingArea} classes={classes} />
)}

{horizontal && (
<ChartsGridHorizontal axis={horizontalAxis} drawingArea={drawingArea} classes={classes} />
)}
</GridRoot>
);
}
Expand Down
38 changes: 38 additions & 0 deletions packages/x-charts/src/ChartsGrid/ChartsHorizontalGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import { DrawingArea } from '../context/DrawingProvider';
import { useTicks } from '../hooks/useTicks';
import { AxisDefaultized, ChartsYAxisProps, ScaleName } from '../models/axis';
import { GridLine } from './styledCommonents';
import { ChartsGridClasses } from './chartsGridClasses';

interface ChartsGridHorizontalProps {
axis: AxisDefaultized<ScaleName, any, ChartsYAxisProps>;
drawingArea: DrawingArea;
classes: Partial<ChartsGridClasses>;
}

/**
* @ignore - internal component.
*/
export function ChartsGridHorizontal(props: ChartsGridHorizontalProps) {
const { axis, drawingArea, classes } = props;

const { scale, tickNumber, tickInterval } = axis;

const yTicks = useTicks({ scale, tickNumber, tickInterval });

return (
<React.Fragment>
{yTicks.map(({ formattedValue, offset }) => (
<GridLine
key={`horizontal-${formattedValue}`}
y1={offset}
y2={offset}
x1={drawingArea.left}
x2={drawingArea.left + drawingArea.width}
className={classes.horizontalLine}
/>
))}
</React.Fragment>
);
}
38 changes: 38 additions & 0 deletions packages/x-charts/src/ChartsGrid/ChartsVerticalGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import { DrawingArea } from '../context/DrawingProvider';
import { useTicks } from '../hooks/useTicks';
import { AxisDefaultized, ChartsXAxisProps, ScaleName } from '../models/axis';
import { GridLine } from './styledCommonents';
import { ChartsGridClasses } from './chartsGridClasses';

interface ChartsGridVerticalProps {
axis: AxisDefaultized<ScaleName, any, ChartsXAxisProps>;
drawingArea: DrawingArea;
classes: Partial<ChartsGridClasses>;
}

/**
* @ignore - internal component.
*/
export function ChartsGridVertical(props: ChartsGridVerticalProps) {
const { axis, drawingArea, classes } = props;

const { scale, tickNumber, tickInterval } = axis;

const xTicks = useTicks({ scale, tickNumber, tickInterval });

return (
<React.Fragment>
{xTicks.map(({ formattedValue, offset }) => (
<GridLine
key={`vertical-${formattedValue}`}
y1={drawingArea.top}
y2={drawingArea.top + drawingArea.height}
x1={offset}
x2={offset}
className={classes.verticalLine}
/>
))}
</React.Fragment>
);
}
22 changes: 22 additions & 0 deletions packages/x-charts/src/ChartsGrid/styledCommonents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { styled } from '@mui/material/styles';
import { chartsGridClasses } from './chartsGridClasses';

export const GridRoot = styled('g', {
name: 'MuiChartsGrid',
slot: 'Root',
overridesResolver: (props, styles) => [
{ [`&.${chartsGridClasses.verticalLine}`]: styles.verticalLine },
{ [`&.${chartsGridClasses.horizontalLine}`]: styles.horizontalLine },
styles.root,
],
})({});

export const GridLine = styled('line', {
name: 'MuiChartsGrid',
slot: 'Line',
overridesResolver: (props, styles) => styles.line,
})(({ theme }) => ({
stroke: (theme.vars || theme).palette.divider,
shapeRendering: 'crispEdges',
strokeWidth: 1,
}));
19 changes: 10 additions & 9 deletions packages/x-charts/src/ChartsLegend/ChartsLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,21 @@ const useUtilityClasses = (ownerState: DefaultizedChartsLegendProps & { theme: T
return composeClasses(slots, getLegendUtilityClass, classes);
};

const defaultProps = {
position: { horizontal: 'middle', vertical: 'top' },
direction: 'row',
} as const;

function ChartsLegend(inProps: ChartsLegendProps) {
const props: DefaultizedChartsLegendProps = useThemeProps({
props: { ...defaultProps, ...inProps },
const props = useThemeProps({
props: inProps,
name: 'MuiChartsLegend',
});
JCQuintas marked this conversation as resolved.
Show resolved Hide resolved

const { position, direction, hidden, slots, slotProps } = props;
const defaultizedProps: DefaultizedChartsLegendProps = {
direction: 'row',
...props,
position: { horizontal: 'middle', vertical: 'top', ...props.position },
};
const { position, direction, hidden, slots, slotProps } = defaultizedProps;

const theme = useTheme();
const classes = useUtilityClasses({ ...props, theme });
const classes = useUtilityClasses({ ...defaultizedProps, theme });

const drawingArea = useDrawingArea();
const series = useSeries();
Expand Down
Loading