CSV table files are comma-delimited, and others have a TSV file with Tab tab as the cell delimiter, and others are table files that define other separators. This article explains how to read them in D3.
1. What is the TSV table file?
TSV (Tab Separated Values), the tab-delimited value, and the CSV file are just delimiters inconsistent. It has the following format:
Nameage Zhang 322 Li 424
2. Read the TSV file in D3
The method to read the TSV file in D3 is the same as the CSV file, as long as you change the function name. Here's how:
D3.TSV ("TABLE.TSV", function (error,tsvdata) {console.log (tsvdata); var str = D3.tsv.format (tsvdata); Console.log ( Str.length); Console.log (str);});
As we can see, this is basically the same as the "Advanced Series-1.0", which is described in detail in this article.
3. Read the nature of CSV and TSV in D3
In the above two sections we can see that reading CSV and TSV are surprisingly similar. In fact, they are essentially a function, let's look at the definition of D3 source code:
D3.csv = D3.DSV (",", "text/csv"); D3.TSV = D3.DSV ("", "text/tab-separated-values");
As you can see, they are actually D3.DSV functions. So what is this DSV function about? A DSV can actually read a table file with any character or string as a delimiter . Let's try using the DSV function to read a table file with a semicolon as a delimiter. Suppose you have the following file:
Name;age Zhang San; 22 John Doe; 24
Read the following code:
var DSV = D3.DSV (";", "Text/plain");d sv ("TABLE.DSV", function (Error,dsvdata) {if (Error) Console.log (error); Console.log (Dsvdata);});
First Use D3.DSV (";", "Text/plain"); The delimiter is defined as a semicolon, and then read in the same way as the CSV and TSV files. Note, however, that the second line is no longer required to use D3.DSV because the variable DSV is already a function.
4. Concluding remarks
CSV files and TSV files are simply delimiters, and their essence is called the DSV function, so the control of the DSV function is critical.
Thank you for reading.
Document Information
- Copyright Notice: Attribution (by)-Non-commercial (NC)-No deduction (ND)
- Published: October 03, 2014
- More content: our D3. JS-Data Visualization special station and CSDN personal blog
- NOTE: Reprint please indicate the source, thank you
"D3.js Advanced Series-1.1" Reading of other table files