Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

loading local csv file using d3.csv

var rowToHtml = function( row ) {
  var result = "";
  for (key in row) {
    result += key + ": " + row[key] + "<br/>"
  }
  return result;
}

var previewCsvUrl = function( csvUrl ) {
  d3.csv( csvUrl, function( rows ) {
    d3.select("div#preview").html(
      "<b>First row:</b><br/>" + rowToHtml( rows[0] ));
  })
}

d3.select("html")
  .style("height","100%")

d3.select("body")
  .style("height","100%")
  .style("font", "12px sans-serif")

  .append("input")
  .attr("type", "file")
  .attr("accept", ".csv")
  .style("margin", "5px")
  .on("change", function() {
  var file = d3.event.target.files[0];
  if (file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
      var dataUrl = evt.target.result;
      // The following call results in an "Access denied" error in IE.
      previewCsvUrl(dataUrl);
    };
    reader.readAsDataURL(file);
  }
})

d3.select("body")
  .append("div")
  .attr("id", "preview")
  .style("margin", "5px")

// Initialize with csv file from server
previewCsvUrl("test.csv")
Source by bl.ocks.org #
 
PREVIOUS NEXT
Tagged: #loading #local #csv #file
ADD COMMENT
Topic
Name
9+7 =