2011-05-02 03:15:09 +04:00
|
|
|
<!DOCTYPE html>
|
2010-09-28 22:34:52 +04:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Dot Plot</title>
|
Simplify data join. Add insert & empty.
The data join is now specified as a single function of data, as with all other
properties. This allows the key to be computed on the previously-bound data,
rather than requiring the key to be serialized into the DOM (say, as an
attribute). In the case that there is no previously-bound data, it is still
possible to access the associated node as the `this` context.
The `enter` operator no longer performs an append. For symmetry with the `exit`
operator, you must call `append` after obtaining the entering selection. This
requires a tiny bit more code, but should make the code more clear. Also, it
provides an opportunity to use a different instantiation operator, such as the
new `insert` operator. This takes a second argument, which is a selector for the
insert-before reference element. For example, the selector ":first-child" will
prepend nodes.
The `empty` operator allows you to query whether a selection is empty (i.e.,
contains zero matching nodes).
2011-01-31 22:00:35 +03:00
|
|
|
<script type="text/javascript" src="../../d3.js"></script>
|
2010-09-28 22:34:52 +04:00
|
|
|
<style type="text/css">
|
|
|
|
|
|
|
|
body {
|
|
|
|
font: 10px sans-serif;
|
|
|
|
}
|
|
|
|
|
2011-02-14 10:20:10 +03:00
|
|
|
path.dot {
|
|
|
|
fill: white;
|
2010-09-28 22:34:52 +04:00
|
|
|
stroke-width: 1.5px;
|
|
|
|
}
|
|
|
|
|
|
|
|
rect {
|
|
|
|
fill: none;
|
|
|
|
stroke: black;
|
|
|
|
shape-rendering: crispEdges;
|
|
|
|
}
|
|
|
|
|
2011-02-14 10:20:10 +03:00
|
|
|
.x line, .y line {
|
2010-09-28 22:34:52 +04:00
|
|
|
stroke: #ccc;
|
|
|
|
shape-rendering: crispEdges;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
2010-10-24 07:32:16 +04:00
|
|
|
var data = d3.range(100).map(function(i) {
|
2010-09-28 22:34:52 +04:00
|
|
|
return {x: i / 99, y: Math.random()};
|
|
|
|
});
|
|
|
|
|
|
|
|
var w = 450,
|
|
|
|
h = 450,
|
2011-02-14 10:20:10 +03:00
|
|
|
p = 20,
|
|
|
|
x = d3.scale.linear().range([0, w]),
|
|
|
|
y = d3.scale.linear().range([h, 0]),
|
|
|
|
symbol = d3.scale.ordinal().range(d3.svg.symbolTypes),
|
|
|
|
color = d3.scale.category10();
|
2010-09-28 22:34:52 +04:00
|
|
|
|
|
|
|
var vis = d3.select("body")
|
2011-11-24 02:46:37 +04:00
|
|
|
.append("svg")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("width", w + p * 2)
|
|
|
|
.attr("height", h + p * 2)
|
2011-11-24 02:46:37 +04:00
|
|
|
.append("g")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("transform", "translate(" + p + "," + p + ")");
|
|
|
|
|
2011-02-14 10:20:10 +03:00
|
|
|
var xrule = vis.selectAll("g.x")
|
|
|
|
.data(x.ticks(10))
|
2011-11-24 02:46:37 +04:00
|
|
|
.enter().append("g")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("class", "x");
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
xrule.append("line")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("x1", x)
|
|
|
|
.attr("x2", x)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("y1", 0)
|
|
|
|
.attr("y2", h);
|
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
xrule.append("text")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("x", x)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("y", h + 3)
|
|
|
|
.attr("dy", ".71em")
|
|
|
|
.attr("text-anchor", "middle")
|
2011-02-14 10:20:10 +03:00
|
|
|
.text(x.tickFormat(10));
|
|
|
|
|
|
|
|
var yrule = vis.selectAll("g.y")
|
|
|
|
.data(y.ticks(10))
|
2011-11-24 02:46:37 +04:00
|
|
|
.enter().append("g")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("class", "y");
|
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
yrule.append("line")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("x1", 0)
|
|
|
|
.attr("x2", w)
|
|
|
|
.attr("y1", y)
|
|
|
|
.attr("y2", y);
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
yrule.append("text")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("x", -3)
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("y", y)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("dy", ".35em")
|
|
|
|
.attr("text-anchor", "end")
|
2011-02-14 10:20:10 +03:00
|
|
|
.text(y.tickFormat(10));
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2011-11-24 02:46:37 +04:00
|
|
|
vis.append("rect")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("width", w)
|
|
|
|
.attr("height", h);
|
|
|
|
|
2011-02-14 10:20:10 +03:00
|
|
|
vis.selectAll("path.dot")
|
2010-09-28 22:34:52 +04:00
|
|
|
.data(data)
|
2011-11-24 02:46:37 +04:00
|
|
|
.enter().append("path")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("class", "dot")
|
2011-02-14 10:20:10 +03:00
|
|
|
.attr("stroke", function(d, i) { return color(i); })
|
|
|
|
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; })
|
|
|
|
.attr("d", d3.svg.symbol()
|
|
|
|
.type(function(d, i) { return symbol(i); }));
|
2010-09-28 22:34:52 +04:00
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|