javascript - 使用 d3.js 加载 csv 数据时出现问题
问题描述
所以我一直在尝试为我自己的项目实现这个2D 可视化,我在加载 CSV 数据而不是随机生成点时遇到问题。我的代码几乎与上面的链接相同,除了这个:
<script>
let data_points = [];
d3.csv("mnist2D.csv", function (d) {
d.forEach(function (d,i) {
data_points[i] = {
x: +d.x,
y: +d.y,
name: 'Knowledge Element ' + i,
group: +d.label,
radius: +d.radius,
frequency: +d.frequency
};
})
...remainder of the script
//d3.csv end
})
</script>
我在浏览器控制台中返回的错误是
Uncaught TypeError: o.beforesend is undefined
send 'https://d3js.org/d3.v3.min.js:1'
n 'https://d3js.org/d3.v3.min.js:1'
Cn 'https://d3js.org/d3.v3.min.js:1'
e 'https://d3js.org/d3.v3.min.js:3'
<anonymous> 'https://...../test2d:22' //Line of the d3.csv call
此代码与不使用 d3-zoom 和工具提示的 3D 版本完美配合,但在此版本中中断。我假设它与 d3.csv 异步有关,但我不是经验丰富的 JS 开发人员,也不知道如何调试这样的问题。使用 d3.js v5 会解决这个问题吗?任何帮助,将不胜感激。太感谢了!
解决方案
很有趣地解决了我自己的问题。切换到 d3 v5 并且加载数据没有问题。
<script>
// Load in data (asynchronously)
d3.csv("mnist2D.csv", function (data) {
return {
x: +data.x*50,
y: +data.y*50,
name: data.name,
label: +data.label,
radius: +data.radius,
frequency: +data.frequency
};
}).then(function(data_points) {
...remainder of the script
//d3.csv end
})
</script>
推荐阅读
- swift - Swift:播放本地视频并实时应用 CIFilter
- reactjs - 在我的 React 组件中使用 TinyMCE 时更改未显示在状态中
- python - Databricks 多选下拉选择/取消选择所有值
- reportportal - 在报告门户中绘制单个测试的响应时间
- twincat - TwinCAT 在发布或调试模式下构建项目有什么不同吗?
- php - 如何从 PHP 代码调用复杂的 Oracle 过程
- node.js - Discord Bot如何为自动进入服务器的新用户分配角色
- c++ - 由于缺少 dll 文件,Qt 在 CLion 中不起作用
- r - 如何使用 data.table 和 lubridate 检查多个日期是否在一个区间内?
- flutter - 预期 3 个位置参数,但找到了 2 个