Last active
December 30, 2015 05:28
-
-
Save zoopoetics/7782522 to your computer and use it in GitHub Desktop.
Interpolating over Béziers in D3.js: Falsy Values
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> | |
<html> | |
<head> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script> | |
// Data | |
var data = [], | |
timestamp = new Date(); | |
timestamp.setMinutes(0); | |
for (var i = 0; i < 24; i++) { | |
var hours = timestamp.getHours() + 1; | |
timestamp.setHours(hours); | |
var value = Math.random(); | |
if (!Math.round(Math.random() * 2)) { | |
value = null; | |
} | |
data.push({ | |
timestamp: new Date(timestamp), | |
value: value | |
}); | |
} | |
// Dimensions | |
var margin = { | |
top: 0, | |
right: 0, | |
bottom: 0, | |
left: 0 | |
}, | |
width = 960, | |
height = 500; | |
// Domains | |
var domainX = d3.extent(data, function(datum) { | |
return datum.timestamp; | |
}); | |
var domainY = d3.extent(data, function(datum) { | |
return datum.value; | |
}); | |
// Ranges | |
var rangeX = [0, width], | |
rangeY = [height, 0]; | |
// Scales | |
var scaleX = d3.time.scale() | |
.domain(domainX) | |
.range(rangeX); | |
var scaleY = d3.scale.linear() | |
.domain(domainY) | |
.range(rangeY); | |
// Shape generators | |
var line = d3.svg.line() | |
.defined(function(datum) { | |
if (datum.value || datum.value === 0) { | |
return true; | |
} | |
}) | |
.interpolate('cardinal') | |
.x(function(datum) { | |
return scaleX(datum.timestamp); | |
}) | |
.y(function(datum) { | |
return scaleY(datum.value); | |
}); | |
// Append an SVG element | |
var svg = d3.select('body') | |
.append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
// Append path | |
var path = svg.append('path') | |
.datum(data) | |
.attr('d', line) | |
.style('fill', 'none') | |
.style('pointer-events', 'none') | |
.style('stroke', '#FB5050') | |
.style('stroke-width', '3px'); | |
// Append marker | |
var marker = svg.append('circle') | |
.attr('r', 7) | |
.style('display', 'none') | |
.style('fill', '#FFFFFF') | |
.style('pointer-events', 'none') | |
.style('stroke', '#FB5050') | |
.style('stroke-width', '3px'); | |
// Get points: | |
var node = path.node(), | |
points = []; | |
for (var i = 0; i < node.getTotalLength(); i++) { | |
points.push(node.getPointAtLength(i)); | |
} | |
// Create custom bisector | |
var bisect = d3.bisector(function(datum) { | |
return datum.x; | |
}).right; | |
// Add event listeners/handlers | |
svg.on('mouseover', function() { | |
marker.style('display', 'inherit'); | |
}).on('mouseout', function() { | |
marker.style('display', 'none'); | |
}).on('mousemove', function() { | |
var mouse = d3.mouse(this), | |
index = bisect(points, mouse[0]), | |
point = points[index], | |
difference = point.x - mouse[0]; | |
marker.style('display', 'inherit'); | |
if (difference > 1) { | |
marker.style('display', 'none'); | |
} | |
marker.attr('cx', point.x); | |
marker.attr('cy', point.y); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment