Parse and upload a csv file in D3.js V5

☆樱花仙子☆ 提交于 2019-12-08 13:37:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!