D3.js Advanced Series-1.2 "garbled solution when reading a CSV file

Source: Internet
Author: User

When you use D3.csv to read a CSV file in D3, there are sometimes garbled problems. How to solve it?

1. Garbled problem

When reading a xxx.csv file using D3.csv, there is no problem if the Xxx.csv file uses UTF-8 encoding. Of course, individuals think that using UTF-8 encoding as much as possible, you can use the same code within the national text .

However, if the Xxx.csv file is using UTF-8 encoding, you may get garbled when you open it using Microsoft Excel, because the domestic Excel default is open with GB2312, and you cannot select the encoding when you open it (OpenOffice does not have this question Questions). Of course, there are methods in Excel that can be opened with UTF-8 encoding. Specific methods can be found on the Internet.

GB2312 and GB18030 are commonly used in the domestic code, if the CSV file is saved with these two encodings, then Excel can be opened directly without garbled. However, if used, read with D3.csv, but also in the visual time garbled, this is the problem.

2. Workaround

The solution is simple, but if you do not know it, it is very much to worry about.

Word, workaround required: in D3, you can manually set what encoding to read the file .

In "Advanced Series 1.0" and "Advanced Series 1.1", using the D3.csv and D3.TSV two functions, it is also mentioned that the difference is only the delimiter is different. Also mentioned, they are essentially D3.DSV functions, namely:

  D3.csv = D3.DSV (",", "text/csv");  D3.TSV = D3.DSV ("", "text/tab-separated-values");

In the second parameter of D3.DSV, you can actually add a coded, shape like:

var csv = D3.DSV (",", "text/csv;charset=gb2312"), var TSV = D3.DSV ("", "text/tab-separated-values;charset=gb2312");

In this way, you can define your own CSV and TSV read functions, which are encoded as above. Use a method of the form such as:

CSV ("Xxx.csv", function (error,csvdata) {}TSV ("XXX.TSV", function (Error,tsvdata) {}

So, garbled problem can be solved. Thank you for reading.

Document Information

    • Copyright Notice: Attribution (by)-Non-commercial (NC)-No deduction (ND)
    • Published: October 18, 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.2 "garbled solution when reading a CSV file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.