fundamental of D3: Bind Data and element Selection

本文介绍如何使用 D3.js 进行数据绑定及页面元素生成,详细讲解了加载数据到浏览器内存、数据绑定到元素、转换元素及响应用户输入等过程。并通过示例演示了如何生成页面元素及实现数据绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. D3 facilitates generation and manipulation of web documents with data. It does this by


1) Loading Data in to the browser's memory


2) Binding data to elements within the document, creating new elements as needed. 


3) Transforming those elements by interpreting each element's bound datum and setting its visual properties accordingly (MAPPING)


4) Transitioning elements between states in response to user input.



Chapter 5: Data


1.  d3, data is text-based data

2.  Data must be attached to elements within the page. So first, we have to generate page elements


Generating page elements

 d3.select("body")

.append("p")

.text("Data science is very funny");


this will generate a new element named p; you can dynamically generate tens or thousands of elements, each one corresponding to a piece of your dataset.


Chain methods


 d3                    // Reference the D3 object, so We can access its methods.

.select("body")   // return a reference to the first element in the DOM that matches (selectAll )

.append("p")      // create whatever new element you specify and append it to the end( but just inside) of whatever selection it's acting on. Finally

it will return a reference to the new element it just created  

.text("Data science is very funny") // it will takes a string and inserts it between the opening and closing tags of the current selection

;  // this indicates the end of this line of code.


Hand -off 

not all D3 methods will return a reference to a element 


Binding Data


  Data in, visual properties out!


method: selection.data()


Load Data

d3.csv("food.csv",function(data) {

// do something here 

});

d3.csv() has 2 arguments, a string representing the path of the csv file to load in

function is called callback function. the callback function is called only after the csv file has been loaded into                         memory.

caution: 1.  d3.csv() or d3.json() is an asynchronous  method, meaning that the rest of your code will executed even while Javascript is simultaneously waiting for the file to finish loading into the browser.

2. handle error, d3.csv("food.csv",function(error,data){

                                 if (error){

} else{

                                      });

Make your selection


var dataset = [1, 2, 3, 4, 5]


e.g.  d3.select("body")

.selectAll("p")      // p not exist yet, so this return an empty selection

.data(dataset)      // Counts and parses our data values. There are 5 values in the dataset. So everything after this point is executed 5 times.

.enter()                  // Create new, data-bound elements, must use enter() 

                                 first look at the current DOM selection, and then look at the data being handed to it. If there are more values than corresponding DOM elements, then enter() creates a new placeholder element on which you can work your magic.  it then hands off a reference to this new placeholder to the next step in the chain 

.append("p")           // Takes the empty placeholder selection created by enter, and append a p element into the DOM. Then it                                   will hands off a reference to the element it just created to the next step int hte chain

.text("Intersting");  // takes the reference to the newly created p and inserted a text value.



When D3 binds data to an element, that data doesn't exist in the DOM, but it does exist in memory as as a _data_ attribute of that element.



Using Data

d3.select("body").selectAll("p")
.data(dataset)
.enter()
.append("p")
.text(function(d){        // must use function to wrap data

return d;});


when chaining methods together, anytime after you call data(), you can create an anonymous function that accepts d as an input,

the magic data() method ensures that d is set to the corresponding value in your value in your original dataset, 

given the current element at hand. 


 Beyond text

.style("color", function(d) {
if (d > 15) { //Threshold of 15
return "red";
} else {
return "black";
}
});








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值