Skip to content

Commit

Permalink
[chart] Fix custom Tooltip demos (#15631)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Fauquette <[email protected]>
  • Loading branch information
alexfauquette authored Nov 27, 2024
1 parent 1992f8d commit ea3fa72
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 204 deletions.
127 changes: 62 additions & 65 deletions docs/data/charts/tooltip/CustomAxisTooltip.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,79 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import { ChartsTooltipContainer, useAxisTooltip } from '@mui/x-charts/ChartsTooltip';
import { useAxisTooltip } from '@mui/x-charts/ChartsTooltip';

export function CustomAxisTooltip() {
const tooltipData = useAxisTooltip();

if (!tooltipData) {
// No data to display
if (tooltipData === null) {
return null;
}
return (
<ChartsTooltipContainer trigger="axis">
<Paper
elevation={0}
sx={{
m: 1,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
table: { borderSpacing: 0 },
thead: {
td: {
px: 1.5,
py: 0.75,
borderBottom: 'solid',
borderWidth: 2,
borderColor: 'divider',
},
<Paper
elevation={0}
sx={{
m: 1,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
table: { borderSpacing: 0 },
thead: {
td: {
px: 1.5,
py: 0.75,
borderBottom: 'solid',
borderWidth: 2,
borderColor: 'divider',
},
tbody: {
'tr:first-child': { td: { paddingTop: 1.5 } },
'tr:last-child': { td: { paddingBottom: 1.5 } },
tr: {
'td:first-child': { paddingLeft: 1.5 },
'td:last-child': { paddingRight: 1.5 },
td: {
paddingRight: '7px',
paddingBottom: '10px',
},
},
tbody: {
'tr:first-child': { td: { paddingTop: 1.5 } },
'tr:last-child': { td: { paddingBottom: 1.5 } },
tr: {
'td:first-child': { paddingLeft: 1.5 },
'td:last-child': { paddingRight: 1.5 },
td: {
paddingRight: '7px',
paddingBottom: '10px',
},
},
}}
>
<table>
<thead>
<tr>
<td colSpan={3}>
<Typography>{tooltipData.axisFormattedValue}</Typography>
},
}}
>
<table>
<thead>
<tr>
<td colSpan={3}>
<Typography>{tooltipData.axisFormattedValue}</Typography>
</td>
</tr>
</thead>
<tbody>
{tooltipData.seriesItems.map((seriesItem) => (
<tr key={seriesItem.seriesId}>
<td aria-label={`${seriesItem.formattedLabel}-series-color`}>
<div
style={{
width: 10,
height: 10,
borderRadius: 2,
backgroundColor: seriesItem.color,
}}
/>
</td>
<td>
<Typography fontWeight="light">
{seriesItem.formattedLabel}
</Typography>
</td>
<td>
<Typography>{seriesItem.formattedValue}</Typography>
</td>
</tr>
</thead>
<tbody>
{tooltipData.seriesItems.map((seriesItem) => (
<tr key={seriesItem.seriesId}>
<td aria-label={`${seriesItem.formattedLabel}-series-color`}>
<div
style={{
width: 10,
height: 10,
borderRadius: 2,
backgroundColor: seriesItem.color,
}}
/>
</td>
<td>
<Typography fontWeight="light">
{seriesItem.formattedLabel}
</Typography>
</td>
<td>
<Typography>{seriesItem.formattedValue}</Typography>
</td>
</tr>
))}
</tbody>
</table>
</Paper>
</ChartsTooltipContainer>
))}
</tbody>
</table>
</Paper>
);
}
127 changes: 62 additions & 65 deletions docs/data/charts/tooltip/CustomAxisTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,82 +1,79 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import { ChartsTooltipContainer, useAxisTooltip } from '@mui/x-charts/ChartsTooltip';
import { useAxisTooltip } from '@mui/x-charts/ChartsTooltip';

export function CustomAxisTooltip() {
const tooltipData = useAxisTooltip();

if (!tooltipData) {
// No data to display
if (tooltipData === null) {
return null;
}
return (
<ChartsTooltipContainer trigger="axis">
<Paper
elevation={0}
sx={{
m: 1,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
table: { borderSpacing: 0 },
thead: {
td: {
px: 1.5,
py: 0.75,
borderBottom: 'solid',
borderWidth: 2,
borderColor: 'divider',
},
<Paper
elevation={0}
sx={{
m: 1,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
table: { borderSpacing: 0 },
thead: {
td: {
px: 1.5,
py: 0.75,
borderBottom: 'solid',
borderWidth: 2,
borderColor: 'divider',
},
tbody: {
'tr:first-child': { td: { paddingTop: 1.5 } },
'tr:last-child': { td: { paddingBottom: 1.5 } },
tr: {
'td:first-child': { paddingLeft: 1.5 },
'td:last-child': { paddingRight: 1.5 },
td: {
paddingRight: '7px',
paddingBottom: '10px',
},
},
tbody: {
'tr:first-child': { td: { paddingTop: 1.5 } },
'tr:last-child': { td: { paddingBottom: 1.5 } },
tr: {
'td:first-child': { paddingLeft: 1.5 },
'td:last-child': { paddingRight: 1.5 },
td: {
paddingRight: '7px',
paddingBottom: '10px',
},
},
}}
>
<table>
<thead>
<tr>
<td colSpan={3}>
<Typography>{tooltipData.axisFormattedValue}</Typography>
},
}}
>
<table>
<thead>
<tr>
<td colSpan={3}>
<Typography>{tooltipData.axisFormattedValue}</Typography>
</td>
</tr>
</thead>
<tbody>
{tooltipData.seriesItems.map((seriesItem) => (
<tr key={seriesItem.seriesId}>
<td aria-label={`${seriesItem.formattedLabel}-series-color`}>
<div
style={{
width: 10,
height: 10,
borderRadius: 2,
backgroundColor: seriesItem.color,
}}
/>
</td>
<td>
<Typography fontWeight="light">
{seriesItem.formattedLabel}
</Typography>
</td>
<td>
<Typography>{seriesItem.formattedValue}</Typography>
</td>
</tr>
</thead>
<tbody>
{tooltipData.seriesItems.map((seriesItem) => (
<tr key={seriesItem.seriesId}>
<td aria-label={`${seriesItem.formattedLabel}-series-color`}>
<div
style={{
width: 10,
height: 10,
borderRadius: 2,
backgroundColor: seriesItem.color,
}}
/>
</td>
<td>
<Typography fontWeight="light">
{seriesItem.formattedLabel}
</Typography>
</td>
<td>
<Typography>{seriesItem.formattedValue}</Typography>
</td>
</tr>
))}
</tbody>
</table>
</Paper>
</ChartsTooltipContainer>
))}
</tbody>
</table>
</Paper>
);
}
5 changes: 4 additions & 1 deletion docs/data/charts/tooltip/CustomAxisTooltipContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BarPlot } from '@mui/x-charts/BarChart';
import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis';
import { ChartsClipPath } from '@mui/x-charts/ChartsClipPath';
import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis';
import { ChartsTooltipContainer } from '@mui/x-charts/ChartsTooltip';
import { CustomAxisTooltip } from './CustomAxisTooltip';
import { dataset, valueFormatter } from '../dataset/weather';

Expand All @@ -27,7 +28,9 @@ export default function CustomAxisTooltipContent() {
</g>
<ChartsXAxis />
<ChartsYAxis />
<CustomAxisTooltip />
<ChartsTooltipContainer trigger="axis">
<CustomAxisTooltip />
</ChartsTooltipContainer>
</ChartContainer>
</div>
);
Expand Down
5 changes: 4 additions & 1 deletion docs/data/charts/tooltip/CustomAxisTooltipContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BarPlot } from '@mui/x-charts/BarChart';
import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis';
import { ChartsClipPath } from '@mui/x-charts/ChartsClipPath';
import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis';
import { ChartsTooltipContainer } from '@mui/x-charts/ChartsTooltip';
import { CustomAxisTooltip } from './CustomAxisTooltip';
import { dataset, valueFormatter } from '../dataset/weather';

Expand All @@ -28,7 +29,9 @@ export default function CustomAxisTooltipContent() {
<ChartsXAxis />
<ChartsYAxis />

<CustomAxisTooltip />
<ChartsTooltipContainer trigger="axis">
<CustomAxisTooltip />
</ChartsTooltipContainer>
</ChartContainer>
</div>
);
Expand Down
57 changes: 26 additions & 31 deletions docs/data/charts/tooltip/CustomItemTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@ import * as React from 'react';
import Paper from '@mui/material/Paper';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { ChartsTooltipContainer, useItemTooltip } from '@mui/x-charts/ChartsTooltip';
import { useItemTooltip } from '@mui/x-charts/ChartsTooltip';

export function CustomItemTooltip() {
const tooltipData = useItemTooltip();

if (!tooltipData) {
// No data to display
return null;
}

return (
<ChartsTooltipContainer trigger="item">
<Paper
elevation={0}
sx={{
m: 1,
p: 1.5,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
}}
>
<Stack direction="row" alignItems="center">
<div
style={{
width: 11,
height: 11,
borderRadius: '50%',
backgroundColor: tooltipData.color,
}}
/>
<Typography sx={{ ml: 2 }} fontWeight="light">
{tooltipData.label}
</Typography>
<Typography sx={{ ml: 2 }}>{tooltipData.formattedValue}</Typography>
</Stack>
</Paper>
</ChartsTooltipContainer>
<Paper
elevation={0}
sx={{
m: 1,
p: 1.5,
border: 'solid',
borderWidth: 2,
borderColor: 'divider',
}}
>
<Stack direction="row" alignItems="center">
<div
style={{
width: 11,
height: 11,
borderRadius: '50%',
backgroundColor: tooltipData.color,
}}
/>
<Typography sx={{ ml: 2 }} fontWeight="light">
{tooltipData.label}
</Typography>
<Typography sx={{ ml: 2 }}>{tooltipData.formattedValue}</Typography>
</Stack>
</Paper>
);
}
Loading

0 comments on commit ea3fa72

Please sign in to comment.