2016-08-22 102 views
2

D3版本4.2.2如何重置/清除d3刷子?

我有一個圖表,我希望用戶能夠跟上刷選擇放大英寸我想在結束事件中清除或重置筆刷,以便用戶看不到舊筆刷。

當前打字稿代碼:

let brushGroup = this.svg.append('g') 
     .attr('class', 'brush'); 

    let b = brushX() 
     .on('start',() => { 
      console.log('start brush'); 
      brushGroup.style('opacity', 100); 
     }) 
     .on('end',() => { 
      console.log('end brush', event.selection); 
      // remove and display none destroy the brush, so use opacity 
      brushGroup.style('opacity', 0); 

      this.updateDateRange(this.xScale.invert(event.selection[0]), 
       this.xScale.invert(event.selection[1])); 
     }); 

    brushGroup.call(b); 
    console.log(brushGroup); 
  • display: none防止未來刷啓動事件
  • brushGroup.remove()也破壞了刷
  • 不透明的作品,但刷仍然存在(鼠標光標會變成上懸停)

那麼清除或重置畫筆的正確方法是什麼?

  1. 我應該把它移動到0,0嗎?
  2. 我應該摧毀舊的刷子,並創建一個新的每一次?
+0

我決定只刪除並重新創建結束事件中的brushGroup(克元件)各一次。這很好,但如果有更好的方法,我仍然很好奇。 – Arlo

回答

3

selection.call(brush.move,null);

https://github.com/d3/d3-brush/issues/10

+1

爲了確保人們知道'selection'和'brush'是什麼意思,'selection'是刷子容器(例如'svg.append(「g」)。<...>''),並且刷子是'd3.brush( )。 <...>'鏈。 – ZitRo