Javascript D3线图获取错误且未绘制

Javascript D3线图获取错误且未绘制,javascript,d3.js,svg,linechart,Javascript,D3.js,Svg,Linechart,我正在为一组关于字母与频率的数据制作一个折线图。我已经为x轴和y轴编写了正确的代码,但在绘制直线时,我遇到了错误,无法绘制直线图。有人能帮忙解决这个问题吗 片段: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script> <script src="https:/

我正在为一组关于字母与频率的数据制作一个折线图。我已经为x轴和y轴编写了正确的代码,但在绘制直线时,我遇到了错误,无法绘制直线图。有人能帮忙解决这个问题吗

片段:

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


                //the line function for path 
                var lineFunction = d3.line()
                    .x(function(d) {return xScale(d.x); })
                    .y(function(d) { return yScale(d.y); })
                    .curve(d3.curveLinear);

                //defining the lines
                var path = g.append("path");

                //plotting lines
                path
                    .attr("d", lineFunction(data))
                    .attr("stroke", "blue")
                    .attr("stroke-width", 2)
                    .attr("fill", "none");

        });

    </script> 

</body> 

</html> 

//模块声明
var-app=angular.module('myApp',[]);
//控制器声明
应用程序控制器('myCtrl',函数($scope){
$scope.svgWidth=800;//svg宽度
$scope.svgHeight=500;//svg高度
//正确格式的数据
风险值数据=[
{“字母”:“A”,“频率”:“5.01”},
{“字母”:“B”,“频率”:“7.80”},
{“字母”:“C”,“频率”:“15.35”},
{“字母”:“D”,“频率”:“22.70”},
{“字母”:“E”,“频率”:“34.25”},
{“字母”:“F”,“频率”:“10.21”},
{“字母”:“G”,“频率”:“7.68”},
];
//删除以前的svg元素,即清理svg
d3.select('svg')。selectAll(“*”).remove();
//在当前svg中重置svg高度和宽度
d3.选择(“svg”).attr(“宽度”、$scope.svgWidth).attr(“高度”、$scope.svgHeight);
//通过正确的计算设置svg
var svg=d3.选择(“svg”);
var-margin={顶部:20,右侧:20,底部:30,左侧:40};
var width=svg.attr(“width”)-margin.left-margin.right;
var height=svg.attr(“height”)-margin.top-margin.bottom;
//在svg中绘制我们的基本区域,其中将显示图表
var g=svg.append(“g”);
//从左侧和顶部移动画布区域
g、 属性(“变换”、“平移”(+margin.left+)、“+margin.top+”);
//X和Y缩放
var x=d3.scaleLinear().rangeRound([0,宽度]);
变量y=d3.scaleBand().rangeRound([height,0])。padding(0.4);
//x轴和y轴上的进给数据点
x、 域([0,d3.max(数据,函数(d){return+d.frequency;})]);
y、 域(data.map(函数(d){返回d.letter;}));
//最终绘图
//对于x轴
g、 附加(“g”)
.attr(“变换”、“平移(0)”、“高度+”)
.call(d3.axisBottom(x));
//对于y轴
g、 附加(“g”)
.呼叫(d3.左(y))
.append(“文本”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,6)
.attr(“dy”,“0.71em”)
.attr(“文本锚定”、“结束”);
//路径的线函数
var lineFunction=d3.line()
.x(函数(d){返回xScale(d.x);})
.y(函数(d){返回yScale(d.y);})
.曲线(d3.曲线线);
//界定界线
var path=g.append(“路径”);
//绘制线
路径
.attr(“d”,线函数(数据))
.attr(“笔划”、“蓝色”)
.attr(“笔划宽度”,2)
.attr(“填充”、“无”);
});
错误:

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


                //the line function for path 
                var lineFunction = d3.line()
                    .x(function(d) {return xScale(d.x); })
                    .y(function(d) { return yScale(d.y); })
                    .curve(d3.curveLinear);

                //defining the lines
                var path = g.append("path");

                //plotting lines
                path
                    .attr("d", lineFunction(data))
                    .attr("stroke", "blue")
                    .attr("stroke-width", 2)
                    .attr("fill", "none");

        });

    </script> 

</body> 

</html> 

新错误:

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


                //the line function for path 
                var lineFunction = d3.line()
                    .x(function(d) {return xScale(d.x); })
                    .y(function(d) { return yScale(d.y); })
                    .curve(d3.curveLinear);

                //defining the lines
                var path = g.append("path");

                //plotting lines
                path
                    .attr("d", lineFunction(data))
                    .attr("stroke", "blue")
                    .attr("stroke-width", 2)
                    .attr("fill", "none");

        });

    </script> 

</body> 

</html> 

看看控制台:您没有
xScale
yScale

因此,线路生成器应为:

var lineFunction = d3.line()
    .x(function(d) {return x(d.frequency); })
    .y(function(d) { return y(d.letter); })
    .curve(d3.curveLinear);
除此之外,
frequency
是一个字符串,而不是一个数字。所以,把它变成一个数字是个好主意。在
数据
变量后写下:

data.forEach(function(d){
    d.frequency = +d.frequency;
});

注意:使用描述性名称正确定义变量名是一种很好的做法,如
xScale
yAxis
chartLegend
formatNumber
。。。看看你的行生成器:在一行中有两个不同的
x
。如果你不小心,你会把它们混在一起。

看看控制台:你没有
xScale
yScale

因此,线路生成器应为:

var lineFunction = d3.line()
    .x(function(d) {return x(d.frequency); })
    .y(function(d) { return y(d.letter); })
    .curve(d3.curveLinear);
除此之外,
frequency
是一个字符串,而不是一个数字。所以,把它变成一个数字是个好主意。在
数据
变量后写下:

data.forEach(function(d){
    d.frequency = +d.frequency;
});

注意:使用描述性名称正确定义变量名是一种很好的做法,如
xScale
yAxis
chartLegend
formatNumber
。。。看看你的行生成器:在一行中有两个不同的
x
。如果你不小心,你会把它们混在一起。

如果你想使用xScale和yScale,你需要定义这些函数。下面给出了语法(忽略值):

以下代码适用于d3版本3

me.xscale = d3.scale.linear()   // for horizontal distances if using for 2D 
        .domain([0, 500])
        .range([0, 700]); 

me.yscale = d3.scale.linear()   // for vertical distances if using for 2D
        .domain([0, 600])
        .range([0, 200]);
这些函数用于定义一个范围内的值到另一个范围内的值的映射

e、 g-假设您想在浏览器屏幕上绘制图形。并且假设浏览器屏幕上的宽度500px在图形中应计为500。
您需要如上所述定义xScale。在这种情况下,此函数将域(0-500)中的每个值映射到范围(0-700)中的唯一值,反之亦然。

如果要使用xScale和yScale,则需要定义这些函数。下面给出了语法(忽略值):

以下代码适用于d3版本3

me.xscale = d3.scale.linear()   // for horizontal distances if using for 2D 
        .domain([0, 500])
        .range([0, 700]); 

me.yscale = d3.scale.linear()   // for vertical distances if using for 2D
        .domain([0, 600])
        .range([0, 200]);
这些函数用于定义一个范围内的值到另一个范围内的值的映射

e、 g-假设您想在浏览器屏幕上绘制图形。并且假设浏览器屏幕上的宽度500px在图形中应计为500。 您需要如上所述定义xScale。在这种情况下,此函数将域(0-500)中的每个值映射到范围中的唯一值