Skip to content

Instantly share code, notes, and snippets.

@hoschi
Forked from mbostock/.block
Last active December 25, 2015 14:29
Show Gist options
  • Save hoschi/6991203 to your computer and use it in GitHub Desktop.
Save hoschi/6991203 to your computer and use it in GitHub Desktop.
Example how it looks without 'defined' usage.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.area {
fill: lightsteelblue;
}
.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(40).map(function(i) {
return {x: i / 39, y: i % 5 ? (Math.sin(i / 3) + 2) / 4 : null};
});
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.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()
//.defined(function(d) { return d.y != null; })
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); });
var area = d3.svg.area()
//.defined(line.defined())
.x(line.x())
.y1(line.y())
.y0(y(0));
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("path")
.attr("class", "area")
.attr("d", area);
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.filter(function(d) { return d.y; }))
.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