Zoom & Pan (only X)
by armensg
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
CSS
body {
font: 12px Arial;
background-color: #000000;
}
path {
stroke: yellow;
stroke-width: 1;
fill: none;
}
.axis path, .axis line {
fill: none;
stroke: #ccc;
stroke-width: 2;
shape-rendering: crispEdges;
}
text {
fill: #ccc;
}
rect {
fill: #000000;
}
JavaScript
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var data = [
{date:"1400144226",close:"58.13"},
{date:"1400144227",close:"53.98"},
{date:"1400144228",close:"67.00"},
{date:"1400144229",close:"89.70"},
{date:"1400144230",close:"99.00"},
{date:"1400144231",close:"58.13"},
{date:"1400144232",close:"53.98"},
{date:"1400144233",close:"17.00"},
{date:"1400144234",close:"89.70"},
{date:"1400144235",close:"99.00"},
{date:"1400144236",close:"53.98"},
{date:"1400144237",close:"67.00"},
{date:"1400144238",close:"84.70"},
{date:"1400144239",close:"99.00"}
];
//d3.tsv("data/data.tsv", function(error, data) {
data.forEach(function(d) {
d.date = d.date * 1000;
d.close = +d.close;
});
// Scale the range of the data
var x = d3.time.scale()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([0, width]);
var y = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.close; })])
.range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.ticks(d3.time.seconds, 1)
.tickFormat(d3.time.format('%X'))
.tickSize(1)
.tickPadding(8);
var xAxisTop = d3.svg.axis().scale(x)
.orient("bottom").tickFormat("").tickSize(0);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
var yAxisRight = d3.svg.axis().scale(y)
.orient("right").tickFormat("").tickSize(0);
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
var valueLinePan = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.scaleExtent([1, 4])
.on("zoom", zoomed);
var svg = d3.select("body")
.append("svg")
.attr("width", width +...