简体   繁体   English

d3.js未捕获的TypeError:o不是函数

[英]d3.js Uncaught TypeError: o is not a function

Here is my JavaScript code for drawing a line chart using d3.jes: 这是我的JavaScript代码,用于使用d3.jes绘制折线图:

var h = 100;
var w = 200;

var monthlySales = [
    {"month":10, "sales":20},
    {"month":20, "sales":14},
    {"month":30, "sales":20},
    {"month":40, "sales":21},
    {"month":50, "sales":15},
    {"month":60, "sales":22},
    {"month":70, "sales":9},
    {"month":80, "sales":6},
    {"month":90, "sales":23},
    {"month":100, "sales":7}
];

var lineFunc = d3.line()
        .x(function(d){ return d.month * 2;})
        .y(function(d){return d.sales;})
        .curve("linear");
var svg = d3.select("body")
        .append("svg")
        .attr("width", w)
        .attr("height", h);
var viz = svg.append("path")
        .attr("d", lineFunc(monthlySales))
        .attr("stroke", "purple")
        .attr("stroke-width", 2)
        .attr("fill", "none");

It shows this error: 它显示此错误:

Uncaught TypeError: o is not a function 未捕获的TypeError:o不是函数

 at t (d3.min.js:2) at d3.html:38 

And this is the d3.js code that triggers error: 这是触发错误的d3.js代码:

for(null==i&&(u=o(s=ve())),a=0;a<=f;++a)!(a<f&&r(c=t[a],a,t))===l.... 

The problem here is just this method in the line generator: 这里的问题就是行生成器中的此方法:

.curve("linear");

In D3 v4.x, there is no curve named "linear" . 在D3 v4.x中,没有名为"linear"曲线。 Have a look at the API . 看一下API

You can simply remove it or choose a correct curve. 您可以简单地将其删除或选择正确的曲线。 For instance, 例如,

.curve(d3.curveBasis);

Here is your code with that change: 这是您所做的更改的代码:

 var h = 100; var w = 200; var monthlySales = [{ "month": 10, "sales": 20 }, { "month": 20, "sales": 14 }, { "month": 30, "sales": 20 }, { "month": 40, "sales": 21 }, { "month": 50, "sales": 15 }, { "month": 60, "sales": 22 }, { "month": 70, "sales": 9 }, { "month": 80, "sales": 6 }, { "month": 90, "sales": 23 }, { "month": 100, "sales": 7 }]; var lineFunc = d3.line() .x(function(d) { return d.month * 2; }) .y(function(d) { return d.sales; }) .curve(d3.curveBasis); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); var viz = svg.append("path") .attr("d", lineFunc(monthlySales)) .attr("stroke", "purple") .attr("stroke-width", 2) .attr("fill", "none"); 
 <script src="https://d3js.org/d3.v4.min.js"></script> 

You're not passing into the function properly, try this: 您没有正确传递函数,请尝试以下操作:

function lineFunc(d) {
    d3.line()
        .x(function(d) {
            return d.month * 2;
        })
        .y(function(d) {
            return d.sales;
        })
        .curve("linear");
}

Here's working fiddle, it creates the svg in the body, see source 这是工作中的小提琴,它在体内创建了svg,请参见源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM