Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get my area filled beneath my d3 line chart to be a gradient?

I'm using d3 v4. I would like to create a line chart where the area beneath the graph is an area filled by a gradient going from dark at the top to light at the bottom. I thought this was the way to configure such a gradient

  svg.append("linearGradient")
        .attr("id", "area-gradient")
        .attr("gradientUnits", "userSpaceOnUse")
        .attr("x1", 0).attr("y1", y(0))
        .attr("x2", 0).attr("y2", y(1000))
    .selectAll("stop")
        .data([
            {offset: "0%", color: "navy"},
            {offset: "30%", color: "navy"},
            {offset: "45%", color: "navy"},
            {offset: "55%", color: "navy"},
            {offset: "60%", color: "navy"},
            {offset: "100%", color: "navy"}
        ])
    .enter().append("stop")
        .attr("offset", function(d) { return d.offset; })
        .attr("stop-color", function(d) { return d.color; });

and using this style

.area {                         
    fill: url(#area-gradient);                  
    stroke-width: 0px;          
}

but as you can see from my Fiddle -- https://jsfiddle.net/yw46ycse/3/, what I have instead is a solid area. What else do I need to do to get the area beneath the graph to be a gradient?

like image 760
Dave Avatar asked Aug 09 '17 20:08

Dave


People also ask

What is the difference between area and line in D3?

Define the chart’s area and line. area () and line () are D3 helper functions. The area function transforms each data point into information that describes the shape, and the line function draws a line according to data values. curveCardinal is the type of line/area curve (check D3 curve explorer for more).

Is there an easier way to create gradients in D3?

Some digging around revealed that an easier way does in fact exist. However, like anything easy, it does have its drawbacks. D3.js is designed to easily render SVG, with its own built-in technique for creating gradients. SVG first requires you to create the gradient definition before you can use it, and it can be a little verbose:

How to build an area chart in D3?

Building an area chart relies on the d3.area () helper function. If you're not familiar with it, have a look to shape section of the gallery to understand its basics. Then follow the examples below to put it in practice. The most basic area chart you can do in d3.js. Keeping only the core code.

What is the use of D3 scale in Python?

And the d3.scaleLinear () function is used to create scale points on the y-axis. These scales will help us find the positions/coordinates on the graph for each data item. 4. Define the chart’s area and line. area () and line () are D3 helper functions.


2 Answers

Is this the version of the solution your looking for. You can use transparent as the stop-color {offset: "95%", color: "transparent"}.

Here is a modified version of your fiddle that i used to make it as you need it.

https://codepen.io/Nasir_T/pen/OjOWxz

Hope this helps.

like image 63
Nasir T Avatar answered Sep 20 '22 16:09

Nasir T


a couple of issues to address:

  1. You have "navy" for every stop, so the gradient won't show
  2. It would be better in this instance to not .attr("gradientUnits", "userSpaceOnUse"). By using the default setting of objectBoundingBox (https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/gradientUnits) then you can just use 0,0 0,1 to specify a vertical linear gradient.

For example:

svg.append("linearGradient")
        .attr("id", "area-gradient")
        .attr("x1", 0).attr("y1", 0)
        .attr("x2", 0).attr("y2", 1)
    .selectAll("stop")
        .data([
            {offset: "0%", color: "navy"},
            {offset: "100%", color: "red"}
        ])
    .enter().append("stop")
        .attr("offset", function(d) { return d.offset; })
        .attr("stop-color", function(d) { return d.color; })

Updated fiddle: https://jsfiddle.net/yw46ycse/4/

like image 20
Tom Shanley Avatar answered Sep 23 '22 16:09

Tom Shanley