Skip to content

How to add LinearGradient area both above and below some x value in a line graph #833

Answered by Jorundur
Jorundur asked this question in Q&A
Discussion options

You must be logged in to vote

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.

  1. Define the graph line, I used d3 for this:
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
  1. Define a function that creates a gradient area either above or below y=0 (positive vs. negative)
export const getGradientAreaSplit = (
  graphLine: string | null, // the line created above
  width: number,
  height: number, // in my use case this is the…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Jorundur
Comment options

Comment options

You must be logged in to vote
1 reply
@carlreiser2
Comment options

Answer selected by Jorundur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants