otmezger
otmezger

Reputation: 10794

create a javascript object from textfile

I'm trying to make a simple website on my raspberry pi that displays a graph from the temperature. I have a sensor and a python script that writes every 30 min to a text file on /var/www/data.csv

Now I want to make a very simple line plot for this data, like here: https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart

how the hell can I read this text file (accesible via './data.csv') into this chart?

I managed to read it as a string, but how can I convert it then into an array?

Here is an example of my data.csv:

1361048396.0 , 20.0 , 47.0
1361048424.0 , 20.0 , 47.0
1361048719.0 , 21.0 , 46.0
1361048761.0 , 21.0 , 46.0
1361048761.0 , 20.0 , 47.0

and what I need at the end is something like this (but of course as a variable!):

data.addRows([
  [1361048396.0, 20, 47],
  [1361048424.0, 20, 47],
  [1361048719.0, 21, 46],
  [1361048396.0, 21, 46],
  [1361048761.0, 20, 47],
]);

Upvotes: 0

Views: 92

Answers (1)

user2077888
user2077888

Reputation: 31

How about using the JavaScript split method? If you are reading the entire data.csv into a single string, then you may need to split twice.

ex:

var result=[];
var a=str.split("\n");
for (var i = 0; i < a.length; i++) {
  result.push(a[i].split(","));
}

Upvotes: 3

Related Questions