"D3.js Starter Series---7" Understanding Update, enter, exit use

Source: Internet
Author: User

The following code appears repeatedly in the previous sections:

[JavaScript]View PlainCopy
    1. Svg.selectall ("rect")
    2. . Data (DataSet)
    3. . Enter ()
    4. . 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
  1. <body>
  2. <p>AAAAAAAAA</p>
  3. <p>BBBBBBBBB</p>
  4. <p>CCCCCCCCC</p>
  5. <script src="http://d3js.org/d3.v3.min.js" charset="Utf-8" ></script>
  6. <script>
  7. var dataset = [10, 20, 30, 40, 50];
  8. var update = D3.select ("Body"). SelectAll ("P"). Data (dataset);
  9. var enter = update;
  10. Update.text (function (d,i) {
  11. return "update" + D;
  12. });
  13. Enter.enter ()
  14. . Append ("P")
  15. . Text (function (d,i) {
  16. return "enter" + D;
  17. });
  18. </script>
  19. </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
  1. var DataSet = [10, 20];
  2. var update = D3.select ("Body"). SelectAll ("P"). Data (dataset);
  3. var exit = update;
  4. Update.text (function (d,i) {
  5. return "update" + D;
  6. });
  7. Exit.exit ()
  8. . Text (function (d,i) {
  9. return "Exit";
  10. });
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
    1. Exit.exit ()
    2. . 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.