Here is my JavaScript code for drawing a line chart using 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
at t (d3.min.js:2) at d3.html:38
And this is the d3.js code that triggers error:
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"
. Have a look at the 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.