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?
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).
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:
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.
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.
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.
a couple of issues to address:
.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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With