-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
379 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
projects/js-packages/charts/changelog/update-charts-library-tooltip-iteration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: changed | ||
|
||
Chart Library: Update tooltip component |
3 changes: 3 additions & 0 deletions
3
projects/js-packages/charts/src/components/bar-chart/bar-chart.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.bar-chart { | ||
position: relative; | ||
} |
107 changes: 107 additions & 0 deletions
107
projects/js-packages/charts/src/components/bar-chart/bar-chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
114
projects/js-packages/charts/src/components/bar-chart/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.