The following code appears repeatedly in the previous sections:
[JavaScript]View PlainCopy
- Svg.selectall ("rect")
- . Data (DataSet)
- . Enter ()
- . Append ("rect")
The above code is usually used when the number of rect selected is less than the number of bound data datasets, and this section details what to do when the selected element is inconsistent with the number of data.
This section will involve three functions.
1. Update () when the corresponding element is exactly satisfied (number of bound data = corresponding Element)
There is actually no such a function, just to be described with the subsequent enter and exit to imagine having such a function. But the corresponding elements just meet, direct operation can, followed directly with the text, style and other operations can be.
2. Enter () when the corresponding element is insufficient (bound data quantity > corresponding Element)
When the corresponding element is insufficient, it is common to add elements that are equal to the number of bound data. The following usually operates first with append.
3. Exit () when the corresponding element is too large (the number of bound data < corresponding element)
When the corresponding element is too large, it is usually necessary to delete the element so that it is equal to the number of bound data. The back is usually followed by the remove operation.
Here's a look at the specific usage:
[JavaScript]View PlainCopy
- <body>
- <p>AAAAAAAAA</p>
- <p>BBBBBBBBB</p>
- <p>CCCCCCCCC</p>
- <script src="http://d3js.org/d3.v3.min.js" charset="Utf-8" ></script>
- <script>
- var dataset = [10, 20, 30, 40, 50];
- var update = D3.select ("Body"). SelectAll ("P"). Data (dataset);
- var enter = update;
- Update.text (function (d,i) {
- return "update" + D;
- });
- Enter.enter ()
- . Append ("P")
- . Text (function (d,i) {
- return "enter" + D;
- });
- </script>
- </body>
The above code uses the variable name update and enter respectively to represent the respective part, the result of the above code is:
The resulting diagram can see the part of the newly added element enter. Change part of the code to see how to use Exit.
[JavaScript]View PlainCopy
- var DataSet = [10, 20];
- var update = D3.select ("Body"). SelectAll ("P"). Data (dataset);
- var exit = update;
- Update.text (function (d,i) {
- return "update" + D;
- });
- Exit.exit ()
- . Text (function (d,i) {
- return "Exit";
- });
As you can see, when you call the exit () function, you actually return an element that has no corresponding data. Usually we can delete the extra elements, such as:
[JavaScript]View PlainCopy
- Exit.exit ()
- . remove ();
In particular, the use of the Enter function is most visible. Because we usually use D3 for data visualization, we all have the data we need, and the amount of data is huge, and there are very few elements in the document that correspond to it. So be particularly skilled in the use of Enter
This article comes from:
Blog for: www.ourd3js.com
CSDN Blog for: blog.csdn.net/lzhlzz
"D3.js Starter Series---7" Understanding Update, enter, exit use