Original link:
Http://d3.decembercafe.org/pages/lessons/3.html
Parse the data and datum principle:
- Datum (): Binds a data to a selection set
- Data (): Binds an array to the selection set, and the values of the array are bound to each element of the selection set, respectively
<! DOCTYPE html>
<meta charset= "UTF-8" >
<meta name= "viewport" content= "Width=device-width, initial-scale=1.0" >
<meta http-equiv= "x-ua-compatible" content= "Ie=edge" >
<title> Data Binding </title>
<script src= "D3.js" ></script>
<body>
<p>apple</p>
<p>organge</p>
<p>banaer</p>
<span></span>
<span></span>
<span></span>
<script>
Datum ()
var str = ' CCZ ';
var BODY = d3.select (' body ');
var p = body.selectall (' P ');
P.datum (str);
Console.log (P);
P.text (function (d,i) {
Return ' +i+ ' binding element is ' +d;
})
Data ()
var obj = [' like1 ', ' like2 '];
var span = Body.selectall (' span ');
Span.data (obj);
Console.log (span);
P.text (function (d,i) {
return D;
})
</script>
</body>
When printing, it is found that the attributes of each P element will be one more __data__, and the corresponding data will be bound on this property.
Datum () binds the value to the __data__ attribute, and the values of all elements are the same;
Data () assigns the array object to the element, and if the value is not sufficient, the text of the element is empty.
D3.JS--02 (Data and Datum principles)