2010-09-28 22:34:52 +04:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Bar Chart</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;
|
|
|
|
}
|
|
|
|
|
|
|
|
svg {
|
|
|
|
shape-rendering: crispEdges;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
2010-10-21 02:22:22 +04:00
|
|
|
var data = d3.range(10).map(Math.random);
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-21 02:22:22 +04:00
|
|
|
var w = 430,
|
|
|
|
h = 230,
|
2010-10-31 02:30:10 +04:00
|
|
|
x = d3.scale.linear().domain([0, 1]).range([0, w]),
|
|
|
|
y = d3.scale.ordinal().domain(d3.range(data.length)).rangeBands([0, h], .2);
|
2010-10-20 22:07:42 +04:00
|
|
|
|
2010-09-28 22:34:52 +04:00
|
|
|
var vis = d3.select("body")
|
2010-10-02 22:00:31 +04:00
|
|
|
.append("svg:svg")
|
2010-10-22 23:55:50 +04:00
|
|
|
.attr("width", w + 40)
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("height", h + 20)
|
2010-10-02 22:00:31 +04:00
|
|
|
.append("svg:g")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("transform", "translate(20,0)");
|
|
|
|
|
|
|
|
var bars = vis.selectAll("g.bar")
|
|
|
|
.data(data)
|
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
|
|
|
.enter().append("svg:g")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("class", "bar")
|
2010-10-24 07:32:16 +04:00
|
|
|
.attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; });
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
bars.append("svg:rect")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("fill", "steelblue")
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("width", x)
|
|
|
|
.attr("height", y.rangeBand());
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
bars.append("svg:text")
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("x", x)
|
|
|
|
.attr("y", y.rangeBand() / 2)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("dx", -6)
|
|
|
|
.attr("dy", ".35em")
|
|
|
|
.attr("fill", "white")
|
|
|
|
.attr("text-anchor", "end")
|
2010-10-23 00:36:48 +04:00
|
|
|
.text(x.tickFormat(100));
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
bars.append("svg:text")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("x", 0)
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("y", y.rangeBand() / 2)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("dx", -6)
|
|
|
|
.attr("dy", ".35em")
|
|
|
|
.attr("text-anchor", "end")
|
2010-10-24 07:32:16 +04:00
|
|
|
.text(function(d, i) { return String.fromCharCode(65 + i); });
|
2010-09-28 22:34:52 +04:00
|
|
|
|
|
|
|
var rules = vis.selectAll("g.rule")
|
2010-10-22 23:55:50 +04:00
|
|
|
.data(x.ticks(10))
|
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
|
|
|
.enter().append("svg:g")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("class", "rule")
|
2010-10-23 00:36:48 +04:00
|
|
|
.attr("transform", function(d) { return "translate(" + x(d) + ",0)"; });
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
rules.append("svg:line")
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("y1", h)
|
|
|
|
.attr("y2", h + 6)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("stroke", "black");
|
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
rules.append("svg:line")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("y1", 0)
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("y2", h)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("stroke", "white")
|
|
|
|
.attr("stroke-opacity", .3);
|
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
rules.append("svg:text")
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("y", h + 9)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("dy", ".71em")
|
|
|
|
.attr("text-anchor", "middle")
|
2010-10-23 00:36:48 +04:00
|
|
|
.text(x.tickFormat(10));
|
2010-09-28 22:34:52 +04:00
|
|
|
|
2010-10-02 22:00:31 +04:00
|
|
|
vis.append("svg:line")
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("y1", 0)
|
2010-10-21 02:22:22 +04:00
|
|
|
.attr("y2", h)
|
2010-09-28 22:34:52 +04:00
|
|
|
.attr("stroke", "black");
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|