d3.event.transform不能用了

解決方案

const zoom = d3.zoom().scaleExtent(this.scaleRange).on('zoom', this.zoomFn);


      //初始化縮放方法
      zoomFn(svg, a, b, c) {
      // 低版本方法
        // const zoom = d3.event.transform;
        // return this.container.attr('transform', svg.transform);
        
// 新版本方法
return this.container.attr('transform', svg.transform);
      },

最近參考一個別人使用的d3js代碼,畫樹形圖的,自己重新下載的d3, 使用的最新版本7.8.2。

問題

但是代碼拉取下拉之後沒法運行:App.vue:179 Uncaught TypeError: Cannot read properties of undefined (reading 'transform'),

查資料

查了下資料,d3.event在v7版本已經沒有了。
然後開始自己的搜索之路........然而,貌似,好像有人也遇到這個問題,但是呢,我的問題依舊在。然後開始自己探索...

Debug


既然這裏的參數已經帶了transform, 就嘗試一下此transform是否爲彼transform。實踐出真知,還真可以,而且縮放功能也正常使用,完美解決!

運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章