0

I'm new to D3js and having consistent problems working with json files. This issue has me very confused. I posted an example on:

http://jsfiddle.net/tommy6s/m5dfmf5r/

I have been working in the console and have tried the following as well as every other combination that I can think of:

 console.log(data) //returns data
 console.log(data.value); //returns undefined 
 console.log(d.value); //returns d is not defined
 console.log(data[0].value); //returns  67.53 first single value

I also get a console error: TypeError: string.slice is not a function var n = d3_time_numberRe.exec(string.slice(i, i + 4));

2 Answers 2

2

An element in d.data array has the following structure:

["2015-02-28", 67.53, 68.67, 67.05, 67.68, 0.0, 67.68 ]

enter image description here

I am not sure why this is not a JSON object (with field-value pair) but in order to access it you need to do something as below:

function(error, d) {
  var data = d.data;
  data.forEach(function(d) {
    var date   = parseDate(d[0]);
    var open = d[1];
    var high = d[2];
    ..
    var close= d[6];
  });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help Max! I agree with you about the JSON. I think that's part of what was confusing me.
The screen is taken from the JsonViewer free program jsonviewer.software.informer.com/1.2
0

It looks like the dates you are returning are pretty much all null.

Do the following in your example as a test for this:

data.forEach(function(d) {
    console.log(d);
    d.date = parseDate(d.date);
    d.value = +d.value;
 });

You'll notice it fails after trying one. Each of the dates from d.date is null. Causing the parseDate function to fail.

Down in your type function I changed it to use the javascript new Date instead to convert it and removed the second call to parseDate in the forEach loop to fix that.

function type(d) {
        var obj = {};
         console.log(d);
        obj.date = new Date(d[0]); //Use the javascript date object is perhaps better?
        obj.value = d[1];

        return obj;
}

http://jsfiddle.net/m5dfmf5r/5/

2 Comments

Thank you very much Rezant! I'm not sure I would have figured this one out.
Not a problem, I definitely think Max answers your actual question better however.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.