Created
August 10, 2012 19:00
-
-
Save snoble/3316859 to your computer and use it in GitHub Desktop.
Monotonic Interpolation Fix
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="https://raw.github.com/snoble/d3/master/d3.v2.js"></script> | |
<script> | |
var data = [ | |
[new Date(2001, 11, 1), 5], | |
[new Date(2002, 0, 1), 4.5], | |
// [new Date(2003, 0, 1), 4.5], | |
[new Date(2004, 0, 1), 5], | |
[new Date(2005, 0, 1), 6], | |
[new Date(2006, 0, 1), 3] | |
]; | |
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.time.scale() | |
.domain([new Date(2001, 0, 1), new Date(2006, 0, 1)]) | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.domain([0, 6]) | |
.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("monotone") | |
.x(function(d) { return x(d[0]); }) | |
.y(function(d) { return y(d[1]); }); | |
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); | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "dot") | |
.attr("cx", line.x()) | |
.attr("cy", line.y()) | |
.attr("r", 3.5); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment