d3/examples/bar/bar.html

93 строки
2.0 KiB
HTML
Исходник Обычный вид История

2010-09-28 22:34:52 +04:00
<html>
<head>
<title>Bar Chart</title>
<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,
x = d3.scale.linear().domain([0, 1]).range([0, w]),
y = d3.scale.ordinal().domain(d3.range(data.length)).rangeBands([0, h], .2);
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)
.enter().append("svg:g")
2010-09-28 22:34:52 +04:00
.attr("class", "bar")
.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")
.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")
.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))
.enter().append("svg:g")
2010-09-28 22:34:52 +04:00
.attr("class", "rule")
.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")
.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>