Solved

Javascript d3, get first row only

Posted on 2014-12-18
8
393 Views
Last Modified: 2014-12-18
Hello,

I have the following code and I would like to get just the titles not the other values.

Can you please let me know how to edit this to work OR if there is a better way.

Please note that the csv delimiter is different in a couple of my files.

<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
/*
Example cars.csv:
Year;Make;Model;Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
*/
d3.csv('cars.csv', function(csv) {
	csv.forEach(function(row) {
		document.write(Object.keys(row));
	});
});
</script>

Open in new window


Thank you!
Matt
0
Comment
Question by:movieprodw
  • 4
  • 4
8 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40508389
What do you mean the titles - Year;Make;Model;Length ???
The code works but the titles should be seperated by commas.
0
 
LVL 1

Author Comment

by:movieprodw
ID: 40508409
Sorry, the column names
0
 
LVL 58

Expert Comment

by:Gary
ID: 40508415
Then what is wrong with what you have?
If you mean you have semicolons then use
https://github.com/mbostock/dsv
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:movieprodw
ID: 40508424
Well I JUST want the first row with the names, I do not want it to repeat for every row, I am good at php but not javascript.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40508439
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
d3.csv('cars.csv', function(data){
	d3.keys(data[0]).forEach(function(item){
		console.log(item)
	})
});
</script>

Open in new window

0
 
LVL 1

Author Comment

by:movieprodw
ID: 40508448
Hello Gary,

That worked.

I am confused because the page never stops loading, any ideas?

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>RSS Feed Manager</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>

<body>

<script type="text/javascript">
d3.csv('cars.csv', function(data){
	d3.keys(data[0]).forEach(function(item){
		document.write(item);
	})
});
</script> 

</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40508454
d3.csv('cars.csv', function(data){
      d3.keys(data[0]).forEach(function(item){
            document.write(item);
      })
     document.close()
});
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 40508561
You are the man!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
If condition on Html with Asp 11 27
Make Session back to empty 9 30
forgetful about js objects 8 46
How to get total count of passed in subject based on marks? 7 11
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question