未捕获的TypeError: Cannot read property 'textContent'nul
Uncaught TypeError: Cannot read property 'textContent' of null error
我试图使用dc.js来实现交叉过滤器和d3。我就快成功了。当我在JSFiddle中运行我的代码时,它工作得非常好!但是当我试图在本地系统上实现确切的代码时,它给了我Uncaught TypeError:无法读取null错误的属性'textContent'。
我的代码是trial1.js
var yearChart = dc.barChart('#year-chart');
//the data
var data = [
{date: "2015-10-01", type: "car", quantity: 3}];
var dispatch = crossfilter(data);
var parseDate = d3.time.format("%Y-%m-%d").parse;
data.forEach(function (d) {
d.date = parseDate(d.date);
d.quantity = +d.quantity;
d.Year = d.date.getFullYear();
});
var dateDim = dispatch.dimension(function (d) {
return d.date;
});
var productions = dateDim.group().reduceSum(function (d) {
return d.quantity;
});
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;
yearChart.width(2000).height(200)
.dimension(dateDim)
.group(productions)
.x(d3.time.scale().domain([minDate, maxDate]))
.brushOn(false)
.centerBar(true)
.yAxisLabel("Productions per day")
.xUnits(function(){return 10;});
yearChart.render();
<html>
<head>
<meta charset="utf-8">
<script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="https://dc-js.github.io/dc.js/js/d3.js"></script>
<script type="text/javascript" src="https://dc-js.github.io/dc.js/js/crossfilter.js"></script>
<script type="text/javascript" src="https://dc-js.github.io/dc.js/js/dc.js"></script>
<script type="text/javascript" src="https://dc-js.github.io/dc.js/js/colorbrewer.js"></script>
<script type="text/javascript" src = "trial1.js"></script>
</head>
<body>
<div id="year-chart"></div>
</body>
</html>
d3.js给出错误的代码行是this.node().textContent;
我也有同样的问题!localy
刚刚发现:
<div id="year-chart"></div>
是的 !
只是尝试设置所有你的html内容在 JS
文件之前。而且会很好!
参见示例:
obs:它正在JSFiddle上工作,因为html(必须)在js
脚本之前更新!
希望对你也有用。
<!DOCTYPE html>
<html lang="en">
<head>
<title>dc.js - Bar Chart Example</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="http://dc-js.github.io/dc.js/css/dc.css"/>
<script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" charset="utf-8"></script>
<script src="http://dc-js.github.io/dc.js/js/d3.js" charset="utf-8"></script>
<script src="http://dc-js.github.io/dc.js/js/crossfilter.js"></script>
<script src="http://dc-js.github.io/dc.js/js/dc.js"></script>
</head>
<body>
<div id="year-chart"></div>
<script type="text/javascript">
var yearChart = dc.barChart('#year-chart');
//the data
var data = [{
date: "2015-10-01",
customer: "BOASG",
quantity: 3
}];
var dispatch = crossfilter(data);
var parseDate = d3.time.format("%Y-%m-%d").parse;
data.forEach(function (d) {
d.date = parseDate(d.date);
d.quantity = +d.quantity;
d.Year = d.date.getFullYear();
});
var dateDim = dispatch.dimension(function (d) {
return d.date;
});
var productions = dateDim.group().reduceSum(function (d) {
return d.quantity;
});
var minDate = dateDim.bottom(1)[0].date;
window.alert(minDate);
var maxDate = dateDim.top(1)[0].date;
window.alert(maxDate);
yearChart.width(2000).height(200)
.dimension(dateDim)
.group(productions)
.x(d3.time.scale().domain([minDate, maxDate]))
.brushOn(false)
.centerBar(true)
.yAxisLabel("Productions per day")
.xUnits(function () {
return 10;
});
dc.renderAll();
</script>
<!-- <div id="year-chart"></div> if you set the code here you will be able to reproduce the issue.-->
</body>
</html>
我将.js文件链接移到html的底部,问题解决了
相关文章:
- $(this).prop('property') vs. this.property
- 为什么JavaScript可以'找不到给定的InnerHTML并返回Cannot set property
- Jquery Dynamic property
- Reactjs错误:babel runtime.js:32 Uncaught TypeError:超级表达式必须为nul
- 为什么“如果( !Object.property)“,如果 Object 未定义,则中断
- 我怎样才能让JSLint停止抱怨Ember.js“.property()”
- ngRepeat filter by array-property
- JasmineJS 'isNot' property
- 如何在Javascript/AngularJS中动态地将foo设置为Object Property Name
- 为什么这个函数在JS中返回undefined.property
- 如何在JavaScript函数中使用s:property
- Jquery Javascript Property Undefined
- Javascript Object Property Undefined (GM_getValue(), GM_list
- 如何从 Javascript 设置 .net Public Property 值
- JQuery .sort() JavaScript Object by property value不起作用
- 用按钮和jquery切换“data-property”
- XHR2 addEventListener vs property
- Javascript:数据格式,类似于Spring Property Editors
- 未捕获的TypeError: Cannot read property 'textContent'nul
- 未捕获的TypeError: Cannot read property 'transaction'nul