簡體   English   中英

D3水平條形圖X軸范圍短於通過AngularJs獲得的數據

[英]D3 Horizontal Bar Chart X-Axis Range shorter than in Data via AngularJs

我繪制了水平條形圖,並且沒有錯誤地進行獲取。 我的問題是X軸必須使用當前數據集反映至少0到35的頻率。 現在我看到的是0到8。有人可以向我解釋錯誤並幫助糾正該錯誤嗎?

片段:

<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": "5.01"},
                  {"letter": "B","frequency": "7.80"},
                  {"letter": "C","frequency": "15.35"},
                  {"letter": "D","frequency": "22.70"},
                  {"letter": "E","frequency": "34.25"},
                  {"letter": "F","frequency": "10.21"},
                  {"letter": "G","frequency": "7.68"},
            ];

                //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");

                //shifting the canvas area from left and top 
                g.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

                //X and Y scaling 
                var x = d3.scaleLinear().rangeRound([0, width]);
                var y = d3.scaleBand().rangeRound([height, 0]).padding(0.4);

                //Feeding data points on x and y axis 
                data.forEach(function(){

                    x.domain([0, d3.max(data, function(d) { return d.frequency; })]);
                    y.domain(data.map(function(d) { return d.letter; }));

                });

                //Final Plotting 

                //for x axis 
                g.append("g")
                    .attr("transform", "translate(0," + height + ")")
                    .call(d3.axisBottom(x));

                //for y axis 
                g.append("g")
                    .call(d3.axisLeft(y))
                    .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", "0.71em")
                    .attr("text-anchor", "end");

                  //for rectangles 
                  g.selectAll(".bar")
                    .data(data)
                    .enter()
                    .append("rect")
                    .attr("class", "bar")
                    .attr("y", function(d) { return y(d.letter); })
                    .attr("x", function(d) { return 0; })
                    .attr("height", y.bandwidth())
                    .attr("width", function(d) { return x(d.frequency); });

        });

    </script> 

</body> 

</html> 

結果:

在此處輸入圖片說明

請幫忙,以使我在當前的條形圖中獲得適當長度的矩形或條形,並帶有要提供的相應數據集。

您的代碼的這一部分是錯誤的:

data.forEach(function(){    
  x.domain([0, d3.max(data, function(d) { return d.frequency; })]);
  y.domain(data.map(function(d) { return d.letter; }));    
});

嘗試將其替換為:

x.domain([0, d3.max(data, function(d) { return +d.frequency; })]);
y.domain(data.map(function(d) { return d.letter; }));    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM