D3饼图更新,IV在while循环中运行不正常 - 为什么?

时间:2015-06-22 01:17:01

标签: javascript d3.js charts

我第一次使用D3库工作,基本上也使用javascript,我需要一些帮助。

当mysql-query为true时,我想在while循环中创建这个漂亮的饼图。有用。它使用无线电输入制作三个饼图和相关表格。但在那之后,当我想要切换单选按钮值时,只有最后一个饼图工作。当我激活未选中的单选按钮时,另外两个饼图不会切换它们的值。

我该怎么办?

    <?php

$gem_stud_data = $db->prepare("SELECT * FROM ergebnisse WHERE studiengang_1 <> 0 AND studiengang_2 <> 0 AND semester_id=1");
$gem_stud_data->execute();

if($_GET["id"] == 1){

    while($gem_stud_datas = $gem_stud_data->fetch()){
        $stud1_data = $db->prepare("SELECT * FROM studiengaenge WHERE id=:eins");
        $stud1_data->bindParam(":eins", $gem_stud_datas["studiengang_1"]);
        $stud1_data->execute();
        $stud1_datas = $stud1_data->fetch();
        $stud2_data = $db->prepare("SELECT * FROM studiengaenge WHERE id=:eins");
        $stud2_data->bindParam(":eins", $gem_stud_datas["studiengang_2"]);
        $stud2_data->execute();
        $stud2_datas = $stud2_data->fetch();
?>

    <form>
      <label><input type="radio" name="dataset" value="stud_1" checked> <?php echo $stud1_datas["kuerzel"]; ?></label>
      <label><input type="radio" name="dataset" value="stud_2"> <?php echo $stud2_datas["kuerzel"]; ?></label>
    </form>
    <script>

    var dataset = {
      stud_1: [53245, 28479, 19697, 24037, 40245],
      stud_2: [200, 200, 200, 200]
    };

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

    var enterClockwise = {
      startAngle: 0,
      endAngle: 0
    };

    var enterAntiClockwise = {
      startAngle: Math.PI * 2,
      endAngle: Math.PI * 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("main").append("svg")
      .attr("width", width)
      .attr("height", height)
      .append("g")
      .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    // set the start and end angles to 0 so we can transition
    // clockwise to the actual values later
    var path = svg.selectAll("path")
      .data(pie(dataset.stud_1))
      .enter().append("path")
        .attr("fill", function(d, i) { return color(i); })
        .attr("d", arc(enterClockwise))
        .each(function(d) {
          this._current = {
            data: d.data,
            value: d.value,
            startAngle: enterClockwise.startAngle,
            endAngle: enterClockwise.endAngle
          }
        }); // store the initial values

    path.transition()  // update
        .duration(750)
        .attrTween("d", arcTween);

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

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

    function change() {
      clearTimeout(timeout);
      path = path.data(pie(dataset[this.value])); // update the data
      // set the start and end angles to Math.PI * 2 so we can transition
      // anticlockwise to the actual values later
      path.enter().append("path")
          .attr("fill", function (d, i) {
            return color(i);
          })
          .attr("d", arc(enterAntiClockwise))
          .each(function (d) {
            this._current = {
              data: d.data,
              value: d.value,
              startAngle: enterAntiClockwise.startAngle,
              endAngle: enterAntiClockwise.endAngle
            };
          }); // store the initial values

      path.exit()
          .transition()
          .duration(750)
          .attrTween('d', arcTweenOut)
          .remove() // now remove the exiting arcs

      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));
      };
    }
    function arcTweenOut(a) {
      var i = d3.interpolate(this._current, {startAngle: Math.PI * 2, endAngle: Math.PI * 2, value: 0});
      this._current = i(0);
      return function (t) {
        return arc(i(t));
      };
    }

    </script>
<?php 
    }
} ?>

