Skip to content

Commit

Permalink
Merge pull request #155 from GNS-Science/feature/mfd_tooltip
Browse files Browse the repository at this point in the history
mfd plot accepts tooltip callback;
  • Loading branch information
benjamineac authored Jul 19, 2023
2 parents ff7c89e + b431a85 commit 5078266
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gns-science/toshi-nest",
"version": "3.9.6",
"version": "3.9.7",
"description": "The toshi-nest is for fledgling work e.g. reusable Node components to share across TUI and other projects",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
16 changes: 15 additions & 1 deletion src/MfdPlot/MfdPlot.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ColorBar from '../ColorBar/ColorBar';
import { LatLngExpression } from 'leaflet';
import LeafletMap from '../LeafletMap';
import geojsonTestDataPGA from '../__tests__/testData/geoJson/geojsonTestDataPGA';
import { Box } from '@mui/material';
import { Box, Typography } from '@mui/material';

export default {
title: 'Charts/MFDPlot',
Expand Down Expand Up @@ -150,6 +150,18 @@ const data = [
cumulative_rate: 0,
},
];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const renderCustomTooltip = (tooltipData: any) => {
const datum = tooltipData?.nearestDatum?.datum as Datum;
console.log(tooltipData);
return (
<>
<Typography>Rate: {datum?.rate?.toExponential(2)}</Typography>
<Typography>Cumulative Rate: {datum?.cumulative_rate?.toExponential(2)}/yr</Typography>
<Typography>Magnitude: {datum?.bin_center.toPrecision(3)}/yr</Typography>
</>
);
};

const adjustedData: Datum[] = data.map((d) => {
if (d.rate === 0) {
Expand Down Expand Up @@ -185,6 +197,7 @@ export const Primary = () => {
yScaleDomain={[1e-6, 1e-1]}
lineColours={['blue', 'red']}
legendDomain={['rate', 'cumulative rate']}
renderCustomTooltip={renderCustomTooltip}
/>
);
};
Expand Down Expand Up @@ -255,6 +268,7 @@ export const HazardMapsLog = () => {
yScaleDomain={[1e-7, 1e-1]}
lineColours={['blue', 'red']}
legendDomain={['rate', 'cumulative rate']}
renderCustomTooltip={renderCustomTooltip}
/>
<ColorBar width={360} height={35} colors={colorsLog} tickValues={valuesLog} heading={'Participation rate'} linear={false} />
</Box>
Expand Down
18 changes: 6 additions & 12 deletions src/MfdPlot/MfdPlot.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useState } from 'react';
import { Axis, AnimatedLineSeries, AnimatedBarSeries, Tooltip, XYChart } from '@visx/xychart';
import { FormControlLabel, Menu, Radio, RadioGroup, MenuItem, IconButton, Typography } from '@mui/material';
import { FormControlLabel, Menu, Radio, RadioGroup, MenuItem, IconButton } from '@mui/material';
import { LegendOrdinal } from '@visx/legend';
import { scaleOrdinal } from '@visx/scale';
import { RectClipPath } from '@visx/clip-path';
import { Group } from '@visx/group';
import SettingsIcon from '@mui/icons-material/Settings';

import { MfdPlotProps, Datum } from './MfdPlot.types';
import { MfdPlotProps } from './MfdPlot.types';

export const MfdPlot = ({
data,
width,
Expand All @@ -23,6 +24,7 @@ export const MfdPlot = ({
yScaleDomain,
legendDomain,
defaultLinesVisible,
renderCustomTooltip,
}: MfdPlotProps) => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const [linesToDisplay, setLinesToDisplay] = useState<string>(defaultLinesVisible || 'Both');
Expand Down Expand Up @@ -116,16 +118,8 @@ export const MfdPlot = ({
snapTooltipToDatumY
showVerticalCrosshair
style={{ zIndex: 120000, position: 'absolute', backgroundColor: 'white', borderRadius: '4px', borderWidth: '1px', border: '2px solid rgba(0,0,0,0.2)', padding: 2 }}
renderTooltip={({ tooltipData }) => {
const datum = tooltipData?.nearestDatum?.datum as Datum;
return (
<>
<Typography>Rate: {datum?.rate?.toExponential(2)}</Typography>
<Typography>Cumulative Rate: {datum?.cumulative_rate?.toExponential(2)}</Typography>
<Typography>Magnitude: {datum?.bin_center.toPrecision(3)}</Typography>
</>
);
}}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderTooltip={({ tooltipData }: any) => renderCustomTooltip(tooltipData)}
/>
</XYChart>
<LegendOrdinal
Expand Down
2 changes: 2 additions & 0 deletions src/MfdPlot/MfdPlot.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ export interface MfdPlotProps {
};
header?: string;
defaultLinesVisible?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderCustomTooltip: any;
}

0 comments on commit 5078266

Please sign in to comment.