0

I am trying to plot a line chart from a Javascript array. While parsing the date i get an error which says

d3.v3.min.js:1 Error: attribute d: Expected number, "MNaN,0LNaN,450".o @ d3.v3.min.js:1

I tried have tried the suggestions mentioned in this stack overflow question : d3.js parsing date error with csv file. But I am still facing the same issue. Any idea what I am doing wrong here. The same works perfectly when I try running with tsv input.

Here is my code :

    <script src="//d3js.org/d3.v3.min.js"></script>
    <script>
        var data = [
            {
                "date": "01-Jan-16",
                "close": "20"
                },
            {
                "date": "05-Jan-16",
                "close": "100"
                }
            ];



var formatDate = d3.time.format("%d-%b-%y");

        function type(d) {
            d.date = formatDate.parse(d.date);
            d.close = +d.close;
            return d;
        }

        var margin = {
                top: 20,
                right: 20,
                bottom: 30,
                left: 50
            },
            width = 960 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom;

        var x = d3.time.scale()
            .range([0, width]);

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

        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

        var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left");

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

        var svg = d3.select("body").append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");


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

        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

        svg.append("g")
            .attr("class", "y axis")
            .call(yAxis)
            .append("text")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", ".71em")
            .style("text-anchor", "end")
            .text("Price ($)");

        svg.append("path")
            .datum(data)
            .attr("class", "line")
            .attr("d", line);
    </script>
Community
  • 1
  • 1
Yogeshwaran
  • 1
  • 1
  • 1

1 Answers1

0

Fixed the code myself. Replaced

function type(d) { d.date = formatDate.parse(d.date); d.close = +d.close; return d; }

by

    data.forEach(function (d) {
        d.date = formatDate.parse(d.date);
        d.close = +d.close;
        return d;
    });
Yogeshwaran
  • 1
  • 1
  • 1