liveBook · Manning

Data conversion

As explained in D3.js in Action, Third Edition

Type conversion is the transformation of a value from one data type to another, such as from a string to a number.

Data conversion transforms a value from one data type into another. When D3 loads a CSV file, numeric values commonly arrive as strings, so counts must be converted before they drive a visualization. section 3.2.2

Convert counts while loading CSV data

Use d3.csv()'s row conversion callback to return only the fields needed by the visualization and convert count with the unary + operator: d3.csv("../data/data.csv", d => { return { technology: d.technology, count: +d.count }; }); The returned key-value pairs are the fields available after the dataset loads. If many columns must remain, format the complete dataset afterward instead. section 3.2.2

Figure 3.9 The third step of the D3 data workflow consists initially of formatting the data so that it’s ready to use when you start building your visualization.
figure

Follow data conversion as the formatting step that prepares imported data for visualization. section 3.2.2

How D3.js in Action, Third Edition covers it

How deep it goes
Explains row-by-row conversion in d3.csv(), retained keys, and formatting the complete dataset as an alternative.
Examples
Uses a technology-and-count CSV row, console inspection, and unary-plus conversion.
What it assumes
Assumes CSV or JSON data and basic JavaScript object access through dot notation.
What you can do after
Apply it via d3.csv() to convert imported numeric strings and retain only fields needed for a visualization.
How it connects
Connects conversion to CSV loading, JSON data, callbacks, asynchronous loading, and visualization construction.

Also explained in

Compare how 21 books explain data conversion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.