Skip to content

Commit

Permalink
Resolving merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz-cp committed Dec 16, 2024
2 parents 14ca95e + 76ed47a commit 6117c79
Show file tree
Hide file tree
Showing 29 changed files with 379 additions and 342 deletions.
15 changes: 15 additions & 0 deletions .pnpm-patches/@wordpress__dataviews@4.10.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Hack for https://github.com/WordPress/gutenberg/issues/67897

diff --git a/package.json b/package.json
index d7af17fea3f59f807a9d7234cf9ce79131538383..c862b012af312c9fc5cf1d2d884ec332ee079d0b 100644
--- a/package.json
+++ b/package.json
@@ -27,7 +27,7 @@
"exports": {
".": {
"types": "./build-types/index.d.ts",
- "import": "./build-module/index.js"
+ "default": "./build/index.js"
},
"./wp": {
"types": "./build-types/index.d.ts",
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@
"node": "^22.9.0",
"pnpm": "^9.3.0 <9.12.0"
},
"packageManager": "pnpm@9.3.0"
"packageManager": "pnpm@9.3.0",
"pnpm": {
"patchedDependencies": {
"@wordpress/dataviews@4.10.0": ".pnpm-patches/@wordpress__dataviews@4.10.0.patch"
}
}
}
33 changes: 19 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Chart Library: Update tooltip component
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bar-chart {
position: relative;
}
107 changes: 107 additions & 0 deletions projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { AxisLeft, AxisBottom } from '@visx/axis';
import { localPoint } from '@visx/event';
import { Group } from '@visx/group';
import { scaleBand, scaleLinear } from '@visx/scale';
import { Bar } from '@visx/shape';
import { useTooltip } from '@visx/tooltip';
import clsx from 'clsx';
import { FC, useCallback, type MouseEvent } from 'react';
import { useChartTheme } from '../../providers/theme';
import { BaseTooltip } from '../tooltip';
import styles from './bar-chart.module.scss';
import type { BaseChartProps, DataPoint } from '../shared/types';

interface BarChartProps extends BaseChartProps {
/**
* Array of data points to display in the chart
*/
data: DataPoint[];
}

const BarChart: FC< BarChartProps > = ( {
data,
width,
height,
margin = { top: 20, right: 20, bottom: 40, left: 40 },
withTooltips = false,
className,
} ) => {
const theme = useChartTheme();
const { tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip } =
useTooltip< DataPoint >();

const margins = margin;
const xMax = width - margins.left - margins.right;
const yMax = height - margins.top - margins.bottom;

const xScale = scaleBand< string >( {
range: [ 0, xMax ],
domain: data.map( d => d.label ),
padding: 0.2,
} );

const yScale = scaleLinear< number >( {
range: [ yMax, 0 ],
domain: [ 0, Math.max( ...data.map( d => d.value ) ) ],
} );

const handleMouseMove = useCallback(
( event: MouseEvent< SVGRectElement >, datum: DataPoint ) => {
const coords = localPoint( event );
if ( ! coords ) return;

showTooltip( {
tooltipData: datum,
tooltipLeft: coords.x,
tooltipTop: coords.y - 10,
} );
},
[ showTooltip ]
);

const handleMouseLeave = useCallback( () => {
hideTooltip();
}, [ hideTooltip ] );

return (
<div className={ clsx( 'bar-chart', className, styles[ 'bar-chart' ] ) }>
<svg width={ width } height={ height }>
<Group left={ margins.left } top={ margins.top }>
{ data.map( d => {
const handleBarMouseMove = event => handleMouseMove( event, d );

return (
<Bar
key={ `bar-${ d.label }` }
x={ xScale( d.label ) }
y={ yScale( d.value ) }
width={ xScale.bandwidth() }
height={ yMax - ( yScale( d.value ) ?? 0 ) }
fill={ theme.colors[ 0 ] }
onMouseMove={ withTooltips ? handleBarMouseMove : undefined }
onMouseLeave={ withTooltips ? handleMouseLeave : undefined }
/>
);
} ) }
<AxisLeft scale={ yScale } />
<AxisBottom scale={ xScale } top={ yMax } />
</Group>
</svg>

{ withTooltips && tooltipOpen && tooltipData && (
<BaseTooltip
data={ {
label: tooltipData.label,
value: tooltipData.value,
} }
top={ tooltipTop }
left={ tooltipLeft }
/>
) }
</div>
);
};

