D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tmcw
Full window
Github gist
Song Length Histogram
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .bar rect { fill: magenta; shape-rendering: crispEdges; } .bar text { fill: #fff; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } </style> <body> <script src="https://d3js.org/d3.v2.min.js?2.10.0"></script> <script> d3.json('all_small.json', function(values) { // A formatter for counts. var formatTick = function(x) { var minutes = Math.round((x) / 60); return minutes + ' minutes'; }; var formatCount = d3.format(",.0f"); var margin = {top: 10, right: 30, bottom: 30, left: 30}, width = 640 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; var x = d3.scale.linear() .domain([0, d3.max(values)]) .range([0, width]); // Generate a histogram using twenty uniformly-spaced bins. var data = d3.layout.histogram() .bins(x.ticks(20)) (values); var y = d3.scale.linear() .domain([0, d3.max(data, function(d) { return d.y; })]) .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .tickFormat(formatTick) .orient("bottom"); 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 + ")"); var bar = svg.selectAll(".bar") .data(data) .enter().append("g") .attr("class", "bar") .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; }); bar.append("rect") .attr("x", 1) .attr("width", x(data[0].dx) - 1) .attr("height", function(d) { return height - y(d.y); }); bar.append("text") .attr("dy", ".75em") .attr("y", 6) .attr("x", x(data[0].dx) / 2) .attr("text-anchor", "middle") .text(function(d) { return formatCount(d.y); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); }); </script>
https://d3js.org/d3.v2.min.js?2.10.0