score:1
Here's an example of using a linearGradient, with a function that finds the y coordinates given an x coordinate. A rect with 0 opacity captures mouse events to show and update the linearGradients's stops and lines coordinates
https://bl.ocks.org/tomshanley/01a87c81b5ed86b6d55e566403c175ba
An area shape is created where the y0 and y1 functions are using the y values from the two lines.
let curve = d3.curveCatmullRom.alpha(0.5)
let area = d3.area()
.x(function (d, i) { return xScale(i) })
.y0(function (d) { return yScale(d.data1) })
.y1(function (d) { return yScale(d.data2) })
.curve(curve);
let line1 = d3.line()
.x(function (d, i) { return xScale(i) })
.y(function (d) { return yScale(d.data1) })
.curve(curve);
let line2 = d3.line()
.x(function (d, i) { return xScale(i) })
.y(function (d) { return yScale(d.data2) })
.curve(curve);
The area's fill is set to a linearGradient, which has 'stops' that are updated with mousemove event. The stops' percentages are at set so that the white and grey change abruptly to give the impression of a solid fill, either side of the mouse's position.
let x = d3.mouse(this)[0]
let middle = x / width
offset1 = (middle - 0.1) < 0 ? 0 : (middle - 0.1)
offset2 = (middle + 0.1) > 1 ? 1 : (middle + 0.1)
stopsData = [
{ "offset": 0, "stopColour": "#FFFFFF" },
{ "offset": offset1, "stopColour": "#FFFFFF" },
{ "offset": offset1, "stopColour": "#777777" },
{ "offset": offset2, "stopColour": "#777777" },
{ "offset": offset2, "stopColour": "#FFFFFF" },
{ "offset": 1, "stopColour": "#FFFFFF" }
]
gradient.selectAll("stop")
.data(stopsData)
.attr("offset", function (d) { return d.offset })
.attr("stop-color", function (d) { return d.stopColour })
Your sketch included lines each side of the area's fill, which I created using two lines, whose y1 and y2 attributes by set by traversing along each path until you get to the point where the x coordinate is same (or just greater than) the mouse's x position.
o1 = width * offset1
o2 = width * offset2
line1Ys = findYs(path1Node, o1, o2)
line2Ys = findYs(path2Node, o1, o2)
areaLine1.attr("x1", width*offset1)
.attr("x2", width*offset1)
.attr("y1", line1Ys[0])
.attr("y2", line2Ys[0])
areaLine2.attr("x1", width*offset2)
.attr("x2", width*offset2)
.attr("y1", line1Ys[1])
.attr("y2", line2Ys[1])
function findYs(p, x1, x2) {
const accuracy = 1 //increase for quicker, but less accurate lines
let ys = [];
let i = x1;
const l = p.getTotalLength()
for (i; i < l; i+=accuracy) {
let pos = p.getPointAtLength(i)
if (pos.x > x1) {
ys.push(pos.y)
break
}
}
for (i; i < l; i+=accuracy) {
let pos = p.getPointAtLength(i)
if (pos.x > x2) {
ys.push(pos.y)
break
}
}
return ys;
}
Source: stackoverflow.com
Related Query
- How to fill the area between the x-segments of two lines in d3.js?
- conditionally fill area between two lines in d3
- How to add the fill in between two line in line graph?
- how to fill the area between the bars in the given example d3js
- Using d3 to shade area between two lines
- How to fill inner area between multiple SVG paths?
- Shade area between two lines using D3
- How to draw area/fill between two curved lines of different lengths (different x values) in d3
- How to add non linear curve between any two points of area chart in d3.js
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- How to visualise the relationship between two nodes in Neo4j using d3.js?
- How can I put a space between the lines of a line chart using d3.js
- how can we reduce the distance between two edges?
- How to add a label next to a generated SVG angle between two lines
- In d3 how can I get the value between two points?
- How to draw a line / link between two points on a D3 map based on latitude / longitude?
- How to change the color of two circles upon overlap?
- How to update the fill color on existing svg elements with d3.js?
- D3.js adding intercept point and area between two paths
- How to draw lines between circles?
- How to use linkRadial to draw a link between two points?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How to change the distance between nodes in a force layout?
- How do I draw an arrow between two points in d3v4?
- What is the difference between these two code blocks in d3.js
- How to get a set of subpoints between two points?
- How to fill area with our choice colour in plotly area grap
- How do I get rid of tiny lines between canvas rects
- D3 how to draw chart with a x data between two y data
- How to make grid lines when axes are aligned at the middle?
More Query from same tag
- Append an element with a name set conditionally with D3
- Dynamic filtering with D3
- How do functions pass parameters in D3 and JS calls
- d3 raster+vector with labels on features
- adding text to a circle in d3
- D3.js spawn new circles at fixed time intervals with forceSimulation
- How to get a SVG's aspect ratio?
- Keeping element order (selection.order) with exit transitions in d3
- d3 line graph does not present data correctly when called again
- Why are labels/text not rendering on this d3.js scatter plot?
- Problems converting a csv file to flare.json
- Uncaught error: cannot construct a line segment with two equal endpoints
- Create d3 linear color legend using d3 colors
- How to match data points in D3.js to match x-axis?
- Dynamically Update Styling on Button Click, D3 Bar Chart
- D3+Leaflet Circle SVG element not displaying any color
- Appending maps as part of group (multiple maps)
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- Scaling an arrowhead on a D3 force layout link marker
- can i attach arrow with the tooltip in d3?
- SVG to PNG Conversion in javascript
- Chaining functions in a D3.js plugin
- Is a horizontal scroll bar for graphs available on Bokeh?
- D3 Zoomable Treemap changing the children accessor
- Update graph in real time from server
- Controlling transparency of D3 orthographic projections
- d3js nested selection unexpected behavior
- Transitions with circles in D3
- D3js How to format the content of some columns in a table
- d3.format "none" type not rounding