1.1 数据生成
基于新生数据(DeepSeek 合成)讲解异步加载与字段映射
Explains asynchronous loading and field mapping using the new-student dataset (synthesized by DeepSeek)
你的原案例
Your Original Case
本章采用中国石油大学(华东)2025 级新生数据,阐述数据的特性。为了防止数据隐私泄露,数据采用 DeepSeek 大模型生成。数据文件 data/newstudent.csv,含 80 条记录。
This chapter uses the 2025 new-student data from China University of Petroleum (East China) to illustrate the characteristics of data. To prevent data privacy leaks, the data was generated by the DeepSeek large language model. The data file data/newstudent.csv contains 80 records.
嵌入理论:AI 合成数据的可信度边界
Embedded Theory: The Trustworthiness Boundary of AI-Synthesized Data
数据质量四维度(Wang & Strong 1996):准确性 / 完整性 / 一致性 / 时效性。
Four Dimensions of Data Quality (Wang & Strong 1996): Accuracy / Completeness / Consistency / Timeliness.
DeepSeek 生成数据:
Data Generated by DeepSeek:
- ✅ 完整性:字段齐全
- ✅ Completeness: all fields present
- ⚠️ 准确性:姓名/籍贯是虚构的,不能用作真实人口分析
- ⚠️ Accuracy: names/hometowns are fictional and cannot be used for real demographic analysis
- ⚠️ 采样偏差:模型倾向"看起来合理"的分布(如民族偏汉族、籍贯偏大城市)
- ⚠️ Sampling bias: the model tends toward 'plausible-looking' distributions (e.g., over-representing the Han ethnicity, favoring large cities for hometowns)
课堂要点:教学场景使用合成数据没问题,但必须标注"合成数据",避免学生误以为真实分布。
Classroom Takeaway: Using synthetic data in teaching is fine, but it must be labeled as 'synthetic data' to prevent students from mistaking it for a real distribution.
关键代码 ① 字段名映射
Key Code ① Field Name Mapping
中文列名映射成英文变量名 —— 避免 JS 中带中文 key 引发语法/编码隐患。
Map Chinese column names to English variable names — to avoid syntax/encoding issues caused by Chinese keys in JS.
⭐ // 关键点 ①:字段映射 —— 中文 → 英文
const COLUMN_MAP = {
"序号": "id",
"姓名": "name",
"性别": "gender",
"生日": "birthday",
"年龄": "age",
"班级": "class",
};
关键点解释:为什么不用中文 key? (1) JS 引擎对中文标识符的容错性差(老浏览器报错);(2) 后端、JSON、URL 编码都可能引入乱码; (3) IDE 自动补全、搜索、重构工具对中文支持不一致。 最佳实践:CSV 用人可读的中文名,JS 内部统一英文变量。
Key-Point Explanation: Why not use Chinese keys? (1) JS engines have poor tolerance for Chinese identifiers (older browsers throw errors); (2) backends, JSON, and URL encoding may introduce mojibake; (3) IDE autocomplete, search, and refactoring tools have inconsistent support for Chinese. Best Practice: Use human-readable Chinese names in CSV, and use unified English variables internally in JS.
关键代码 ② 类型转换函数
Key Code ② Type Conversion Functions
CSV 加载后所有字段都是字符串,需要把序号/年龄 → Number,生日 → Date 对象。
After CSV loading, all fields are strings; you need to convert the index/age to Number and the birthday to a Date object.
⭐ // 关键点 ②:类型转换函数 —— 用户原文「推荐方式」
function transformRow(d) {
return {
id: +d["序号"], // 一元 + 把字符串转 Number
name: d["姓名"].trim(), // trim() 去掉首尾空白
birthday: new Date(d["生日"]), // 字符串 → Date 对象
age: +d["年龄"],
class: d["班级"].trim(),
};
}
关键点解释:
+d["年龄"] 是一元加运算符,是 JS 里把字符串转数字的最快写法(比 Number() / parseInt() 都短);
new Date(str) 能解析 "2007-08-15" 这类 ISO 格式,但对 "2007/8/15" 兼容性差,要用 d3.timeParse("%Y-%m-%d") 兜底。
坑:没有 trim() 的话,"软件工程1班 " 里的空格会让后续过滤/聚合出错。
Key-Point Explanation:
+d["年龄"] is the unary plus operator, the fastest way in JS to convert a string to a number (shorter than Number() / parseInt());
new Date(str) can parse ISO formats like "2007-08-15", but has poor compatibility with "2007/8/15", so fall back to d3.timeParse("%Y-%m-%d").
Pitfall: Without trim(), the trailing space in "软件工程1班 " will cause later filtering/aggregation to fail.
关键代码 ③ d3.csv 第二参直接传 row 回调
Key Code ③ Pass the row callback directly as the second argument of d3.csv
⭐ // 关键点 ③:一行完成加载 + 转换(用户原文「代码示例 1」)
const data = await d3.csv("data/newstudent.csv", transformRow);
// 验证类型确实转换了:
console.log(typeof data[0].age); // "number"
console.log(data[0].birthday instanceof Date); // true
关键点解释:d3.csv 是异步函数,必须 await。
第二参的 row 回调会在每一行解析后立即调用——比先 await d3.csv(...) 再 .map(transformRow) 更省内存(边读边转)。
异步加载的意义:不阻塞页面渲染,用户能继续交互。
Key-Point Explanation: d3.csv is an asynchronous function and must be awaited.
The row callback in the second argument is invoked immediately after each row is parsed — which saves memory compared to first await d3.csv(...) then .map(transformRow) (transform while reading).
Significance of Asynchronous Loading: It does not block page rendering, so the user can keep interacting.
点击「重新加载」查看 CSV 异步加载、字段映射、类型转换 → 输出转换后表格 + 控制台日志。
Click 'Reload' to see the asynchronous CSV loading, field mapping, and type conversion → output the transformed table + console logs.