Naga Lokesh Kannumoori
Naga Lokesh Kannumoori

Reputation: 603

d3 returns error `e is not a function` while passing nest data to d3.stack

The following code is throwing the error e is not a function from D3:

var stack = d3.stack()
    .offset("zero")
    .keys(Object.keys(data[0]))(nest);

The below images show the error in detail:

This is the error when I run it.

error is taking to d3.min.js

Formatted code in browser

This is the error when I run it, the error appears in d3.min.js. How do I fix this?

Upvotes: 4

Views: 820

Answers (1)

Andrew Reid
Andrew Reid

Reputation: 38151

In v3 of d3, with d3.layout.stack(), an offset value could be provided with a string:

If offset is specified, sets the stack offset algorithm to the specified value. If offset is not specified, returns the current offset algorithm. The following string values are supported:

silhouette - center the stream, as in ThemeRiver.
wiggle - minimize weighted change in slope.
expand - normalize layers to fill the range [0,1].
zero - use a zero baseline, i.e., the y-axis.

In addition to a string, offset may be specified as a function. (v3 docs)

However, you are using d3v4+ with d3.stack(). The change in the method name was enough to break old examples, but the method was also changed to accept only a function (or array). No string values could be used to specify offset functionality:

If offset is specified, sets the offset accessor to the specified function or array and returns this stack generator. If offset is not specified, returns the current offset acccesor, which defaults to stackOffsetNone; this uses a zero baseline. See stack offsets for the built-in offsets. (v5(/4) docs)

So in your case, you don't need to specify an offset: the default offset is a zero baseline. If you wanted another offset to give to stack.offset(), D3 comes with a few out of the box:

  • d3.stackOffsetExpand
  • d3.stackOffsetDiverging
  • d3.stackOffsetNone
  • d3.stackOffsetSilhouette
  • d3.stackOffsetWiggle

Upvotes: 2

Related Questions