score:0
I was looking for the answer to this, but it seems D3 has already evolved a couple of versions.
Although Majkl and cjds's answers helped me solve my problem, I thought it would help to leave more up to date information, since it was hard finding v5.4 examples out there, until I found Observable at least.
// Applies event transformation to the Group element's attribute
const zoom_action = () => g.attr("transform", d3.event.transform)
// Create the zoom handler
const zoom = d3
.zoom()
.on("zoom", zoom_action)
// Get SVG element and apply zoom behaviour
var svg = d3
.select("svg")
.call(zoom)
// Create Group that will be zoomed
var g = svg.append("g")
// Create circle
g.append("circle")
.attr("cx",0)
.attr("cy",0)
.attr("r", 5)
// Set initial scale and translation
zoom.scaleBy(svg, 5)
zoom.translateBy(svg, 50, 50)
<script src="https://d3js.org/d3.v5.js"></script>
<svg height="100px" width="100px"></svg>
score:1
That is a problem with the zoom function itself. I would suggest zooming the children as opposed to the parent if that would work
var zoom = d3.behavior.zoom().on("zoom",function(){
var t = d3.event.translate;
var s = d3.event.scale;
svg.selectAll("rect").attr("transform","translate("+t[0]+","+t[1]+") scale("+s+")")
}).scaleExtent([1,10]);
EDIT
The problem with the above code is that d3.js does not register the transformation or initial state of the SVG. This problem runs deeper. As d3
does not keep track of the SVG transformations and just executes them. It only keeps track of transformations you've run on the library in a variable called __chart__
.
So when the zoom function is run it just interpolates the variables and gives the output. As no functions have been run on this yet the __chart__
variable has not been set and causing the jerky start from (x=0, y=0, k=1).
Solution:
Run this code before the zoom transformation to set the initial chart manually
svg.transition().each(function(){ this.__chart__={x:25,y:25,k:0.25}; //or you can pick those values using attr });
Zoom the svg programmatically to 25,25,0.25 first before any other function. (this is why your workaround works as the
__chart__
variable gets set)
score:1
To set the initial value of the zoom, try something like this:
// Init zoom
var zoom = d3.behavior.zoom().on("zoom", function () {
svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
});
// Get SVG element
var svg = d3.select("svg")
.call(zoom)
.append("g");
// Create circle
svg.append("circle")
.attr("cx",0)
.attr("cy",0)
.attr("r", 5);
// Create init value
var scale = 5;
var translate = [50, 50];
// Set init value
zoom.scale(scale);
zoom.translate(translate);
// Call zoom event
svg.call(zoom.event);
// or svg.transition().call(zoom.event);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg height="100px" width="100px"></svg>
Source: stackoverflow.com
Related Query
- When a d3.behavior.zoom event is triggered programatically, how do you set inital values for translate and scale?
- why isn't the checkbox change event triggered when i do it programatically in d3js
- How do you change the interpolation for a zoom behavior in d3.js
- With NVD3.js (nv.models.lineWithFocusChart), how do you set specific ticks on X-axis, when x values are dates?
- How to remove zoom capability after you set it in D3?
- How to remove event listener of an element when I remove the corresponding element in the midst of the event being triggered in D3?
- How do you query your dataset when you zoom into and out of a d3 map?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- How to show full text when zoom in & truncate it when zoom out
- how do you set a fixed link distance with d3 v4
- How do you make an SVG element mouse event bubble up through another element?
- D3js: How do I clear the zoom scale set by the d3.zoom event?
- How do you set periodic timers in d3.js?
- How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library
- D3: How do I set "click" event and "dbclick" event at the same time?
- How to simulate mouse move in D3 so when you drag nodes, other nodes move automatically?
- d3 zoom behavior when window is resized
- How to show and hides nodes when you move the mouse over a node in D3 Javascript
- How to use D3 zoom behavior with ViewBox instead of transform
- D3.js - how to add zoom button with the default wheelmouse zoom behavior
- How to set color of D3 Zoom Packed Circle children
- How do you retrieve the key values of a nested data set in D3
- Zoom event overrides Drag behavior in d3js
- How to access original event object when d3 event binding
- How do you get selected datums in brush "brush" event handler?
- How to set the zoom factor in D3.js v.4.x?
- Dispatch event when you click in legend of pieChart nvd3
- D3 zoom behavior prevent click event in IE9
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- Set inital zoom using brush in D3
More Query from same tag
- How to draw D3 Map with United States, Canada, Mexico and Puerto Rico
- Tool tip on a d3.js (v3) line chart
- D3.js - How to use different styles for tick labels in one plot?
- Drag bubble in d3js bubblechart
- d3 changing x Axis Time ticks only full years and full months
- Heatmap dc.js - provide the color range manually
- D3: Not able to click on SVG element
- sum of columns in csv using d3
- D3js chart with 2 lines - display only lines
- Reset drag position after repositioning map in D3.js
- What is the use of the plus sign here?
- hide y axis label on mouse hover in nvd3 bubble chart
- D3 forceCollide not working properly after updating the underlying data
- D3 rendering map not completely
- Creating tooltips for SVG objects after they were translated and scaled
- Custom Text filter for DC.js dataTable
- How to recursively toggle nodes in a d3.js tree diagram
- D3 - Appending Rectangle Stops Tooltip From Working
- limit a voronoi diagram within country border in d3.js
- save-svg-as-png not loading the svg css, while downloading the svg as png
- d3.js Maximum call stack size exceeded error
- How to implement variable tension functionality, D3 hierarchical edge bundling?
- How to call controller action from d3.js or any javascript in rails
- How to convert D3 time scale from V3 to V4?
- Why is my HeatMap not showing up correctly?
- how to generate a list of successive colors in javascript
- Angular2 component not called when I do d3JS append
- How can I use D3 tree data in the VEGA Api?
- D3 - Select SVG center path when drilled into and display zoom out background image
- How to create a javascript function using Scala.js?