-
Notifications
You must be signed in to change notification settings - Fork 723
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
new(xychart): add BarSeries #808
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5577e14
new(xychart): add withRegisteredData, pull out SeriesProps
williaster a07c8bf
internal(xychart): factor out logic from LineSeries, fix Datum types
williaster 643fe43
new(xychart): iterate on BarSeries + add to example
williaster 4518b2b
fix(xychart): rewrite useDataRegistry to update properly with hooks
williaster 8fd1053
new(demo/xychart): add series rendering controls
williaster cf251e9
test(xychart): add useDataRegistry, withRegisteredData, BarSeries tests
williaster c8ae284
deps(xychart): remove uuid dep
williaster ee94420
fix(xychart): fix withRegisteredData, improve BarSeries logic
williaster 46b973f
fix(withRegisteredData): one more round on generics
williaster 127eb5b
fix(withRegisteredData): two more rounds on generics
williaster 7ce551f
fix(withRegisteredData): remove unused test var
williaster f8ce13a
internal(xychart/useDataRegistry): use single memo
williaster 96e2d85
lint(xychart/useDataRegistry)
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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,92 @@ | ||
import React, { useContext, useCallback, useMemo } from 'react'; | ||
import { AxisScale } from '@visx/axis'; | ||
import DataContext from '../../context/DataContext'; | ||
import { SeriesProps } from '../../types'; | ||
import withRegisteredData, { WithRegisteredDataProps } from '../../enhancers/withRegisteredData'; | ||
import getScaledValueFactory from '../../utils/getScaledValueFactory'; | ||
import isValidNumber from '../../typeguards/isValidNumber'; | ||
import getScaleBandwidth from '../../utils/getScaleBandwidth'; | ||
|
||
type BarSeriesProps< | ||
XScale extends AxisScale, | ||
YScale extends AxisScale, | ||
Datum extends object | ||
> = SeriesProps<XScale, YScale, Datum> & { | ||
/** Whether bars should be rendered horizontally instead of vertically. */ | ||
horizontal?: boolean; | ||
/** | ||
* Specify bar padding when bar thickness does not come from a `band` scale. | ||
* Accepted values are [0, 1], 0 = no padding, 1 = no bar, defaults to 0.1. | ||
*/ | ||
barPadding?: number; | ||
}; | ||
|
||
// Fallback bandwidth estimate assumes no missing data values (divides chart space by # datum) | ||
const getFallbackBandwidth = (fullBarWidth: number, barPadding: number) => | ||
// clamp padding to [0, 1], bar thickness = (1-padding) * availableSpace | ||
fullBarWidth * (1 - Math.min(1, Math.max(0, barPadding))); | ||
|
||
function BarSeries<XScale extends AxisScale, YScale extends AxisScale, Datum extends object>({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most of this logic can become |
||
barPadding = 0.1, | ||
data, | ||
dataKey, | ||
horizontal, | ||
xAccessor, | ||
xScale, | ||
yAccessor, | ||
yScale, | ||
}: BarSeriesProps<XScale, YScale, Datum> & WithRegisteredDataProps<XScale, YScale, Datum>) { | ||
const { colorScale, theme, innerWidth = 0, innerHeight = 0 } = useContext(DataContext); | ||
const getScaledX = useCallback(getScaledValueFactory(xScale, xAccessor), [xScale, xAccessor]); | ||
const getScaledY = useCallback(getScaledValueFactory(yScale, yAccessor), [yScale, yAccessor]); | ||
const [xMin, xMax] = xScale.range().map(Number); | ||
const [yMax, yMin] = yScale.range().map(Number); | ||
|
||
const scaleBandwidth = getScaleBandwidth(horizontal ? yScale : xScale); | ||
const barThickness = | ||
scaleBandwidth || | ||
getFallbackBandwidth((horizontal ? innerHeight : innerWidth) / data.length, barPadding); | ||
|
||
// try to figure out the 0 baseline for correct rendering of negative values | ||
// we aren't sure if these are numeric scales or not ahead of time | ||
const maybeXZero = xScale(0); | ||
const maybeYZero = yScale(0); | ||
const xZeroPosition = isValidNumber(maybeXZero) | ||
? // if maybeXZero _is_ a number, but the scale is not clamped and it's outside the domain | ||
// fallback to the scale's minimum | ||
Math.max(maybeXZero, Math.min(xMin, xMax)) | ||
: Math.min(xMin, xMax); | ||
const yZeroPosition = isValidNumber(maybeYZero) | ||
? Math.min(maybeYZero, Math.max(yMin, yMax)) | ||
: Math.max(yMin, yMax); | ||
|
||
const color = colorScale?.(dataKey) ?? theme?.colors?.[0] ?? '#222'; | ||
|
||
const bars = useMemo(() => { | ||
const xOffset = horizontal ? 0 : -barThickness / 2; | ||
const yOffset = horizontal ? -barThickness / 2 : 0; | ||
return data.map(datum => { | ||
const x = getScaledX(datum) + xOffset; | ||
const y = getScaledY(datum) + yOffset; | ||
const barLength = horizontal ? x - xZeroPosition : y - yZeroPosition; | ||
|
||
return { | ||
x: horizontal ? xZeroPosition + Math.min(0, barLength) : x, | ||
y: horizontal ? y : yZeroPosition + Math.min(0, barLength), | ||
width: horizontal ? Math.abs(barLength) : barThickness, | ||
height: horizontal ? barThickness : Math.abs(barLength), | ||
fill: color, // @TODO allow prop overriding | ||
}; | ||
}); | ||
}, [barThickness, color, data, getScaledX, getScaledY, horizontal, xZeroPosition, yZeroPosition]); | ||
|
||
return ( | ||
<g className="vx-bar-series"> | ||
{bars.map(({ x, y, width, height, fill }, i) => ( | ||
<rect key={i} x={x} y={y} width={width} height={height} fill={fill} /> | ||
))} | ||
</g> | ||
); | ||
} | ||
|
||
export default withRegisteredData(BarSeries); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is backwards and was updated to match d3's band scale logic:
1=no bar, 0=no padding
. will fix.