Undefined data while loading from CSV for specific columns in D3How do I return the response from an asynchronous call?Save PL/pgSQL output from PostgreSQL to a CSV fileAdding new “columns” to csv data file in TclHow to import CSV file data into a PostgreSQL table?csv to array in d3.jsPython: Iterating through a csv in a while loopCopy data from csv into array in D3Add time-stamp data from multiple csv files to highchartNumpy: Read csv, deal with undefined valuesLoading csv data into HTML divparse data from csv and visualize it in graph javascript

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

Is it a bad idea to to run 24 tap and shock lands in standard

Electricity free spaceship

Why was this person allowed to become Grand Maester?

Print lines between start & end pattern, but if end pattern does not exist, don't print

Why is a common reference string needed in zero knowledge proofs?

US doctor working in Tripoli wants me to open online account

Artificer Creativity

Russian word for a male zebra

What is the color of artificial intelligence?

Is it legal for a bar bouncer to confiscate a fake ID

Should I ask for an extra raise?

Heap allocation on microcontroller

Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

Fixing obscure 8080 emulator bug?

Has there been a multiethnic Star Trek character?

Writing an augmented sixth chord on the flattened supertonic

Extreme flexible working hours: how to get to know people and activities?

What ways have you found to get edits from non-LaTeX users?

Why can my keyboard only digest 6 keypresses at a time?

Is it possible to have a wealthy country without a middle class?

Teaching a class likely meant to inflate the GPA of student athletes

Bb13b9 confusion



Undefined data while loading from CSV for specific columns in D3


How do I return the response from an asynchronous call?Save PL/pgSQL output from PostgreSQL to a CSV fileAdding new “columns” to csv data file in TclHow to import CSV file data into a PostgreSQL table?csv to array in d3.jsPython: Iterating through a csv in a while loopCopy data from csv into array in D3Add time-stamp data from multiple csv files to highchartNumpy: Read csv, deal with undefined valuesLoading csv data into HTML divparse data from csv and visualize it in graph javascript






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I am trying to read data from a csv file, and I want to store the data of each column in an array as in the code below. The problem that I get and I didn't know how to fix is that all the values are defined inside the brackets, but once I try to deal with the arrays else where, the data is undefined. Any ideas about what is going wrong?
My version of D3 is v3.



<script>

var computerid = [];
var timestamp = [];
var percentage = [];

d3.csv("cpu-util.csv", function(data)
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp;
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]); //prints value
console.log(timestamp[i]);
console.log(percentage[i]);


);

console.log(computerid[1]); //here, it prints undefined although inside the loop it prints values


Part of the csv file:



Computer_ID, timestamp, value, Percentage



1, 01-07-11 0:00, 0.8, 8










share|improve this question
























  • There is not enough information to provide an answer for this question. Please consider providing more information.

    – Coola
    Mar 24 at 18:38











  • I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

    – Maha Alrasheed
    Mar 24 at 19:00











  • Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

    – Coola
    Mar 24 at 19:05











  • It wasn't declared inside any function, all the arrays are global variables.

    – Maha Alrasheed
    Mar 24 at 19:09











  • Could you post a part of the csv file as well. To see the data structure.

    – Coola
    Mar 24 at 19:10

















-1















I am trying to read data from a csv file, and I want to store the data of each column in an array as in the code below. The problem that I get and I didn't know how to fix is that all the values are defined inside the brackets, but once I try to deal with the arrays else where, the data is undefined. Any ideas about what is going wrong?
My version of D3 is v3.



<script>

var computerid = [];
var timestamp = [];
var percentage = [];

d3.csv("cpu-util.csv", function(data)
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp;
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]); //prints value
console.log(timestamp[i]);
console.log(percentage[i]);


);

console.log(computerid[1]); //here, it prints undefined although inside the loop it prints values


Part of the csv file:



Computer_ID, timestamp, value, Percentage



1, 01-07-11 0:00, 0.8, 8










