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 7 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`;
}
71 changes: 71 additions & 0 deletions docs/data/charts/tooltip/CustomItemTooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as React from 'react';
import NoSsr from '@mui/material/NoSsr';
import Popper from '@mui/material/Popper';
import Paper from '@mui/material/Paper';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { useItemTooltip, useMouseTracker } from '@mui/x-charts/ChartsTooltip';
import { generateVirtualElement } from './generateVirtualElement';

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

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

return (
<NoSsr>
<Popper
sx={{
pointerEvents: 'none',
zIndex: (theme) => theme.zIndex.modal,
}}
open
placement={mousePosition?.pointerType === 'mouse' ? 'top-end' : 'top'}
anchorEl={generateVirtualElement(mousePosition)}
modifiers={[
{
name: 'offset',
options: {
offset: [
0,
mousePosition?.pointerType === 'touch'
? 40 - mousePosition.height
: 0,
],
},
},
]}
>
<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>
</Popper>
</NoSsr>
);
}
75 changes: 75 additions & 0 deletions docs/data/charts/tooltip/CustomItemTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as React from 'react';
import NoSsr from '@mui/material/NoSsr';
import Popper from '@mui/material/Popper';
import Paper from '@mui/material/Paper';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import { useItemTooltip, useMouseTracker } from '@mui/x-charts/ChartsTooltip';
import { generateVirtualElement } from './generateVirtualElement';

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

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

return (
<NoSsr>
<Popper
sx={{
pointerEvents: 'none',
zIndex: (theme) => theme.zIndex.modal,
}}
open
placement={
mousePosition?.pointerType === 'mouse'
? ('top-end' as const)
: ('top' as const)
}
anchorEl={generateVirtualElement(mousePosition)}
modifiers={[
{
name: 'offset',
options: {
offset: [
0,
mousePosition?.pointerType === 'touch'
? 40 - mousePosition.height
: 0,
],
},
},
]}
>
<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>
</Popper>
</NoSsr>
);
}
37 changes: 37 additions & 0 deletions docs/data/charts/tooltip/CustomItemTooltipContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import Paper from '@mui/material/Paper';

/**
* Render a basic tooltip content for an item.
*/
export function CustomItemTooltipContent(props) {
return (
<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: props.color,
}}
/>
<Typography sx={{ ml: 2 }} fontWeight="light">
{props.label}
</Typography>
<Typography sx={{ ml: 2 }}>{props.formattedValue}</Typography>
</Stack>
</Paper>
);
}
Loading