简体   繁体   中英

Loading csv file with d3.js v.4

I am new to d3 and javascript and can't work out the following.

I'm trying to load a csv file with d3.csv but encountered a problem.

d3.csv("data2.csv")
    .row(function (d){
        return {
            ax: Number(d.ax.trim().slice(1)),
            ay: Number(d.ay.trim().slice(1))
        };
    })
    .get(function (error, data){
        console.log(data)
    });

My csv file is

ax,ay,az 
1,40,50 
2,41,49 
3,39,52 
4,35,49 
5,34,46 
6,33,45 
7,32,42 
8,30,40 
9,29,32 
10,27,26

console.log is giving me the following:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, columns: Array(3)]
0    :    {ax: 0, ay: 0}
1    :    {ax: 0, ay: 1}
2    :    {ax: 0, ay: 9}
3    :    {ax: 0, ay: 5}
4    :    {ax: 0, ay: 4}
5    :    {ax: 0, ay: 3}
6    :    {ax: 0, ay: 2}
7    :    {ax: 0, ay: 0}
8    :    {ax: 0, ay: 9}
9    :    {ax: 0, ay: 7} 

The results for ax are 0 which is incorrect and ay is obviously also incorrect. How can I get the numbers correct, stored in a array, so that I can continue plotting the data using d3.js version 4?

try this

d3.csv("/data/employees.csv")
  .row(function(d) {
        return {
            age: d.age,
            name: d.name.toUpperCase() // converting name to upper case 
        }; 
   })
  .get(function(data) {
      console.log(data);
  }); 

enter link description here

d3.csv('https://gist.githubusercontent.com/xrd/9a73ccf7ac1e41b728202f3c70cdb959/raw/9c3a1bde4dee9f0d5f80e705bb4f25353ea6b398/test.csv')
            .row(function (d){
                console.log( d );
                return {
                    ax: Number(d.ax),
                    ay: Number(d.ay)
                };
            })
            .get(function (error, data){
                console.log(data)
            });

You can see it here: http://jsfiddle.net/g3xfbL9m/9/ and, if you open the console, can see it output to the console with the correct values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM