<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.bar rect {
fill: steelblue;
shape-rendering: crispEdges;
}
.bar text {
fill: #fff;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
</body>
</html>
var values = [1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 2, 3, 4, 5, 3, 3, 3, 3, 3, 3, 0, 3, 3, 6, 3, 3, 3, 6, 3, 4, 4, 5, 1, 3, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 2, 3, 3, 3, 4, 5, 1, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, 6, 1, 2, 2, 3, 3, 4, 5, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 6, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 6, 3, 3, 3, 3, 3, 3, 0, 1, 2, 3, 4, 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 6, 3, 2, 3, 3, 2, 3, 3, 2, 2, 3, 3, 3, 3, 1, 2, 3, 4, 5, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 2, 3, 2, 3, 5, 5, 5, 5, 5, 4, 4, 2, 3, 3, 3, 3, 2, 3, 3, 2, 5, 0, 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 4, 3, 2, 3, 4, 5, 3, 3, 2, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 2, 3, 4, 5, 3, 2, 3, 4, 3, 3, 3, 2, 3, 3, 3, 4, 3, 3, 3, 2, 3, 2, 2, 4, 2, 3, 3, 3, 2, 3, 3, 3, 6, 2, 2, 3, 4, 6, 3, 3, 3, 3, 3, 2, 3];
var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'];
// A formatter for counts.
var formatCount = d3.format(",.0f");
var margin = {top: 10, right: 30, bottom: 30, left: 30},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Ordinal scale does not seem to cooperate
var x = d3.scale.ordinal()
.domain(values.map(function(item) { return weekdays[item]; }))
.rangeBands([0, width]);
// var x = d3.scale.linear()
// .domain([0, 6])
// .range([0, width]);
var data = d3.layout.histogram()(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)
.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) {
var xx = x(d.x);
var yy = y(d.y);
return "translate(" + xx + "," + yy + ")";
});
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);
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |