2

I'm trying to reformat JSON Data from a REST Api in order to display the data with d3. The d3 code is within an angular component, but I think the problem is more related to javascript. The template I want to use is here: https://bl.ocks.org/mbostock/3884955

The data I get is formated as array of objects (of which I just want to print the first one) formated like that:

{"time":["2018-09-17T12:44:16.985Z","2018-09-17T12:44:17.982Z"],
    "counts":[
            [3539.8,3539.4],
            Arr(2),
            Arr(2),
            Arr(2),
            Arr(2)
    ]
}

--> five measuring points with two (in this example) measurements each

this is how the d3-code expects the data:

(local var) data: {
id: string;
values: {
    date: any;
    count: any;
}[];

the code I tried to achieve that:

   var keys_all=d3.keys(data[0].counts);
   var measurementData = keys_all.map(function(id) {
   return {
    id: id.toString(),
    values: data[0].counts[id].map(function(d,i) {
      return {date: data[0].time[i], count: d};
    })
   };
   });  

When I console.log(), the result is exactly what I expected. But I cannot access the properties "date" and "time" in my following code.

When I hover over the result variable, VS Code only shows:

(local var) data: {
id: string;
values: any;
}[]

I also tried to log the stringified Object and this works as well.

Any ideas what could go wrong?

JohnnyD.
  • 41
  • 3
  • What is `Arr(2)` supposed to mean? That's not valid JSON. – Pointy Sep 24 '18 at 13:45
  • Not sure why you would be using map inside of the other map.... – epascarello Sep 24 '18 at 13:46
  • Sorry,I was just lazy, Arr(2) is an array with 2 measurements like the one above... Is there a better way? I think I need both mappings, the first is for the measurepoints and the second for the measurements. – JohnnyD. Sep 24 '18 at 13:53
  • _How exactly_ are you trying to access these `date` and `time` properties, starting from the object returned by the formatting function you show? (From here I don't see any `time` property in your returned object, but there is a `returned.values[n].date`.) – Stock Overflaw Sep 24 '18 at 15:16
  • This is how I try to access the count property for example: `this.yScale.domain([ d3.min(measurementData, function(c) { return d3.min(c.values, function(d) { return d.count; }); }), d3.max(measurementData, function(c) { return d3.max(c.values, function(d) { return d.count; }); }) ]); ` – JohnnyD. Sep 24 '18 at 19:47

0 Answers0