javascript - D3: Panning not working in bar chart -
i've tried implement panning bar chart. have written zoom function, bar charts not displayed @ all. want horizontal scrolling , want dragging bound, such when drag elements values go until last minimum value or maximum value defined on x axis.
this have tried far:
var data = [{ x: 2, y: 4 }, { x: 5, y: 8 }, { x: 8, y: 10 }, { x: 2, y: 4 }, { x: 5, y: 8 }, { x: 8, y: 19 }, { x: 2, y: 4 }, { x: 5, y: 8 }, { x: 8, y: 10 }, { x: 2, y: 4 }, { x: 5, y: 8 }, { x: 8, y: 10 }, { x: 2, y: 4 }, { x: 5, y: 8 }, { x: 8, y: 10 }, { x: 2, y: 4 }, { x: 3, y: 8 }, { x: 8, y: 10 }, { x: 24, y: 4 }, { x: 15, y: 8 }, { x: 18, y: 10 }]; var margin = { top: 20, right: 30, bottom: 30, left: 40 }, w = 1000 - margin.left - margin.right, h = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .domain([0, d3.max(data, function(d) { return d.x; })]) .range([0, w], 0.6); var y = d3.scale.linear() .domain([0, d3.max(data, function(d) { return d.y;})]) .range([h, 0]); var xaxis = d3.svg.axis() .scale(x) .orient("bottom"); var yaxis = d3.svg.axis() .scale(y) .orient("left"); var zoom = d3.behavior.zoom() .scaleextent([1, 1]) .x(x) .on("zoom", zoomed); var chart = d3.select("#testchart").append("svg") .attr("width", w + margin.left + margin.right) .attr("height", h + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")") .call(zoom); var rect = chart.append("rect") .attr("width", w) .attr("height", h) .style("fill", "none") .style("pointer-events", "all"); chart.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + h + ")") .call(xaxis) chart.append("g") .attr("class", "y axis") .call(yaxis); var bars = chart.append("g") .attr("class", "chartobjects"); bars.selectall(".rect") .data(data) .enter().append("rect") .attr("class", "rectbar") .on("click", hello) .attr('x', function(d) { return x(d.x); }) .attr("y", function(d) { return y(d.y); }) .attr("height", function(d) { return h - y(d.y); }) .attr("width", x.rangeband()) .attr("fill", function(d) { return d.y > 6 ? "blue" : "red" }); function hello() { alert("hello world!!") } function zoomed() { console.log("entered zoom function!!!"); var tx = math.max(0, d3.event.translate[0]), ty = math.min(0, d3.event.translate[1]); zoom.translate([tx, ty]); bars.attr("transform", "translate(" + [tx,ty] + ")scale(" + d3.event.scale + ")"); chart.select(".x.axis") .call(xaxis); }
Comments
Post a Comment