Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

d3-funnel

jakezatecky32.1kMIT2.1.3TypeScript support: included

A library for rendering SVG funnel charts using D3.js

d3, funnel, pyramid, svg, chart

readme

d3-funnel

npm Build Status GitHub license

d3-funnel is an extensible, open-source JavaScript library for rendering funnel charts using the D3.js library.

d3-funnel is focused on providing practical and visually appealing funnels through a variety of customization options. Check out the examples page to get a showcasing of the several possible options.

Installation

Install this library via npm, yarn, pnpm, or your preferred package manager:

npm install d3-funnel --save

You can then load this library into your app using import:

import D3Funnel from 'd3-funnel';

Usage

To use this library, you must create a container element and instantiate a new funnel chart. By default, the chart will assume the width and height of the parent container:

<div id="funnel"></div>

<script>
    const data = [
        { label: 'Inquiries', value: 5000 },
        { label: 'Applicants', value: 2500 },
        { label: 'Admits', value: 500 },
        { label: 'Deposits', value: 200 },
    ];
    const options = {
        block: {
            dynamicHeight: true,
            minHeight: 15,
        },
    };

    const chart = new D3Funnel('#funnel');
    chart.draw(data, options);
</script>

Options

Option Description Type Default
chart.width The width of the chart in pixels or a percentage. mixed Container's width
chart.height The height of the chart in pixels or a percentage. mixed Container's height
chart.bottomWidth The percent of total width the bottom should be. number 1 / 3
chart.bottomPinch How many blocks to pinch on the bottom to create a funnel "neck". number 0
chart.inverted Whether the funnel direction is inverted (like a pyramid). bool false
chart.animate The load animation speed in milliseconds. number 0 (disabled)
chart.curve.enabled Whether the funnel is curved. bool false
chart.curve.height The curvature amount. number 20
chart.totalCount Override the total count used in ratio calculations. number null
block.dynamicHeight Whether the block heights are proportional to their weight. bool false
block.dynamicSlope Whether the block widths are proportional to their value decrease. bool false
block.barOverlay Whether the blocks have bar chart overlays proportional to its weight. bool false
block.fill.scale The background color scale as an array or function. mixed d3.schemeCategory10
block.fill.type Either 'solid' or 'gradient'. string 'solid'
block.minHeight The minimum pixel height of a block. number 0
block.highlight Whether the blocks are highlighted on hover. bool false
label.enabled Whether the block labels should be displayed. bool true
label.fontFamily Any valid font family for the labels. string null
label.fontSize Any valid font size for the labels. string '14px'
label.fill Any valid hex color for the label color. string '#fff'
label.format Either function(label, value) or a format string. See below. mixed '{l}: {f}'
tooltip.enabled Whether tooltips should be enabled on hover. bool false
tooltip.format Either function(label, value) or a format string. See below. mixed '{l}: {f}'
events.click.block Callback function(data) for when a block is clicked. function null

Label/Tooltip Format

The option label.format can either be a function or a string. The following keys will be substituted by the string formatter:

Key Description
'{l}' The block's supplied label.
'{v}' The block's raw value.
'{f}' The block's formatted value.

Event Data

Block-based events are passed a data object containing the following elements:

Key Type Description
index number The index of the block.
node object The DOM node of the block.
value number The numerical value.
fill string The background color.
label.raw string The unformatted label.
label.formatted string The result of options.label.format.
label.color string The label color.

Example:

{
    index: 0,
    node: { ... },
    value: 150,
    fill: '#c33',
    label: {
        raw: 'Visitors',
        formatted: 'Visitors: 150',
        color: '#fff',
    },
},

Overriding Defaults

You may wish to override the default chart options. For example, you may wish for every funnel to have proportional heights. To do this, simply modify the D3Funnel.defaults property:

D3Funnel.defaults.block.dynamicHeight = true;

Should you wish to override multiple properties at a time, you may consider using lodash's _.merge or jQuery's $.extend:

D3Funnel.defaults = _.merge(D3Funnel.defaults, {
    block: {
        dynamicHeight: true,
        fill: {
            type: 'gradient',
        },
    },
    label: {
        format: '{l}: ${f}',
    },
});

Advanced Data

In the examples above, both label and value were just to describe a block within the funnel. A complete listing of the available options is included below:

Option Type Description Example
label mixed Required. The label to associate with the block. 'Students'
value number Required. The value (or count) to associate with the block. 500
backgroundColor string A row-level override for block.fill.scale. Hex only. '#008080'
formattedValue mixed A row-level override for label.format. 'USD: $150'
hideLabel bool Whether to hide the formatted label for this block. true
labelColor string A row-level override for label.fill. Hex only. '#333'

API

Additional methods beyond draw() are accessible after instantiating the chart:

Method Description
destroy() Removes the funnel and its events from the DOM.

License

MIT license.

changelog

v2.1.3 (2025-09-11)

