- D3.js 4.x Data Visualization(Third Edition)
- ?ndrew Rininsland Swizec Teller
- 264字
- 2021-07-02 23:20:26
What exactly did we do here?
The key is in the three for...each statements that we used. One loops through the array of table header strings, and appends a table cell (td) element with each value into the thead row. There are then two nested .forEach() statements that do the same for each row in the body. We technically only have one row in the body right now, so probably didn't need that messy double for...each, but now all we have to do to add another row to the table is simply append another data array to the rows variable. We'll talk more about Array.prototype.forEach and other array functions in the next chapter.
This might seem like a lot of work for such a simple table, but the advantages of doing it this way are huge. Instead of wasting a bunch of time typing out a totally static table that you'll never use again, you've effectively created a basic JavaScript library that will produce a basic table for you whenever you need it. You could even extend your tableFactory function to do different things than it does now, without ever altering the code you just wrote.
Those nested for...each loops are really ugly though. Thankfully, d3-selection provides a mechanism for adding, updating, and removing data points, which we'll get into really quickly. Instead of tracking where everything is in your project, we just need to tell D3 to manage our data and it will update any attached elements. The key part of this is selections and building functions that intelligently manipulate them.
- PWA入門與實踐
- Redis Applied Design Patterns
- R語言經典實例(原書第2版)
- 編程的修煉
- INSTANT Sencha Touch
- 64位匯編語言的編程藝術
- 手把手教你學C語言
- NetBeans IDE 8 Cookbook
- 圖數據庫實戰
- Visual Studio Code 權威指南
- 進入IT企業必讀的324個Java面試題
- Web編程基礎:HTML5、CSS3、JavaScript(第2版)
- Mastering JavaScript Promises
- Unreal Engine Game Development Cookbook
- Mastering Machine Learning with scikit-learn