-
I'm trying to do something similar to this image in The only thing I'm asking about here is just the linear gradient part - i.e. how to get a linear gradient fill both below some y value (here y = 0) and above it. Since this is a graph whose purpose is similar to the graph in the Wallet example in this repo, I've been looking into that example and attempting to change it to my needs but haven't got very far. Can anyone help me create a line with the linear gradient like shown in the image? Many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Here's an idea on how to do it. How idea if this a good approach or not:
|
Beta Was this translation helpful? Give feedback.
-
I finally got round to looking into this again. I didn't fully understand how to get this working with the bounding boxes @wcandillon, but ended up doing something similar to what you mentioned with splitting this into two parts.
const graphLine = line<DataPoint>()
.x((d) => x(new Date(d.date)))
.y((d) => y(d.price))(data); // data is an array of { date: string, price: number } objects
export const getGradientAreaSplit = (
graphLine: string | null, // the line created above
width: number,
height: number, // in my use case this is the graph height / 2, i.e. area above or below y=0
variant: 'positive' | 'negative'
) => {
// 1) The initial graph line (which can go below or above y=0)
const gradientAreaSplit = Skia.Path.MakeFromSVGString(graphLine ?? '0') ?? Skia.Path.Make();
const useFirstY =
(variant === 'positive' && gradientAreaSplit.getPoint(0).y > 0) ||
(variant === 'negative' && gradientAreaSplit.getPoint(0).y < 0);
gradientAreaSplit
// 2) Extend line to y=0 at the right end
.lineTo(width, height)
// 3) Extend line to y=0 at the left end
.lineTo(0, height)
// 4) Extend line to first point to close the area if that point is in the area, otherwise to y=0 (see useFirstY logic)
.lineTo(0, useFirstY ? gradientAreaSplit.getPoint(0).y : height);
return gradientAreaSplit;
}; This can create the areas shown in below (I've crudely added a gold connector at y=0 (bottom of 1st img, top of 2nd) to the images just to understand it better, it's not actually rendered):
<Group>
<Path path={gradientAreaPosPath}>
<LinearGradient start={vec(0, RANGE_OFFSET)} end={vec(0, GRAPH_HEIGHT / 2)} colors={gradientAreaColors} />
</Path>
<Path path={gradientAreaNegPath}>
<LinearGradient
start={vec(0, GRAPH_HEIGHT - RANGE_OFFSET)}
end={vec(0, GRAPH_HEIGHT / 2)}
colors={gradientAreaColors}
/>
</Path>
</Group> |
Beta Was this translation helpful? Give feedback.
I finally got round to looking into this again. I didn't fully understand how to get this working with the bounding boxes @wcandillon, but ended up doing something similar to what you mentioned with splitting this into two parts.
d3
for this: