0

So I have this csv file (the csv file might sit on server side preferably , but not decided yet). Most probably the user will be presented the options of selecting from these csv files from a drop-down menu. Then after selcting a particular file, user clicks "enter" , then this selected csv file should be read in javascript as an array. So how to do this ?? :-

My question is on the lines of How to pass variables and data from PHP to JavaScript?

___csv_file____

Class,      Subclass,           Company,    Product,
Chocolate,  Wafer chocolate,    Nestle, KitKat,
Chocolate,  White chocolate,    Nestle, Milkybar,
Chocolate,  White chocolate,    Nestle, Milkybar,
Chocolate,  Caramel chocolate,  Nestle, BarOne,
Chocolate,  Milk chocolate,     Nestle, Nestle Milk chocolate,
Chocolate,  Milk chocolate,     Nestle, Nestle Milk chocolate,
Chocolate,  Milk chocolate,     Nestle, Nestle Milk chocolate,
Chocolate,  Milk chocolate,     Nestle, Nestle Milk chocolate,
Chocolate,  Milk chocolate,     Cadbury,    Dairy milk,

In order to read it manually this is what has been done. But as you can see it has all the csv_data hardcoded, but I'd like to get it automated (i.e. get generated by reading from a csv file )

    root = {
 "name": "Chocolate", "tag":"class",
 "children": [
  {
   "name": "Wafer", "tag":"subclass",
   "children": [
    {
      "name": "Nestle", "tag":"company",
     "children": [
      {"name": "KitKat", "tag":"product"}
     ]
    }
   ]
  },

  {
   "name": "White", "tag":"subclass",
   "children": [
    {
      "name": "Nestle", "tag":"company",
     "children": [
      {"name": "Milkybar", "tag":"product"}
     ]
    }
   ]
  },

    {
   "name": "Caramel", "tag":"subclass",
   "children": [
    {
      "name": "Nestle", "tag":"company",
     "children": [
      {"name": "BarOne", "tag":"product"}
     ]
    }
   ]
  },    
    {
   "name": "Milk", "tag":"subclass",
   "children": [
    {
      "name": "Nestle", "tag":"company",
     "children": [
      {"name": "Nestle Milk", "tag":"product"}
     ]
    },  {
      "name": "Cadbury", "tag":"company",
     "children": [
      {"name": "Dairy Milk", "tag":"product"}
     ]
    }
   ]
  }




 ]
};

The problem now is that I want the javascript to just read this csv file from local location. But hows it possible to read a CSV file into such a complex dimensional array in JAVASCRIPT ?

Community
  • 1
  • 1
  • *"In order to read it manually this is what has been done http://jsfiddle.net/5Lv8gkqv/"* Put the relevant code (and *only* the relevant code) **in** the question, don't just link. This is one of the fundamentals of questions and answers on SO. – T.J. Crowder Nov 03 '14 at 08:53
  • The emphasis is on **relevant code**. Even your links contains a lot of unrelated d3-code. – Sirko Nov 03 '14 at 08:55
  • 1
    First, I'm fairly sure Javascript cannot access local files for any reason, as that seems like an enormous security risk. Second, You'd have to get the CSV string from a Server Side element and pass it to your javascript. Third, you'd have to then create a function to manually parse the data into an object. – Nunners Nov 03 '14 at 08:57
  • Hi all. @Sirko Ive added the relevant JS code. Please have a look and help –  Nov 03 '14 at 08:57
  • What is the use case for this application, is the user supossed to have the csv on his machine is this supposed to be a offline app writen in html/css/js ? Or is the csv supposed to be on a server, I'm thinking this should be the case then the csv reading should not be handled by javascript. – George Bora Nov 03 '14 at 08:58
  • @GeorgeBora there would be a menu given to user. Suppose 4 cases and he selects the chocolate case. Then te csv file for chocolate will be read in and displayed –  Nov 03 '14 at 08:59
  • @rzach I don't see, how all that d3 code is related to generating a multidimensional array. The solution is pretty simple, but I have no time to look through all that code ... – Sirko Nov 03 '14 at 09:01
  • @rzach I'm still not getting you, where is the .csv file located on the client side or on the server side ? – George Bora Nov 03 '14 at 09:04
  • @GeorgeBora it will be on the server side. Not decided yet though. Please suggest what would be better and easier to do –  Nov 03 '14 at 09:06
  • 1
    @rzach Every server side language can read CSV ex: http://www.w3schools.com/php/func_filesystem_fgetcsv.asp, from there I'd send the data in JSON format via AJAX to your script, once it arrives client side your code seems to be ok at displaying it. PS: I'd sugest tagging this question with the server side language you're going to use. – George Bora Nov 03 '14 at 09:12
  • @GeorgeBora So suppose I use PHP and the $file stores all the csv data. Now how to use AJAX on this $file to convert it to JSON. Im really new to this stuff and I can see that its JS,php,CSS and what not. Can you please help me on the steps elaborately, that would be awesome –  Nov 03 '14 at 09:20
  • @Sirko could you please suggest the solution if you have figured it out!!. Thanks –  Nov 03 '14 at 09:32
  • @rzach It might be better if you re-name the question / re-ask with "How to convert CSV Data to JSON Object" and only give your CSV Data and the `root` object you have in JS as the examples – Nunners Nov 03 '14 at 09:34
  • @Nunners have made the changes as per your request. Please help –  Nov 03 '14 at 10:11
  • Check this [site](http://www.convertcsv.com/csv-to-json.htm) – MrDumb Nov 03 '14 at 10:40
  • @ashutiwari4 :-) Tiwari ji thats good link, but the csv file is on server side. The user selects the csv file via drop down menu, and then the JS will read this csv file into array and display it. –  Nov 03 '14 at 10:47
  • Anybody ?? please help !!!! –  Nov 03 '14 at 12:18
  • @rzach Javascript **cannot** read the file directly, you'll need PHP to read the file and either return JSON or an array of Strings to then parse it into JSON in javascript. – Nunners Nov 03 '14 at 13:22
  • It would be better if you can share more details from the beginning. This Json is a personalized way to build an interactive graph (an answer of your first question) http://stackoverflow.com/questions/26651975/how-to-build-an-interactive-network-using-python/26653889. You should understand how u constructed the first graph, and then build JS function to read CSV(as suggested by others) and make this kind of graph using ur function. – user3378649 Nov 03 '14 at 15:26

2 Answers2

1

Possible setup:

PHP web server (or NodeJS server - my personal favourite)

Possible workflow:

  • php upload page for CSV file / or upload by FTP/SSH
  • csv is on server
  • php page with param datafromid=csvfile provides the data conversion from csv to json.. (if the csv file does not contain too many rows, read it into an php array, and spit it out with json_encode() by getting each row (fgetcsv()) and adding a sub-array to your total array)
  • depending on how you provide the data to d3, use ajax to access that php-page to access the json or include it in your page (i think loading it by ajax, accessing the file from the browser where the page lies, is better - you may use jQuery get())
BananaAcid
  • 3,221
  • 35
  • 38
0

First off on the server create a PHP file giving it a relevant name like candyData.php in which you'd have:

//code to read your data.csv
//code which stores the data from the csv into an object $candyData
$json = json_encode($candyData);
return $json

Then on the client side with Javascript and I'm going to also use JQuery:

jQuery.ajax({
    url: "http://example.mysite.com/rest/candyData.php",
    type: "GET",
    success: function (response) {
        var candyData = response;
        //Here you'd put all the code you currently have so you can visualize the data
    },
    error: function (response) {
        console.log("failed");
    }
});
George Bora
  • 1,618
  • 6
  • 26
  • 45