score:8
Problem:
The d3 zoom behavior does not track or know about what transforms you've applied to an element. It is initialized with scale equal to 1 and translate equal to 0,0. So, when you use
gridELement.attr("transform", d3.event.transform);
in your zoomed function, you are zooming relative to this initial scale and translate.
Rationale
Not every d3 zoom manipulates the transform of the SVG element it is called on: you may want to have zoom interaction everywhere on a plot, but only zoom the plot area and not the margins. Many d3 zooms don't manipulate any SVG transform: they may use semantic zooming, or the zoom may be altering a canvas rather than SVG, etc. As such, a d3-zoom behavior is independent of any SVG transorm attribute on the element(s) it is called on.
Solution
If your zooming by manipulating the transform attribute of an SVG element, let D3 zoom do all of the manipulation - no need to manipulate it manually and have a zoom, this is how the zoom gets out of step.
So,
You can programmatically trigger a zoom event prior to rendering anything. The easiest way to do so is to use:
selection.call(zoom.transform, d3.zoomIdentity.translate(x,y).scale(k));
This line triggers a zoom event. We can use this prior to rendering anything and rely on the zoomed function to set the initial value of the SVG transform attribute. All subsequent zooms will be relative to this and we can proceed as normal:
var svg = d3.select("svg");
var g = svg.append("g");
// define a zoom behavior:
var zoom = d3.zoom()
.on("zoom", zoomed);
// call the zoom:
svg.call(zoom)
// trigger tha initial zoom with an initial transform.
svg.call(zoom.transform,d3.zoomIdentity.scale(20).translate(-100,-100));
// draw the visualization:
var rect = g.append("rect")
.attr("width", 5)
.attr("height", 5)
.attr("x", 100)
.attr("y", 100);
// zoomed function:
// zoom as normal.
function zoomed() {
g.attr("transform",d3.event.transform);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="300"></svg>
The above snippet applies an initial zoom (scale 20, translate -100,-100) and uses this as the starting point for the zoom interaction. The comment should help explain the process.
score:0
// It appears that you're moving #mainGrid and not #svg. So when you call zoom from svgElement it is zooming from it's 0,0 position.
// Or it may because you don't have pound sign in d3.select
var svgElement = d3.select("#svg");
// Try changing svgElement to gridElement
gridELement.call(d3.zoom()
.scaleExtent([1 / 2, 4])
.on("zoom", zoomed));
Source: stackoverflow.com
Related Query
- How can I offset the initial starting position of a d3 zoom?
- How do I specify the initial zoom level in D3?
- How can I set the position of a drop down list with D3.js?
- How can I dynamically change the position and the size of direction arrows on a directed d3.js force layout?
- How can I offset the source and target points of a bezier curve using D3's link generator?
- How can i get the new X and Y position after rotating?
- How can I make a bar chart starting from the 0 point of the y axis and not from the bottom of the svg?
- How to move D3 graph to initial zoom and at 0,0 position - RESET Graph?
- D3.js - how can I position a force chart upwards and to the left of a page?
- how to implement zoom in d3 graph of force directed layout and position the nodes
- How can I bring a circle to the front with d3?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- How can I position rotated x-axis labels on column chart using nvd3?
- How can I get the D3.js axis ticks and positions as an array?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How do I change the legend position in a NVD3 chart?
- how to know the current Zoom level in D3.js
- D3js: How do I clear the zoom scale set by the d3.zoom event?
- How can I change the radius and opacity of a circle in d3?
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- New to nvD3 - how can I make my unix timestamps appear as dates on the x-axis?
- How can I start with all the nodes collapsed in d3js?
- How can I toggle the class of all elements in a selection?
- How can I set min/max of the x-axis in Rickshaw / D3?
- How can I set the each line color or width in SVG path
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- d3.js: how can I get 'full width' ticks on the y-axis
- How do I position subgroups of small multiples together on the page?
- How can I display a placeholder image in my SVG until the real image is loaded?
- D3.js - how to add zoom button with the default wheelmouse zoom behavior
More Query from same tag
- How do I insert an html leafletmap into a blog post (hugo website)?
- horizontal legend is going out of svg
- D3 fill shape when draw with path
- Not able add zoom/pan on scatter plot
- D3.js, Global Chloropleth / Heat Map . Make Legend Horizontal and Add Black Borders Around Countries
- Highcharts chart creation using d3.csv
- Associative array in JS: what am I seeing in the debugger
- D3: How can I close a JSON file?
- Represent the same item twice in a D3 visualisation
- How to draw a path smoothly from start point to end point in D3.js
- How do I make a basic column(vertical) chart in d3js?
- d3 donut charts of varying radius
- Rotate a rectangle at origin
- d3.js - calculating width of a previously drawn element
- angular.js and d3.js - geometric example
- How do I influence link distance in 3d-force-graph?
- Visualisation of sorting algorithms
- D3.js click being blocked by X-XSS-Protection using chrome
- d3.js v5 - d3.json reads file locally on windows but not on linux?
- Prevent panning outside of map bounds in d3v5
- dc.js Composite Graph - Plot New Line for Each Person
- Positioning the edges using dagre-d3
- Using JSON data with identical keys (in D3)
- D3 parcoords axis scale not updating correctly
- what is the default format of loading multi line data in d3?
- How to handle D3 drag events when mousedown and mouseup happen on different elements?
- How to integrate d3.js chart in C# application?
- D3.JS SVG Rectangles don't appear
- D3 forceRadial() with nested data centered around parent
- flowcharts in d3js using dagre-d3 or colajs