Interface Stack<This, Datum, Key>

A stack generator.

Some shape types can be stacked, placing one shape adjacent to another. For example, a bar chart of monthly sales might be broken down into a multi-series bar chart by product category, stacking bars vertically. This is equivalent to subdividing a bar chart by an ordinal dimension (such as product category) and applying a color encoding.

Stacked charts can show overall value and per-category value simultaneously; however, it is typically harder to compare across categories, as only the bottom layer of the stack is aligned. So, chose the stack order carefully, and consider a streamgraph. (See also grouped charts.)

Like the pie generator, the stack generator does not produce a shape directly. Instead it computes positions which you can then pass to an area generator or use directly, say to position bars.

The first generic corresponds to the "this" context in which the stack generator and its accessor functions are invoked.

The second generic corresponds to the data type of an element in the data array passed into the stack generator.

The third generic corresponds to the data type of key used to identify a series.

interface Stack {
    keys(): ((this, data, ...args) => Key[]);
    keys(keys): Stack<This, Datum, Key>;
    offset(): ((series, order) => void);
    offset(offset): Stack<This, Datum, Key>;
    offset(offset): Stack<This, Datum, Key>;
    order(): ((series) => Iterable<number>);
    order(order): Stack<This, Datum, Key>;
    order(order): Stack<This, Datum, Key>;
    value(): ((d, key, i, data) => number);
    value(value): Stack<This, Datum, Key>;
    value(value): Stack<This, Datum, Key>;
    (data, ...args): Series<Datum, Key>[];
}

Type Parameters

  • This

  • Datum

  • Key

  • Generates a stack for the given array of data, returning an array representing each series. The resulting array has one element per series. Each series in then typically passed to an area generator to render an area chart, or used to construct rectangles for a bar chart.

    Any additional arguments are arbitrary; they are simply propagated to the generator’s accessor functions along with the this object.

    Parameters

    • data: Iterable<Datum>

      Array of data elements.

    • Rest ...args: any[]

    Returns Series<Datum, Key>[]

Methods

  • Returns the current keys accessor, which defaults to the empty array.

    Returns ((this, data, ...args) => Key[])

      • (this, data, ...args): Key[]
      • Returns the current keys accessor, which defaults to the empty array.

        Parameters

        • this: This
        • data: Datum[]
        • Rest ...args: any[]

        Returns Key[]

  • Sets the keys accessor to the specified function or array and returns this stack generator. A series (layer) is generated for each key. Keys are typically strings, but they may be arbitrary values. The series’ key is passed to the value accessor, along with each data point, to compute the point’s value.

    Parameters

    • keys: Iterable<Key> | ((this, data, ...args) => Key[])

    Returns Stack<This, Datum, Key>

  • Returns the current offset accessor, which defaults to stackOffsetNone; this uses a zero baseline.

    Returns ((series, order) => void)

      • (series, order): void
      • Returns the current offset accessor, which defaults to stackOffsetNone; this uses a zero baseline.

        Parameters

        • series: Series<Datum, Key>
        • order: number[]

        Returns void

  • Reset the offset to use stackOffsetNone; this uses a zero baseline.

    Parameters

    • offset: null

      null to set to the default stackOffsetNone.

    Returns Stack<This, Datum, Key>

  • Sets the offset accessor to the specified function and returns this stack generator.

    Parameters

    • offset: ((series, order) => void)

      A function which is passed the generated series array and the order index array; it is then responsible for updating the lower and upper values in the series array.

        • (series, order): void
        • Parameters

          • series: Series<Datum, Key>
          • order: number[]

          Returns void

    Returns Stack<This, Datum, Key>

  • Returns the current order accessor, which defaults to stackOrderNone; this uses the order given by the key accessor.

    Returns ((series) => Iterable<number>)

      • (series): Iterable<number>
      • Returns the current order accessor, which defaults to stackOrderNone; this uses the order given by the key accessor.

        Parameters

        Returns Iterable<number>

  • Sets the order accessor to the specified array and returns this stack generator.

    Parameters

    • order: Iterable<number>

    Returns Stack<This, Datum, Key>

  • Sets the order accessor to the specified function and returns this stack generator.

    The stack order is computed prior to the offset; thus, the lower value for all points is zero at the time the order is computed. The index attribute for each series is also not set until after the order is computed.

    See stack orders for the built-in orders.

    Parameters

    • order: ((series) => Iterable<number>)

      A function returning a sort order array. It is passed the generated series array and must return an array of numeric indexes representing the stack order.

        • (series): Iterable<number>
        • Parameters

          Returns Iterable<number>

    Returns Stack<This, Datum, Key>

  • Returns the current value accessor, which defaults to a function return the property corresponding to the relevant key from the data element.

    Thus, by default the stack generator assumes that the input data is an array of objects, with each object exposing named properties with numeric values; see stack for an example.

    Returns ((d, key, i, data) => number)

      • (d, key, i, data): number
      • Returns the current value accessor, which defaults to a function return the property corresponding to the relevant key from the data element.

        Thus, by default the stack generator assumes that the input data is an array of objects, with each object exposing named properties with numeric values; see stack for an example.

        Parameters

        • d: Datum
        • key: Key
        • i: number
        • data: Datum[]

        Returns number

  • Sets the value accessor to the specified number and returns this stack generator.

    Parameters

    • value: number

      A constant value.

    Returns Stack<This, Datum, Key>

  • Sets the value accessor to the specified function and returns this stack generator.

    Parameters

    • value: ((d, key, i, data) => number)

      A value accessor function which returns the numeric value for a given data element and key combination. The accessor function is invoked for each data element and key being passed the datum, the key, index of the data element in the input data array, and the complete data array.

        • (d, key, i, data): number
        • Parameters

          • d: Datum
          • key: Key
          • i: number
          • data: Datum[]

          Returns number

    Returns Stack<This, Datum, Key>