我正在尝试使用dc.js来实现crossfilter和d3。我几乎成功了。当我在JSFiddle中运行我的代码时,它完全正常!但是,当我尝试在本地系统上实现确切的代码时,它给了我未捕获的TypeError:无法读取属性' textContent'为null 错误。
我的代码是
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>
&#13;
d3.js 中提供错误的代码行 this.node()。textContent;
答案 0 :(得分:8)
我有同样的问题! localy
刚发现:
<div id="year-chart"></div>
错位!
尝试在JS
文件之前设置所有您的html内容 。并且工作正常!
参见示例:
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>
&#13;
答案 1 :(得分:1)
我将 .js文件链接移到了html的底部,并且问题已解决。