Skip to content

Instantly share code, notes, and snippets.

@refactornator
Last active August 29, 2015 13:57
Show Gist options
  • Save refactornator/9418018 to your computer and use it in GitHub Desktop.
Save refactornator/9418018 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
html {
-webkit-user-select: none; /* Chrome all / Safari all */
-moz-user-select: none; /* Firefox all */
-ms-user-select: none; /* IE 10+ */
/* No support for these yet, use at own risk */
-o-user-select: none;
user-select: none;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
barWidth = 36,
n = 200,
scrollWidth = barWidth * n,
drag = d3.behavior.drag()
.origin(function() {
var groupPos = this.parentNode.getBBox();
return {x: groupPos.x, y: 0};
})
.on("drag", dragmove),
data = d3.range(n).map(function(d, i) { return { id: i, size: Math.random() * 90 + 10}; });
console.log(data);
var x = d3.scale.ordinal()
.rangeBands([0, scrollWidth], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(11);
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 + ")")
svg.append("defs")
.append("clipPath")
.attr("id", "clip-path")
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width + margin.right)
.attr("height", height);
var barsGroup = svg.append("g")
.attr("class", "bar-area")
.attr("clip-path", "url(#clip-path)")
.append("g")
.attr("class", "bars-group")
.call(drag);
barsGroup.append("rect")
.attr("class", "bars-background")
.attr("x", 0)
.attr("y", 0)
.attr("width", scrollWidth)
.attr("height", height)
.style("fill", "white");
x.domain(data.map(function(d) { return d.id; }));
y.domain([0, d3.max(data, function(d) { return d.size; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
barsGroup.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.id); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.size); })
.attr("height", function(d) { return height - y(d.size); });
function dragmove() {
console.log('dragging');
//return if no scrolling is required.
if(scrollWidth <= width - margin.left - margin.right) {
return;
}
var x = d3.event.x
if(x > 0) {
x = 0;
} else if(x < -scrollWidth + width - margin.right) {
x = -scrollWidth + (width - margin.right);
}
d3.select(this).attr("transform", "translate(" + x + ")");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment