1

I am trying to create a D3 Zoom and Brush Graph. Copied the code from the docs: https://bl.ocks.org/mbostock/34f08d5e11952a80609169b7917d4172

ERROR: TypeError: node is null

d3.v4.min.js:2:104320 attr https://d3js.org/d3.v4.min.js:2:104320

http://localhost:3000/javascripts/script.js:24:14

Line 1239 function in d3js file is:

function selection_attr(name, value) {
  var fullname = namespace(name);

  if (arguments.length < 2) {
    var node = this.node();
    return fullname.local
        ? node.getAttributeNS(fullname.space, fullname.local)
        : node.getAttribute(fullname);
  }

  return this.each((value == null
      ? (fullname.local ? attrRemoveNS : attrRemove) : (typeof value === "function"
      ? (fullname.local ? attrFunctionNS : attrFunction)
      : (fullname.local ? attrConstantNS : attrConstant)))(fullname, value));
}

index.pug

div.section2 
  div.col.col1
    .graph
      h1 Sentiment Analysis Graph 
      svg(width="960", height="500")

style.css

 /* D3 Brush and Zoom Chart Style */
 .graph {
   margin: 2rem 0;
 }
 .area {
  fill: steelblue;
  clip-path: url(#clip);
}

.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

script.js

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%b %Y");

var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.price); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(d.date); })
    .y0(height2)
    .y1(function(d) { return y2(d.price); });

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

d3.csv("public/csv/sp500.csv", type, function(error, data) {
  if (error) throw error;

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain([0, d3.max(data, function(d) { return d.price; })]);
  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

  focus.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);
});

function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}

function type(d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
  return d;
}

I don't understand what's null? ideas?

Shaz
  • 1,443
  • 1
  • 27
  • 67
  • post the stack trace? – Ayush Gupta Oct 05 '18 at 07:13
  • "object" == typeof exports && "undefined" != typeof module ? n(exports) : "function" == typeof define && define.amd ? define(["exports"], n) : n(t.d3 = t.d3 || {}) in the d3js file @AyushGupta – Shaz Oct 05 '18 at 07:24
  • don't use the `.min` version of d3 you get a better error location – rioV8 Oct 05 '18 at 08:37
  • Question updated: @rioV8 I changed, the error says: Node is null ? – Shaz Oct 05 '18 at 11:51
  • line 24 is the `d3.zoom`, there is no `node`/`selection` anywhere on that line, what is the full stack trace, you now use d3.v4.js, which line in script.js is used? – rioV8 Oct 05 '18 at 11:57
  • In console log, the line the error is shown is on 24 of my script; which is this: width = +svg.attr("width") - margin.left - margin.right, – Shaz Oct 05 '18 at 12:05
  • I'm not sure if that's helpful? how do I print out the stack trace in javascript - still learning :) – Shaz Oct 05 '18 at 12:06
  • @Shaz That's indeed helpful as it points to the first usage of `.attr()` in your code. There is still information missing, but without knowing the details I am pretty sure it is a variation of [*"selectAll not selecting any nodes on D3"*](/q/52442181) or, more generally, [*"Where should I put – altocumulus Oct 05 '18 at 12:40

1 Answers1

1

I just had this similar error of .node() being null. I had been trying to select a div referencing it's class when I had given it an id. I would suggest you to check if you are trying to select the right element.

theBird
  • 31
  • 3