"D3.js Starter Series---5" How to add axes

Source: Internet
Author: User

The 3rd section makes an icon, but does not add a corresponding axis for it, so you don't know how long each bar is. This section makes an axis.

The axes in D3 are in the form of SVG diagrams, which is why the method of SVG is used in the 3rd section to do column charts. In the 4th section, we explain the usage of scale, and we need to use the scale when we do the axes. In the 4th section, we say that scale is a function, and the axes in this section are a function, but the usage is a little different.

Look at the following code, we define the data separately, scale (proportional), axis:

[JavaScript]View PlainCopy
  1. var DataSet = [30, 20, 45, 12, 21];
  2. var XScale = D3.scale.linear ()
  3. . domain ([0,d3.max (DataSet)])
  4. . Range ([0,500]);
  5. var axis = D3.svg.axis ()
  6. . Scale (XScale)
  7. . Orient ("bottom");

1-4 rows are defined data and scale (proportional), and the content of the scale can be used in a section.

5-7 is the definition axis:

    • D3.svg.axis () Call this function to generate an axis function
    • Scale () This function is used to specify the scales (proportions) of the axes
    • Orient () This function is used to specify the dividing point of the axis and the direction of the number in which to display. Bottom is displayed below the axis.
The code for the axis is drawn as follows: [JavaScript]View PlainCopy
    1. Svg.append ("G")
    2. . Call (axis);
Adding a g,g in SVG is an attribute in SVG, which is the group meaning, which represents a set of things, such as a group lines, rects, circles actually the axis is composed of these things. It also calls a call function, which is more important. In D3, call is the equivalent of defining a function and then giving it the selected element, which is equivalent to the following code: [JavaScript]View PlainCopy
    1. function foo (selection) {
    2. Selection
    3. . attr ("name1", "value1")
    4. . attr ("name2", "value2");
    5. }
    6. Foo (D3.selectall ("div"))
This code is equivalent to: [JavaScript]View PlainCopy
    1. D3.selectall ("div"). Call (foo);
So Svg.append ("G"). Call (axis) is equivalent to passing the selected G element to the axis function. After the call, the axes are displayed in the appropriate SVG.
You can also define several CSS styles to change the weight of the axes, fonts, and so on. The Transform property is used to move the position of the axis in SVG. [JavaScript]View PlainCopy
    1. Svg.append ("G")
    2. . attr ("Class","axis")
    3. . attr ("transform","translate (10,160)")
    4. . Call (axis);
The complete code is as follows: [JavaScript]View PlainCopy
  1. <style>
  2. . Axis Path,
  3. . Axis line{
  4. Fill:none;
  5. Stroke:black;
  6. Shape-rendering:crispedges;
  7. }
  8. . Axis text {
  9. Font-family:sans-serif;
  10. font-size:11px;
  11. }
  12. </style>
  13. <body>
  14. <script src="http://d3js.org/d3.v3.min.js" charset="Utf-8" ></script>
  15. <script>
  16. var width = 600;
  17. var height = 600;
  18. var dataset = [30, 20, 45, 12, 21];
  19. var svg = d3.select ("Body"). Append ("SVG")
  20. . attr ("width", width)
  21. . attr ("height", height);
  22. var XScale = d3.scale.linear ()
  23. . domain ([0,d3.max (DataSet)])
  24. . Range ([0,500]);
  25. var axis = D3.svg.axis ()
  26. . Scale (XScale)
  27. . Orient ("bottom");
  28. Svg.append ("G")
  29. . attr ("Class","axis")
  30. . attr ("transform","translate (10,160)")
  31. . Call (axis);
  32. Svg.selectall ("rect")
  33. . Data (DataSet)
  34. . Enter ()
  35. . Append ("rect")
  36. . attr ("X", ten)
  37. . attr ("y",function (d,i) {
  38. return I * 30;
  39. })
  40. . attr ("width", XScale) //Note here
  41. . attr ("height",)
  42. . attr ("Fill","Red");
  43. </script>

Results such as:

Blog for: www.ourd3js.com csdn Blog: blog.csdn.net/lzhlzz

"D3.js Starter Series---5" How to add axes

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.