Bug Fixes

  • Fix issue with default export/import in ESM modules (#267)

v2.1.2 (2025-09-08)

  • Release with modern optimizations

v2.1.0 (2022-02-25)

Other

  • [#156]: Add TypeScript index file

v2.0.0 (2021-06-04)

Bug Fixes

  • [#138]: Fix an issue with tooltip alignment in newer versions of Chrome

Dependencies

  • Breaking: Upgrade to using D3 v6 (changes events.click.block(d) to events.click.block(event, d))

v1.2.2 (2019-01-26)

Performance

  • [#97]: Significantly reduce package size to around 27% of its original size

v1.2.1 (2018-10-13)

Build Process

  • [#93]: Fix issue where dist/d3-funnel.js was being minified alongside dist/d3-funnel.min.js

v1.2.0 (2018-06-25)

Dependencies

  • [#87]: Add official support for D3 v5 (while continuing support for D3 v4)

Bug Fixes

  • [#86]: Fix issue where heights were being calculated incorrectly when the sum of values was zero

v1.1.1 (2017-07-31)

This is a patch for the npm release, which was shipped without the updated /dist directory.

v1.1.0 (2017-07-31)

Release v1.1.0 adds a variety of new functionality to the funnel, and introduces a new data structure that allows for more flexibility on a row level than previously capable:

funnel.draw([{
    label: 'Prospects',
    value: 5000,
    backgroundColor: '#d33',
}]);

The old structure of an array-of-arrays has been deprecated and will be removed in the v2.0 release. Please update to the newest data structure as soon as possible. Refer to the README for the list of available options, which includes all of the capabilities previously held in the data array.

Deprecations

  • [#73]: The old array-of-arrays data structure has been deprecated in favor of a data objects

New Features

  • [#45]: Add support for tooltips via tooltip.enabled and tooltip.format
  • [#71]: Add hideLabel option to the data object
  • [#74]: Add label.enabled chart option
  • [#79]: Add support for HTMLElement in the D3Funnel constructor

Bug Fixes

  • [#77]: Fix an issue where containers with zero width and/or height would not inherit from the default dimensions

v1.0.1 (2017-01-30)

Bug Fixes

  • [#67]: Add missing cursor: pointer style to blocks when clickable
  • [#70]: Fix NaN and Infinity values in block paths when height is zero and dynamicHeight: true

v1.0.0 (2016-08-02)

This release breaks major backwards compatibility by upgrading D3 3.x to D3 4.x. Refer to D3's changes documentation for more info.

Behavior Changes

  • [#62]: Upgrade D3 3.x to 4.x

v0.8.0 (2016-07-21)

New Features

  • [#19]: Add support for percentages in chart.width and chart.height (e.g. '75%')
  • [#38]: Split line break characters found in label.format into multiple lines

Bug Fixes

  • [#49]: Fix issue where gradient definitions could conflict with existing definitions

v0.7.7 (2016-07-15)

New Features

  • [#50]: Add block.barOverlay option to display bar charts proportional to block value
  • [#52]: Add chart.totalCount option to override total counts used in ratio calculations

Other

  • Simplify and clean up examples

v0.7.6 (2016-07-12)

New Features

  • [#53]: Add label.fontSize option
  • [#57]: Add block.dynamicSlope option to make the funnel width proportional to its value

Bug Fixes

  • [#59]: Fix issue where formatted array values were not being passed to the label formatter

v0.7.5 (2015-12-19)

New Features

  • [#44]: Pass DOM node to event data

v0.7.4 (2015-12-11)

Build Changes

  • [#42]: Use ES6 imports and exports in source files
  • [#43]: Require D3.js for CommonJS environments

v0.7.3 (skipped)

D3Funnel v0.7.3 is an NPM-only hotfix that adds in missing compiled files.

v0.7.2 (2015-11-18)

Bug Fixes

  • [#41]: Fix issue where events.click.block would error on null

v0.7.1 (2015-10-28)

Behavior Changes

  • Errors thrown on data validation are now more descriptive and context-aware

Bug Fixes

  • [#35]: Fix issue where gradient background would not persist after mouse out
  • [#36]: Fix issue where non-SVG entities were not being removed from container

v0.7.0 (2015-10-04)

D3Funnel v0.7 is a backwards-incompatible release that resolves some outstanding bugs, standardizes several option names and formats, and introduces a few new features.

No new features will be added to the v0.6 series, but minor patches will be available for a few months.

Behavior Changes

New Features

  • [#9]: Block can now have their color scale specified in addition to data points
  • [#34]: Default options are now statically available and overridable

Bug Fixes

  • [#25]: Fix issues with isInverted and dynamicArea producing odd pyramids
  • [#32]: Fix issue where pinched blocks were not having the same width as bottomWidth

Upgrading from v0.6.x

Several options have been renamed for standardization. Please refer to the table below for the new equivalent option:

Old option New option Notes
animation chart.animate
bottomPinch chart.bottomPinch
bottomWidth chart.bottomWidth
curveHeight chart.curve.height
dynamicArea block.dynamicHeight See change #29.
fillType block.fill.type
height chart.height
hoverEffects block.hightlight
isCurved chart.curve.enabled
isInverted chart.inverted
onItemClick events.click.block
minHeight block.minHeight
width chart.width

In addition, please refer to change #29.

v0.6.13 (2015-10-02)

Bug Fixes

  • [#33]: Fix issue where package.json pointed to the incorrect main file

v0.6.12 (2015-09-25)

New Features

  • [#16]: Add support for formatted labels

Bug Fixes

  • [#26]: Fix issues with closed range intervals in bottomWidth
  • [#28]: Fix issue where short hex colors did not translate properly in color manipulations