Question

I'm new to D3 charts and I'm getting an error saying 'Error: Problem Parsing' when trying to run my code. The code and error can be seen below. I have no idea what the problem is except it has something to do with parsing the 'd.value'. Both X and Y axis show up as well as the X axis dates. But there are no lines. The value coming in is already a number, but I've tried to use parseFloat, parseInt, and parseDouble to make it work. I'm out of ideas.

 Error: Problem parsing d="M0,NaNL0.47187163062091714,NaNL0.9437432612418343,NaNL1.4156148918627514,NaNL1.8874865224836685,NaNL2.3593581531045857,NaNL2.8312297837255027, etc. etc.


    function drawLineChart(element, chartData){
    var margin = {top: 20, right: 20, bottom: 30, left: 50},
        width = 380 - margin.left - margin.right,
        height = 200 - margin.top - margin.bottom;

    var parseDateD3 = d3.time.format("%d-%m-%y").parse;

    var defaultMax = 50;

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

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

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

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

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

    var svg = d3.selectAll(element).append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .style("margin", "auto")
        .style("background-color", "white")
        .style("border", "2px solid white")
        .style("border-radius", "25px")
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    //d3.tsv("/resources/sampledata/data.tsv", function(error, data) {
      chartData.forEach(function(d) {
        var day = JSON.stringify(d.day.date);
        var month = JSON.stringify(d.day.month);
        var year = JSON.stringify(d.day.year);
        if(day < 10) day = "0" + day;
        if(month < 10) month = "0" + month;
        year = year.substring(1, year.length);

        d.date = parseDateD3(day + "-" + month + "-" + year);
        var NUMBER = parseInt(d.value);
        d.number = +NUMBER;
      });

      x.domain(d3.extent(chartData, function(d,i) { return d.date; }));
      y.domain(d3.extent(chartData, function(d) { return d.number; }));

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

      svg.append("g")
          .attr("class", "y axis")
          .call(yAxis);

      svg.append("path")
          .datum(chartData)
          .attr("class", "line")
          .attr("d", line);

      svg.append("rect")
        .attr("class", "goal")
        .style("opacity", 0.2)
        .attr("width", width)
        .attr("height", height - defaultMax)
        .attr("transform", "translate(0," + defaultMax + ")");
    }
Was it helpful?

Solution

In the definition of your line, you should have

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

instead of

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

The code was referencing .close and you're setting and using .number elsewhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top