based on http://bl.ocks.org/mbostock/3310323 this shows how you put gaps in your line for missinng data -- here denoted by randomly inserting NaN values (so you may need to refresh the page if you don't see any breaks)
Last active
February 10, 2018 01:45
-
-
Save tomgp/77772ee20f8d509ad5b4 to your computer and use it in GitHub Desktop.
line interpolation with gaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
.dot { | |
fill: white; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var data = d3.range(20).map(function(i) { | |
return {x: i / 19, y: (Math.sin(i / 3) + 2) / 4}; | |
}).map(function(d){ //remove y values at random | |
if(Math.random() < 0.1){ | |
return { | |
x:d.x, | |
y:NaN | |
} | |
} | |
return d; | |
}); | |
var margin = {top: 20, right: 30, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.domain([0, 1]) | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.domain([0, 1]) | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
var line = d3.svg.line() | |
.interpolate(function(points){ //interpolate straight lines with gaps for NaN | |
var section = 0; | |
var arrays = [[]]; | |
points.forEach(function(d,i){ | |
if(isNaN(d[1])){ | |
section++; | |
arrays[section] = []; | |
}else{ | |
arrays[section].push(d) | |
} | |
}); | |
var pathSections = []; | |
arrays.forEach(function(points){ | |
pathSections.push(d3.svg.line()(points)); | |
}) | |
var joined = pathSections.join(''); | |
return joined.substr(1); //substring becasue D£ always adds an M to a path so we end up with MM at the start | |
}) | |
.x(function(d) { return x(d.x); }) | |
.y(function(d) { return y(d.y); }); | |
var svg = d3.select("body").append("svg") | |
.datum(data) | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
svg.append("path") | |
.attr("class", "line") | |
.attr("d", line); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment