Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming bar-graph with d3.js

I am trying to create a bar graph with a time scale where its possible to zoom into any time period. I was able to create the zooming functionality for my x-axis(time scale) however my data (in this case rects) doesn't zoom along with my axis.

Here is a simplified version of my graph: http://jsfiddle.net/gorkem/Mf457/5/embedded/result/ As you can see I can zoom in to my x-axis but the bars do not zoom along.

and here is the jfiddle link: http://jsfiddle.net/gorkem/Mf457/6/

var y = d3.scale.linear().domain([0, max_val]).range([graph_height, 0]);
var x = d3.time.scale().domain([minDate, maxDate]).range([0, graph_width]);

var chart = d3.select(location).append("svg")
    .attr("class", "chart")
    .attr("width", graph_width+20)
    .attr("height", graph_height+20)
    .call(d3.behavior.zoom().x(x).scaleExtent([1, 8]).on("zoom", zoom));

var lines = chart.selectAll("line");

var lines_y = lines
    .data(x.ticks(5))
    .enter().append("line")
    .attr("x1", x)
    .attr("x2", x)
    .attr("y1", function (d) {return graph_height - 20 - d;})
    .attr("y2", graph_height)
    .style("stroke", "#ccc");

var lines_x = lines
        .data(y.ticks(10))
        .enter().append("line")
        .attr("x1", 0)
        .attr("x2", graph_width)
        .attr("y1",  y)
        .attr("y2",  y)
        .style("stroke", "#ccc");

xAxis = d3.svg.axis().scale(x);
yAxis = d3.svg.axis().scale(y).orient("left");

chart.append("svg:g")
    .attr("class", "xaxis")
    .attr("transform","translate(0,300)")
    .call(xAxis);
chart.append("svg:g")
    .attr("class", "yaxis")
    .attr("transform", "translate(25,0)")
    .call(yAxis);


var rect = chart.selectAll("rect")
    .data(data)
    .enter().append("rect")
    .attr("x", function (d,i) {return x(new Date(d["date"]))+20; })
    .attr("y", function (d,i) { return graph_height - (d["num"] *v_scale);})
    .attr("width", x(new Date(data[1]["date"])))
    .attr("height", function (d,i) {return d["num"]*v_scale;});

rect.call(d3.behavior.zoom().x(x).scaleExtent([1, 8]).on("zoom", zoom));

function zoom() {
    chart.select(".xaxis").call(xAxis);
    chart.select(".yaxis").call(yAxis);
}

}

I feel like I should be adding more functionality to my zoom function for the function to be effective on bars(rects). I would really appreciate any help.

like image 940
s_curry_s Avatar asked Jun 20 '12 18:06

s_curry_s


1 Answers

Using 'selectAll', I've applied the scaling and translation to each bar respectively, restricting both scale and translation to the x-axis only.

function zoom() {
    chart.select(".xaxis").call(xAxis);
    chart.select(".yaxis").call(yAxis);
    chart.selectAll(".chart rect").attr("transform", "translate(" + d3.event.translate[0] + ",0)scale(" + d3.event.scale + ", 1)");
}

This works equally well with a single path element, which is what I was trying to figure out when I stumbled upon your question.

like image 176
nverba Avatar answered Oct 21 '22 11:10

nverba