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] Provide hooks to create custom tooltip #14377

Merged
merged 21 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 20 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
4 changes: 1 addition & 3 deletions docs/data/charts/axis/AxisTextCustomizationNoSnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import * as React from 'react';
import Box from '@mui/material/Box';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
height: 300,
};

const valueFormatter = (value) => `${value}mm`;

export default function AxisTextCustomizationNoSnap() {
return (
<ChartsUsageDemo
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/axis/GridDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import * as React from 'react';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { chartsGridClasses } from '@mui/x-charts/ChartsGrid';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
yAxis: [{ label: 'rainfall (mm)' }],
height: 300,
};

const valueFormatter = (value) => `${value}mm`;

export default function GridDemo() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/axis/GridDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import * as React from 'react';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { chartsGridClasses } from '@mui/x-charts/ChartsGrid';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
yAxis: [{ label: 'rainfall (mm)' }],
height: 300,
};

const valueFormatter = (value: number | null) => `${value}mm`;

export default function GridDemo() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/BarsDataset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
yAxis: [
Expand All @@ -18,8 +18,6 @@ const chartSetting = {
},
};

const valueFormatter = (value) => `${value}mm`;

export default function BarsDataset() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/BarsDataset.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
yAxis: [
Expand All @@ -18,8 +18,6 @@ const chartSetting = {
},
};

const valueFormatter = (value: number | null) => `${value}mm`;

export default function BarsDataset() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/GridDemo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
xAxis: [
Expand All @@ -12,8 +12,6 @@ const chartSetting = {
height: 400,
};

const valueFormatter = (value) => `${value}mm`;

export default function GridDemo() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/GridDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
xAxis: [
Expand All @@ -12,8 +12,6 @@ const chartSetting = {
height: 400,
};

const valueFormatter = (value: number | null) => `${value}mm`;

export default function GridDemo() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/HorizontalBars.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
xAxis: [
Expand All @@ -12,8 +12,6 @@ const chartSetting = {
height: 400,
};

const valueFormatter = (value) => `${value}mm`;

export default function HorizontalBars() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/HorizontalBars.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

const chartSetting = {
xAxis: [
Expand All @@ -12,8 +12,6 @@ const chartSetting = {
height: 400,
};

const valueFormatter = (value: number | null) => `${value}mm`;

export default function HorizontalBars() {
return (
<BarChart
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/TickPlacementBars.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FormControlLabel from '@mui/material/FormControlLabel';
import Radio from '@mui/material/Radio';
import { BarChart } from '@mui/x-charts/BarChart';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

function TickParamsSelector({
tickPlacement,
Expand Down Expand Up @@ -57,8 +57,6 @@ function TickParamsSelector({
);
}

const valueFormatter = (value) => `${value}mm`;

const chartSetting = {
yAxis: [
{
Expand Down
4 changes: 1 addition & 3 deletions docs/data/charts/bars/TickPlacementBars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FormControlLabel from '@mui/material/FormControlLabel';
import Radio from '@mui/material/Radio';
import { BarChart } from '@mui/x-charts/BarChart';
import { axisClasses } from '@mui/x-charts/ChartsAxis';
import { dataset } from '../dataset/weather';
import { dataset, valueFormatter } from '../dataset/weather';

type TickParamsSelectorProps = {
tickPlacement: 'end' | 'start' | 'middle' | 'extremities';
Expand Down Expand Up @@ -72,8 +72,6 @@ function TickParamsSelector({
);
}

const valueFormatter = (value: number | null) => `${value}mm`;

const chartSetting = {
yAxis: [
{
Expand Down
4 changes: 4 additions & 0 deletions docs/data/charts/dataset/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export const dataset = [
month: 'Dec',
},
];

export function valueFormatter(value: number | null) {
return `${value}mm`;
}
110 changes: 110 additions & 0 deletions docs/data/charts/tooltip/CustomAxisTooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as React from 'react';
import NoSsr from '@mui/material/NoSsr';
import Popper from '@mui/material/Popper';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
import { useAxisTooltip, useMouseTracker } from '@mui/x-charts/ChartsTooltip';
import { generateVirtualElement } from './generateVirtualElement';

export function CustomAxisTooltip() {
const tooltipData = useAxisTooltip();
const mousePosition = useMouseTracker(); // Track the mouse position on chart.

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

// The pointer type can be used to have different behavior based on pointer type.
const isMousePointer = mousePosition?.pointerType === 'mouse';
// Adapt the tooltip offset to the size of the pointer.
const yOffset = isMousePointer ? 0 : 40 - mousePosition.height;

return (
<NoSsr>
<Popper
sx={{
pointerEvents: 'none',
zIndex: (theme) => theme.zIndex.modal,
}}
open
placement={isMousePointer ? 'top-end' : 'top'}
anchorEl={generateVirtualElement(mousePosition)}
modifiers={[
{
name: 'offset',
options: {
offset: [0, yOffset],
},
},
]}
>
<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',
},
},
},
}}
>
<table>
<thead>
<tr>
<td colSpan={3}>
<Typography>{tooltipData.axisFormattedValue}</Typography>
</td>
</tr>
</thead>
<tbody>
{tooltipData.seriesItems.map((seriesItem) => (
<tr key={seriesItem.seriesId}>
<td>
<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>
</Popper>
</NoSsr>
);
}
Loading