最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3.JS 'zoom' undefined - Stack Overflow

programmeradmin3浏览0评论

I am trying to make simple zoom/pan solution, using this code

  var map = document.getElementById('map'),
      svg = d3.select('#map')
            .call(d3.behavior.zoom().on("zoom", function () {
                svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
            }))
            .append('g');

But at this moment it gives:

Uncaught TypeError: Cannot read property 'zoom' of undefined

In html I have just this:

<svg id="map" class="map" viewBox="0 0 8000 2000"></svg>

P.S. Inspired by this

I am trying to make simple zoom/pan solution, using this code

  var map = document.getElementById('map'),
      svg = d3.select('#map')
            .call(d3.behavior.zoom().on("zoom", function () {
                svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
            }))
            .append('g');

But at this moment it gives:

Uncaught TypeError: Cannot read property 'zoom' of undefined

In html I have just this:

<svg id="map" class="map" viewBox="0 0 8000 2000"></svg>

P.S. Inspired by this

Share Improve this question asked Aug 8, 2016 at 19:45 David ArutiunianDavid Arutiunian 3031 gold badge4 silver badges11 bronze badges 6
  • 7 I started with d3 today, so I won't leave a real answer yet. You using version 4 of the api? Looks like d3.behavior.zoom moved to d3.zoom. – Quotidian Commented Aug 8, 2016 at 19:54
  • d3 v4 doesn't have d3.behavior,zoom. Its just d3.zoom – Umesh Maharshi Commented Aug 8, 2016 at 20:00
  • Also with the v4 you can use directly svg.attr("transform", d3.event.transform) as the event triggered on zooming now has a transform object with a toString method (Doc.) – mgc Commented Aug 8, 2016 at 20:42
  • i am having the exact same issue. did you ever get a solution? – Paul Fitzgerald Commented Nov 1, 2016 at 10:27
  • 1 @PaulFitzgerald the solution, as mentioned above, is to use d3.zoom instead. This works for v4. – MarkyDD Commented Dec 9, 2016 at 14:11
 |  Show 1 more ment

1 Answer 1

Reset to default 12

Try Below code: in v4 of d3.js, d3.behavior.zoom changed to d3.zoom

var svg;

svg= d3.select("svg")
        .call(d3.zoom().on("zoom", function () {
              svg.attr("transform", d3.event.transform)
         }))
        .append("g");
发布评论

评论列表(0)

  1. 暂无评论