D3 - 制作一个有错误的简单圆环图

时间:2016-12-01 11:38:27

标签: javascript angularjs d3.js svg donut-chart

使用D3-V4,我制作了一个简单的圆环图。我只有三个输入字母--A,B,C。它们的频率分别为60,30,10。我想要一个圆环图,但我却得错了。

我从这里接受帮助...... http://jsfiddle.net/Qh9X5/1196/

虽然它基于V3,但我正在使用V4并进行了适当的更改。

SNIPPET:

<html>

<head>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script> 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>

</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 

  <svg></svg>

  <script>

      //module declaration 
      var app = angular.module('myApp',[]);

      //Controller declaration
      app.controller('myCtrl',function($scope){

          $scope.svgWidth = 800;//svg Width
          $scope.svgHeight = 500;//svg Height 

          //Data in proper format 
          var data = [
                {"letter": "A","frequency": "60"},
                {"letter": "B","frequency": "30"},
                {"letter": "C","frequency": "10"}
          ];

          //removing prior svg elements ie clean up svg 
          d3.select('svg').selectAll("*").remove();

          //resetting svg height and width in current svg 
          d3.select("svg").attr("width", $scope.svgWidth).attr("height", $scope.svgHeight);

          //Setting up of our svg with proper calculations 
          var svg = d3.select("svg");
          var margin = {top: 20, right: 20, bottom: 30, left: 40};
          var width = svg.attr("width") - margin.left - margin.right;
          var height = svg.attr("height") - margin.top - margin.bottom;

          //Plotting our base area in svg in which chart will be shown 
          var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

          var piedata = pie(data.frequency);


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

          var path = g.selectAll("path")
              .data(piedata)
              .enter().append("path")
              .attr("fill",function() { return "hsl(" + Math.random() * 360 + ",100%,50%)"; })
              .attr("d", arc);


          g.selectAll("text").data(data.letter)
              .enter()
              .append("text")
              .attr("text-anchor", "middle")
              .attr("x", function(d) {
                  var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
                  d.cx = Math.cos(a) * (radius - 75);
                  return d.x = Math.cos(a) * (radius - 20);
              })
              .attr("y", function(d) {
                  var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
                  d.cy = Math.sin(a) * (radius - 75);
                  return d.y = Math.sin(a) * (radius - 20);
              })
      });

  </script> 

</body> 

</html> 

错误:

enter image description here

任何帮助将不胜感激。请帮我画一个简单的圆环图,其中 D3 - V4

2 个答案:

答案 0 :(得分:2)

使用 new version

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

应该如下,

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

答案 1 :(得分:2)

这里有很多问题:

  1. 正如@Sajeetharan所说,它现在是d3.pie()
  2. 您尚未定义radius变量
  3. 由于您尚未设置pie function,因此您无法提供.value函数正确的数据。
  4. text选择器中的绑定方式有误,您从不设置文本。
  5. 这里全部修好了:

    &#13;
    &#13;
    $scope = {}
    
    $scope.svgWidth = 800; //svg Width
    $scope.svgHeight = 500; //svg Height 
    
    //Data in proper format 
    var data = [{
      "letter": "A",
      "frequency": "60"
    }, {
      "letter": "B",
      "frequency": "30"
    }, {
      "letter": "C",
      "frequency": "10"
    }];
    
    //removing prior svg elements ie clean up svg 
    d3.select('svg').selectAll("*").remove();
    
    //resetting svg height and width in current svg 
    d3.select("svg").attr("width", $scope.svgWidth).attr("height", $scope.svgHeight);
    
    //Setting up of our svg with proper calculations 
    var svg = d3.select("svg");
    var margin = {
      top: 20,
      right: 20,
      bottom: 30,
      left: 40
    };
    var width = svg.attr("width") - margin.left - margin.right;
    var height = svg.attr("height") - margin.top - margin.bottom;
    var radius = 200;
    
    //Plotting our base area in svg in which chart will be shown 
    var g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
    
    var pie = d3.pie().sort(null)
      .value(function(d) {
        return d.frequency;
      });
    
    var piedata = pie(data);
    
    var arc = d3.arc()
      .innerRadius(radius - 100)
      .outerRadius(radius - 50);
    
    var path = g.selectAll("path")
      .data(piedata)
      .enter().append("path")
      .attr("fill", function() {
        return "hsl(" + Math.random() * 360 + ",100%,50%)";
      })
      .attr("d", arc);
    
    
    g.selectAll("text").data(piedata)
      .enter()
      .append("text")
      .attr("text-anchor", "middle")
      .attr("x", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle) / 2 - Math.PI / 2;
        d.cx = Math.cos(a) * (radius - 75);
        return d.x = Math.cos(a) * (radius - 20);
      })
      .attr("y", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle) / 2 - Math.PI / 2;
        d.cy = Math.sin(a) * (radius - 75);
        return d.y = Math.sin(a) * (radius - 20);
      })
      .text(function(d) {
        return d.data.letter;
      })
    &#13;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"></script>
    <svg>
    </svg>
    &#13;
    &#13;
    &#13;