Skip to content

Instantly share code, notes, and snippets.

@jpbyrne
Last active March 23, 2017 14:29
Show Gist options
  • Save jpbyrne/511440756d9894cbb8ff844045ea63c9 to your computer and use it in GitHub Desktop.
Save jpbyrne/511440756d9894cbb8ff844045ea63c9 to your computer and use it in GitHub Desktop.
bar block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<style>
svg {
font: 10px sans-serif;
}
path.line {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
path.area {
fill: #e7e7e7;
}
.axis {
shape-rendering: crispEdges;
}
.x.axis line {
stroke: #fff;
}
.x.axis .minor {
stroke-opacity: .5;
}
.x.axis path {
display: none;
}
.y.axis line,
.y.axis path {
fill: none;
stroke: #000;
}
</style>
<script>
var margin = {
top: 20,
right: 50,
bottom: 30,
left: 50
};
var width = 960 - margin.right - margin.left;
var height = 500 - margin.top - margin.bottom;
var data = [
{
label: 'Balon Greyjoy',
value: 1
},
{
label: ' Stannis Baratheon',
value: 2
},
{
label: 'Robb Stark',
value: 3
},
{
label: ' Renly Baratheon',
value: 4
},
{
label: ' Joffrey Baratheon',
value: 5
}
];
var xScale = d3.scale.ordinal()
.domain(data.map(function (d) { return d.value }))
.rangeRoundBands([0, width], 0.2, 0.1);
var yScale = d3.scale.linear()
.domain([0, d3.max(data.map(function (d) { return d.value }))])
.range([height, 0]);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom);
var g = svg.append('g')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var bars = g.selectAll('rect').data(data)
.enter().append('rect')
.style('fill', '#66B5AF')
.attr('x', function (d, i) { return xScale(d.value); })
.attr('y', function (d, i) { return yScale(d.value); })
.attr('width', xScale.rangeBand())
.attr('height', function (d) { return height - yScale(d.value); });
var xAxis = d3.svg.axis()
// .tickValues(data.map(function (d) { return d.label }))
.scale(xScale)
.orient('bottom');
var yAxis = d3.svg.axis()
.scale(yScale)
.orient('left');
g.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, ' + height + ')')
.call(xAxis);
g.append('g')
.attr('class', 'y axis')
.call(yAxis);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment