简体   繁体   English

用D3.js制作一个旭日形图

[英]Making a sunburst diagram with D3.js

I am learning D3 and love it! 我正在学习D3而且喜欢它! I am reading books, and I am stuck at an exercise making a sunburst diagram. 我正在看书,我被困在一个做旭日图的练习中。

Here is my code so far: 到目前为止,这是我的代码:

<div id="sunburst"></div>

   <%--Then write/append script to container--%>
   <script type="text/javascript">

       <%--Calculate the maximum radius--%>
       var width = 640, height = 400, maxRadius = Math.min(width,height)/2;

       var svg = d3.select("sunburst").append("svg")
           .attr("width", width)
           .attr("height", height);

      //Center sunburst parameter at (0,0), so it's easier to parameters
      var g = sag.append("g");
           .attr("transform", "translate(" + (width/2) + "," + (height/2) + ")");

      //Scales are D3 objects used to convert input values into svg coordinates (i.e. pixels).
      var theta = d3.scale.linear()
          .range([0,2 * Math.PI]);
      var radius = d3.scale.sqrt()
          .range([0,maxRadius]);

At this point, the author says that the default domains of [0,1] are exactly what we need in this example. 在这一点上,作者说默认域[0,1]正是我们在这个例子中所需要的。 I am confused because theta and the radius can be more that 1, no? 我很困惑,因为theta和半径可以超过1,不是吗? I must be missing something obvious. 我一定错过了一些明显的东西。

Thank you very much for your time! 非常感谢您的宝贵时间! :) :)

The values output from the scale function can indeed be more than 1. This is what the range defines. scale函数输出的值确实可以大于1.这是range定义的值。 The scale function also has an input interval for the scaling, the domain , which is not specified in the example. scale函数还有一个用于缩放的输入间隔,即domain ,在示例中未指定。 The default for this is [0, 1] . 默认值为[0, 1] The input values (across the domain interval) will then be mapped onto an output value (across the range interval), using the type of scaling specified: linear in this example. 然后,输入值( domain间隔)将使用指定的缩放类型映射到输出值(跨越range间隔):在此示例中为linear

So the full version would be: 所以完整版将是:

  var theta = d3.scale.linear()
      .domain([0,1])
      .range([0,2 * Math.PI]);

meaning 0 maps to 0; 意思是0映射到0; 1 maps to 2 * pi; 1映射到2 * pi; 0.5 maps to pi ... 0.5映射到pi ...

Anyway, in the example from the exercise, I assume the inputs using the theta scale will be in the range [0,1] , hence the author's comment about this being an appropriate default. 无论如何,在练习的例子中,我假设使用theta比例的输入将在[0,1]范围内,因此作者对此的评论是适当的默认值。

Reference: 参考:
d3 v3 API reference on github g3ub上的d3 v3 API参考
Example from dashingd3js.com 来自dashingd3js.com的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM