- D3.js 4.x Data Visualization(Third Edition)
- ?ndrew Rininsland Swizec Teller
- 238字
- 2021-07-02 23:20:26
Manipulating content
We can do far more with D3 than just playing around with selections and changing the properties of the elements. We can change the properties of elements on the page.
With D3, we can change the contents of an element, add new elements, or remove elements we don't want.
Let's add a new column to the table from our preceding example:
const newCol = d3.selectAll('tr').append('td')
We selected all the table rows (which returned a new selection object) and then appended a new cell to each using the .append() selection. All D3 actions return the current selection, so we can chain actions or assign the new selection to a variable newCol for later use.
We have an empty invisible column on our hands. Let's add some text to spruce things up:
newCol.text('a')
At least now that it's full of instances of a, we can say a column is present. However, that's kind of pointless, so let's follow the pattern set by other columns:
newCol.text((d, i) => ['Seven', 'u', 'j', 'm'][i])
The trick of dynamically defining the content via a function helps us pick the right string from a list of values depending on the column we're in, which we identify by the index i. Figured out the pattern, yet?
Similarly, we can remove elements using .remove(). For instance, to get rid of the last row in the table, you'd write something as follows:
d3.selectAll('tbody tr:last-child').remove()
- x86匯編語言:從實模式到保護模式(第2版)
- Podman實戰
- Redis Essentials
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Hands-On Automation Testing with Java for Beginners
- Visual Basic程序設計上機實驗教程
- 3ds Max印象 電視欄目包裝動畫與特效制作
- Kotlin極簡教程
- Windows Phone 8 Game Development
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- Android移動應用開發項目教程
- C語言程序設計實踐
- Java面向對象程序設計教程
- 虛擬現實:引領未來的人機交互革命
- Python編程基礎與數據分析