Skip to content

Commit fa8b76f

Browse files
author
Robert Crocker
committed
Refactored the bar, scatter, and line to position the cursor based on the bounds of the chart rather than the window.
1 parent fb3f55e commit fa8b76f

File tree

7 files changed

+14
-6
lines changed

7 files changed

+14
-6
lines changed

src/charts/BarChart/BarChart.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export default function BarChart({
301301
: 0
302302
}
303303
margin={margin}
304+
cWidth={cWidth}
304305
fill={colorScale(layer.key[i])}
305306
setTooltip={setTooltip}
306307
/>
@@ -330,6 +331,7 @@ export default function BarChart({
330331
margin={margin}
331332
fill={colorScale(yKey)}
332333
setTooltip={setTooltip}
334+
cWidth={cWidth}
333335
/>
334336
);
335337
})}

src/charts/LineChart/LineChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ export default function LineChart({
302302
yAccessor={yAccessor}
303303
setTooltip={setTooltip}
304304
margin={margin}
305+
cWidth={cWidth}
305306
/>
306307
)}
307308

src/charts/ScatterPlot/ScatterPlot.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export default function ScatterPlot({
286286
yAccessor={yAccessor}
287287
setTooltip={setTooltip}
288288
margin={margin}
289+
cWidth={cWidth}
289290
/>
290291
)}
291292

src/components/Rectangle.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable @typescript-eslint/restrict-plus-operands */
22
import React from 'react';
33
import { RectangleProps } from '../../types';
4-
import useWindowDimensions from '../hooks/useWindowDimensions';
54

65
import styled from 'styled-components';
76
const Bar = styled.rect`
@@ -16,10 +15,11 @@ const RectangleComp = ({
1615
width,
1716
height,
1817
margin,
18+
cWidth,
1919
fill,
2020
setTooltip,
2121
}: RectangleProps): JSX.Element => {
22-
const clientWidth = useWindowDimensions().width;
22+
2323
let tooltipState = {
2424
cursorX: 0,
2525
cursorY: 0,
@@ -45,7 +45,7 @@ const RectangleComp = ({
4545
cursorY: e.pageY - e.nativeEvent.offsetY + (y ?? 0),
4646
distanceFromTop: offsetFromTop + margin.top + rectTop,
4747
distanceFromRight:
48-
clientWidth - (offsetFromLeft + margin.left + rectMidPoint),
48+
(margin.left + cWidth + margin.right) - (offsetFromLeft + margin.left + rectMidPoint),
4949
distanceFromLeft: offsetFromLeft + margin.left + rectMidPoint,
5050
data,
5151
};

src/components/VoronoiCell.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable @typescript-eslint/restrict-plus-operands */
22
import React from 'react';
33
import { VoronoiProps } from '../../types';
4-
import useWindowDimensions from '../hooks/useWindowDimensions';
54

65
export const VoronoiCell = ({
76
fill,
@@ -12,8 +11,8 @@ export const VoronoiCell = ({
1211
setTooltip,
1312
data,
1413
margin,
14+
cWidth,
1515
}: VoronoiProps): JSX.Element => {
16-
const { width } = useWindowDimensions();
1716

1817
// The code below was commented out because of the performance issues we ran
1918
// into when charts are taking in large data sets
@@ -54,7 +53,7 @@ export const VoronoiCell = ({
5453

5554
tooltipState.distanceFromTop = cellCenter.cy + margin.top;
5655
tooltipState.distanceFromRight =
57-
width - (margin.left + tooltipState.cursorX);
56+
(margin.left + cWidth + margin.right) - (margin.left + tooltipState.cursorX);
5857
tooltipState.distanceFromLeft = margin.left + tooltipState.cursorX;
5958

6059
setTooltip ? setTooltip(tooltipState) : null;

src/components/VoronoiWrapper.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const VoronoiWrapper = React.memo(
1212
yAccessor,
1313
setTooltip,
1414
margin,
15+
cWidth
1516
}: VoronoiBody): JSX.Element => {
1617
return (
1718
<g className="voronoi-wrapper">
@@ -28,6 +29,7 @@ export const VoronoiWrapper = React.memo(
2829
tooltipData: element,
2930
}}
3031
margin={margin}
32+
cWidth={cWidth}
3133
setTooltip={setTooltip}
3234
data={element}
3335
/>

types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export interface RectangleProps {
321321
width: number;
322322
height: number;
323323
margin: Margin;
324+
cWidth: number;
324325
fill: string;
325326
setTooltip?: React.Dispatch<any>;
326327
}
@@ -356,6 +357,7 @@ export interface VoronoiProps {
356357
data?: any;
357358
setTooltip?: React.Dispatch<any>;
358359
margin: Margin;
360+
cWidth: number;
359361
}
360362

361363
export type ColorScale = d3.ScaleOrdinal<string, string, never>;
@@ -418,5 +420,6 @@ export interface VoronoiBody {
418420
yAccessor: yAccessorFunc;
419421
setTooltip: React.Dispatch<any> | undefined;
420422
margin: Margin;
423+
cWidth: number;
421424
}
422425
export type GroupAccessorFunc = (d: any) => number | Date;

0 commit comments

Comments
 (0)