[xy-chart] How to make axis labels vertical or angled somehow? #1000
-
I have an XYChart and some may get pretty loaded with ticks. I guess I can just divide by the length of dataset to show no more than 10 or 20 or whatever.
That's the code of mine. import React, { useEffect, useMemo } from 'react'
import {
lightTheme,
darkTheme,
Grid,
AreaSeries,
Axis,
XYChart,
Tooltip,
} from '@visx/xychart'
import { curveCardinal } from '@visx/curve'
import { ParentSize } from '@visx/responsive'
import { Box, useColorMode } from '@chakra-ui/react'
import { MetricHistoryPoint } from '__generated__/graphql-types.generated'
const dateScaleConfig = { type: 'band', paddingInner: 1 } as const
const moneyScaleConfig = { type: 'linear' } as const
export const DashboardGraph = ({ data }: { data: MetricHistoryPoint[] }) => {
const config = useMemo(
() => ({
x: dateScaleConfig,
y: moneyScaleConfig,
}),
[],
)
const { colorMode } = useColorMode()
const theme = colorMode === 'light' ? lightTheme : darkTheme
useEffect(() => {
setTimeout(() => {
window.dispatchEvent(new CustomEvent('scroll'))
}, 300)
})
return (
<ParentSize>
{({ width }) => (
<XYChart
theme={theme}
xScale={config.x}
yScale={config.y}
height={300}
width={width}
>
<Grid rows columns={false} strokeDasharray="10, 5" />
<AreaSeries
dataKey="Cash in"
data={data}
xAccessor={(it) => it.date}
yAccessor={(it) => it.value}
fillOpacity={0.4}
curve={curveCardinal}
/>
<Axis orientation="bottom" strokeWidth={0} />
<Axis orientation="left" />
<Tooltip<{ date: string; value: number }>
showVerticalCrosshair
snapTooltipToDatumX
snapTooltipToDatumY
showSeriesGlyphs
renderTooltip={({ tooltipData }) => (
<Box>
{tooltipData?.nearestDatum?.datum
? tooltipData?.nearestDatum.datum.value
: '–'}
$
</Box>
)}
/>
</XYChart>
)}
</ParentSize>
)
} Also, is using I wasn't able to find good docs with examples regarding |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @RIP21, good questions. These parts of
To reduce ticks, you can use
The Here is a modified axis demo showing how you can change
We should improve these docs, these configs are hard to understand if you aren't intimately familiar with
If you want the first/last points to lie on the edge of the graph, I would set Hope that helps, lmk if you have follow up questions 👍 |
Beta Was this translation helpful? Give feedback.
Hey @RIP21, good questions. These parts of
XYChart
actually map pretty cleanly to othervisx
packages, let me take your questions one by one.To reduce ticks, you can use
Axis
numTicks: number
(which is approximate! due to the underlyingd3
algorithm), you can specify exact tick values you want viatickValues: TickValue[]
, or you could render all ticks and simply render no label for some of them (e.g., every other). To do the latter you would usetickFormat
(see demo in next question).The
prop
you're looking for he…