我真的希望你能帮助我!我很沮丧:( 我提前谢谢你了!

1 个答案:

答案 0 :(得分:0)

请参阅下文,这适用于我并更改您创建的超时数据。

<form>
  <label><input type="radio" name="dataset" value="stud_1" checked> Option 1 </label>
  <label><input type="radio" name="dataset" value="stud_2"> Option 2 </label>
</form>

<script>

var dataset = {
  stud_1: [53245, 28479, 19697, 24037, 40245],
  stud_2: [200, 200, 200, 200]
};

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

var enterClockwise = {
  startAngle: 0,
  endAngle: 0
};

var enterAntiClockwise = {
  startAngle: Math.PI * 2,
  endAngle: Math.PI * 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("#main").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

// set the start and end angles to 0 so we can transition
// clockwise to the actual values later
var path = svg.selectAll("path")
  .data(pie(dataset.stud_1))
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc(enterClockwise))
    .each(function(d) {
      this._current = {
        data: d.data,
        value: d.value,
        startAngle: enterClockwise.startAngle,
        endAngle: enterClockwise.endAngle
      }
    }); // store the initial values

path.transition()  // update
    .duration(750)
    .attrTween("d", arcTween);

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

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

function change() {
  clearTimeout(timeout);
  path = path.data(pie(dataset[this.value])); // update the data
  // set the start and end angles to Math.PI * 2 so we can transition
  // anticlockwise to the actual values later
  path.enter().append("path")
      .attr("fill", function (d, i) {
        return color(i);
      })
      .attr("d", arc(enterAntiClockwise))
      .each(function (d) {
        this._current = {
          data: d.data,
          value: d.value,
          startAngle: enterAntiClockwise.startAngle,
          endAngle: enterAntiClockwise.endAngle
        };
      }); // store the initial values

  path.exit()
      .transition()
      .duration(750)
      .attrTween('d', arcTweenOut)
      .remove() // now remove the exiting arcs

  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));
  };
}
function arcTweenOut(a) {
  var i = d3.interpolate(this._current, {startAngle: Math.PI * 2, endAngle: Math.PI * 2, value: 0});
  this._current = i(0);
  return function (t) {
    return arc(i(t));
  };
}

</script>

请参阅修订后的解决方案......在那里你必须抓住所有svgs,然后选择路径,然后附加数据......请参阅更改功能中的更改。

<form>
  <label><input type="radio" name="dataset" value="stud_1" checked> Option 1 </label>
  <label><input type="radio" name="dataset" value="stud_2"> Option 2 </label>
</form>

<script>

var dataset = {
  stud_1: [53245, 28479, 19697, 24037, 40245],
  stud_2: [200, 200, 200, 200]
};

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

var enterClockwise = {
  startAngle: 0,
  endAngle: 0
};

var enterAntiClockwise = {
  startAngle: Math.PI * 2,
  endAngle: Math.PI * 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("#main").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + width / 3 + "," + height / 3 + ")");

var svg2 = d3.select("#main2").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + (width / 3 * 2) + "," + (height / 3 * 2) + ")");

var svg3 = d3.select("#main3").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + (width / 3 * 2) + "," + (height / 3 * 2)+ ")");


// set the start and end angles to 0 so we can transition
// clockwise to the actual values later
var path = d3.selectAll('svg').selectAll("path")
  .data(pie(dataset.stud_1))
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc(enterClockwise))
    .each(function(d) {
      this._current = {
        data: d.data,
        value: d.value,
        startAngle: enterClockwise.startAngle,
        endAngle: enterClockwise.endAngle
      }
    }); // store the initial values

path.transition()  // update
    .duration(750)
    .attrTween("d", arcTween);

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

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

function change() {
  clearTimeout(timeout);
  path = d3.selectAll('svg').selectAll('path').data(pie(dataset[this.value])); // update the data
  // set the start and end angles to Math.PI * 2 so we can transition
  // anticlockwise to the actual values later
  path.enter().append("path")
      .attr("fill", function (d, i) {
        return color(i);
      })
      .attr("d", arc(enterAntiClockwise))
      .each(function (d) {
        this._current = {
          data: d.data,
          value: d.value,
          startAngle: enterAntiClockwise.startAngle,
          endAngle: enterAntiClockwise.endAngle
        };
      }); // store the initial values

  path.exit()
      .transition()
      .duration(750)
      .attrTween('d', arcTweenOut)
      .remove() // now remove the exiting arcs

  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));
  };
}
function arcTweenOut(a) {
  var i = d3.interpolate(this._current, {startAngle: Math.PI * 2, endAngle: Math.PI * 2, value: 0});
  this._current = i(0);
  return function (t) {
    return arc(i(t));
  };
}

</script>