如何解決d3.event在v7版本無效影響zoom拖拽縮放問題
近期由于代碼內(nèi)關(guān)于d3版本的更新,由原來的v3 v4更新值v7.0.0
導(dǎo)致原有關(guān)于d3的波點圖內(nèi)zoom方法的拖拽縮放、tooltip提示框問題開始報錯,即d3.event數(shù)據(jù)廢棄了
1.設(shè)置d3圖形的縮放配置
在d3.event.transform.x中,即提示
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'x')
此時可在zoom方法內(nèi),使用匿名函數(shù)獲取當前參數(shù)current
參數(shù)內(nèi)即含有所需的x,y,k值
let zoom = d3.zoom().scaleExtent([0, 8]).on('zoom', function (current){
zoomed(current.transform)
})
....
let svg = d3
...
.call(zoom)
function zoomed(transform) {
svg.style('transition', 'none')
svg.attr('transform', `translate(${transform.x},${transform.y}) scale(${transform.k})`
)}
2.d3圖形的鼠標tooltip配置事件
let node = svg
...
.on('mouseover', function (current) {
clearTimeout(that.timer)
that.timer = setTimeout(() => {
that.drawTooltip(current.target.__data__, current)
}, 100)
})
.on('mouseout', function (current) {
clearTimeout(that.timer)
that.toolTip.style('visibility', 'hidden')
})
此時關(guān)于圖例內(nèi)的數(shù)據(jù)
需要通過current.target.__data__來獲取
- 而layerX則直接在匿名函數(shù)內(nèi)的current參數(shù)獲取
- 而非d3.event獲取
drawTooltip(data, event) {
this.toolTip
.style('visibility', 'visible')
.style('left', `${event.layerX}px`)
.style('top', `${event.layerY}px`)
.text(`名稱:${data.title}`)
.append('p')
.text(`類別:${data.type}`)
.append('p')
.text(`數(shù)量:${data.value}`)
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用watch同時監(jiān)聽多個值的實現(xiàn)方法示例
這篇文章主要為大家介紹了Vue中使用watch同時監(jiān)聽多個值的實現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05

