D3.js饼图II - 范围类型输入的动态值

时间:2013-05-05 11:37:34

标签: javascript d3.js data-visualization

我想将动态值传递给D3生成的饼图(http://bl.ocks.org/mbostock/1346410),但我无法直接输入范围字段的值。传递它,然后通过示例单选按钮更新图表onchange工作正常。

我试图替换

d3.selectAll("input").on("change", change);

d3.select("#my_id").on("change", change);

然后根据

修改函数change()
function change() {
        path = path.data(pie(dataset.salary));
        path.transition().duration(750).attrTween("d", arcTween);
    }
没有多少爱情。

我错过了什么?

PS:这是输入字段,以防万一。这也调用了一个函数,它包含了上述所有函数和其他一些函数。

<input type="range" min="0" max="500000" name="dataset" id="salary" step="5000" value="salary" class="salary" onchange="calculate();" />

2 个答案:

答案 0 :(得分:4)

可能是一个非常典型的场景:在上面睡觉,切掉垃圾并重新开始:

这肯定可以进一步改进,但它现在有效(请注意我已将原始线条留在原位,注释掉以突出显示更改):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body onload="getData()">
  <form>
    <input type="range" name="dataset" value="salary" in="0" max="10" step="1"  onchange="getData" />
  </form>
  <script src="http://d3js.org/d3.v3.min.js"></script>
  <script>

      var salary_input = 3;

      function getData(salary){
        salary_input = document.forms[0].dataset.value;
        dataset.salary = [salary_input, 10 - salary_input];
        return dataset.salary;
      }


      var dataset = {
        salary:[5,5]
      }

    var width = 960,
        height = 500,
        radius = Math.min(width, height) / 2;

    var color = d3.scale.category20();

    var pie = d3.layout.pie()
        .sort(null);

    var arc = d3.svg.arc()
        .innerRadius(radius - 100)
        .outerRadius(radius - 20);

    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height)
      .append("g")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    var path = svg.selectAll("path")
        // .data(pie(dataset.apples))
        .data(pie(dataset.salary))
      .enter().append("path")
        .attr("fill", function(d, i) { return color(i); })
        .attr("d", arc)
        .each(function(d) { this._current = d; }); // store the initial values

    d3.selectAll("input").on("change", change);
    // d3.select("#salary").on("change", change);

    var timeout = setTimeout(function() {
      // d3.select("input[value=\"oranges\"]").property("checked", true).each(change);
      d3.select("input[value=\"salary\"]").each(change);
    }, 2000);

    function change() {
      getData(salary_input);
      console.log(dataset.salary);
      clearTimeout(timeout);
      // path = path.data(pie(dataset[this.value])); // update the data
      path = path.data(pie(dataset.salary)); // update the data
      path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
    }

    // Store the displayed angles in _current.
    // Then, interpolate from _current to the new angles.
    // During the transition, _current is updated in-place by d3.interpolate.
    function arcTween(a) {
      var i = d3.interpolate(this._current, a);
      this._current = i(0);
      return function(t) {
        return arc(i(t));
      };
    }

  </script>
  </body>
</html>

答案 1 :(得分:1)

@datafunk:除了根据我的需要添加一两个功能以进行说明外,我还将您的代码修改为最低限度,以便更容易理解,因为有几个没有用的东西,我也不喜欢转换(警告:我是d3新手,但我测试了这段代码):

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head><body>
<form><input type="range" name="r0" in="0" max="100" step="1" />
<br><input type="range" name="r1" in="0" max="100" step="1" />
<br><input type="range" name="r2" in="0" max="100" step="1" />
</form>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var dataset={salary:[50,50,50]};d3.selectAll("input[type=\"range\"]").on("change",adjustPie);
function adjustPie() {
    dataset.salary=[parseInt(document.forms[0].r0.value),
            parseInt(document.forms[0].r1.value),
            parseInt(document.forms[0].r2.value)];//    console.log(dataset.salary);
    path.data(pie(dataset.salary)); // update the data
    path.attr("d",arc);//<<<Might replace with a transition as per @datafunk example
}
var width=400,height=400,r=Math.min(width,height)/2;
var color=d3.scale.category20c();d3.selectAll("input[type=\"range\"]").style("width","400px");
var pie =d3.layout.pie()    .sort(null);
var svg =d3.select("body").append("svg")
    .attr("width",width)    .attr("height",height)
    .append("g")        .attr("transform", "translate("+r+","+r+")");//Make external margins and backgrounds with CSS on the SVG if you want them...
var arc =d3.svg.arc()       .outerRadius(r-20);//Sort of prototype for later arcs...?
var path=svg.selectAll("path")
    .data(pie(dataset.salary))
    .enter().append("path")//<<<For implementations allowing run-time addition of range controls, paths may need to be added/filled/coloured at run-time.
    .attr("fill",function(d,i){return color(i);})
    .attr("d",arc)//    .each(function(d){this._current=d;});//store the initial values (useful for transitions, see example by @datafunk)
adjustPie();
</script></body></html>