在D3.js V5

时间:2018-05-01 02:38:50

标签: csv d3.js upload

Murray(2017)建议使用以下代码加载csv文件并解析列使用D3.js V4。此代码不再适用于V5。为了工作,如何进行重组?感谢

<!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 个答案:

答案 0 :(得分:3)

对于v5,d3-fetch是您的朋友,d3-request已被弃用。

例如:

d3.csv("/path/to/file.csv", rowConverter).then(function(data){ do whatever })