Lesson 2 · Lección 2 · Cargando Datos de CSV
Our visualization gets its information from a CSV file — a spreadsheet saved as plain text. In this lesson you'll see how the code fetches that file and turns numbers into 3D bars.
Nuestra visualización obtiene información de un archivo CSV — una hoja de cálculo guardada como texto simple.
CSV stands for Comma-Separated Values. It's just a text file where each line is a row and commas separate columns. Open it in any text editor or spreadsheet app.
# This is what our data looks like in the CSV file: Label,Population,Percent,Fiscal Year Spanish Total,87798,21.5,2016-2017 Spanish Limited English,62341,15.2,2016-2017 Chinese Total,28543,6.9,2016-2017
Real Oakland data! 87,798 people speak Spanish — that's 21.5% of Oakland.
| Label | Population | Percent | Fiscal Year |
|---|---|---|---|
| Spanish Total | 87,798 | 21.5% | 2016-2017 |
| Spanish Limited English | 62,341 | 15.2% | 2016-2017 |
| Chinese Total | 28,543 | 6.9% | 2016-2017 |
| Chinese Limited English | 18,234 | 4.4% | 2016-2017 |
Here's the journey data takes from the CSV file to a 3D pedestal:
Open docs/js/csvParser.js. The key function is
parseCSV(). Here's a simplified version:
// 1. Tell the browser which year we want const YEAR_FILTER = '2016-2017'; // 2. Download the CSV file async function parseCSV(path) { const response = await fetch(path); const text = await response.text(); // 3. Split into lines, then split each line by comma const lines = text.split('\n'); const rows = lines.map(line => line.split(',')); // 4. Filter: only keep rows from our year return rows.filter(row => row[3] === YEAR_FILTER); }
async / await — downloading
a file takes time. These keywords tell JavaScript "wait here until the
download finishes before continuing." Without them, the code would try
to draw bars before the data arrived!
After loading, each row gets a category based on keywords in its Label column. This determines which color and position it gets in the 3D scene:
let category; const label = row.label.toLowerCase(); if (label.includes('bilingual pcp')) { category = 'bilingual_pcp'; } else if (label.includes('limited english')) { category = 'limited_english'; } else if (label.includes('total')) { category = 'total'; } else { category = 'other'; }
Want to add a new language? Add a new else if
block with a keyword that appears in your data's Label column.
We'll cover this more in later lessons!
¿Quieres agregar un nuevo idioma? Agrega un nuevo bloque else if.
The most important step: turn a raw population number into a height that makes visual sense in 3D space.
// The tallest bar (Spanish Total: 87,798 people) gets MAX height const MAX_POPULATION = 87798; const MAX_PEDESTAL_HEIGHT = 5.0; // units in 3D space const MIN_PEDESTAL_HEIGHT = 0.35; // so tiny bars still show // Scale: 87,798 people = 5.0 height units // 1,000 people = 5.0 * (1000/87798) = 0.057 units const height = MIN_PEDESTAL_HEIGHT + (population / MAX_POPULATION) * (MAX_PEDESTAL_HEIGHT - MIN_PEDESTAL_HEIGHT);
This is called linear scaling — the same math used in every chart you've ever seen, just applied to 3D height!
Open docs/data/Equal_Access_Accommodations_20260410.csv
in a text editor. Find the row for "Vietnamese" or another language.
What's the population number? How tall do you think its bar should be
compared to Spanish?
Abre el archivo CSV. Busca una fila para otro idioma. ¿Cuánta gente habla ese idioma en Oakland?