BarChart.displayName = 'BarChart';

export default BarChart;
114 changes: 1 addition & 113 deletions projects/js-packages/charts/src/components/bar-chart/index.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1 @@
import { AxisLeft, AxisBottom } from '@visx/axis';
import { localPoint } from '@visx/event';
import { Group } from '@visx/group';
import { scaleBand, scaleLinear } from '@visx/scale';
import { Bar } from '@visx/shape';
import { useTooltip } from '@visx/tooltip';
import clsx from 'clsx';
import React from 'react';
import { useChartTheme } from '../../providers/theme';
import { Tooltip } from '../tooltip';
import type { BaseChartProps, DataPoint } from '../shared/types';

interface BarChartProps extends BaseChartProps {
/**
* Array of data points to display in the chart
*/
data: DataPoint[];
}

/**
* Renders a bar chart using the provided data.
*
* @param {BarChartProps} props - Component props
* @return {JSX.Element} The rendered bar chart component
*/
function BarChart( {
data,
width,
height,
margin,
withTooltips = false,
className,
}: BarChartProps ) {
const { tooltipOpen, tooltipLeft, tooltipTop, tooltipData, hideTooltip, showTooltip } =
useTooltip< DataPoint >();

const theme = useChartTheme();
const margins = { top: 20, right: 20, bottom: 40, left: 40, ...margin };
const xMax = width - margins.left - margins.right;
const yMax = height - margins.top - margins.bottom;

const xScale = scaleBand< string >( {
range: [ 0, xMax ],
domain: data.map( d => d.label ),
padding: 0.2,
} );

const yScale = scaleLinear< number >( {
range: [ yMax, 0 ],
domain: [ 0, Math.max( ...data.map( d => d.value ) ) ],
} );

const handleMouseMove = React.useCallback(
( event: React.MouseEvent< SVGRectElement >, datum: DataPoint ) => {
const coords = localPoint( event );
if ( ! coords ) return;

showTooltip( {
tooltipData: datum,
tooltipLeft: coords.x,
tooltipTop: coords.y - 10,
} );
},
[ showTooltip ]
);

const handleMouseLeave = React.useCallback( () => {
hideTooltip();
}, [ hideTooltip ] );

const getMouseMoveHandler = React.useCallback(
( d: DataPoint ) => {
if ( ! withTooltips ) return undefined;
return ( event: React.MouseEvent< SVGRectElement > ) => handleMouseMove( event, d );
},
[ withTooltips, handleMouseMove ]
);

return (
<div className={ clsx( 'bar-chart', className ) } style={ { position: 'relative' } }>
<svg width={ width } height={ height }>
<Group left={ margins.left } top={ margins.top }>
{ data.map( d => (
<Bar
key={ `bar-${ d.label }` }
x={ xScale( d.label ) }
y={ yScale( d.value ) }
width={ xScale.bandwidth() }
height={ yMax - ( yScale( d.value ) ?? 0 ) }
fill={ theme.colors[ 0 ] }
onMouseMove={ getMouseMoveHandler( d ) }
onMouseLeave={ withTooltips ? handleMouseLeave : undefined }
/>
) ) }
<AxisLeft scale={ yScale } />
<AxisBottom scale={ xScale } top={ yMax } />
</Group>
</svg>
{ tooltipOpen && tooltipData && (
<Tooltip
data={ tooltipData }
top={ tooltipTop }
left={ tooltipLeft }
style={ {
transform: 'translate(-50%, -100%)',
} }
/>
) }
</div>
);
}

export default BarChart;
export { default } from './bar-chart';
Loading

0 comments on commit 6117c79

Please sign in to comment.