share|improve this question
























  • There is not enough information to provide an answer for this question. Please consider providing more information.

    – Coola
    Mar 24 at 18:38











  • I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

    – Maha Alrasheed
    Mar 24 at 19:00











  • Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

    – Coola
    Mar 24 at 19:05











  • It wasn't declared inside any function, all the arrays are global variables.

    – Maha Alrasheed
    Mar 24 at 19:09











  • Could you post a part of the csv file as well. To see the data structure.

    – Coola
    Mar 24 at 19:10













-1












-1








-1








I am trying to read data from a csv file, and I want to store the data of each column in an array as in the code below. The problem that I get and I didn't know how to fix is that all the values are defined inside the brackets, but once I try to deal with the arrays else where, the data is undefined. Any ideas about what is going wrong?
My version of D3 is v3.



<script>

var computerid = [];
var timestamp = [];
var percentage = [];

d3.csv("cpu-util.csv", function(data)
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp;
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]); //prints value
console.log(timestamp[i]);
console.log(percentage[i]);


);

console.log(computerid[1]); //here, it prints undefined although inside the loop it prints values


Part of the csv file:



Computer_ID, timestamp, value, Percentage



1, 01-07-11 0:00, 0.8, 8










share|improve this question
















I am trying to read data from a csv file, and I want to store the data of each column in an array as in the code below. The problem that I get and I didn't know how to fix is that all the values are defined inside the brackets, but once I try to deal with the arrays else where, the data is undefined. Any ideas about what is going wrong?
My version of D3 is v3.



<script>

var computerid = [];
var timestamp = [];
var percentage = [];

d3.csv("cpu-util.csv", function(data)
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp;
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]); //prints value
console.log(timestamp[i]);
console.log(percentage[i]);


);

console.log(computerid[1]); //here, it prints undefined although inside the loop it prints values


Part of the csv file:



Computer_ID, timestamp, value, Percentage



1, 01-07-11 0:00, 0.8, 8







csv d3.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 19:24







Maha Alrasheed

















asked Mar 24 at 18:34









Maha AlrasheedMaha Alrasheed

114




114












  • There is not enough information to provide an answer for this question. Please consider providing more information.

    – Coola
    Mar 24 at 18:38











  • I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

    – Maha Alrasheed
    Mar 24 at 19:00











  • Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

    – Coola
    Mar 24 at 19:05











  • It wasn't declared inside any function, all the arrays are global variables.

    – Maha Alrasheed
    Mar 24 at 19:09











  • Could you post a part of the csv file as well. To see the data structure.

    – Coola
    Mar 24 at 19:10

















  • There is not enough information to provide an answer for this question. Please consider providing more information.

    – Coola
    Mar 24 at 18:38











  • I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

    – Maha Alrasheed
    Mar 24 at 19:00











  • Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

    – Coola
    Mar 24 at 19:05











  • It wasn't declared inside any function, all the arrays are global variables.

    – Maha Alrasheed
    Mar 24 at 19:09











  • Could you post a part of the csv file as well. To see the data structure.

    – Coola
    Mar 24 at 19:10
















There is not enough information to provide an answer for this question. Please consider providing more information.

– Coola
Mar 24 at 18:38





There is not enough information to provide an answer for this question. Please consider providing more information.

– Coola
Mar 24 at 18:38













I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

– Maha Alrasheed
Mar 24 at 19:00





I have updated the question after figuring out somethings. The problem is that my values are defined inside the loop, but outside it, the values are not defined.

– Maha Alrasheed
Mar 24 at 19:00













Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

– Coola
Mar 24 at 19:05





Yes the scope of the variable could be a major issue. In JS, the variable scope would be defined based on where it is declared.

– Coola
Mar 24 at 19:05













It wasn't declared inside any function, all the arrays are global variables.

– Maha Alrasheed
Mar 24 at 19:09





It wasn't declared inside any function, all the arrays are global variables.

– Maha Alrasheed
Mar 24 at 19:09













Could you post a part of the csv file as well. To see the data structure.

– Coola
Mar 24 at 19:10





