"d3.js starter series---1" First program HelloWorld

Source: Internet
Author: User

Now let's start with d3.js to deal with the first simple question, and look at the following code:

[html]View PlainCopy
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>helloworld</title>
  5. </head>
  6. <body>
  7. <P>hello World 1</P>
  8. <p>hello World 2</P>
  9. </Body>
  10. </html>

If you have studied html, you should know that two lines of text will be printed on the screen, such as:

What if you want to use JavaScript to change these two lines of text? We will add the code into: [html]View PlainCopy
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>helloworld</title>
  5. </head>
  6. <body>
  7. <P>hello World 1</P>
  8. <p>hello World 2</P>
  9. <script>
  10. var paragraphs = document.getElementsByTagName ("p");
  11. For (var i = 0; i < paragraphs.length; I++) {
  12. var paragraph = Paragraphs.item (i);
  13. paragraph.innerhtml = "I like dog.";
  14. }
  15. </Script>
  16. </Body>
  17. </html>

The result becomes:


As you can see, using javascript, we've added 4 lines of code, What if we use d3.js? Just add one line of Code. Be careful not to forget to refer to the D3.js source File.

[html]View PlainCopy
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>helloworld</title>
  5. </head>
  6. <body>
  7. <P>hello World 1</P>
  8. <p>hello World 2</P>
  9. <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></ Script>
  10. <script>
  11. D3.select ("body"). selectall ("p"). text ("www.ourd3js.com");
  12. </Script>
  13. </Body>
  14. </html>

The results will change to:


Similar to JavaScript like jquery, it simplifies the process of using Javascript.

Next change the font color and size, as Follows:

[html]View PlainCopy
    1. var p = d3.select ("body"). selectall ("p"). text ("www.ourd3js.com");
    2. P.style ("color", "red");
    3. P.style ("font-size", "72px");

You can see that the above code assigns a value to the variable p before using P. This will make the code cleaner.

From: blog home: http://www.ourd3js.com/, csdn blog Home for: http://blog.csdn.net/lzhlzz/. Reprint Please indicate the source, thank you.

"d3.js starter series---1" First program HelloWorld

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.