サンプル
* 軸あり、アニメーション、なんちゃってツールチップ<!DOCTYPE html> <meta charset="utf-8"> <style> .bar:hover { fill: steelblue; } .axis { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .tooltip { border: 1px solid #eee; background-color: #fff; padding: 8px 8px 0 8px; border-radius: 3px; } </style> <body> <script src="">https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"> <script> var dataset = [ {graphId: "001", yVal: 15, graphColor: "aliceblue"}, {graphId: "002", yVal: 3, graphColor: "lavender"}, {graphId: "003", yVal: 22, graphColor: "lightsteelblue"}, {graphId: "004", yVal: 9, graphColor: "lightslategray"}, {graphId: "005", yVal: 19, graphColor: "slategray"}, {graphId: "006", yVal: 15, graphColor: "steelblue"}, {graphId: "007", yVal: 3, graphColor: "royalblue"}, {graphId: "008", yVal: 22, graphColor: "midnightblue"}, {graphId: "009", yVal: 9, graphColor: "navy"}, {graphId: "010", yVal: 19, graphColor: "darkblue"}, ]; 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.ordinal() .rangeRoundBands([0, width], .1); 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") .ticks(10, ""); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); x.domain(dataset.map(function(d) { return d.graphId; })); y.domain([0, d3.max(dataset, function(d) { return d.yVal; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Y value"); var barChart = svg.selectAll(".bar") .data(dataset) .enter() .append("rect") .attr("class", "bar") .attr("fill", function(d) { return d.graphColor; }) .attr("x", function(d) { return x(d.graphId); }) .attr("width", x.rangeBand()) .attr("height", 0) .attr("y", function(d) { return height; }); // アニメーション barChart.transition() .delay(function (d, i) { return i * 100; }) .duration(1000) .ease("bounce") // bounceの動きを指定する .attr("y", function(d) { return y(d.yVal); }) .attr("height", function(d) { return height - y(d.yVal); }); // ツールチップ var tooltip = d3.select("body") .append("div") .attr("class", "tooltip") .style("position", "absolute") .style("z-index", "10") .style("visibility", "hidden"); barChart .on("mouseover", function(){ return tooltip.style("visibility", "visible"); }) .on("mousemove", function(d){ return tooltip .style("top", (d3.event.pageY - 10) + "px") .style("left",(d3.event.pageX + 10) + "px") .html("<span>" + d.graphId + " : " + d.yVal + "</span>"); }) .on("mouseout", function(){ return tooltip.style("visibility", "hidden"); }); </script>
参考文献
http://bl.ocks.org/mbostock/3885304* Tooltip
http://jsdo.it/benchan/psWz
* アニメーション
http://alexcoco.com/lets-make-a-bar-chart/
http://akiniwa.hatenablog.jp/entry/2014/02/23/121344