javascriptd3.jsdc.jscrossfilter

Uncaught TypeError: Cannot read property 'textContent' of null error


I am trying to use dc.js to implement the crossfilter and d3. I am almost successful. When I run my code in JSFiddle, it works perfectly ok! But when i try to implement the exact code on the local system, it gives me Uncaught TypeError: Cannot read property 'textContent' of null error.

My code is

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>

the line of code in d3.js that gives the error is this.node().textContent;


Solution

  • I had the same issue! localy Just found out that the:

     <div id="year-chart"></div>
    

    was misplaced!

    just try to setup all your html content before the JS files. And will work fine!

    see the example:

    obs: It was working on JSFiddle because the html (must) rend before the js scripts!

    Hopefully will work for you too.

    <!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>