2.1.1 • Published 2 years ago

d3-funnel v2.1.1

Weekly downloads
1,587
License
MIT
Repository
github
Last release
2 years ago

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

To install this library, simply include both D3.js and D3Funnel:

<script src="/path/to/d3.js"></script>
<script src="/path/to/dist/d3-funnel.js"></script>

Alternatively, if you are using Webpack or Browserify, you can install the npm package and import the module. This will include a compatible version of D3.js for you:

npm install d3-funnel --save
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

OptionDescriptionTypeDefault
chart.widthThe width of the chart in pixels or a percentage.mixedContainer's width
chart.heightThe height of the chart in pixels or a percentage.mixedContainer's height
chart.bottomWidthThe percent of total width the bottom should be.number1 / 3
chart.bottomPinchHow many blocks to pinch on the bottom to create a funnel "neck".number0
chart.invertedWhether the funnel direction is inverted (like a pyramid).boolfalse
chart.animateThe load animation speed in milliseconds.number0 (disabled)
chart.curve.enabledWhether the funnel is curved.boolfalse
chart.curve.heightThe curvature amount.number20
chart.totalCountOverride the total count used in ratio calculations.numbernull
block.dynamicHeightWhether the block heights are proportional to their weight.boolfalse
block.dynamicSlopeWhether the block widths are proportional to their value decrease.boolfalse
block.barOverlayWhether the blocks have bar chart overlays proportional to its weight.boolfalse
block.fill.scaleThe background color scale as an array or function.mixedd3.schemeCategory10
block.fill.typeEither 'solid' or 'gradient'.string'solid'
block.minHeightThe minimum pixel height of a block.number0
block.highlightWhether the blocks are highlighted on hover.boolfalse
label.enabledWhether the block labels should be displayed.booltrue
label.fontFamilyAny valid font family for the labels.stringnull
label.fontSizeAny valid font size for the labels.string'14px'
label.fillAny valid hex color for the label color.string'#fff'
label.formatEither function(label, value) or a format string. See below.mixed'{l}: {f}'
tooltip.enabledWhether tooltips should be enabled on hover.boolfalse
tooltip.formatEither function(label, value) or a format string. See below.mixed'{l}: {f}'
events.click.blockCallback function(data) for when a block is clicked.functionnull

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:

KeyDescription
'{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:

KeyTypeDescription
indexnumberThe index of the block.
nodeobjectThe DOM node of the block.
valuenumberThe numerical value.
fillstringThe background color.
label.rawstringThe unformatted label.
label.formattedstringThe result of options.label.format.
label.colorstringThe 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:

OptionTypeDescriptionExample
labelmixedRequired. The label to associate with the block.'Students'
valuenumberRequired. The value (or count) to associate with the block.500
backgroundColorstringA row-level override for block.fill.scale. Hex only.'#008080'
formattedValuemixedA row-level override for label.format.'USD: $150'
hideLabelboolWhether to hide the formatted label for this block.true
labelColorstringA row-level override for label.fill. Hex only.'#333'

API

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

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

License

MIT license.

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.2

5 years ago

1.2.2-alpha.0

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

8 years ago

0.8.0

8 years ago

0.7.7

8 years ago

0.7.6

8 years ago

0.7.5

8 years ago

0.7.4

8 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

9 years ago

0.7.0

9 years ago

0.6.13

9 years ago

0.6.12

9 years ago

0.6.11

9 years ago

0.6.10

9 years ago

0.6.9

9 years ago

0.6.8

9 years ago

0.6.7

9 years ago

0.6.6

9 years ago

0.6.5

9 years ago

0.6.4

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago