D3 - 未捕获的类型错误:无法调用未定义的方法“文本”
D3 - Uncaught TypeError: Cannot call method 'text' of undefined
我试图绘制美国历史上过去30年的所有重大事件。这是代码片段 -
var mevent = svg.append("text")
.attr("class", "year event")
.attr("text-anchor", "end")
.attr("y", height - 450)
.attr("x", width - 300)
.text("mevent");
var mevents =
{"1987":[""],
"1988":["Pan Am Flight 103", "US President Election"],
"1989":["Cyclone in NE", "Bay Area Earth Quake"],
"1990":["Tornado in MW", "Gulf War 1"],
"1991":["Rodney King LA"],
"1992":["Cyclone in SE", "US President Election"],
"1993":["Great Flood of 1993 in MidWest", "Blizzard in NorthEast"],
"1994":["Earthquake in LA"],
"1995":["Flood in SouthEast", "Heat Wave in MidWest", "OJ Simpson Trial"],
"1996":["Summer Olympics in Atlanta", "US President Election"],
"1997":["Flood in MidWest", "Death of Princess Diana"],
"1998":["Blizzard Ice Storm in NorthEast"],
"1999":["Landslide in WA", "66 Tornadoes across MidWest and South", "Heat Wave in MidWest and NorthEast"],
"2000":["Dot com bubble burst", "US President Election and Florida Recount"],
"2001":["Cyclone in South", "9/11"],
"2002":["US Invasion of Afganistan", "Winter Olympics in Salt Lake City, Utha", "US Airways Bankruptcy", "United Airlines Bankruptcy"],
"2003":["Gulf War 2", "United Airlines Bankruptcy"],
"2004":["Cyclones across TX, FL and East Coast", "US President Election", "Asia Tsunami"],
"2005":["7 Tornadoes across MidWest, South and SouthEast", "Death of Pope John Paul 2", "Hurrican Katrina"],
"2006":["United Airlines comes out of Bankruptcy"],
"2007":["Wildfires in CA"],
"2008":["Tornados across South", "Lehman Brothers", "US President Election", "Mumbai Terror Attacks"]};
console.log(mevents[year]);
event.text(typeof mevents[year] + " " + mevents[year]);
我可以在控制台中打印值。但是我不能将它们分配给文本变量。我错过了什么?
假设svg
只包含一个节点,你实际上只附加了一个text
元素(字符串值为"mevent")。
听起来你想要的是做一个数据联接,这将为数据数组中的每个元素生成一个text
节点。
var mevent = svg.selectAll("text")
.data(mevents)
.enter().append("text")
.attr("class", "year event")
.attr("text-anchor", "end")
.attr("y", height - 450)
.attr("x", width - 300)
.text("mevent");
您可以通过阅读使用联接进行思考教程来了解有关此内容的更多信息。 这是使用 d3 的一个非常基本的部分,因此值得花一些时间来真正了解它是如何工作的。
相关文章:
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- AngularJS和promise值在调用本地函数时的效果-未定义
- 为什么这个函数调用会破坏程序并导致未定义的变量
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- PhoneGap无法调用未定义的方法loadurl
- TinyMCE验证给出错误:无法调用方法'getContent'的未定义
- setInterval调用具有未定义参数的函数
- 返回List<字符串>Jquery中的from JSON调用具有未定义的长度
- REST调用总是返回未定义的(Node.JS)
- 如何处理 getJSON 调用上的“未捕获类型错误:无法设置未定义的属性'prop'”
- Javascript/jQuery - “无法调用未定义的方法'push'”,而它是定义的
- 对象不支持在调用 indexOf 定义的数组时 IE8 中未定义的此属性或方法
- 未捕获的类型错误:无法调用方法'至小写'的未定义
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 无法调用方法'应用'关于为jquery谷歌地图插件设置方向的未定义
- moment:Array.prototype.some在null或未定义时被调用
- 调用javascript函数时未定义
- 未捕获的类型错误:无法调用方法'每个'的未定义
- Alt flux action:_this.actions未定义,即使调用了this.generateActions