Could you post a part of the csv file as well. To see the data structure.

– Coola
Mar 24 at 19:10












1 Answer
1






active

oldest

votes


















0














Your CSV data needs to be in the correct format. There are a few unnecessary spaces which makes it difficult to parse as it includes the spaces in the header names on the basis of which it keeps the property names in the objects.



cpu-util.csv should be



Computer_ID,timestamp,value,Percentage
1,01-07-11 0:00,0.8,8


In addition d3.js parses the data keeping the header labels. therefore computerid array should be filled using the Computer_ID property of the data. Thus your code should be something like:



<script>
var timestamp = [],
computerid = [],
percentage = [];

d3.csv("cpu-util.csv", function(data)
console.log(data); //see the data structure
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp; //use the property names.
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]);
console.log(timestamp[i]);
console.log(percentage[i]);

console.log(computerid[0]); //this will appear as it is within the function and
//the array is filled by the time this line is run
);

console.log(computerid[0]); //this will be undefined due to the asynchronous nature of javascript.
//This line of code runs before the for loop within the function
</script>


If you see the console log, the console.log(computerid[0]) will appear first in the log before the other three in the function, due to the asynchronous nature of javascript. There are ways in which you can chain functions to make them synchronous using either Async/Await or Promises.



Also d3.js parses all the information as strings. So number values such as Percentage would need to be converted to number data types using a function. Just keep this in mind.






share|improve this answer

























  • Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

    – Maha Alrasheed
    Mar 26 at 19:20











  • Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

    – Coola
    Mar 26 at 19:26











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327142%2fundefined-data-while-loading-from-csv-for-specific-columns-in-d3%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your CSV data needs to be in the correct format. There are a few unnecessary spaces which makes it difficult to parse as it includes the spaces in the header names on the basis of which it keeps the property names in the objects.



cpu-util.csv should be



Computer_ID,timestamp,value,Percentage
1,01-07-11 0:00,0.8,8


In addition d3.js parses the data keeping the header labels. therefore computerid array should be filled using the Computer_ID property of the data. Thus your code should be something like:



<script>
var timestamp = [],
computerid = [],
percentage = [];

d3.csv("cpu-util.csv", function(data)
console.log(data); //see the data structure
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp; //use the property names.
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]);
console.log(timestamp[i]);
console.log(percentage[i]);

console.log(computerid[0]); //this will appear as it is within the function and
//the array is filled by the time this line is run
);

console.log(computerid[0]); //this will be undefined due to the asynchronous nature of javascript.
//This line of code runs before the for loop within the function
</script>


If you see the console log, the console.log(computerid[0]) will appear first in the log before the other three in the function, due to the asynchronous nature of javascript. There are ways in which you can chain functions to make them synchronous using either Async/Await or Promises.



Also d3.js parses all the information as strings. So number values such as Percentage would need to be converted to number data types using a function. Just keep this in mind.






share|improve this answer

























  • Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

    – Maha Alrasheed
    Mar 26 at 19:20











  • Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

    – Coola
    Mar 26 at 19:26















0














Your CSV data needs to be in the correct format. There are a few unnecessary spaces which makes it difficult to parse as it includes the spaces in the header names on the basis of which it keeps the property names in the objects.



cpu-util.csv should be



Computer_ID,timestamp,value,Percentage
1,01-07-11 0:00,0.8,8


In addition d3.js parses the data keeping the header labels. therefore computerid array should be filled using the Computer_ID property of the data. Thus your code should be something like:



<script>
var timestamp = [],
computerid = [],
percentage = [];

d3.csv("cpu-util.csv", function(data)
console.log(data); //see the data structure
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp; //use the property names.
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]);
console.log(timestamp[i]);
console.log(percentage[i]);

console.log(computerid[0]); //this will appear as it is within the function and
//the array is filled by the time this line is run
);

console.log(computerid[0]); //this will be undefined due to the asynchronous nature of javascript.
//This line of code runs before the for loop within the function
</script>


