-
How to make my AreaSeries fade down from solid color to fully transparent. Literally.... how would you build this one below :D I kinda got one done but it's far from looking like that :) |
Beta Was this translation helpful? Give feedback.
Answered by
RIP21
Jan 7, 2021
Replies: 2 comments 5 replies
-
I guess the answer is here https://airbnb.io/visx/docs/gradient |
Beta Was this translation helpful? Give feedback.
0 replies
-
And here is the answer. Full example: import React, { useEffect, useMemo } from 'react'
import {
lightTheme as lt,
darkTheme as dt,
Grid,
AreaSeries,
Axis,
XYChart,
Tooltip,
buildChartTheme,
} from '@visx/xychart'
import { ThemeConfig } from '@visx/xychart/lib/theme/buildChartTheme'
import { curveMonotoneX } from '@visx/curve'
import { LinearGradient } from '@visx/gradient'
import { ParentSize } from '@visx/responsive'
import { Box, useColorMode, Stack } from '@chakra-ui/react'
import { css } from '@emotion/css'
import { MetricHistoryPoint } from '__generated__/graphql-types.generated'
const dateScaleConfig = { type: 'band', paddingInner: 1 } as const
const moneyScaleConfig = { type: 'linear' } as const
const darkTheme = buildChartTheme(({
...dt,
colors: ['#109CF1'],
} as unknown) as ThemeConfig)
const lightTheme = buildChartTheme(({
...lt,
colors: ['#109CF1'],
} as unknown) as ThemeConfig)
const lineStyle = css`
stroke-width: 3;
`
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}
>
<LinearGradient
id="area-gradient"
from="#109CF1"
to="#109CF1"
toOpacity={0.1}
/>
<Grid
rows
columns={false}
strokeDasharray="10, 5"
// @ts-expect-error
strokeOpacity={0.3}
/>
<AreaSeries
dataKey="Cash in"
data={data}
fillOpacity={0.4}
xAccessor={(it) => it.date}
yAccessor={(it) => it.value}
fill="url(#area-gradient)"
stroke="url(#area-gradient)"
lineProps={{
className: lineStyle,
}}
curve={curveMonotoneX}
/>
<Axis
orientation="bottom"
strokeWidth={0}
numTicks={5}
hideAxisLine
hideTicks
/>
<Axis orientation="left" numTicks={5} hideAxisLine hideTicks />
<Tooltip<{ date: string; value: number }>
showVerticalCrosshair
snapTooltipToDatumX
snapTooltipToDatumY
verticalCrosshairStyle={{
strokeOpacity: 0.4,
}}
showSeriesGlyphs
renderTooltip={({ tooltipData }) => (
<Stack borderRadius={4}>
<Box>{tooltipData?.nearestDatum?.datum.date}</Box>
<Box>{tooltipData?.nearestDatum?.datum.value}$</Box>
</Stack>
)}
/>
</XYChart>
)}
</ParentSize>
)
} Main parts are: const darkTheme = buildChartTheme(({
...dt,
colors: ['#109CF1'],
} as unknown) as ThemeConfig)
const lightTheme = buildChartTheme(({
...lt,
colors: ['#109CF1'],
} as unknown) as ThemeConfig)
const lineStyle = css`
stroke-width: 3;
` <LinearGradient
id="area-gradient"
from="#109CF1"
to="#109CF1"
toOpacity={0.1}
/> and <AreaSeries
data={data}
fillOpacity={0.4}
xAccessor={(it) => it.date}
yAccessor={(it) => it.value}
fill="url(#area-gradient)"
stroke="url(#area-gradient)"
lineProps={{
className: lineStyle,
}}
curve={curveMonotoneX}
/> |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
RIP21
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is the answer.
Full example: