d3.js - D3js Zooming chart with polylinear axis -
i trying create zoomable chart has polylinear x-axis. works expected if don't have polylinear axis. however, when using polylinear axis panning (and zooming) distorted. if drag chart pan see points , x-axis distortions. example uses default d3js zoom behaviour. suggestions smooth panning/zooming appreciated.
i using transforms position elements because had issues when specifying cx , cy values (especially when adding new data panned or zoomed chart).
here zoom function:
var onzoom = function () { if (d3.event.scale == 1) { //panning } else { //zooming // don't scale points , lines graph.selectall(".point") .attr("r", 10 / d3.event.scale) .attr("stroke-width", 1 / d3.event.scale); } graph.selectall(".point") .attr("transform", function (d) { return "translate(" + xmap(d) + "," + ymap(d) + ")scale(" + d3.event.scale + ")"; }); graph.select(".xaxis.axis").call(xaxis); graph.select(".yaxis.axis").call(yaxis); };
see example: https://jsfiddle.net/deanb/spjn8zl7/
i found out how pan/zoom polylinear axis without distortion.
i figured out while reading accepted answer question: semantic zooming of force directed graph in d3. "the d3 zoom behaviour object modifies scales changing domain. similar effect changing scale range, since important part changing relationship between domain , range.".
i therefore modified zoom behaviour not update x domain , update range in zoom behaviour:
// update range in zoom event - leave domain x.range(d3.range(xticks.length) .map(function (d) { return (d * xrangedelta) * d3.event.scale + d3.event.translate[0]; })) graph.selectall(".point") .attr("transform", function (d) { return "translate(" + xmap(d) + "," + ymap(d) + ")scale(" + d3.event.scale + ")"; }); graph.select(".xaxis.axis").call(xaxis); graph.select(".yaxis.axis").call(yaxis);
here updated fiddle: https://jsfiddle.net/deanb/hsfsx6b9/1/.
Comments
Post a Comment