If you see the console log, the console.log(computerid[0]) will appear first in the log before the other three in the function, due to the asynchronous nature of javascript. There are ways in which you can chain functions to make them synchronous using either Async/Await or Promises.



Also d3.js parses all the information as strings. So number values such as Percentage would need to be converted to number data types using a function. Just keep this in mind.






share|improve this answer

























  • Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

    – Maha Alrasheed
    Mar 26 at 19:20











  • Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

    – Coola
    Mar 26 at 19:26













0












0








0







Your CSV data needs to be in the correct format. There are a few unnecessary spaces which makes it difficult to parse as it includes the spaces in the header names on the basis of which it keeps the property names in the objects.



cpu-util.csv should be



Computer_ID,timestamp,value,Percentage
1,01-07-11 0:00,0.8,8


In addition d3.js parses the data keeping the header labels. therefore computerid array should be filled using the Computer_ID property of the data. Thus your code should be something like:



<script>
var timestamp = [],
computerid = [],
percentage = [];

d3.csv("cpu-util.csv", function(data)
console.log(data); //see the data structure
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp; //use the property names.
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]);
console.log(timestamp[i]);
console.log(percentage[i]);

console.log(computerid[0]); //this will appear as it is within the function and
//the array is filled by the time this line is run
);

console.log(computerid[0]); //this will be undefined due to the asynchronous nature of javascript.
//This line of code runs before the for loop within the function
</script>


If you see the console log, the console.log(computerid[0]) will appear first in the log before the other three in the function, due to the asynchronous nature of javascript. There are ways in which you can chain functions to make them synchronous using either Async/Await or Promises.



Also d3.js parses all the information as strings. So number values such as Percentage would need to be converted to number data types using a function. Just keep this in mind.






share|improve this answer















Your CSV data needs to be in the correct format. There are a few unnecessary spaces which makes it difficult to parse as it includes the spaces in the header names on the basis of which it keeps the property names in the objects.



cpu-util.csv should be



Computer_ID,timestamp,value,Percentage
1,01-07-11 0:00,0.8,8


In addition d3.js parses the data keeping the header labels. therefore computerid array should be filled using the Computer_ID property of the data. Thus your code should be something like:



<script>
var timestamp = [],
computerid = [],
percentage = [];

d3.csv("cpu-util.csv", function(data)
console.log(data); //see the data structure
for (var i = 0; i < data.length; i++)
timestamp[i] = data[i].timestamp; //use the property names.
computerid[i] = data[i].Computer_ID;
percentage[i] = data[i].Percentage;
console.log(computerid[i]);
console.log(timestamp[i]);
console.log(percentage[i]);

console.log(computerid[0]); //this will appear as it is within the function and
//the array is filled by the time this line is run
);

console.log(computerid[0]); //this will be undefined due to the asynchronous nature of javascript.
//This line of code runs before the for loop within the function
</script>


If you see the console log, the console.log(computerid[0]) will appear first in the log before the other three in the function, due to the asynchronous nature of javascript. There are ways in which you can chain functions to make them synchronous using either Async/Await or Promises.



Also d3.js parses all the information as strings. So number values such as Percentage would need to be converted to number data types using a function. Just keep this in mind.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 19:49

























answered Mar 24 at 19:41









CoolaCoola

410514




410514












  • Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

    – Maha Alrasheed
    Mar 26 at 19:20











  • Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

    – Coola
    Mar 26 at 19:26

















  • Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

    – Maha Alrasheed
    Mar 26 at 19:20











  • Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

    – Coola
    Mar 26 at 19:26
















Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

– Maha Alrasheed
Mar 26 at 19:20





Thank you so much! I didn't know about this asynchronous behavior. Again, thanks!

– Maha Alrasheed
Mar 26 at 19:20













Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

– Coola
Mar 26 at 19:26





Please mark the question as answered if you are happy with the answer and also up vote it if you like it. stackoverflow.com/help/someone-answers

– Coola
Mar 26 at 19:26



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327142%2fundefined-data-while-loading-from-csv-for-specific-columns-in-d3%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript