Nishi
Nishi

Reputation: 85

D3.js - Error : d3.line() is not a function (changed from version 4 to 3)

I have the following segment in my D3 code.

  var line = d3.line()
             .x(function (d) {
                 return x(parseTime(d.date));
             })
             .y(function (d) {
                 return y(d.close);
             });

and it says d3.line() is not a function. I initially wrote the code in version 4. Then again changed it to version 3.

I have pasted my code below.

   var data = [{ "date": "2016.07.19", "close": 185697.89 }, { "date": "2016.07.20", "close": 185697.89 }, { "date": "2016.07.21", "close": 186601.1 }, { "date": "2016.07.22", "close": 187273.89 }];

           var svg = d3.select("svg"),
             margin = {
                 top: 20,
                 right: 20,
                 bottom: 30,
                 left: 50
             },
             width = +svg.attr("width") - margin.left - margin.right,
             height = +svg.attr("height") - margin.top - margin.bottom,
             g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

           var parseTime = d3.time.format("%Y.%m.%d");

           var x = d3.scale.linear()
             .rangeRound([0, width]);

           var y = d3.scale.linear()
             .rangeRound([height, 0]);

           var line = d3.line()
             .x(function (d) {
                 return x(parseTime(d.date));
             })
             .y(function (d) {
                 return y(d.close);
             });

           x.domain(d3.extent(data, function (d) {
               return parseTime(d.date);
           }));
           y.domain(d3.extent(data, function (d) {
               return d.close;
           }));

           g.append("g")
             .attr("transform", "translate(0," + height + ")")
             .call(d3.axisBottom(x))
             .select(".domain")
             .remove();

           g.append("g")
             .call(d3.axisLeft(y))
             .append("text")
             .attr("fill", "#000")
             .attr("transform", "rotate(-90)")
             .attr("y", 6)
             .attr("dy", "0.71em")
             .attr("text-anchor", "end")
             .text("Price ($)");

           g.append("path")
             .datum(data)
             .attr("fill", "none")
             .attr("stroke", "steelblue")
             .attr("stroke-linejoin", "round")
             .attr("stroke-linecap", "round")
             .attr("stroke-width", 1.5)
             .attr("d", line);

Can someone please point out any changes that needs to be done according to version 3 ?

Upvotes: 1

Views: 9302

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9931

As pointed out by @Cyril d3.line() is not a function but instead you have to use d3.svg.line(). Also look here for more explanation.

Refs: docs

Upvotes: 1

Related Questions