javascript - How to print in console from d3 function
问题描述
So I have this function to draw let's say rectangles:
draw(data) {
this.canvas.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', (d) => d.x)
.attr('y', (d) => d.y)
.attr('width', (d) => d.width)
.attr('height', (d) => d.height)
.style('fill', (d) => d.color);
}
and I want to print in console d.x and d.y. I wrote this function:
printData(data) {
this.canvas.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', function(d) { console.log(d.x); });
}
but it isn't printing anything in console. Can anybody help me to tell what am I doing wrong here?
解决方案
我假设你在printData
之后打电话draw
。如果是这种情况,您将看不到任何内容,因为您的“输入”选择为空(因此不会enter()
调用之后的任何内容)。
在这种情况下,解决方案只是移动console.log
todraw
或使用each()
in printData
:
printData(data) {
this.canvas.selectAll('rect')
.each(function(d) {
console.log(d.x);
});
}
推荐阅读
- c# - 从 Caliburn.Micro、WPF、MVVM 中的另一个窗口获取信息
- ionic-framework - 带有按钮和复选框的离子列表在同一行
- reactjs - React 不会在 redux 存储更改时重新渲染地图功能(使用 MaterializeCss 选择选项)
- git - GIT - 如何撤消丢弃的提交
- amcharts - 在 xy 图表 amcharts 中为每个图寻找单个工具提示
- python - 在python函数中返回多个值
- .htaccess - .htaccess RewriteRule 查询字符串
- c# - 使用 BigInteger 的 C# 字节数组计算无法正常工作
- python - Plotly 中的 Choropleth 地图:颜色显示不正确
- docker - 谷歌云身份验证 json 密钥文件