-
Notifications
You must be signed in to change notification settings - Fork 715
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(vx-react-spring): add package + AnimatedAxis #779
Changes from all commits
a75cd1e
92a1b2e
6d6117e
3127ea5
88239bb
ffc5235
7082246
46d1979
ec05f4b
b15a1c4
0af9877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,20 +46,21 @@ export default function Axis<Scale extends AxisScale>({ | |
horizontal, | ||
); | ||
|
||
const ticks = (tickValues ?? getTicks(scale, numTicks)) | ||
const filteredTickValues = (tickValues ?? getTicks(scale, numTicks)) | ||
.map((value, index) => ({ value, index })) | ||
.filter(({ value }) => !hideZero || (value !== 0 && value !== '0')) | ||
.map(({ value, index }) => { | ||
const scaledValue = coerceNumber(tickPosition(value)); | ||
.filter(({ value }) => !hideZero || (value !== 0 && value !== '0')); | ||
|
||
return { | ||
value, | ||
index, | ||
from: createPoint({ x: scaledValue, y: 0 }, horizontal), | ||
to: createPoint({ x: scaledValue, y: tickLength * tickSign }, horizontal), | ||
formattedValue: format(value, index), | ||
}; | ||
}); | ||
const ticks = filteredTickValues.map(({ value, index }) => { | ||
const scaledValue = coerceNumber(tickPosition(value)); | ||
|
||
return { | ||
value, | ||
index, | ||
from: createPoint({ x: scaledValue, y: 0 }, horizontal), | ||
to: createPoint({ x: scaledValue, y: tickLength * tickSign }, horizontal), | ||
formattedValue: format(value, index, filteredTickValues), | ||
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. have been wanting to add this for a while (will update the PR description): we now pass all ticks as a third argument (common d3 pattern) so that users can determine if a tick is first/last, etc. with |
||
}; | ||
}); | ||
|
||
return ( | ||
<Group className={cx('vx-axis', axisClassName)} top={top} left={left}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,19 @@ import Axis from './Axis'; | |
import Orientation from '../constants/orientation'; | ||
import { SharedAxisProps, AxisScale } from '../types'; | ||
|
||
export default function AxisBottom<Scale extends AxisScale>({ | ||
axisClassName, | ||
labelOffset = 8, | ||
tickLabelProps = (/** tickValue, index */) => ({ | ||
export const bottomTickLabelProps = (/** tickValue, index */) => | ||
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. exporting these so that they could be used outside the component. tick label props are the primary differences between axis configurations |
||
({ | ||
dy: '0.25em', | ||
fill: '#222', | ||
fontFamily: 'Arial', | ||
fontSize: 10, | ||
textAnchor: 'middle', | ||
}), | ||
} as const); | ||
|
||
export default function AxisBottom<Scale extends AxisScale>({ | ||
axisClassName, | ||
labelOffset = 8, | ||
tickLabelProps = bottomTickLabelProps, | ||
tickLength = 8, | ||
...restProps | ||
}: SharedAxisProps<Scale>) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { Line } from '@vx/shape'; | ||
import { Group } from '@vx/group'; | ||
import { Text } from '@vx/text'; | ||
|
||
import { TextProps } from '@vx/text/lib/Text'; | ||
import Orientation from '../constants/orientation'; | ||
import getLabelTransform from '../utils/getLabelTransform'; | ||
import { AxisRendererProps, AxisScale } from '../types'; | ||
import Ticks from './Ticks'; | ||
|
||
const defaultTextProps: Partial<TextProps> = { | ||
textAnchor: 'middle', | ||
|
@@ -30,51 +29,36 @@ export default function AxisRenderer<Scale extends AxisScale>({ | |
orientation, | ||
scale, | ||
stroke = '#222', | ||
strokeWidth = 1, | ||
strokeDasharray, | ||
strokeWidth = 1, | ||
tickClassName, | ||
tickComponent, | ||
tickLabelProps = (/** tickValue, index */) => defaultTextProps, | ||
tickLength, | ||
tickStroke = '#222', | ||
tickTransform, | ||
ticks, | ||
ticksComponent = Ticks, | ||
}: AxisRendererProps<Scale>) { | ||
let tickLabelFontSize = 10; // track the max tick label size to compute label offset | ||
|
||
// compute the max tick label size to compute label offset | ||
const allTickLabelProps = ticks.map(({ value, index }) => tickLabelProps(value, index)); | ||
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. this is the jank logic I alluded to above. The label and ticks were previously coupled with this, so I updated the logic to pass an array of Might be worth a breaking change to remove this? |
||
const maxTickLabelFontSize = Math.max( | ||
10, | ||
...allTickLabelProps.map(props => (typeof props.fontSize === 'number' ? props.fontSize : 0)), | ||
); | ||
return ( | ||
<> | ||
{ticks.map(({ value, index, from, to, formattedValue }) => { | ||
const tickLabelPropsObj = tickLabelProps(value, index); | ||
tickLabelFontSize = Math.max( | ||
tickLabelFontSize, | ||
(typeof tickLabelPropsObj.fontSize === 'number' && tickLabelPropsObj.fontSize) || 0, | ||
); | ||
|
||
const tickYCoord = | ||
to.y + (horizontal && orientation !== Orientation.top ? tickLabelFontSize : 0); | ||
|
||
return ( | ||
<Group | ||
key={`vx-tick-${value}-${index}`} | ||
className={cx('vx-axis-tick', tickClassName)} | ||
transform={tickTransform} | ||
> | ||
{!hideTicks && <Line from={from} to={to} stroke={tickStroke} strokeLinecap="square" />} | ||
{tickComponent ? ( | ||
tickComponent({ | ||
...tickLabelPropsObj, | ||
x: to.x, | ||
y: tickYCoord, | ||
formattedValue, | ||
}) | ||
) : ( | ||
<Text x={to.x} y={tickYCoord} {...tickLabelPropsObj}> | ||
{formattedValue} | ||
</Text> | ||
)} | ||
</Group> | ||
); | ||
{ticksComponent({ | ||
hideTicks, | ||
horizontal, | ||
orientation, | ||
scale, | ||
tickClassName, | ||
tickComponent, | ||
tickLabelProps: allTickLabelProps, | ||
tickStroke, | ||
tickTransform, | ||
ticks, | ||
})} | ||
|
||
{!hideAxisLine && ( | ||
|
@@ -96,7 +80,7 @@ export default function AxisRenderer<Scale extends AxisScale>({ | |
labelProps, | ||
orientation, | ||
range: scale.range(), | ||
tickLabelFontSize, | ||
tickLabelFontSize: maxTickLabelFontSize, | ||
tickLength, | ||
})} | ||
{...labelProps} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { Line } from '@vx/shape'; | ||
import { Group } from '@vx/group'; | ||
import { Text } from '@vx/text'; | ||
|
||
import Orientation from '../constants/orientation'; | ||
import { TicksRendererProps, AxisScale } from '../types'; | ||
|
||
export default function Ticks<Scale extends AxisScale>({ | ||
hideTicks, | ||
horizontal, | ||
orientation, | ||
tickClassName, | ||
tickComponent, | ||
tickLabelProps: allTickLabelProps, | ||
tickStroke = '#222', | ||
tickTransform, | ||
ticks, | ||
}: TicksRendererProps<Scale>) { | ||
return ticks.map(({ value, index, from, to, formattedValue }) => { | ||
const tickLabelProps = allTickLabelProps[index] ?? {}; | ||
const tickLabelFontSize = Math.max( | ||
10, | ||
(typeof tickLabelProps.fontSize === 'number' && tickLabelProps.fontSize) || 0, | ||
); | ||
|
||
const tickYCoord = | ||
to.y + (horizontal && orientation !== Orientation.top ? tickLabelFontSize : 0); | ||
|
||
return ( | ||
<Group | ||
key={`vx-tick-${value}-${index}`} | ||
className={cx('vx-axis-tick', tickClassName)} | ||
transform={tickTransform} | ||
> | ||
{!hideTicks && <Line from={from} to={to} stroke={tickStroke} strokeLinecap="square" />} | ||
{tickComponent ? ( | ||
tickComponent({ | ||
...tickLabelProps, | ||
x: to.x, | ||
y: tickYCoord, | ||
formattedValue, | ||
}) | ||
) : ( | ||
<Text x={to.x} y={tickYCoord} {...tickLabelProps}> | ||
{formattedValue} | ||
</Text> | ||
)} | ||
</Group> | ||
); | ||
}); | ||
} |
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 now a
dep
so don't need to duplicate it