问题
Murray (2017) suggests the following code for loading a csv file and parsing columns usign D3.js V4. This code no longer works in V5. How can it be restructured in order to work? Thanks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Line chart</title>
<script type="text/javascript" src="/../../d3.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var dataset;
//Function for converting CSV values from strings to Dates and numbers
var rowConverter = function(d) {
return {
date: new Date(+d.year, (+d.month - 1)), //Make a new Date object for each year + month
average: parseFloat(d.average) //Convert from string to float
};
}
//Load in data
d3.csv("mauna_loa_co2_monthly_averages.csv", rowConverter, function(data) {
var dataset = data;
//Print data to console as table, for verification
console.table(dataset, ["date", "average"]);
}
回答1:
For v5, d3-fetch is your friend as d3-request has been deprecated.
For instance:
d3.csv("/path/to/file.csv", rowConverter).then(function(data){ do whatever })
来源:https://stackoverflow.com/questions/50110717/parse-and-upload-a-csv-file-in-d3-js-v5