D3.js版本6:加载CSV并更改变量类型 [英] D3.js version 6: Loading a CSV and changing Variable types
问题描述
我对D3.js(第6版)比较陌生.我可以成功加载以下.csv" prices.csv(请参见末尾的github回购):
I am relatively new to D3.js (version 6). I am successfully able to load the following .csv "prices.csv (see github repo at the end):
但是我不能更改变量类型.
However I can not change the variable types.
我使用了以下代码:
var parseDate = d3.timeParse("%m/%d/%Y");
var rowConverter = function(d){
return{
month: parseDate(d.month),
price: Number(d.price.trim().slice(1))
};
}
d3.csv("prices.csv", rowConverter, function(data){
console.log(data);
});
控制台日志显示月份和价格变量仍然是字符串:
The console log says the month and price variable are still strings:
{月:"2003年1月1日",价格:"54美元"}月:"2003年1月1日";价格:"$ 54"
{month: "1/1/2003", price: "$54"} month: "1/1/2003" price: "$54"
注意:我已经将.csv上传到了我的github页面! https://raw.githubusercontent.com/AhmadMobin/D3-Learning/main/prices.csv
Note: I have uploaded the .csv on my github page! https://raw.githubusercontent.com/AhmadMobin/D3-Learning/main/prices.csv
非常感谢您!
推荐答案
var parseDate = d3.timeParse("%m/%d/%Y");
d3.csv("https://raw.githubusercontent.com/AhmadMobin/D3-Learning/main/prices.csv", function(d) {
return {
month: parseDate(d.month),
price: Number(d.price.trim().slice(1))
}
}).then(function(data) {
console.log(data)
});
<script src="https://d3js.org/d3.v6.min.js"></script>
这篇关于D3.js版本6:加载CSV并更改变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!