// Version 2.9.0 timelines-chart - https://github.com/vasturiano/timelines-chart
(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
        typeof define === 'function' && define.amd ? define(factory) :
            (global = global || self, global.TimelinesChart = factory());
}(this, (function () {
    'use strict';

    function _toConsumableArray(arr) {
        return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
    }

    function _arrayWithoutHoles(arr) {
        if (Array.isArray(arr)) return _arrayLikeToArray(arr);
    }

    function _iterableToArray(iter) {
        if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
    }

    function _unsupportedIterableToArray(o, minLen) {
        if (!o) return;
        if (typeof o === "string") return _arrayLikeToArray(o, minLen);
        var n = Object.prototype.toString.call(o).slice(8, -1);
        if (n === "Object" && o.constructor) n = o.constructor.name;
        if (n === "Map" || n === "Set") return Array.from(n);
        if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
    }

    function _arrayLikeToArray(arr, len) {
        if (len == null || len > arr.length) len = arr.length;

        for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];

        return arr2;
    }

    function _nonIterableSpread() {
        throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
    }

    function styleInject(css, ref) {
        if (ref === void 0) ref = {};
        var insertAt = ref.insertAt;

        if (!css || typeof document === 'undefined') {
            return;
        }

        var head = document.head || document.getElementsByTagName('head')[0];
        var style = document.createElement('style');
        style.type = 'text/css';

        if (insertAt === 'top') {
            if (head.firstChild) {
                head.insertBefore(style, head.firstChild);
            } else {
                head.appendChild(style);
            }
        } else {
            head.appendChild(style);
        }

        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }
    }

    var css_248z = ".timelines-chart {\n\n  text-align: center;\n\n  /* Cancel selection interaction */\n  -webkit-touch-callout: none;\n  -webkit-user-select: none;\n  -khtml-user-select: none;\n  -moz-user-select: none;\n  -ms-user-select: none;\n  user-select: none;\n}\n\n  .timelines-chart .axises line, .timelines-chart .axises path {\n      stroke: #808080;\n    }\n\n  .timelines-chart .axises .x-axis {\n      font: 12px sans-serif;\n    }\n\n  .timelines-chart .axises .x-grid line {\n      stroke: #D3D3D3;\n    }\n\n  .timelines-chart .axises .y-axis line, .timelines-chart .axises .y-axis path, .timelines-chart .axises .grp-axis line, .timelines-chart .axises .grp-axis path {\n        stroke: none;\n      }\n\n  .timelines-chart .axises .y-axis text, .timelines-chart .axises .grp-axis text {\n        fill: #2F4F4F;\n      }\n\n  .timelines-chart line.x-axis-date-marker {\n    stroke-width: 1;\n    stroke: #293cb7;\n    fill: \"none\";\n  }\n\n  .timelines-chart .series-group {\n    fill-opacity: 0.6;\n    stroke: #808080;\n    stroke-opacity: 0.2;\n  }\n\n  .timelines-chart .series-segment {\n    stroke: none;\n  }\n\n  .timelines-chart .series-group, .timelines-chart .series-segment {\n    cursor: crosshair;\n  }\n\n  .timelines-chart .legend {\n    font-family: Sans-Serif;\n  }\n\n  .timelines-chart .legend .legendText {\n      fill: #666;\n    }\n\n  .timelines-chart .reset-zoom-btn {\n    font-family: sans-serif;\n    fill: blue;\n    opacity: .6;\n    cursor: pointer;\n  }\n\n.brusher .grid-background {\n    fill: lightgrey;\n  }\n\n.brusher .axis path {\n    display: none;\n  }\n\n.brusher .tick text {\n    text-anchor: middle;\n  }\n\n.brusher .grid line, .brusher .grid path {\n      stroke: #fff;\n    }\n\n.chart-zoom-selection, .brusher .brush .selection {\n  stroke: blue;\n  stroke-opacity: 0.6;\n  fill: blue;\n  fill-opacity: 0.3;\n  shape-rendering: crispEdges;\n}\n\n.chart-tooltip {\n  color: #eee;\n  background: rgba(0,0,140,0.85);\n  padding: 5px;\n  border-radius: 3px;\n  font: 11px sans-serif;\n  z-index: 4000;\n}\n\n.chart-tooltip.group-tooltip {\n    font-size: 14px;\n  }\n\n.chart-tooltip.line-tooltip {\n    font-size: 13px;\n  }\n\n.chart-tooltip.group-tooltip, .chart-tooltip.line-tooltip {\n    font-weight: bold;\n  }\n\n.chart-tooltip.segment-tooltip {\n     text-align: center;\n  }";
    styleInject(css_248z);

    /**
     * Returns a function, that, as long as it continues to be invoked, will not
     * be triggered. The function will be called after it stops being called for
     * N milliseconds. If `immediate` is passed, trigger the function on the
     * leading edge, instead of the trailing. The function also has a property 'clear'
     * that is a function which will clear the timer to prevent previously scheduled executions.
     *
     * @source underscore.js
     * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
     * @param {Function} function to wrap
     * @param {Number} timeout in ms (`100`)
     * @param {Boolean} whether to execute at the beginning (`false`)
     * @api public
     */
    function debounce(func, wait, immediate) {
        var timeout, args, context, timestamp, result;
        if (null == wait) wait = 100;

        function later() {
            var last = Date.now() - timestamp;

            if (last < wait && last >= 0) {
                timeout = setTimeout(later, wait - last);
            } else {
                timeout = null;
                if (!immediate) {
                    result = func.apply(context, args);
                    context = args = null;
                }
            }
        }

        var debounced = function () {
            context = this;
            args = arguments;
            timestamp = Date.now();
            var callNow = immediate && !timeout;
            if (!timeout) timeout = setTimeout(later, wait);
            if (callNow) {
                result = func.apply(context, args);
                context = args = null;
            }

            return result;
        };

        debounced.clear = function () {
            if (timeout) {
                clearTimeout(timeout);
                timeout = null;
            }
        };

        debounced.flush = function () {
            if (timeout) {
                result = func.apply(context, args);
                context = args = null;

                clearTimeout(timeout);
                timeout = null;
            }
        };

        return debounced;
    }

    // Adds compatibility for ES modules
    debounce.debounce = debounce;

    var debounce_1 = debounce;

    function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }

    function _slicedToArray(arr, i) {
        return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _arrayWithHoles(arr) {
        if (Array.isArray(arr)) return arr;
    }

    function _iterableToArrayLimit(arr, i) {
        if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
            return;
        }

        var _arr = [];
        var _n = true;
        var _d = false;
        var _e = undefined;

        try {
            for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
                _arr.push(_s.value);

                if (i && _arr.length === i) break;
            }
        } catch (err) {
            _d = true;
            _e = err;
        } finally {
            try {
                if (!_n && _i["return"] != null) _i["return"]();
            } finally {
                if (_d) throw _e;
            }
        }

        return _arr;
    }

    function _nonIterableRest() {
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    var Prop = function Prop(name, _ref) {
        var _ref$default = _ref["default"],
            defaultVal = _ref$default === void 0 ? null : _ref$default,
            _ref$triggerUpdate = _ref.triggerUpdate,
            triggerUpdate = _ref$triggerUpdate === void 0 ? true : _ref$triggerUpdate,
            _ref$onChange = _ref.onChange,
            onChange = _ref$onChange === void 0 ? function (newVal, state) {
            } : _ref$onChange;

        _classCallCheck(this, Prop);

        this.name = name;
        this.defaultVal = defaultVal;
        this.triggerUpdate = triggerUpdate;
        this.onChange = onChange;
    };

    function index(_ref2) {
        var _ref2$stateInit = _ref2.stateInit,
            stateInit = _ref2$stateInit === void 0 ? function () {
                return {};
            } : _ref2$stateInit,
            _ref2$props = _ref2.props,
            rawProps = _ref2$props === void 0 ? {} : _ref2$props,
            _ref2$methods = _ref2.methods,
            methods = _ref2$methods === void 0 ? {} : _ref2$methods,
            _ref2$aliases = _ref2.aliases,
            aliases = _ref2$aliases === void 0 ? {} : _ref2$aliases,
            _ref2$init = _ref2.init,
            initFn = _ref2$init === void 0 ? function () {
            } : _ref2$init,
            _ref2$update = _ref2.update,
            updateFn = _ref2$update === void 0 ? function () {
            } : _ref2$update;
        // Parse props into Prop instances
        var props = Object.keys(rawProps).map(function (propName) {
            return new Prop(propName, rawProps[propName]);
        });
        return function () {
            var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
            // Holds component state
            var state = Object.assign({}, stateInit instanceof Function ? stateInit(options) : stateInit, // Support plain objects for backwards compatibility
                {
                    initialised: false
                }); // keeps track of which props triggered an update

            var changedProps = {}; // Component constructor

            function comp(nodeElement) {
                initStatic(nodeElement, options);
                digest();
                return comp;
            }

            var initStatic = function initStatic(nodeElement, options) {
                initFn.call(comp, nodeElement, state, options);
                state.initialised = true;
            };

            var digest = debounce_1(function () {
                if (!state.initialised) {
                    return;
                }

                updateFn.call(comp, state, changedProps);
                changedProps = {};
            }, 1); // Getter/setter methods

            props.forEach(function (prop) {
                comp[prop.name] = getSetProp(prop);

                function getSetProp(_ref3) {
                    var prop = _ref3.name,
                        _ref3$triggerUpdate = _ref3.triggerUpdate,
                        redigest = _ref3$triggerUpdate === void 0 ? false : _ref3$triggerUpdate,
                        _ref3$onChange = _ref3.onChange,
                        onChange = _ref3$onChange === void 0 ? function (newVal, state) {
                        } : _ref3$onChange,
                        _ref3$defaultVal = _ref3.defaultVal,
                        defaultVal = _ref3$defaultVal === void 0 ? null : _ref3$defaultVal;
                    return function (_) {
                        var curVal = state[prop];

                        if (!arguments.length) {
                            return curVal;
                        } // Getter mode


                        var val = _ === undefined ? defaultVal : _; // pick default if value passed is undefined

                        state[prop] = val;
                        onChange.call(comp, val, state, curVal); // track changed props

                        !changedProps.hasOwnProperty(prop) && (changedProps[prop] = curVal);

                        if (redigest) {
                            digest();
                        }

                        return comp;
                    };
                }
            }); // Other methods

            Object.keys(methods).forEach(function (methodName) {
                comp[methodName] = function () {
                    var _methods$methodName;

                    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
                        args[_key] = arguments[_key];
                    }

                    return (_methods$methodName = methods[methodName]).call.apply(_methods$methodName, [comp, state].concat(args));
                };
            }); // Link aliases

            Object.entries(aliases).forEach(function (_ref4) {
                var _ref5 = _slicedToArray(_ref4, 2),
                    alias = _ref5[0],
                    target = _ref5[1];

                return comp[alias] = comp[target];
            }); // Reset all component props to their default value

            comp.resetProps = function () {
                props.forEach(function (prop) {
                    comp[prop.name](prop.defaultVal);
                });
                return comp;
            }; //


            comp.resetProps(); // Apply all prop defaults

            state._rerender = digest; // Expose digest method

            return comp;
        };
    }

    function d3Ascending(a, b) {
        return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
    }

    function bisector(compare) {
        if (compare.length === 1) compare = ascendingComparator(compare);
        return {
            left: function (a, x, lo, hi) {
                if (lo == null) lo = 0;
                if (hi == null) hi = a.length;
                while (lo < hi) {
                    var mid = lo + hi >>> 1;
                    if (compare(a[mid], x) < 0) lo = mid + 1;
                    else hi = mid;
                }
                return lo;
            },
            right: function (a, x, lo, hi) {
                if (lo == null) lo = 0;
                if (hi == null) hi = a.length;
                while (lo < hi) {
                    var mid = lo + hi >>> 1;
                    if (compare(a[mid], x) > 0) hi = mid;
                    else lo = mid + 1;
                }
                return lo;
            }
        };
    }

    function ascendingComparator(f) {
        return function (d, x) {
            return d3Ascending(f(d), x);
        };
    }

    var ascendingBisect = bisector(d3Ascending);
    var bisectRight = ascendingBisect.right;

    function d3Range(start, stop, step) {
        start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;

        var i = -1,
            n = Math.max(0, Math.ceil((stop - start) / step)) | 0,
            range = new Array(n);

        while (++i < n) {
            range[i] = start + i * step;
        }

        return range;
    }

    var e10 = Math.sqrt(50),
        e5 = Math.sqrt(10),
        e2 = Math.sqrt(2);

    function ticks(start, stop, count) {
        var reverse,
            i = -1,
            n,
            ticks,
            step;

        stop = +stop, start = +start, count = +count;
        if (start === stop && count > 0) return [start];
        if (reverse = stop < start) n = start, start = stop, stop = n;
        if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) return [];

        if (step > 0) {
            start = Math.ceil(start / step);
            stop = Math.floor(stop / step);
            ticks = new Array(n = Math.ceil(stop - start + 1));
            while (++i < n) ticks[i] = (start + i) * step;
        } else {
            start = Math.floor(start * step);
            stop = Math.ceil(stop * step);
            ticks = new Array(n = Math.ceil(start - stop + 1));
            while (++i < n) ticks[i] = (start - i) / step;
        }

        if (reverse) ticks.reverse();

        return ticks;
    }

    function tickIncrement(start, stop, count) {
        var step = (stop - start) / Math.max(0, count),
            power = Math.floor(Math.log(step) / Math.LN10),
            error = step / Math.pow(10, power);
        return power >= 0
            ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
            : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
    }

    function tickStep(start, stop, count) {
        var step0 = Math.abs(stop - start) / Math.max(0, count),
            step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)),
            error = step0 / step1;
        if (error >= e10) step1 *= 10;
        else if (error >= e5) step1 *= 5;
        else if (error >= e2) step1 *= 2;
        return stop < start ? -step1 : step1;
    }

    function max(values, valueof) {
        let max;
        if (valueof === undefined) {
            for (const value of values) {
                if (value != null
                    && (max < value || (max === undefined && value >= value))) {
                    max = value;
                }
            }
        } else {
            let index = -1;
            for (let value of values) {
                if ((value = valueof(value, ++index, values)) != null
                    && (max < value || (max === undefined && value >= value))) {
                    max = value;
                }
            }
        }
        return max;
    }

    function min(values, valueof) {
        let min;
        if (valueof === undefined) {
            for (const value of values) {
                if (value != null
                    && (min > value || (min === undefined && value >= value))) {
                    min = value;
                }
            }
        } else {
            let index = -1;
            for (let value of values) {
                if ((value = valueof(value, ++index, values)) != null
                    && (min > value || (min === undefined && value >= value))) {
                    min = value;
                }
            }
        }
        return min;
    }

    var slice = Array.prototype.slice;

    function identity(x) {
        return x;
    }

    var top = 1,
        right = 2,
        bottom = 3,
        left = 4,
        epsilon = 1e-6;

    function translateX(x) {
        return "translate(" + (x + 0.5) + ",0)";
    }

    function translateY(y) {
        return "translate(0," + (y + 0.5) + ")";
    }

    function number(scale) {
        return function (d) {
            return +scale(d);
        };
    }

    function center(scale) {
        var offset = Math.max(0, scale.bandwidth() - 1) / 2; // Adjust for 0.5px offset.
        if (scale.round()) offset = Math.round(offset);
        return function (d) {
            return +scale(d) + offset;
        };
    }

    function entering() {
        return !this.__axis;
    }

    function axis(orient, scale) {
        var tickArguments = [],
            tickValues = null,
            tickFormat = null,
            tickSizeInner = 6,
            tickSizeOuter = 6,
            tickPadding = 3,
            k = orient === top || orient === left ? -1 : 1,
            x = orient === left || orient === right ? "x" : "y",
            transform = orient === top || orient === bottom ? translateX : translateY;

        function axis(context) {
            var values = tickValues == null ? (scale.ticks ? scale.ticks.apply(scale, tickArguments) : scale.domain()) : tickValues,
                format = tickFormat == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments) : identity) : tickFormat,
                spacing = Math.max(tickSizeInner, 0) + tickPadding,
                range = scale.range(),
                range0 = +range[0] + 0.5,
                range1 = +range[range.length - 1] + 0.5,
                position = (scale.bandwidth ? center : number)(scale.copy()),
                selection = context.selection ? context.selection() : context,
                path = selection.selectAll(".domain").data([null]),
                tick = selection.selectAll(".tick").data(values, scale).order(),
                tickExit = tick.exit(),
                tickEnter = tick.enter().append("g").attr("class", "tick"),
                line = tick.select("line"),
                text = tick.select("text");

            path = path.merge(path.enter().insert("path", ".tick")
                .attr("class", "domain")
                .attr("stroke", "currentColor"));

            tick = tick.merge(tickEnter);

            line = line.merge(tickEnter.append("line")
                .attr("stroke", "currentColor")
                .attr(x + "2", k * tickSizeInner));

            text = text.merge(tickEnter.append("text")
                .attr("fill", "currentColor")
                .attr(x, k * spacing)
                .attr("dy", orient === top ? "0em" : orient === bottom ? "0.71em" : "0.32em"));

            if (context !== selection) {
                path = path.transition(context);
                tick = tick.transition(context);
                line = line.transition(context);
                text = text.transition(context);

                tickExit = tickExit.transition(context)
                    .attr("opacity", epsilon)
                    .attr("transform", function (d) {
                        return isFinite(d = position(d)) ? transform(d) : this.getAttribute("transform");
                    });

                tickEnter
                    .attr("opacity", epsilon)
                    .attr("transform", function (d) {
                        var p = this.parentNode.__axis;
                        return transform(p && isFinite(p = p(d)) ? p : position(d));
                    });
            }

            tickExit.remove();

            path
                .attr("d", orient === left || orient == right
                    ? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H0.5V" + range1 + "H" + k * tickSizeOuter : "M0.5," + range0 + "V" + range1)
                    : (tickSizeOuter ? "M" + range0 + "," + k * tickSizeOuter + "V0.5H" + range1 + "V" + k * tickSizeOuter : "M" + range0 + ",0.5H" + range1));

            tick
                .attr("opacity", 1)
                .attr("transform", function (d) {
                    return transform(position(d));
                });

            line
                .attr(x + "2", k * tickSizeInner);

            text
                .attr(x, k * spacing)
                .text(format);

            selection.filter(entering)
                .attr("fill", "none")
                .attr("font-size", 10)
                .attr("font-family", "sans-serif")
                .attr("text-anchor", orient === right ? "start" : orient === left ? "end" : "middle");

            selection
                .each(function () {
                    this.__axis = position;
                });
        }

        axis.scale = function (_) {
            return arguments.length ? (scale = _, axis) : scale;
        };

        axis.ticks = function () {
            return tickArguments = slice.call(arguments), axis;
        };

        axis.tickArguments = function (_) {
            return arguments.length ? (tickArguments = _ == null ? [] : slice.call(_), axis) : tickArguments.slice();
        };

        axis.tickValues = function (_) {
            return arguments.length ? (tickValues = _ == null ? null : slice.call(_), axis) : tickValues && tickValues.slice();
        };

        axis.tickFormat = function (_) {
            return arguments.length ? (tickFormat = _, axis) : tickFormat;
        };

        axis.tickSize = function (_) {
            return arguments.length ? (tickSizeInner = tickSizeOuter = +_, axis) : tickSizeInner;
        };

        axis.tickSizeInner = function (_) {
            return arguments.length ? (tickSizeInner = +_, axis) : tickSizeInner;
        };

        axis.tickSizeOuter = function (_) {
            return arguments.length ? (tickSizeOuter = +_, axis) : tickSizeOuter;
        };

        axis.tickPadding = function (_) {
            return arguments.length ? (tickPadding = +_, axis) : tickPadding;
        };

        return axis;
    }

    function axisTop(scale) {
        return axis(top, scale);
    }

    function axisRight(scale) {
        return axis(right, scale);
    }

    function axisBottom(scale) {
        return axis(bottom, scale);
    }

    function axisLeft(scale) {
        return axis(left, scale);
    }

    function initRange(domain, range) {
        switch (arguments.length) {
            case 0:
                break;
            case 1:
                this.range(domain);
                break;
            default:
                this.range(range).domain(domain);
                break;
        }
        return this;
    }

    function initInterpolator(domain, interpolator) {
        switch (arguments.length) {
            case 0:
                break;
            case 1: {
                if (typeof domain === "function") this.interpolator(domain);
                else this.range(domain);
                break;
            }
            default: {
                this.domain(domain);
                if (typeof interpolator === "function") this.interpolator(interpolator);
                else this.range(interpolator);
                break;
            }
        }
        return this;
    }

    const implicit = Symbol("implicit");

    function ordinal() {
        var index = new Map(),
            domain = [],
            range = [],
            unknown = implicit;

        function scale(d) {
            var key = d + "", i = index.get(key);
            if (!i) {
                if (unknown !== implicit) return unknown;
                index.set(key, i = domain.push(d));
            }
            return range[(i - 1) % range.length];
        }

        scale.domain = function (_) {
            if (!arguments.length) return domain.slice();
            domain = [], index = new Map();
            for (const value of _) {
                const key = value + "";
                if (index.has(key)) continue;
                index.set(key, domain.push(value));
            }
            return scale;
        };

        scale.range = function (_) {
            return arguments.length ? (range = Array.from(_), scale) : range.slice();
        };

        scale.unknown = function (_) {
            return arguments.length ? (unknown = _, scale) : unknown;
        };

        scale.copy = function () {
            return ordinal(domain, range).unknown(unknown);
        };

        initRange.apply(scale, arguments);

        return scale;
    }

    function band() {
        var scale = ordinal().unknown(undefined),
            domain = scale.domain,
            ordinalRange = scale.range,
            r0 = 0,
            r1 = 1,
            step,
            bandwidth,
            round = false,
            paddingInner = 0,
            paddingOuter = 0,
            align = 0.5;

        delete scale.unknown;

        function rescale() {
            var n = domain().length,
                reverse = r1 < r0,
                start = reverse ? r1 : r0,
                stop = reverse ? r0 : r1;
            step = (stop - start) / Math.max(1, n - paddingInner + paddingOuter * 2);
            if (round) step = Math.floor(step);
            start += (stop - start - step * (n - paddingInner)) * align;
            bandwidth = step * (1 - paddingInner);
            if (round) start = Math.round(start), bandwidth = Math.round(bandwidth);
            var values = d3Range(n).map(function (i) {
                return start + step * i;
            });
            return ordinalRange(reverse ? values.reverse() : values);
        }

        scale.domain = function (_) {
            return arguments.length ? (domain(_), rescale()) : domain();
        };

        scale.range = function (_) {
            return arguments.length ? ([r0, r1] = _, r0 = +r0, r1 = +r1, rescale()) : [r0, r1];
        };

        scale.rangeRound = function (_) {
            return [r0, r1] = _, r0 = +r0, r1 = +r1, round = true, rescale();
        };

        scale.bandwidth = function () {
            return bandwidth;
        };

        scale.step = function () {
            return step;
        };

        scale.round = function (_) {
            return arguments.length ? (round = !!_, rescale()) : round;
        };

        scale.padding = function (_) {
            return arguments.length ? (paddingInner = Math.min(1, paddingOuter = +_), rescale()) : paddingInner;
        };

        scale.paddingInner = function (_) {
            return arguments.length ? (paddingInner = Math.min(1, _), rescale()) : paddingInner;
        };

        scale.paddingOuter = function (_) {
            return arguments.length ? (paddingOuter = +_, rescale()) : paddingOuter;
        };

        scale.align = function (_) {
            return arguments.length ? (align = Math.max(0, Math.min(1, _)), rescale()) : align;
        };

        scale.copy = function () {
            return band(domain(), [r0, r1])
                .round(round)
                .paddingInner(paddingInner)
                .paddingOuter(paddingOuter)
                .align(align);
        };

        return initRange.apply(rescale(), arguments);
    }

    function pointish(scale) {
        var copy = scale.copy;

        scale.padding = scale.paddingOuter;
        delete scale.paddingInner;
        delete scale.paddingOuter;

        scale.copy = function () {
            return pointish(copy());
        };

        return scale;
    }

    function point() {
        return pointish(band.apply(null, arguments).paddingInner(1));
    }

    function define(constructor, factory, prototype) {
        constructor.prototype = factory.prototype = prototype;
        prototype.constructor = constructor;
    }

    function extend(parent, definition) {
        var prototype = Object.create(parent.prototype);
        for (var key in definition) prototype[key] = definition[key];
        return prototype;
    }

    function Color() {
    }

    var darker = 0.7;
    var brighter = 1 / darker;

    var reI = "\\s*([+-]?\\d+)\\s*",
        reN = "\\s*([+-]?\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)\\s*",
        reP = "\\s*([+-]?\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)%\\s*",
        reHex = /^#([0-9a-f]{3,8})$/,
        reRgbInteger = new RegExp("^rgb\\(" + [reI, reI, reI] + "\\)$"),
        reRgbPercent = new RegExp("^rgb\\(" + [reP, reP, reP] + "\\)$"),
        reRgbaInteger = new RegExp("^rgba\\(" + [reI, reI, reI, reN] + "\\)$"),
        reRgbaPercent = new RegExp("^rgba\\(" + [reP, reP, reP, reN] + "\\)$"),
        reHslPercent = new RegExp("^hsl\\(" + [reN, reP, reP] + "\\)$"),
        reHslaPercent = new RegExp("^hsla\\(" + [reN, reP, reP, reN] + "\\)$");

    var named = {
        aliceblue: 0xf0f8ff,
        antiquewhite: 0xfaebd7,
        aqua: 0x00ffff,
        aquamarine: 0x7fffd4,
        azure: 0xf0ffff,
        beige: 0xf5f5dc,
        bisque: 0xffe4c4,
        black: 0x000000,
        blanchedalmond: 0xffebcd,
        blue: 0x0000ff,
        blueviolet: 0x8a2be2,
        brown: 0xa52a2a,
        burlywood: 0xdeb887,
        cadetblue: 0x5f9ea0,
        chartreuse: 0x7fff00,
        chocolate: 0xd2691e,
        coral: 0xff7f50,
        cornflowerblue: 0x6495ed,
        cornsilk: 0xfff8dc,
        crimson: 0xdc143c,
        cyan: 0x00ffff,
        darkblue: 0x00008b,
        darkcyan: 0x008b8b,
        darkgoldenrod: 0xb8860b,
        darkgray: 0xa9a9a9,
        darkgreen: 0x006400,
        darkgrey: 0xa9a9a9,
        darkkhaki: 0xbdb76b,
        darkmagenta: 0x8b008b,
        darkolivegreen: 0x556b2f,
        darkorange: 0xff8c00,
        darkorchid: 0x9932cc,
        darkred: 0x8b0000,
        darksalmon: 0xe9967a,
        darkseagreen: 0x8fbc8f,
        darkslateblue: 0x483d8b,
        darkslategray: 0x2f4f4f,
        darkslategrey: 0x2f4f4f,
        darkturquoise: 0x00ced1,
        darkviolet: 0x9400d3,
        deeppink: 0xff1493,
        deepskyblue: 0x00bfff,
        dimgray: 0x696969,
        dimgrey: 0x696969,
        dodgerblue: 0x1e90ff,
        firebrick: 0xb22222,
        floralwhite: 0xfffaf0,
        forestgreen: 0x228b22,
        fuchsia: 0xff00ff,
        gainsboro: 0xdcdcdc,
        ghostwhite: 0xf8f8ff,
        gold: 0xffd700,
        goldenrod: 0xdaa520,
        gray: 0x808080,
        green: 0x008000,
        greenyellow: 0xadff2f,
        grey: 0x808080,
        honeydew: 0xf0fff0,
        hotpink: 0xff69b4,
        indianred: 0xcd5c5c,
        indigo: 0x4b0082,
        ivory: 0xfffff0,
        khaki: 0xf0e68c,
        lavender: 0xe6e6fa,
        lavenderblush: 0xfff0f5,
        lawngreen: 0x7cfc00,
        lemonchiffon: 0xfffacd,
        lightblue: 0xadd8e6,
        lightcoral: 0xf08080,
        lightcyan: 0xe0ffff,
        lightgoldenrodyellow: 0xfafad2,
        lightgray: 0xd3d3d3,
        lightgreen: 0x90ee90,
        lightgrey: 0xd3d3d3,
        lightpink: 0xffb6c1,
        lightsalmon: 0xffa07a,
        lightseagreen: 0x20b2aa,
        lightskyblue: 0x87cefa,
        lightslategray: 0x778899,
        lightslategrey: 0x778899,
        lightsteelblue: 0xb0c4de,
        lightyellow: 0xffffe0,
        lime: 0x00ff00,
        limegreen: 0x32cd32,
        linen: 0xfaf0e6,
        magenta: 0xff00ff,
        maroon: 0x800000,
        mediumaquamarine: 0x66cdaa,
        mediumblue: 0x0000cd,
        mediumorchid: 0xba55d3,
        mediumpurple: 0x9370db,
        mediumseagreen: 0x3cb371,
        mediumslateblue: 0x7b68ee,
        mediumspringgreen: 0x00fa9a,
        mediumturquoise: 0x48d1cc,
        mediumvioletred: 0xc71585,
        midnightblue: 0x191970,
        mintcream: 0xf5fffa,
        mistyrose: 0xffe4e1,
        moccasin: 0xffe4b5,
        navajowhite: 0xffdead,
        navy: 0x000080,
        oldlace: 0xfdf5e6,
        olive: 0x808000,
        olivedrab: 0x6b8e23,
        orange: 0xffa500,
        orangered: 0xff4500,
        orchid: 0xda70d6,
        palegoldenrod: 0xeee8aa,
        palegreen: 0x98fb98,
        paleturquoise: 0xafeeee,
        palevioletred: 0xdb7093,
        papayawhip: 0xffefd5,
        peachpuff: 0xffdab9,
        peru: 0xcd853f,
        pink: 0xffc0cb,
        plum: 0xdda0dd,
        powderblue: 0xb0e0e6,
        purple: 0x800080,
        rebeccapurple: 0x663399,
        red: 0xff0000,
        rosybrown: 0xbc8f8f,
        royalblue: 0x4169e1,
        saddlebrown: 0x8b4513,
        salmon: 0xfa8072,
        sandybrown: 0xf4a460,
        seagreen: 0x2e8b57,
        seashell: 0xfff5ee,
        sienna: 0xa0522d,
        silver: 0xc0c0c0,
        skyblue: 0x87ceeb,
        slateblue: 0x6a5acd,
        slategray: 0x708090,
        slategrey: 0x708090,
        snow: 0xfffafa,
        springgreen: 0x00ff7f,
        steelblue: 0x4682b4,
        tan: 0xd2b48c,
        teal: 0x008080,
        thistle: 0xd8bfd8,
        tomato: 0xff6347,
        turquoise: 0x40e0d0,
        violet: 0xee82ee,
        wheat: 0xf5deb3,
        white: 0xffffff,
        whitesmoke: 0xf5f5f5,
        yellow: 0xffff00,
        yellowgreen: 0x9acd32
    };

    define(Color, color, {
        copy: function (channels) {
            return Object.assign(new this.constructor, this, channels);
        },
        displayable: function () {
            return this.rgb().displayable();
        },
        hex: color_formatHex, // Deprecated! Use color.formatHex.
        formatHex: color_formatHex,
        formatHsl: color_formatHsl,
        formatRgb: color_formatRgb,
        toString: color_formatRgb
    });

    function color_formatHex() {
        return this.rgb().formatHex();
    }

    function color_formatHsl() {
        return hslConvert(this).formatHsl();
    }

    function color_formatRgb() {
        return this.rgb().formatRgb();
    }

    function color(format) {
        var m, l;
        format = (format + "").trim().toLowerCase();
        return (m = reHex.exec(format)) ? (l = m[1].length, m = parseInt(m[1], 16), l === 6 ? rgbn(m) // #ff0000
            : l === 3 ? new Rgb((m >> 8 & 0xf) | (m >> 4 & 0xf0), (m >> 4 & 0xf) | (m & 0xf0), ((m & 0xf) << 4) | (m & 0xf), 1) // #f00
                : l === 8 ? new Rgb(m >> 24 & 0xff, m >> 16 & 0xff, m >> 8 & 0xff, (m & 0xff) / 0xff) // #ff000000
                    : l === 4 ? new Rgb((m >> 12 & 0xf) | (m >> 8 & 0xf0), (m >> 8 & 0xf) | (m >> 4 & 0xf0), (m >> 4 & 0xf) | (m & 0xf0), (((m & 0xf) << 4) | (m & 0xf)) / 0xff) // #f000
                        : null) // invalid hex
            : (m = reRgbInteger.exec(format)) ? new Rgb(m[1], m[2], m[3], 1) // rgb(255, 0, 0)
                : (m = reRgbPercent.exec(format)) ? new Rgb(m[1] * 255 / 100, m[2] * 255 / 100, m[3] * 255 / 100, 1) // rgb(100%, 0%, 0%)
                    : (m = reRgbaInteger.exec(format)) ? rgba(m[1], m[2], m[3], m[4]) // rgba(255, 0, 0, 1)
                        : (m = reRgbaPercent.exec(format)) ? rgba(m[1] * 255 / 100, m[2] * 255 / 100, m[3] * 255 / 100, m[4]) // rgb(100%, 0%, 0%, 1)
                            : (m = reHslPercent.exec(format)) ? hsla(m[1], m[2] / 100, m[3] / 100, 1) // hsl(120, 50%, 50%)
                                : (m = reHslaPercent.exec(format)) ? hsla(m[1], m[2] / 100, m[3] / 100, m[4]) // hsla(120, 50%, 50%, 1)
                                    : named.hasOwnProperty(format) ? rgbn(named[format]) // eslint-disable-line no-prototype-builtins
                                        : format === "transparent" ? new Rgb(NaN, NaN, NaN, 0)
                                            : null;
    }

    function rgbn(n) {
        return new Rgb(n >> 16 & 0xff, n >> 8 & 0xff, n & 0xff, 1);
    }

    function rgba(r, g, b, a) {
        if (a <= 0) r = g = b = NaN;
        return new Rgb(r, g, b, a);
    }

    function rgbConvert(o) {
        if (!(o instanceof Color)) o = color(o);
        if (!o) return new Rgb;
        o = o.rgb();
        return new Rgb(o.r, o.g, o.b, o.opacity);
    }

    function rgb(r, g, b, opacity) {
        return arguments.length === 1 ? rgbConvert(r) : new Rgb(r, g, b, opacity == null ? 1 : opacity);
    }

    function Rgb(r, g, b, opacity) {
        this.r = +r;
        this.g = +g;
        this.b = +b;
        this.opacity = +opacity;
    }

    define(Rgb, rgb, extend(Color, {
        brighter: function (k) {
            k = k == null ? brighter : Math.pow(brighter, k);
            return new Rgb(this.r * k, this.g * k, this.b * k, this.opacity);
        },
        darker: function (k) {
            k = k == null ? darker : Math.pow(darker, k);
            return new Rgb(this.r * k, this.g * k, this.b * k, this.opacity);
        },
        rgb: function () {
            return this;
        },
        displayable: function () {
            return (-0.5 <= this.r && this.r < 255.5)
                && (-0.5 <= this.g && this.g < 255.5)
                && (-0.5 <= this.b && this.b < 255.5)
                && (0 <= this.opacity && this.opacity <= 1);
        },
        hex: rgb_formatHex, // Deprecated! Use color.formatHex.
        formatHex: rgb_formatHex,
        formatRgb: rgb_formatRgb,
        toString: rgb_formatRgb
    }));

    function rgb_formatHex() {
        return "#" + hex(this.r) + hex(this.g) + hex(this.b);
    }

    function rgb_formatRgb() {
        var a = this.opacity;
        a = isNaN(a) ? 1 : Math.max(0, Math.min(1, a));
        return (a === 1 ? "rgb(" : "rgba(")
            + Math.max(0, Math.min(255, Math.round(this.r) || 0)) + ", "
            + Math.max(0, Math.min(255, Math.round(this.g) || 0)) + ", "
            + Math.max(0, Math.min(255, Math.round(this.b) || 0))
            + (a === 1 ? ")" : ", " + a + ")");
    }

    function hex(value) {
        value = Math.max(0, Math.min(255, Math.round(value) || 0));
        return (value < 16 ? "0" : "") + value.toString(16);
    }

    function hsla(h, s, l, a) {
        if (a <= 0) h = s = l = NaN;
        else if (l <= 0 || l >= 1) h = s = NaN;
        else if (s <= 0) h = NaN;
        return new Hsl(h, s, l, a);
    }

    function hslConvert(o) {
        if (o instanceof Hsl) return new Hsl(o.h, o.s, o.l, o.opacity);
        if (!(o instanceof Color)) o = color(o);
        if (!o) return new Hsl;
        if (o instanceof Hsl) return o;
        o = o.rgb();
        var r = o.r / 255,
            g = o.g / 255,
            b = o.b / 255,
            min = Math.min(r, g, b),
            max = Math.max(r, g, b),
            h = NaN,
            s = max - min,
            l = (max + min) / 2;
        if (s) {
            if (r === max) h = (g - b) / s + (g < b) * 6;
            else if (g === max) h = (b - r) / s + 2;
            else h = (r - g) / s + 4;
            s /= l < 0.5 ? max + min : 2 - max - min;
            h *= 60;
        } else {
            s = l > 0 && l < 1 ? 0 : h;
        }
        return new Hsl(h, s, l, o.opacity);
    }

    function hsl(h, s, l, opacity) {
        return arguments.length === 1 ? hslConvert(h) : new Hsl(h, s, l, opacity == null ? 1 : opacity);
    }

    function Hsl(h, s, l, opacity) {
        this.h = +h;
        this.s = +s;
        this.l = +l;
        this.opacity = +opacity;
    }

    define(Hsl, hsl, extend(Color, {
        brighter: function (k) {
            k = k == null ? brighter : Math.pow(brighter, k);
            return new Hsl(this.h, this.s, this.l * k, this.opacity);
        },
        darker: function (k) {
            k = k == null ? darker : Math.pow(darker, k);
            return new Hsl(this.h, this.s, this.l * k, this.opacity);
        },
        rgb: function () {
            var h = this.h % 360 + (this.h < 0) * 360,
                s = isNaN(h) || isNaN(this.s) ? 0 : this.s,
                l = this.l,
                m2 = l + (l < 0.5 ? l : 1 - l) * s,
                m1 = 2 * l - m2;
            return new Rgb(
                hsl2rgb(h >= 240 ? h - 240 : h + 120, m1, m2),
                hsl2rgb(h, m1, m2),
                hsl2rgb(h < 120 ? h + 240 : h - 120, m1, m2),
                this.opacity
            );
        },
        displayable: function () {
            return (0 <= this.s && this.s <= 1 || isNaN(this.s))
                && (0 <= this.l && this.l <= 1)
                && (0 <= this.opacity && this.opacity <= 1);
        },
        formatHsl: function () {
            var a = this.opacity;
            a = isNaN(a) ? 1 : Math.max(0, Math.min(1, a));
            return (a === 1 ? "hsl(" : "hsla(")
                + (this.h || 0) + ", "
                + (this.s || 0) * 100 + "%, "
                + (this.l || 0) * 100 + "%"
                + (a === 1 ? ")" : ", " + a + ")");
        }
    }));

    /* From FvD 13.37, CSS Color Module Level 3 */
    function hsl2rgb(h, m1, m2) {
        return (h < 60 ? m1 + (m2 - m1) * h / 60
            : h < 180 ? m2
                : h < 240 ? m1 + (m2 - m1) * (240 - h) / 60
                    : m1) * 255;
    }

    function basis(t1, v0, v1, v2, v3) {
        var t2 = t1 * t1, t3 = t2 * t1;
        return ((1 - 3 * t1 + 3 * t2 - t3) * v0
            + (4 - 6 * t2 + 3 * t3) * v1
            + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
            + t3 * v3) / 6;
    }

    function basis$1(values) {
        var n = values.length - 1;
        return function (t) {
            var i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n),
                v1 = values[i],
                v2 = values[i + 1],
                v0 = i > 0 ? values[i - 1] : 2 * v1 - v2,
                v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
            return basis((t - i / n) * n, v0, v1, v2, v3);
        };
    }

    function constant(x) {
        return function () {
            return x;
        };
    }

    function linear(a, d) {
        return function (t) {
            return a + t * d;
        };
    }

    function exponential(a, b, y) {
        return a = Math.pow(a, y), b = Math.pow(b, y) - a, y = 1 / y, function (t) {
            return Math.pow(a + t * b, y);
        };
    }

    function gamma(y) {
        return (y = +y) === 1 ? nogamma : function (a, b) {
            return b - a ? exponential(a, b, y) : constant(isNaN(a) ? b : a);
        };
    }

    function nogamma(a, b) {
        var d = b - a;
        return d ? linear(a, d) : constant(isNaN(a) ? b : a);
    }

    var interpolateRgb = (function rgbGamma(y) {
        var color = gamma(y);

        function rgb$1(start, end) {
            var r = color((start = rgb(start)).r, (end = rgb(end)).r),
                g = color(start.g, end.g),
                b = color(start.b, end.b),
                opacity = nogamma(start.opacity, end.opacity);
            return function (t) {
                start.r = r(t);
                start.g = g(t);
                start.b = b(t);
                start.opacity = opacity(t);
                return start + "";
            };
        }

        rgb$1.gamma = rgbGamma;

        return rgb$1;
    })(1);

    function rgbSpline(spline) {
        return function (colors) {
            var n = colors.length,
                r = new Array(n),
                g = new Array(n),
                b = new Array(n),
                i, color;
            for (i = 0; i < n; ++i) {
                color = rgb(colors[i]);
                r[i] = color.r || 0;
                g[i] = color.g || 0;
                b[i] = color.b || 0;
            }
            r = spline(r);
            g = spline(g);
            b = spline(b);
            color.opacity = 1;
            return function (t) {
                color.r = r(t);
                color.g = g(t);
                color.b = b(t);
                return color + "";
            };
        };
    }

    var rgbBasis = rgbSpline(basis$1);

    function numberArray(a, b) {
        if (!b) b = [];
        var n = a ? Math.min(b.length, a.length) : 0,
            c = b.slice(),
            i;
        return function (t) {
            for (i = 0; i < n; ++i) c[i] = a[i] * (1 - t) + b[i] * t;
            return c;
        };
    }

    function isNumberArray(x) {
        return ArrayBuffer.isView(x) && !(x instanceof DataView);
    }

    function genericArray(a, b) {
        var nb = b ? b.length : 0,
            na = a ? Math.min(nb, a.length) : 0,
            x = new Array(na),
            c = new Array(nb),
            i;

        for (i = 0; i < na; ++i) x[i] = interpolate(a[i], b[i]);
        for (; i < nb; ++i) c[i] = b[i];

        return function (t) {
            for (i = 0; i < na; ++i) c[i] = x[i](t);
            return c;
        };
    }

    function date(a, b) {
        var d = new Date;
        return a = +a, b = +b, function (t) {
            return d.setTime(a * (1 - t) + b * t), d;
        };
    }

    function interpolateNumber(a, b) {
        return a = +a, b = +b, function (t) {
            return a * (1 - t) + b * t;
        };
    }

    function object(a, b) {
        var i = {},
            c = {},
            k;

        if (a === null || typeof a !== "object") a = {};
        if (b === null || typeof b !== "object") b = {};

        for (k in b) {
            if (k in a) {
                i[k] = interpolate(a[k], b[k]);
            } else {
                c[k] = b[k];
            }
        }

        return function (t) {
            for (k in i) c[k] = i[k](t);
            return c;
        };
    }

    var reA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g,
        reB = new RegExp(reA.source, "g");

    function zero(b) {
        return function () {
            return b;
        };
    }

    function one(b) {
        return function (t) {
            return b(t) + "";
        };
    }

    function interpolateString(a, b) {
        var bi = reA.lastIndex = reB.lastIndex = 0, // scan index for next number in b
            am, // current match in a
            bm, // current match in b
            bs, // string preceding current number in b, if any
            i = -1, // index in s
            s = [], // string constants and placeholders
            q = []; // number interpolators

        // Coerce inputs to strings.
        a = a + "", b = b + "";

        // Interpolate pairs of numbers in a & b.
        while ((am = reA.exec(a))
        && (bm = reB.exec(b))) {
            if ((bs = bm.index) > bi) { // a string precedes the next number in b
                bs = b.slice(bi, bs);
                if (s[i]) s[i] += bs; // coalesce with previous string
                else s[++i] = bs;
            }
            if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
                if (s[i]) s[i] += bm; // coalesce with previous string
                else s[++i] = bm;
            } else { // interpolate non-matching numbers
                s[++i] = null;
                q.push({i: i, x: interpolateNumber(am, bm)});
            }
            bi = reB.lastIndex;
        }

        // Add remains of b.
        if (bi < b.length) {
            bs = b.slice(bi);
            if (s[i]) s[i] += bs; // coalesce with previous string
            else s[++i] = bs;
        }

        // Special optimization for only a single match.
        // Otherwise, interpolate each of the numbers and rejoin the string.
        return s.length < 2 ? (q[0]
            ? one(q[0].x)
            : zero(b))
            : (b = q.length, function (t) {
                for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
                return s.join("");
            });
    }

    function interpolate(a, b) {
        var t = typeof b, c;
        return b == null || t === "boolean" ? constant(b)
            : (t === "number" ? interpolateNumber
                : t === "string" ? ((c = color(b)) ? (b = c, interpolateRgb) : interpolateString)
                    : b instanceof color ? interpolateRgb
                        : b instanceof Date ? date
                            : isNumberArray(b) ? numberArray
                                : Array.isArray(b) ? genericArray
                                    : typeof b.valueOf !== "function" && typeof b.toString !== "function" || isNaN(b) ? object
                                        : interpolateNumber)(a, b);
    }

    function interpolateRound(a, b) {
        return a = +a, b = +b, function (t) {
            return Math.round(a * (1 - t) + b * t);
        };
    }

    var degrees = 180 / Math.PI;

    var identity$1 = {
        translateX: 0,
        translateY: 0,
        rotate: 0,
        skewX: 0,
        scaleX: 1,
        scaleY: 1
    };

    function decompose(a, b, c, d, e, f) {
        var scaleX, scaleY, skewX;
        if (scaleX = Math.sqrt(a * a + b * b)) a /= scaleX, b /= scaleX;
        if (skewX = a * c + b * d) c -= a * skewX, d -= b * skewX;
        if (scaleY = Math.sqrt(c * c + d * d)) c /= scaleY, d /= scaleY, skewX /= scaleY;
        if (a * d < b * c) a = -a, b = -b, skewX = -skewX, scaleX = -scaleX;
        return {
            translateX: e,
            translateY: f,
            rotate: Math.atan2(b, a) * degrees,
            skewX: Math.atan(skewX) * degrees,
            scaleX: scaleX,
            scaleY: scaleY
        };
    }

    var cssNode,
        cssRoot,
        cssView,
        svgNode;

    function parseCss(value) {
        if (value === "none") return identity$1;
        if (!cssNode) cssNode = document.createElement("DIV"), cssRoot = document.documentElement, cssView = document.defaultView;
        cssNode.style.transform = value;
        value = cssView.getComputedStyle(cssRoot.appendChild(cssNode), null).getPropertyValue("transform");
        cssRoot.removeChild(cssNode);
        value = value.slice(7, -1).split(",");
        return decompose(+value[0], +value[1], +value[2], +value[3], +value[4], +value[5]);
    }

    function parseSvg(value) {
        if (value == null) return identity$1;
        if (!svgNode) svgNode = document.createElementNS("http://www.w3.org/2000/svg", "g");
        svgNode.setAttribute("transform", value);
        if (!(value = svgNode.transform.baseVal.consolidate())) return identity$1;
        value = value.matrix;
        return decompose(value.a, value.b, value.c, value.d, value.e, value.f);
    }

    function interpolateTransform(parse, pxComma, pxParen, degParen) {

        function pop(s) {
            return s.length ? s.pop() + " " : "";
        }

        function translate(xa, ya, xb, yb, s, q) {
            if (xa !== xb || ya !== yb) {
                var i = s.push("translate(", null, pxComma, null, pxParen);
                q.push({i: i - 4, x: interpolateNumber(xa, xb)}, {i: i - 2, x: interpolateNumber(ya, yb)});
            } else if (xb || yb) {
                s.push("translate(" + xb + pxComma + yb + pxParen);
            }
        }

        function rotate(a, b, s, q) {
            if (a !== b) {
                if (a - b > 180) b += 360; else if (b - a > 180) a += 360; // shortest path
                q.push({i: s.push(pop(s) + "rotate(", null, degParen) - 2, x: interpolateNumber(a, b)});
            } else if (b) {
                s.push(pop(s) + "rotate(" + b + degParen);
            }
        }

        function skewX(a, b, s, q) {
            if (a !== b) {
                q.push({i: s.push(pop(s) + "skewX(", null, degParen) - 2, x: interpolateNumber(a, b)});
            } else if (b) {
                s.push(pop(s) + "skewX(" + b + degParen);
            }
        }

        function scale(xa, ya, xb, yb, s, q) {
            if (xa !== xb || ya !== yb) {
                var i = s.push(pop(s) + "scale(", null, ",", null, ")");
                q.push({i: i - 4, x: interpolateNumber(xa, xb)}, {i: i - 2, x: interpolateNumber(ya, yb)});
            } else if (xb !== 1 || yb !== 1) {
                s.push(pop(s) + "scale(" + xb + "," + yb + ")");
            }
        }

        return function (a, b) {
            var s = [], // string constants and placeholders
                q = []; // number interpolators
            a = parse(a), b = parse(b);
            translate(a.translateX, a.translateY, b.translateX, b.translateY, s, q);
            rotate(a.rotate, b.rotate, s, q);
            skewX(a.skewX, b.skewX, s, q);
            scale(a.scaleX, a.scaleY, b.scaleX, b.scaleY, s, q);
            a = b = null; // gc
            return function (t) {
                var i = -1, n = q.length, o;
                while (++i < n) s[(o = q[i]).i] = o.x(t);
                return s.join("");
            };
        };
    }

    var interpolateTransformCss = interpolateTransform(parseCss, "px, ", "px)", "deg)");
    var interpolateTransformSvg = interpolateTransform(parseSvg, ", ", ")", ")");

    function constant$1(x) {
        return function () {
            return x;
        };
    }

    function number$1(x) {
        return +x;
    }

    var unit = [0, 1];

    function identity$2(x) {
        return x;
    }

    function normalize(a, b) {
        return (b -= (a = +a))
            ? function (x) {
                return (x - a) / b;
            }
            : constant$1(isNaN(b) ? NaN : 0.5);
    }

    function clamper(a, b) {
        var t;
        if (a > b) t = a, a = b, b = t;
        return function (x) {
            return Math.max(a, Math.min(b, x));
        };
    }

    // normalize(a, b)(x) takes a domain value x in [a,b] and returns the corresponding parameter t in [0,1].
    // interpolate(a, b)(t) takes a parameter t in [0,1] and returns the corresponding range value x in [a,b].
    function bimap(domain, range, interpolate) {
        var d0 = domain[0], d1 = domain[1], r0 = range[0], r1 = range[1];
        if (d1 < d0) d0 = normalize(d1, d0), r0 = interpolate(r1, r0);
        else d0 = normalize(d0, d1), r0 = interpolate(r0, r1);
        return function (x) {
            return r0(d0(x));
        };
    }

    function polymap(domain, range, interpolate) {
        var j = Math.min(domain.length, range.length) - 1,
            d = new Array(j),
            r = new Array(j),
            i = -1;

        // Reverse descending domains.
        if (domain[j] < domain[0]) {
            domain = domain.slice().reverse();
            range = range.slice().reverse();
        }

        while (++i < j) {
            d[i] = normalize(domain[i], domain[i + 1]);
            r[i] = interpolate(range[i], range[i + 1]);
        }

        return function (x) {
            var i = bisectRight(domain, x, 1, j) - 1;
            return r[i](d[i](x));
        };
    }

    function copy(source, target) {
        return target
            .domain(source.domain())
            .range(source.range())
            .interpolate(source.interpolate())
            .clamp(source.clamp())
            .unknown(source.unknown());
    }

    function transformer() {
        var domain = unit,
            range = unit,
            interpolate$1 = interpolate,
            transform,
            untransform,
            unknown,
            clamp = identity$2,
            piecewise,
            output,
            input;

        function rescale() {
            var n = Math.min(domain.length, range.length);
            if (clamp !== identity$2) clamp = clamper(domain[0], domain[n - 1]);
            piecewise = n > 2 ? polymap : bimap;
            output = input = null;
            return scale;
        }

        function scale(x) {
            return isNaN(x = +x) ? unknown : (output || (output = piecewise(domain.map(transform), range, interpolate$1)))(transform(clamp(x)));
        }

        scale.invert = function (y) {
            return clamp(untransform((input || (input = piecewise(range, domain.map(transform), interpolateNumber)))(y)));
        };

        scale.domain = function (_) {
            return arguments.length ? (domain = Array.from(_, number$1), rescale()) : domain.slice();
        };

        scale.range = function (_) {
            return arguments.length ? (range = Array.from(_), rescale()) : range.slice();
        };

        scale.rangeRound = function (_) {
            return range = Array.from(_), interpolate$1 = interpolateRound, rescale();
        };

        scale.clamp = function (_) {
            return arguments.length ? (clamp = _ ? true : identity$2, rescale()) : clamp !== identity$2;
        };

        scale.interpolate = function (_) {
            return arguments.length ? (interpolate$1 = _, rescale()) : interpolate$1;
        };

        scale.unknown = function (_) {
            return arguments.length ? (unknown = _, scale) : unknown;
        };

        return function (t, u) {
            transform = t, untransform = u;
            return rescale();
        };
    }

    function continuous() {
        return transformer()(identity$2, identity$2);
    }

    // Computes the decimal coefficient and exponent of the specified number x with
    // significant digits p, where x is positive and p is in [1, 21] or undefined.
    // For example, formatDecimal(1.23) returns ["123", 0].
    function formatDecimal(x, p) {
        if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
        var i, coefficient = x.slice(0, i);

        // The string returned by toExponential either has the form \d\.\d+e[-+]\d+
        // (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
        return [
            coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
            +x.slice(i + 1)
        ];
    }

    function exponent(x) {
        return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN;
    }

    function formatGroup(grouping, thousands) {
        return function (value, width) {
            var i = value.length,
                t = [],
                j = 0,
                g = grouping[0],
                length = 0;

            while (i > 0 && g > 0) {
                if (length + g + 1 > width) g = Math.max(1, width - length);
                t.push(value.substring(i -= g, i + g));
                if ((length += g + 1) > width) break;
                g = grouping[j = (j + 1) % grouping.length];
            }

            return t.reverse().join(thousands);
        };
    }

    function formatNumerals(numerals) {
        return function (value) {
            return value.replace(/[0-9]/g, function (i) {
                return numerals[+i];
            });
        };
    }

    // [[fill]align][sign][symbol][0][width][,][.precision][~][type]
    var re = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;

    function formatSpecifier(specifier) {
        if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
        var match;
        return new FormatSpecifier({
            fill: match[1],
            align: match[2],
            sign: match[3],
            symbol: match[4],
            zero: match[5],
            width: match[6],
            comma: match[7],
            precision: match[8] && match[8].slice(1),
            trim: match[9],
            type: match[10]
        });
    }

    formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof

    function FormatSpecifier(specifier) {
        this.fill = specifier.fill === undefined ? " " : specifier.fill + "";
        this.align = specifier.align === undefined ? ">" : specifier.align + "";
        this.sign = specifier.sign === undefined ? "-" : specifier.sign + "";
        this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + "";
        this.zero = !!specifier.zero;
        this.width = specifier.width === undefined ? undefined : +specifier.width;
        this.comma = !!specifier.comma;
        this.precision = specifier.precision === undefined ? undefined : +specifier.precision;
        this.trim = !!specifier.trim;
        this.type = specifier.type === undefined ? "" : specifier.type + "";
    }

    FormatSpecifier.prototype.toString = function () {
        return this.fill
            + this.align
            + this.sign
            + this.symbol
            + (this.zero ? "0" : "")
            + (this.width === undefined ? "" : Math.max(1, this.width | 0))
            + (this.comma ? "," : "")
            + (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0))
            + (this.trim ? "~" : "")
            + this.type;
    };

    // Trims insignificant zeros, e.g., replaces 1.2000k with 1.2k.
    function formatTrim(s) {
        out: for (var n = s.length, i = 1, i0 = -1, i1; i < n; ++i) {
            switch (s[i]) {
                case ".":
                    i0 = i1 = i;
                    break;
                case "0":
                    if (i0 === 0) i0 = i;
                    i1 = i;
                    break;
                default:
                    if (!+s[i]) break out;
                    if (i0 > 0) i0 = 0;
                    break;
            }
        }
        return i0 > 0 ? s.slice(0, i0) + s.slice(i1 + 1) : s;
    }

    var prefixExponent;

    function formatPrefixAuto(x, p) {
        var d = formatDecimal(x, p);
        if (!d) return x + "";
        var coefficient = d[0],
            exponent = d[1],
            i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
            n = coefficient.length;
        return i === n ? coefficient
            : i > n ? coefficient + new Array(i - n + 1).join("0")
                : i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
                    : "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y!
    }

    function formatRounded(x, p) {
        var d = formatDecimal(x, p);
        if (!d) return x + "";
        var coefficient = d[0],
            exponent = d[1];
        return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
            : coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
                : coefficient + new Array(exponent - coefficient.length + 2).join("0");
    }

    var formatTypes = {
        "%": function (x, p) {
            return (x * 100).toFixed(p);
        },
        "b": function (x) {
            return Math.round(x).toString(2);
        },
        "c": function (x) {
            return x + "";
        },
        "d": function (x) {
            return Math.round(x).toString(10);
        },
        "e": function (x, p) {
            return x.toExponential(p);
        },
        "f": function (x, p) {
            return x.toFixed(p);
        },
        "g": function (x, p) {
            return x.toPrecision(p);
        },
        "o": function (x) {
            return Math.round(x).toString(8);
        },
        "p": function (x, p) {
            return formatRounded(x * 100, p);
        },
        "r": formatRounded,
        "s": formatPrefixAuto,
        "X": function (x) {
            return Math.round(x).toString(16).toUpperCase();
        },
        "x": function (x) {
            return Math.round(x).toString(16);
        }
    };

    function identity$3(x) {
        return x;
    }

    var map = Array.prototype.map,
        prefixes = ["y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y"];

    function formatLocale(locale) {
        var group = locale.grouping === undefined || locale.thousands === undefined ? identity$3 : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""),
            currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "",
            currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "",
            decimal = locale.decimal === undefined ? "." : locale.decimal + "",
            numerals = locale.numerals === undefined ? identity$3 : formatNumerals(map.call(locale.numerals, String)),
            percent = locale.percent === undefined ? "%" : locale.percent + "",
            minus = locale.minus === undefined ? "-" : locale.minus + "",
            nan = locale.nan === undefined ? "NaN" : locale.nan + "";

        function newFormat(specifier) {
            specifier = formatSpecifier(specifier);

            var fill = specifier.fill,
                align = specifier.align,
                sign = specifier.sign,
                symbol = specifier.symbol,
                zero = specifier.zero,
                width = specifier.width,
                comma = specifier.comma,
                precision = specifier.precision,
                trim = specifier.trim,
                type = specifier.type;

            // The "n" type is an alias for ",g".
            if (type === "n") comma = true, type = "g";

            // The "" type, and any invalid type, is an alias for ".12~g".
            else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";

            // If zero fill is specified, padding goes after sign and before digits.
            if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";

            // Compute the prefix and suffix.
            // For SI-prefix, the suffix is lazily computed.
            var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
                suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";

            // What format function should we use?
            // Is this an integer type?
            // Can this type generate exponential notation?
            var formatType = formatTypes[type],
                maybeSuffix = /[defgprs%]/.test(type);

            // Set the default precision if not specified,
            // or clamp the specified precision to the supported range.
            // For significant precision, it must be in [1, 21].
            // For fixed precision, it must be in [0, 20].
            precision = precision === undefined ? 6
                : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
                    : Math.max(0, Math.min(20, precision));

            function format(value) {
                var valuePrefix = prefix,
                    valueSuffix = suffix,
                    i, n, c;

                if (type === "c") {
                    valueSuffix = formatType(value) + valueSuffix;
                    value = "";
                } else {
                    value = +value;

                    // Determine the sign. -0 is not less than 0, but 1 / -0 is!
                    var valueNegative = value < 0 || 1 / value < 0;

                    // Perform the initial formatting.
                    value = isNaN(value) ? nan : formatType(Math.abs(value), precision);

                    // Trim insignificant zeros.
                    if (trim) value = formatTrim(value);

                    // If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
                    if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;

                    // Compute the prefix and suffix.
                    valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
                    valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");

                    // Break the formatted value into the integer “value” part that can be
                    // grouped, and fractional or exponential “suffix” part that is not.
                    if (maybeSuffix) {
                        i = -1, n = value.length;
                        while (++i < n) {
                            if (c = value.charCodeAt(i), 48 > c || c > 57) {
                                valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
                                value = value.slice(0, i);
                                break;
                            }
                        }
                    }
                }

                // If the fill character is not "0", grouping is applied before padding.
                if (comma && !zero) value = group(value, Infinity);

                // Compute the padding.
                var length = valuePrefix.length + value.length + valueSuffix.length,
                    padding = length < width ? new Array(width - length + 1).join(fill) : "";

                // If the fill character is "0", grouping is applied after padding.
                if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";

                // Reconstruct the final output based on the desired alignment.
                switch (align) {
                    case "<":
                        value = valuePrefix + value + valueSuffix + padding;
                        break;
                    case "=":
                        value = valuePrefix + padding + value + valueSuffix;
                        break;
                    case "^":
                        value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length);
                        break;
                    default:
                        value = padding + valuePrefix + value + valueSuffix;
                        break;
                }

                return numerals(value);
            }

            format.toString = function () {
                return specifier + "";
            };

            return format;
        }

        function formatPrefix(specifier, value) {
            var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
                e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
                k = Math.pow(10, -e),
                prefix = prefixes[8 + e / 3];
            return function (value) {
                return f(k * value) + prefix;
            };
        }

        return {
            format: newFormat,
            formatPrefix: formatPrefix
        };
    }

    var locale;
    var format;
    var formatPrefix;

    defaultLocale({
        decimal: ".",
        thousands: ",",
        grouping: [3],
        currency: ["$", ""],
        minus: "-"
    });

    function defaultLocale(definition) {
        locale = formatLocale(definition);
        format = locale.format;
        formatPrefix = locale.formatPrefix;
        return locale;
    }

    function precisionFixed(step) {
        return Math.max(0, -exponent(Math.abs(step)));
    }

    function precisionPrefix(step, value) {
        return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
    }

    function precisionRound(step, max) {
        step = Math.abs(step), max = Math.abs(max) - step;
        return Math.max(0, exponent(max) - exponent(step)) + 1;
    }

    function tickFormat(start, stop, count, specifier) {
        var step = tickStep(start, stop, count),
            precision;
        specifier = formatSpecifier(specifier == null ? ",f" : specifier);
        switch (specifier.type) {
            case "s": {
                var value = Math.max(Math.abs(start), Math.abs(stop));
                if (specifier.precision == null && !isNaN(precision = precisionPrefix(step, value))) specifier.precision = precision;
                return formatPrefix(specifier, value);
            }
            case "":
            case "e":
            case "g":
            case "p":
            case "r": {
                if (specifier.precision == null && !isNaN(precision = precisionRound(step, Math.max(Math.abs(start), Math.abs(stop))))) specifier.precision = precision - (specifier.type === "e");
                break;
            }
            case "f":
            case "%": {
                if (specifier.precision == null && !isNaN(precision = precisionFixed(step))) specifier.precision = precision - (specifier.type === "%") * 2;
                break;
            }
        }
        return format(specifier);
    }

    function linearish(scale) {
        var domain = scale.domain;

        scale.ticks = function (count) {
            var d = domain();
            return ticks(d[0], d[d.length - 1], count == null ? 10 : count);
        };

        scale.tickFormat = function (count, specifier) {
            var d = domain();
            return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier);
        };

        scale.nice = function (count) {
            if (count == null) count = 10;

            var d = domain(),
                i0 = 0,
                i1 = d.length - 1,
                start = d[i0],
                stop = d[i1],
                step;

            if (stop < start) {
                step = start, start = stop, stop = step;
                step = i0, i0 = i1, i1 = step;
            }

            step = tickIncrement(start, stop, count);

            if (step > 0) {
                start = Math.floor(start / step) * step;
                stop = Math.ceil(stop / step) * step;
                step = tickIncrement(start, stop, count);
            } else if (step < 0) {
                start = Math.ceil(start * step) / step;
                stop = Math.floor(stop * step) / step;
                step = tickIncrement(start, stop, count);
            }

            if (step > 0) {
                d[i0] = Math.floor(start / step) * step;
                d[i1] = Math.ceil(stop / step) * step;
                domain(d);
            } else if (step < 0) {
                d[i0] = Math.ceil(start * step) / step;
                d[i1] = Math.floor(stop * step) / step;
                domain(d);
            }

            return scale;
        };

        return scale;
    }

    function linear$1() {
        var scale = continuous();

        scale.copy = function () {
            return copy(scale, linear$1());
        };

        initRange.apply(scale, arguments);

        return linearish(scale);
    }

    function nice(domain, interval) {
        domain = domain.slice();

        var i0 = 0,
            i1 = domain.length - 1,
            x0 = domain[i0],
            x1 = domain[i1],
            t;

        if (x1 < x0) {
            t = i0, i0 = i1, i1 = t;
            t = x0, x0 = x1, x1 = t;
        }

        domain[i0] = interval.floor(x0);
        domain[i1] = interval.ceil(x1);
        return domain;
    }

    var t0 = new Date,
        t1 = new Date;

    function newInterval(floori, offseti, count, field) {

        function interval(date) {
            return floori(date = arguments.length === 0 ? new Date : new Date(+date)), date;
        }

        interval.floor = function (date) {
            return floori(date = new Date(+date)), date;
        };

        interval.ceil = function (date) {
            return floori(date = new Date(date - 1)), offseti(date, 1), floori(date), date;
        };

        interval.round = function (date) {
            var d0 = interval(date),
                d1 = interval.ceil(date);
            return date - d0 < d1 - date ? d0 : d1;
        };

        interval.offset = function (date, step) {
            return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date;
        };

        interval.range = function (start, stop, step) {
            var range = [], previous;
            start = interval.ceil(start);
            step = step == null ? 1 : Math.floor(step);
            if (!(start < stop) || !(step > 0)) return range; // also handles Invalid Date
            do range.push(previous = new Date(+start)), offseti(start, step), floori(start);
            while (previous < start && start < stop);
            return range;
        };

        interval.filter = function (test) {
            return newInterval(function (date) {
                if (date >= date) while (floori(date), !test(date)) date.setTime(date - 1);
            }, function (date, step) {
                if (date >= date) {
                    if (step < 0) while (++step <= 0) {
                        while (offseti(date, -1), !test(date)) {
                        } // eslint-disable-line no-empty
                    } else while (--step >= 0) {
                        while (offseti(date, +1), !test(date)) {
                        } // eslint-disable-line no-empty
                    }
                }
            });
        };

        if (count) {
            interval.count = function (start, end) {
                t0.setTime(+start), t1.setTime(+end);
                floori(t0), floori(t1);
                return Math.floor(count(t0, t1));
            };

            interval.every = function (step) {
                step = Math.floor(step);
                return !isFinite(step) || !(step > 0) ? null
                    : !(step > 1) ? interval
                        : interval.filter(field
                            ? function (d) {
                                return field(d) % step === 0;
                            }
                            : function (d) {
                                return interval.count(0, d) % step === 0;
                            });
            };
        }

        return interval;
    }

    var millisecond = newInterval(function () {
        // noop
    }, function (date, step) {
        date.setTime(+date + step);
    }, function (start, end) {
        return end - start;
    });

    // An optimized implementation for this simple case.
    millisecond.every = function (k) {
        k = Math.floor(k);
        if (!isFinite(k) || !(k > 0)) return null;
        if (!(k > 1)) return millisecond;
        return newInterval(function (date) {
            date.setTime(Math.floor(date / k) * k);
        }, function (date, step) {
            date.setTime(+date + step * k);
        }, function (start, end) {
            return (end - start) / k;
        });
    };

    var durationSecond = 1e3;
    var durationMinute = 6e4;
    var durationHour = 36e5;
    var durationDay = 864e5;
    var durationWeek = 6048e5;

    var second = newInterval(function (date) {
        date.setTime(date - date.getMilliseconds());
    }, function (date, step) {
        date.setTime(+date + step * durationSecond);
    }, function (start, end) {
        return (end - start) / durationSecond;
    }, function (date) {
        return date.getUTCSeconds();
    });

    var minute = newInterval(function (date) {
        date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond);
    }, function (date, step) {
        date.setTime(+date + step * durationMinute);
    }, function (start, end) {
        return (end - start) / durationMinute;
    }, function (date) {
        return date.getMinutes();
    });

    var hour = newInterval(function (date) {
        date.setTime(date - date.getMilliseconds() - date.getSeconds() * durationSecond - date.getMinutes() * durationMinute);
    }, function (date, step) {
        date.setTime(+date + step * durationHour);
    }, function (start, end) {
        return (end - start) / durationHour;
    }, function (date) {
        return date.getHours();
    });

    var day = newInterval(function (date) {
        date.setHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setDate(date.getDate() + step);
    }, function (start, end) {
        return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay;
    }, function (date) {
        return date.getDate() - 1;
    });

    function weekday(i) {
        return newInterval(function (date) {
            date.setDate(date.getDate() - (date.getDay() + 7 - i) % 7);
            date.setHours(0, 0, 0, 0);
        }, function (date, step) {
            date.setDate(date.getDate() + step * 7);
        }, function (start, end) {
            return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek;
        });
    }

    var sunday = weekday(0);
    var monday = weekday(1);
    var tuesday = weekday(2);
    var wednesday = weekday(3);
    var thursday = weekday(4);
    var friday = weekday(5);
    var saturday = weekday(6);

    var month = newInterval(function (date) {
        date.setDate(1);
        date.setHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setMonth(date.getMonth() + step);
    }, function (start, end) {
        return end.getMonth() - start.getMonth() + (end.getFullYear() - start.getFullYear()) * 12;
    }, function (date) {
        return date.getMonth();
    });

    var year = newInterval(function (date) {
        date.setMonth(0, 1);
        date.setHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setFullYear(date.getFullYear() + step);
    }, function (start, end) {
        return end.getFullYear() - start.getFullYear();
    }, function (date) {
        return date.getFullYear();
    });

    // An optimized implementation for this simple case.
    year.every = function (k) {
        return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : newInterval(function (date) {
            date.setFullYear(Math.floor(date.getFullYear() / k) * k);
            date.setMonth(0, 1);
            date.setHours(0, 0, 0, 0);
        }, function (date, step) {
            date.setFullYear(date.getFullYear() + step * k);
        });
    };

    var utcMinute = newInterval(function (date) {
        date.setUTCSeconds(0, 0);
    }, function (date, step) {
        date.setTime(+date + step * durationMinute);
    }, function (start, end) {
        return (end - start) / durationMinute;
    }, function (date) {
        return date.getUTCMinutes();
    });

    var utcHour = newInterval(function (date) {
        date.setUTCMinutes(0, 0, 0);
    }, function (date, step) {
        date.setTime(+date + step * durationHour);
    }, function (start, end) {
        return (end - start) / durationHour;
    }, function (date) {
        return date.getUTCHours();
    });

    var utcDay = newInterval(function (date) {
        date.setUTCHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setUTCDate(date.getUTCDate() + step);
    }, function (start, end) {
        return (end - start) / durationDay;
    }, function (date) {
        return date.getUTCDate() - 1;
    });

    function utcWeekday(i) {
        return newInterval(function (date) {
            date.setUTCDate(date.getUTCDate() - (date.getUTCDay() + 7 - i) % 7);
            date.setUTCHours(0, 0, 0, 0);
        }, function (date, step) {
            date.setUTCDate(date.getUTCDate() + step * 7);
        }, function (start, end) {
            return (end - start) / durationWeek;
        });
    }

    var utcSunday = utcWeekday(0);
    var utcMonday = utcWeekday(1);
    var utcTuesday = utcWeekday(2);
    var utcWednesday = utcWeekday(3);
    var utcThursday = utcWeekday(4);
    var utcFriday = utcWeekday(5);
    var utcSaturday = utcWeekday(6);

    var utcMonth = newInterval(function (date) {
        date.setUTCDate(1);
        date.setUTCHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setUTCMonth(date.getUTCMonth() + step);
    }, function (start, end) {
        return end.getUTCMonth() - start.getUTCMonth() + (end.getUTCFullYear() - start.getUTCFullYear()) * 12;
    }, function (date) {
        return date.getUTCMonth();
    });

    var utcYear = newInterval(function (date) {
        date.setUTCMonth(0, 1);
        date.setUTCHours(0, 0, 0, 0);
    }, function (date, step) {
        date.setUTCFullYear(date.getUTCFullYear() + step);
    }, function (start, end) {
        return end.getUTCFullYear() - start.getUTCFullYear();
    }, function (date) {
        return date.getUTCFullYear();
    });

    // An optimized implementation for this simple case.
    utcYear.every = function (k) {
        return !isFinite(k = Math.floor(k)) || !(k > 0) ? null : newInterval(function (date) {
            date.setUTCFullYear(Math.floor(date.getUTCFullYear() / k) * k);
            date.setUTCMonth(0, 1);
            date.setUTCHours(0, 0, 0, 0);
        }, function (date, step) {
            date.setUTCFullYear(date.getUTCFullYear() + step * k);
        });
    };

    function localDate(d) {
        if (0 <= d.y && d.y < 100) {
            var date = new Date(-1, d.m, d.d, d.H, d.M, d.S, d.L);
            date.setFullYear(d.y);
            return date;
        }
        return new Date(d.y, d.m, d.d, d.H, d.M, d.S, d.L);
    }

    function utcDate(d) {
        if (0 <= d.y && d.y < 100) {
            var date = new Date(Date.UTC(-1, d.m, d.d, d.H, d.M, d.S, d.L));
            date.setUTCFullYear(d.y);
            return date;
        }
        return new Date(Date.UTC(d.y, d.m, d.d, d.H, d.M, d.S, d.L));
    }

    function newDate(y, m, d) {
        return {y: y, m: m, d: d, H: 0, M: 0, S: 0, L: 0};
    }

    function formatLocale$1(locale) {
        var locale_dateTime = locale.dateTime,
            locale_date = locale.date,
            locale_time = locale.time,
            locale_periods = locale.periods,
            locale_weekdays = locale.days,
            locale_shortWeekdays = locale.shortDays,
            locale_months = locale.months,
            locale_shortMonths = locale.shortMonths;

        var periodRe = formatRe(locale_periods),
            periodLookup = formatLookup(locale_periods),
            weekdayRe = formatRe(locale_weekdays),
            weekdayLookup = formatLookup(locale_weekdays),
            shortWeekdayRe = formatRe(locale_shortWeekdays),
            shortWeekdayLookup = formatLookup(locale_shortWeekdays),
            monthRe = formatRe(locale_months),
            monthLookup = formatLookup(locale_months),
            shortMonthRe = formatRe(locale_shortMonths),
            shortMonthLookup = formatLookup(locale_shortMonths);

        var formats = {
            "a": formatShortWeekday,
            "A": formatWeekday,
            "b": formatShortMonth,
            "B": formatMonth,
            "c": null,
            "d": formatDayOfMonth,
            "e": formatDayOfMonth,
            "f": formatMicroseconds,
            "H": formatHour24,
            "I": formatHour12,
            "j": formatDayOfYear,
            "L": formatMilliseconds,
            "m": formatMonthNumber,
            "M": formatMinutes,
            "p": formatPeriod,
            "q": formatQuarter,
            "Q": formatUnixTimestamp,
            "s": formatUnixTimestampSeconds,
            "S": formatSeconds,
            "u": formatWeekdayNumberMonday,
            "U": formatWeekNumberSunday,
            "V": formatWeekNumberISO,
            "w": formatWeekdayNumberSunday,
            "W": formatWeekNumberMonday,
            "x": null,
            "X": null,
            "y": formatYear,
            "Y": formatFullYear,
            "Z": formatZone,
            "%": formatLiteralPercent
        };

        var utcFormats = {
            "a": formatUTCShortWeekday,
            "A": formatUTCWeekday,
            "b": formatUTCShortMonth,
            "B": formatUTCMonth,
            "c": null,
            "d": formatUTCDayOfMonth,
            "e": formatUTCDayOfMonth,
            "f": formatUTCMicroseconds,
            "H": formatUTCHour24,
            "I": formatUTCHour12,
            "j": formatUTCDayOfYear,
            "L": formatUTCMilliseconds,
            "m": formatUTCMonthNumber,
            "M": formatUTCMinutes,
            "p": formatUTCPeriod,
            "q": formatUTCQuarter,
            "Q": formatUnixTimestamp,
            "s": formatUnixTimestampSeconds,
            "S": formatUTCSeconds,
            "u": formatUTCWeekdayNumberMonday,
            "U": formatUTCWeekNumberSunday,
            "V": formatUTCWeekNumberISO,
            "w": formatUTCWeekdayNumberSunday,
            "W": formatUTCWeekNumberMonday,
            "x": null,
            "X": null,
            "y": formatUTCYear,
            "Y": formatUTCFullYear,
            "Z": formatUTCZone,
            "%": formatLiteralPercent
        };

        var parses = {
            "a": parseShortWeekday,
            "A": parseWeekday,
            "b": parseShortMonth,
            "B": parseMonth,
            "c": parseLocaleDateTime,
            "d": parseDayOfMonth,
            "e": parseDayOfMonth,
            "f": parseMicroseconds,
            "H": parseHour24,
            "I": parseHour24,
            "j": parseDayOfYear,
            "L": parseMilliseconds,
            "m": parseMonthNumber,
            "M": parseMinutes,
            "p": parsePeriod,
            "q": parseQuarter,
            "Q": parseUnixTimestamp,
            "s": parseUnixTimestampSeconds,
            "S": parseSeconds,
            "u": parseWeekdayNumberMonday,
            "U": parseWeekNumberSunday,
            "V": parseWeekNumberISO,
            "w": parseWeekdayNumberSunday,
            "W": parseWeekNumberMonday,
            "x": parseLocaleDate,
            "X": parseLocaleTime,
            "y": parseYear,
            "Y": parseFullYear,
            "Z": parseZone,
            "%": parseLiteralPercent
        };

        // These recursive directive definitions must be deferred.
        formats.x = newFormat(locale_date, formats);
        formats.X = newFormat(locale_time, formats);
        formats.c = newFormat(locale_dateTime, formats);
        utcFormats.x = newFormat(locale_date, utcFormats);
        utcFormats.X = newFormat(locale_time, utcFormats);
        utcFormats.c = newFormat(locale_dateTime, utcFormats);

        function newFormat(specifier, formats) {
            return function (date) {
                var string = [],
                    i = -1,
                    j = 0,
                    n = specifier.length,
                    c,
                    pad,
                    format;

                if (!(date instanceof Date)) date = new Date(+date);

                while (++i < n) {
                    if (specifier.charCodeAt(i) === 37) {
                        string.push(specifier.slice(j, i));
                        if ((pad = pads[c = specifier.charAt(++i)]) != null) c = specifier.charAt(++i);
                        else pad = c === "e" ? " " : "0";
                        if (format = formats[c]) c = format(date, pad);
                        string.push(c);
                        j = i + 1;
                    }
                }

                string.push(specifier.slice(j, i));
                return string.join("");
            };
        }

        function newParse(specifier, Z) {
            return function (string) {
                var d = newDate(1900, undefined, 1),
                    i = parseSpecifier(d, specifier, string += "", 0),
                    week, day$1;
                if (i != string.length) return null;

                // If a UNIX timestamp is specified, return it.
                if ("Q" in d) return new Date(d.Q);
                if ("s" in d) return new Date(d.s * 1000 + ("L" in d ? d.L : 0));

                // If this is utcParse, never use the local timezone.
                if (Z && !("Z" in d)) d.Z = 0;

                // The am-pm flag is 0 for AM, and 1 for PM.
                if ("p" in d) d.H = d.H % 12 + d.p * 12;

                // If the month was not specified, inherit from the quarter.
                if (d.m === undefined) d.m = "q" in d ? d.q : 0;

                // Convert day-of-week and week-of-year to day-of-year.
                if ("V" in d) {
                    if (d.V < 1 || d.V > 53) return null;
                    if (!("w" in d)) d.w = 1;
                    if ("Z" in d) {
                        week = utcDate(newDate(d.y, 0, 1)), day$1 = week.getUTCDay();
                        week = day$1 > 4 || day$1 === 0 ? utcMonday.ceil(week) : utcMonday(week);
                        week = utcDay.offset(week, (d.V - 1) * 7);
                        d.y = week.getUTCFullYear();
                        d.m = week.getUTCMonth();
                        d.d = week.getUTCDate() + (d.w + 6) % 7;
                    } else {
                        week = localDate(newDate(d.y, 0, 1)), day$1 = week.getDay();
                        week = day$1 > 4 || day$1 === 0 ? monday.ceil(week) : monday(week);
                        week = day.offset(week, (d.V - 1) * 7);
                        d.y = week.getFullYear();
                        d.m = week.getMonth();
                        d.d = week.getDate() + (d.w + 6) % 7;
                    }
                } else if ("W" in d || "U" in d) {
                    if (!("w" in d)) d.w = "u" in d ? d.u % 7 : "W" in d ? 1 : 0;
                    day$1 = "Z" in d ? utcDate(newDate(d.y, 0, 1)).getUTCDay() : localDate(newDate(d.y, 0, 1)).getDay();
                    d.m = 0;
                    d.d = "W" in d ? (d.w + 6) % 7 + d.W * 7 - (day$1 + 5) % 7 : d.w + d.U * 7 - (day$1 + 6) % 7;
                }

                // If a time zone is specified, all fields are interpreted as UTC and then
                // offset according to the specified time zone.
                if ("Z" in d) {
                    d.H += d.Z / 100 | 0;
                    d.M += d.Z % 100;
                    return utcDate(d);
                }

                // Otherwise, all fields are in local time.
                return localDate(d);
            };
        }

        function parseSpecifier(d, specifier, string, j) {
            var i = 0,
                n = specifier.length,
                m = string.length,
                c,
                parse;

            while (i < n) {
                if (j >= m) return -1;
                c = specifier.charCodeAt(i++);
                if (c === 37) {
                    c = specifier.charAt(i++);
                    parse = parses[c in pads ? specifier.charAt(i++) : c];
                    if (!parse || ((j = parse(d, string, j)) < 0)) return -1;
                } else if (c != string.charCodeAt(j++)) {
                    return -1;
                }
            }

            return j;
        }

        function parsePeriod(d, string, i) {
            var n = periodRe.exec(string.slice(i));
            return n ? (d.p = periodLookup[n[0].toLowerCase()], i + n[0].length) : -1;
        }

        function parseShortWeekday(d, string, i) {
            var n = shortWeekdayRe.exec(string.slice(i));
            return n ? (d.w = shortWeekdayLookup[n[0].toLowerCase()], i + n[0].length) : -1;
        }

        function parseWeekday(d, string, i) {
            var n = weekdayRe.exec(string.slice(i));
            return n ? (d.w = weekdayLookup[n[0].toLowerCase()], i + n[0].length) : -1;
        }

        function parseShortMonth(d, string, i) {
            var n = shortMonthRe.exec(string.slice(i));
            return n ? (d.m = shortMonthLookup[n[0].toLowerCase()], i + n[0].length) : -1;
        }

        function parseMonth(d, string, i) {
            var n = monthRe.exec(string.slice(i));
            return n ? (d.m = monthLookup[n[0].toLowerCase()], i + n[0].length) : -1;
        }

        function parseLocaleDateTime(d, string, i) {
            return parseSpecifier(d, locale_dateTime, string, i);
        }

        function parseLocaleDate(d, string, i) {
            return parseSpecifier(d, locale_date, string, i);
        }

        function parseLocaleTime(d, string, i) {
            return parseSpecifier(d, locale_time, string, i);
        }

        function formatShortWeekday(d) {
            return locale_shortWeekdays[d.getDay()];
        }

        function formatWeekday(d) {
            return locale_weekdays[d.getDay()];
        }

        function formatShortMonth(d) {
            return locale_shortMonths[d.getMonth()];
        }

        function formatMonth(d) {
            return locale_months[d.getMonth()];
        }

        function formatPeriod(d) {
            return locale_periods[+(d.getHours() >= 12)];
        }

        function formatQuarter(d) {
            return 1 + ~~(d.getMonth() / 3);
        }

        function formatUTCShortWeekday(d) {
            return locale_shortWeekdays[d.getUTCDay()];
        }

        function formatUTCWeekday(d) {
            return locale_weekdays[d.getUTCDay()];
        }

        function formatUTCShortMonth(d) {
            return locale_shortMonths[d.getUTCMonth()];
        }

        function formatUTCMonth(d) {
            return locale_months[d.getUTCMonth()];
        }

        function formatUTCPeriod(d) {
            return locale_periods[+(d.getUTCHours() >= 12)];
        }

        function formatUTCQuarter(d) {
            return 1 + ~~(d.getUTCMonth() / 3);
        }

        return {
            format: function (specifier) {
                var f = newFormat(specifier += "", formats);
                f.toString = function () {
                    return specifier;
                };
                return f;
            },
            parse: function (specifier) {
                var p = newParse(specifier += "", false);
                p.toString = function () {
                    return specifier;
                };
                return p;
            },
            utcFormat: function (specifier) {
                var f = newFormat(specifier += "", utcFormats);
                f.toString = function () {
                    return specifier;
                };
                return f;
            },
            utcParse: function (specifier) {
                var p = newParse(specifier += "", true);
                p.toString = function () {
                    return specifier;
                };
                return p;
            }
        };
    }

    var pads = {"-": "", "_": " ", "0": "0"},
        numberRe = /^\s*\d+/, // note: ignores next directive
        percentRe = /^%/,
        requoteRe = /[\\^$*+?|[\]().{}]/g;

    function pad(value, fill, width) {
        var sign = value < 0 ? "-" : "",
            string = (sign ? -value : value) + "",
            length = string.length;
        return sign + (length < width ? new Array(width - length + 1).join(fill) + string : string);
    }

    function requote(s) {
        return s.replace(requoteRe, "\\$&");
    }

    function formatRe(names) {
        return new RegExp("^(?:" + names.map(requote).join("|") + ")", "i");
    }

    function formatLookup(names) {
        var map = {}, i = -1, n = names.length;
        while (++i < n) map[names[i].toLowerCase()] = i;
        return map;
    }

    function parseWeekdayNumberSunday(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 1));
        return n ? (d.w = +n[0], i + n[0].length) : -1;
    }

    function parseWeekdayNumberMonday(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 1));
        return n ? (d.u = +n[0], i + n[0].length) : -1;
    }

    function parseWeekNumberSunday(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.U = +n[0], i + n[0].length) : -1;
    }

    function parseWeekNumberISO(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.V = +n[0], i + n[0].length) : -1;
    }

    function parseWeekNumberMonday(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.W = +n[0], i + n[0].length) : -1;
    }

    function parseFullYear(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 4));
        return n ? (d.y = +n[0], i + n[0].length) : -1;
    }

    function parseYear(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.y = +n[0] + (+n[0] > 68 ? 1900 : 2000), i + n[0].length) : -1;
    }

    function parseZone(d, string, i) {
        var n = /^(Z)|([+-]\d\d)(?::?(\d\d))?/.exec(string.slice(i, i + 6));
        return n ? (d.Z = n[1] ? 0 : -(n[2] + (n[3] || "00")), i + n[0].length) : -1;
    }

    function parseQuarter(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 1));
        return n ? (d.q = n[0] * 3 - 3, i + n[0].length) : -1;
    }

    function parseMonthNumber(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.m = n[0] - 1, i + n[0].length) : -1;
    }

    function parseDayOfMonth(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.d = +n[0], i + n[0].length) : -1;
    }

    function parseDayOfYear(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 3));
        return n ? (d.m = 0, d.d = +n[0], i + n[0].length) : -1;
    }

    function parseHour24(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.H = +n[0], i + n[0].length) : -1;
    }

    function parseMinutes(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.M = +n[0], i + n[0].length) : -1;
    }

    function parseSeconds(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 2));
        return n ? (d.S = +n[0], i + n[0].length) : -1;
    }

    function parseMilliseconds(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 3));
        return n ? (d.L = +n[0], i + n[0].length) : -1;
    }

    function parseMicroseconds(d, string, i) {
        var n = numberRe.exec(string.slice(i, i + 6));
        return n ? (d.L = Math.floor(n[0] / 1000), i + n[0].length) : -1;
    }

    function parseLiteralPercent(d, string, i) {
        var n = percentRe.exec(string.slice(i, i + 1));
        return n ? i + n[0].length : -1;
    }

    function parseUnixTimestamp(d, string, i) {
        var n = numberRe.exec(string.slice(i));
        return n ? (d.Q = +n[0], i + n[0].length) : -1;
    }

    function parseUnixTimestampSeconds(d, string, i) {
        var n = numberRe.exec(string.slice(i));
        return n ? (d.s = +n[0], i + n[0].length) : -1;
    }

    function formatDayOfMonth(d, p) {
        return pad(d.getDate(), p, 2);
    }

    function formatHour24(d, p) {
        return pad(d.getHours(), p, 2);
    }

    function formatHour12(d, p) {
        return pad(d.getHours() % 12 || 12, p, 2);
    }

    function formatDayOfYear(d, p) {
        return pad(1 + day.count(year(d), d), p, 3);
    }

    function formatMilliseconds(d, p) {
        return pad(d.getMilliseconds(), p, 3);
    }

    function formatMicroseconds(d, p) {
        return formatMilliseconds(d, p) + "000";
    }

    function formatMonthNumber(d, p) {
        return pad(d.getMonth() + 1, p, 2);
    }

    function formatMinutes(d, p) {
        return pad(d.getMinutes(), p, 2);
    }

    function formatSeconds(d, p) {
        return pad(d.getSeconds(), p, 2);
    }

    function formatWeekdayNumberMonday(d) {
        var day = d.getDay();
        return day === 0 ? 7 : day;
    }

    function formatWeekNumberSunday(d, p) {
        return pad(sunday.count(year(d) - 1, d), p, 2);
    }

    function formatWeekNumberISO(d, p) {
        var day = d.getDay();
        d = (day >= 4 || day === 0) ? thursday(d) : thursday.ceil(d);
        return pad(thursday.count(year(d), d) + (year(d).getDay() === 4), p, 2);
    }

    function formatWeekdayNumberSunday(d) {
        return d.getDay();
    }

    function formatWeekNumberMonday(d, p) {
        return pad(monday.count(year(d) - 1, d), p, 2);
    }

    function formatYear(d, p) {
        return pad(d.getFullYear() % 100, p, 2);
    }

    function formatFullYear(d, p) {
        return pad(d.getFullYear() % 10000, p, 4);
    }

    function formatZone(d) {
        var z = d.getTimezoneOffset();
        return (z > 0 ? "-" : (z *= -1, "+"))
            + pad(z / 60 | 0, "0", 2)
            + pad(z % 60, "0", 2);
    }

    function formatUTCDayOfMonth(d, p) {
        return pad(d.getUTCDate(), p, 2);
    }

    function formatUTCHour24(d, p) {
        return pad(d.getUTCHours(), p, 2);
    }

    function formatUTCHour12(d, p) {
        return pad(d.getUTCHours() % 12 || 12, p, 2);
    }

    function formatUTCDayOfYear(d, p) {
        return pad(1 + utcDay.count(utcYear(d), d), p, 3);
    }

    function formatUTCMilliseconds(d, p) {
        return pad(d.getUTCMilliseconds(), p, 3);
    }

    function formatUTCMicroseconds(d, p) {
        return formatUTCMilliseconds(d, p) + "000";
    }

    function formatUTCMonthNumber(d, p) {
        return pad(d.getUTCMonth() + 1, p, 2);
    }

    function formatUTCMinutes(d, p) {
        return pad(d.getUTCMinutes(), p, 2);
    }

    function formatUTCSeconds(d, p) {
        return pad(d.getUTCSeconds(), p, 2);
    }

    function formatUTCWeekdayNumberMonday(d) {
        var dow = d.getUTCDay();
        return dow === 0 ? 7 : dow;
    }

    function formatUTCWeekNumberSunday(d, p) {
        return pad(utcSunday.count(utcYear(d) - 1, d), p, 2);
    }

    function formatUTCWeekNumberISO(d, p) {
        var day = d.getUTCDay();
        d = (day >= 4 || day === 0) ? utcThursday(d) : utcThursday.ceil(d);
        return pad(utcThursday.count(utcYear(d), d) + (utcYear(d).getUTCDay() === 4), p, 2);
    }

    function formatUTCWeekdayNumberSunday(d) {
        return d.getUTCDay();
    }

    function formatUTCWeekNumberMonday(d, p) {
        return pad(utcMonday.count(utcYear(d) - 1, d), p, 2);
    }

    function formatUTCYear(d, p) {
        return pad(d.getUTCFullYear() % 100, p, 2);
    }

    function formatUTCFullYear(d, p) {
        return pad(d.getUTCFullYear() % 10000, p, 4);
    }

    function formatUTCZone() {
        return "+0000";
    }

    function formatLiteralPercent() {
        return "%";
    }

    function formatUnixTimestamp(d) {
        return +d;
    }

    function formatUnixTimestampSeconds(d) {
        return Math.floor(+d / 1000);
    }

    var locale$1;
    var timeFormat;
    var timeParse;
    var utcFormat;
    var utcParse;

    defaultLocale$1({
        dateTime: "%x, %X",
        date: "%-m/%-d/%Y",
        time: "%-I:%M:%S %p",
        periods: ["AM", "PM"],
        days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
        shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
        months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    });

    function defaultLocale$1(definition) {
        locale$1 = formatLocale$1(definition);
        timeFormat = locale$1.format;
        timeParse = locale$1.parse;
        utcFormat = locale$1.utcFormat;
        utcParse = locale$1.utcParse;
        return locale$1;
    }

    var durationSecond$1 = 1000,
        durationMinute$1 = durationSecond$1 * 60,
        durationHour$1 = durationMinute$1 * 60,
        durationDay$1 = durationHour$1 * 24,
        durationWeek$1 = durationDay$1 * 7,
        durationMonth = durationDay$1 * 30,
        durationYear = durationDay$1 * 365;

    function date$1(t) {
        return new Date(t);
    }

    function number$2(t) {
        return t instanceof Date ? +t : +new Date(+t);
    }

    function calendar(year, month, week, day, hour, minute, second, millisecond, format) {
        var scale = continuous(),
            invert = scale.invert,
            domain = scale.domain;

        var formatMillisecond = format(".%L"),
            formatSecond = format(":%S"),
            formatMinute = format("%I:%M"),
            formatHour = format("%I %p"),
            formatDay = format("%a %d"),
            formatWeek = format("%b %d"),
            formatMonth = format("%B"),
            formatYear = format("%Y");

        var tickIntervals = [
            [second, 1, durationSecond$1],
            [second, 5, 5 * durationSecond$1],
            [second, 15, 15 * durationSecond$1],
            [second, 30, 30 * durationSecond$1],
            [minute, 1, durationMinute$1],
            [minute, 5, 5 * durationMinute$1],
            [minute, 15, 15 * durationMinute$1],
            [minute, 30, 30 * durationMinute$1],
            [hour, 1, durationHour$1],
            [hour, 3, 3 * durationHour$1],
            [hour, 6, 6 * durationHour$1],
            [hour, 12, 12 * durationHour$1],
            [day, 1, durationDay$1],
            [day, 2, 2 * durationDay$1],
            [week, 1, durationWeek$1],
            [month, 1, durationMonth],
            [month, 3, 3 * durationMonth],
            [year, 1, durationYear]
        ];

        function tickFormat(date) {
            return (second(date) < date ? formatMillisecond
                : minute(date) < date ? formatSecond
                    : hour(date) < date ? formatMinute
                        : day(date) < date ? formatHour
                            : month(date) < date ? (week(date) < date ? formatDay : formatWeek)
                                : year(date) < date ? formatMonth
                                    : formatYear)(date);
        }

        function tickInterval(interval, start, stop) {
            if (interval == null) interval = 10;

            // If a desired tick count is specified, pick a reasonable tick interval
            // based on the extent of the domain and a rough estimate of tick size.
            // Otherwise, assume interval is already a time interval and use it.
            if (typeof interval === "number") {
                var target = Math.abs(stop - start) / interval,
                    i = bisector(function (i) {
                        return i[2];
                    }).right(tickIntervals, target),
                    step;
                if (i === tickIntervals.length) {
                    step = tickStep(start / durationYear, stop / durationYear, interval);
                    interval = year;
                } else if (i) {
                    i = tickIntervals[target / tickIntervals[i - 1][2] < tickIntervals[i][2] / target ? i - 1 : i];
                    step = i[1];
                    interval = i[0];
                } else {
                    step = Math.max(tickStep(start, stop, interval), 1);
                    interval = millisecond;
                }
                return interval.every(step);
            }

            return interval;
        }

        scale.invert = function (y) {
            return new Date(invert(y));
        };

        scale.domain = function (_) {
            return arguments.length ? domain(Array.from(_, number$2)) : domain().map(date$1);
        };

        scale.ticks = function (interval) {
            var d = domain(),
                t0 = d[0],
                t1 = d[d.length - 1],
                r = t1 < t0,
                t;
            if (r) t = t0, t0 = t1, t1 = t;
            t = tickInterval(interval, t0, t1);
            t = t ? t.range(t0, t1 + 1) : []; // inclusive stop
            return r ? t.reverse() : t;
        };

        scale.tickFormat = function (count, specifier) {
            return specifier == null ? tickFormat : format(specifier);
        };

        scale.nice = function (interval) {
            var d = domain();
            return (interval = tickInterval(interval, d[0], d[d.length - 1]))
                ? domain(nice(d, interval))
                : scale;
        };

        scale.copy = function () {
            return copy(scale, calendar(year, month, week, day, hour, minute, second, millisecond, format));
        };

        return scale;
    }

    function d3ScaleTime() {
        return initRange.apply(calendar(year, month, sunday, day, hour, minute, second, millisecond, timeFormat).domain([new Date(2000, 0, 1), new Date(2000, 0, 2)]), arguments);
    }

    function d3ScaleUtc() {
        return initRange.apply(calendar(utcYear, utcMonth, utcSunday, utcDay, utcHour, utcMinute, second, millisecond, utcFormat).domain([Date.UTC(2000, 0, 1), Date.UTC(2000, 0, 2)]), arguments);
    }

    function transformer$1() {
        var x0 = 0,
            x1 = 1,
            t0,
            t1,
            k10,
            transform,
            interpolator = identity$2,
            clamp = false,
            unknown;

        function scale(x) {
            return isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x));
        }

        scale.domain = function (_) {
            return arguments.length ? ([x0, x1] = _, t0 = transform(x0 = +x0), t1 = transform(x1 = +x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0), scale) : [x0, x1];
        };

        scale.clamp = function (_) {
            return arguments.length ? (clamp = !!_, scale) : clamp;
        };

        scale.interpolator = function (_) {
            return arguments.length ? (interpolator = _, scale) : interpolator;
        };

        function range(interpolate) {
            return function (_) {
                var r0, r1;
                return arguments.length ? ([r0, r1] = _, interpolator = interpolate(r0, r1), scale) : [interpolator(0), interpolator(1)];
            };
        }

        scale.range = range(interpolate);

        scale.rangeRound = range(interpolateRound);

        scale.unknown = function (_) {
            return arguments.length ? (unknown = _, scale) : unknown;
        };

        return function (t) {
            transform = t, t0 = t(x0), t1 = t(x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0);
            return scale;
        };
    }

    function copy$1(source, target) {
        return target
            .domain(source.domain())
            .interpolator(source.interpolator())
            .clamp(source.clamp())
            .unknown(source.unknown());
    }

    function sequential() {
        var scale = linearish(transformer$1()(identity$2));

        scale.copy = function () {
            return copy$1(scale, sequential());
        };

        return initInterpolator.apply(scale, arguments);
    }

    var xhtml = "http://www.w3.org/1999/xhtml";

    var namespaces = {
        svg: "http://www.w3.org/2000/svg",
        xhtml: xhtml,
        xlink: "http://www.w3.org/1999/xlink",
        xml: "http://www.w3.org/XML/1998/namespace",
        xmlns: "http://www.w3.org/2000/xmlns/"
    };

    function namespace(name) {
        var prefix = name += "", i = prefix.indexOf(":");
        if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1);
        return namespaces.hasOwnProperty(prefix) ? {space: namespaces[prefix], local: name} : name;
    }

    function creatorInherit(name) {
        return function () {
            var document = this.ownerDocument,
                uri = this.namespaceURI;
            return uri === xhtml && document.documentElement.namespaceURI === xhtml
                ? document.createElement(name)
                : document.createElementNS(uri, name);
        };
    }

    function creatorFixed(fullname) {
        return function () {
            return this.ownerDocument.createElementNS(fullname.space, fullname.local);
        };
    }

    function creator(name) {
        var fullname = namespace(name);
        return (fullname.local
            ? creatorFixed
            : creatorInherit)(fullname);
    }

    function none() {
    }

    function selector(selector) {
        return selector == null ? none : function () {
            return this.querySelector(selector);
        };
    }

    function selection_select(select) {
        if (typeof select !== "function") select = selector(select);

        for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, subgroup = subgroups[j] = new Array(n), node, subnode, i = 0; i < n; ++i) {
                if ((node = group[i]) && (subnode = select.call(node, node.__data__, i, group))) {
                    if ("__data__" in node) subnode.__data__ = node.__data__;
                    subgroup[i] = subnode;
                }
            }
        }

        return new Selection(subgroups, this._parents);
    }

    function empty() {
        return [];
    }

    function selectorAll(selector) {
        return selector == null ? empty : function () {
            return this.querySelectorAll(selector);
        };
    }

    function selection_selectAll(select) {
        if (typeof select !== "function") select = selectorAll(select);

        for (var groups = this._groups, m = groups.length, subgroups = [], parents = [], j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
                if (node = group[i]) {
                    subgroups.push(select.call(node, node.__data__, i, group));
                    parents.push(node);
                }
            }
        }

        return new Selection(subgroups, parents);
    }

    function matcher(selector) {
        return function () {
            return this.matches(selector);
        };
    }

    function selection_filter(match) {
        if (typeof match !== "function") match = matcher(match);

        for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, subgroup = subgroups[j] = [], node, i = 0; i < n; ++i) {
                if ((node = group[i]) && match.call(node, node.__data__, i, group)) {
                    subgroup.push(node);
                }
            }
        }

        return new Selection(subgroups, this._parents);
    }

    function sparse(update) {
        return new Array(update.length);
    }

    function selection_enter() {
        return new Selection(this._enter || this._groups.map(sparse), this._parents);
    }

    function EnterNode(parent, datum) {
        this.ownerDocument = parent.ownerDocument;
        this.namespaceURI = parent.namespaceURI;
        this._next = null;
        this._parent = parent;
        this.__data__ = datum;
    }

    EnterNode.prototype = {
        constructor: EnterNode,
        appendChild: function (child) {
            return this._parent.insertBefore(child, this._next);
        },
        insertBefore: function (child, next) {
            return this._parent.insertBefore(child, next);
        },
        querySelector: function (selector) {
            return this._parent.querySelector(selector);
        },
        querySelectorAll: function (selector) {
            return this._parent.querySelectorAll(selector);
        }
    };

    function constant$2(x) {
        return function () {
            return x;
        };
    }

    var keyPrefix = "$"; // Protect against keys like “__proto__”.

    function bindIndex(parent, group, enter, update, exit, data) {
        var i = 0,
            node,
            groupLength = group.length,
            dataLength = data.length;

        // Put any non-null nodes that fit into update.
        // Put any null nodes into enter.
        // Put any remaining data into enter.
        for (; i < dataLength; ++i) {
            if (node = group[i]) {
                node.__data__ = data[i];
                update[i] = node;
            } else {
                enter[i] = new EnterNode(parent, data[i]);
            }
        }

        // Put any non-null nodes that don’t fit into exit.
        for (; i < groupLength; ++i) {
            if (node = group[i]) {
                exit[i] = node;
            }
        }
    }

    function bindKey(parent, group, enter, update, exit, data, key) {
        var i,
            node,
            nodeByKeyValue = {},
            groupLength = group.length,
            dataLength = data.length,
            keyValues = new Array(groupLength),
            keyValue;

        // Compute the key for each node.
        // If multiple nodes have the same key, the duplicates are added to exit.
        for (i = 0; i < groupLength; ++i) {
            if (node = group[i]) {
                keyValues[i] = keyValue = keyPrefix + key.call(node, node.__data__, i, group);
                if (keyValue in nodeByKeyValue) {
                    exit[i] = node;
                } else {
                    nodeByKeyValue[keyValue] = node;
                }
            }
        }

        // Compute the key for each datum.
        // If there a node associated with this key, join and add it to update.
        // If there is not (or the key is a duplicate), add it to enter.
        for (i = 0; i < dataLength; ++i) {
            keyValue = keyPrefix + key.call(parent, data[i], i, data);
            if (node = nodeByKeyValue[keyValue]) {
                update[i] = node;
                node.__data__ = data[i];
                nodeByKeyValue[keyValue] = null;
            } else {
                enter[i] = new EnterNode(parent, data[i]);
            }
        }

        // Add any remaining nodes that were not bound to data to exit.
        for (i = 0; i < groupLength; ++i) {
            if ((node = group[i]) && (nodeByKeyValue[keyValues[i]] === node)) {
                exit[i] = node;
            }
        }
    }

    function selection_data(value, key) {
        if (!value) {
            data = new Array(this.size()), j = -1;
            this.each(function (d) {
                data[++j] = d;
            });
            return data;
        }

        var bind = key ? bindKey : bindIndex,
            parents = this._parents,
            groups = this._groups;

        if (typeof value !== "function") value = constant$2(value);

        for (var m = groups.length, update = new Array(m), enter = new Array(m), exit = new Array(m), j = 0; j < m; ++j) {
            var parent = parents[j],
                group = groups[j],
                groupLength = group.length,
                data = value.call(parent, parent && parent.__data__, j, parents),
                dataLength = data.length,
                enterGroup = enter[j] = new Array(dataLength),
                updateGroup = update[j] = new Array(dataLength),
                exitGroup = exit[j] = new Array(groupLength);

            bind(parent, group, enterGroup, updateGroup, exitGroup, data, key);

            // Now connect the enter nodes to their following update node, such that
            // appendChild can insert the materialized enter node before this node,
            // rather than at the end of the parent node.
            for (var i0 = 0, i1 = 0, previous, next; i0 < dataLength; ++i0) {
                if (previous = enterGroup[i0]) {
                    if (i0 >= i1) i1 = i0 + 1;
                    while (!(next = updateGroup[i1]) && ++i1 < dataLength) ;
                    previous._next = next || null;
                }
            }
        }

        update = new Selection(update, parents);
        update._enter = enter;
        update._exit = exit;
        return update;
    }

    function selection_exit() {
        return new Selection(this._exit || this._groups.map(sparse), this._parents);
    }

    function selection_join(onenter, onupdate, onexit) {
        var enter = this.enter(), update = this, exit = this.exit();
        enter = typeof onenter === "function" ? onenter(enter) : enter.append(onenter + "");
        if (onupdate != null) update = onupdate(update);
        if (onexit == null) exit.remove(); else onexit(exit);
        return enter && update ? enter.merge(update).order() : update;
    }

    function selection_merge(selection) {

        for (var groups0 = this._groups, groups1 = selection._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
            for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
                if (node = group0[i] || group1[i]) {
                    merge[i] = node;
                }
            }
        }

        for (; j < m0; ++j) {
            merges[j] = groups0[j];
        }

        return new Selection(merges, this._parents);
    }

    function selection_order() {

        for (var groups = this._groups, j = -1, m = groups.length; ++j < m;) {
            for (var group = groups[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
                if (node = group[i]) {
                    if (next && node.compareDocumentPosition(next) ^ 4) next.parentNode.insertBefore(node, next);
                    next = node;
                }
            }
        }

        return this;
    }

    function selection_sort(compare) {
        if (!compare) compare = ascending;

        function compareNode(a, b) {
            return a && b ? compare(a.__data__, b.__data__) : !a - !b;
        }

        for (var groups = this._groups, m = groups.length, sortgroups = new Array(m), j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, sortgroup = sortgroups[j] = new Array(n), node, i = 0; i < n; ++i) {
                if (node = group[i]) {
                    sortgroup[i] = node;
                }
            }
            sortgroup.sort(compareNode);
        }

        return new Selection(sortgroups, this._parents).order();
    }

    function ascending(a, b) {
        return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
    }

    function selection_call() {
        var callback = arguments[0];
        arguments[0] = this;
        callback.apply(null, arguments);
        return this;
    }

    function selection_nodes() {
        var nodes = new Array(this.size()), i = -1;
        this.each(function () {
            nodes[++i] = this;
        });
        return nodes;
    }

    function selection_node() {

        for (var groups = this._groups, j = 0, m = groups.length; j < m; ++j) {
            for (var group = groups[j], i = 0, n = group.length; i < n; ++i) {
                var node = group[i];
                if (node) return node;
            }
        }

        return null;
    }

    function selection_size() {
        var size = 0;
        this.each(function () {
            ++size;
        });
        return size;
    }

    function selection_empty() {
        return !this.node();
    }

    function selection_each(callback) {

        for (var groups = this._groups, j = 0, m = groups.length; j < m; ++j) {
            for (var group = groups[j], i = 0, n = group.length, node; i < n; ++i) {
                if (node = group[i]) callback.call(node, node.__data__, i, group);
            }
        }

        return this;
    }

    function attrRemove(name) {
        return function () {
            this.removeAttribute(name);
        };
    }

    function attrRemoveNS(fullname) {
        return function () {
            this.removeAttributeNS(fullname.space, fullname.local);
        };
    }

    function attrConstant(name, value) {
        return function () {
            this.setAttribute(name, value);
        };
    }

    function attrConstantNS(fullname, value) {
        return function () {
            this.setAttributeNS(fullname.space, fullname.local, value);
        };
    }

    function attrFunction(name, value) {
        return function () {
            var v = value.apply(this, arguments);
            if (v == null) this.removeAttribute(name);
            else this.setAttribute(name, v);
        };
    }

    function attrFunctionNS(fullname, value) {
        return function () {
            var v = value.apply(this, arguments);
            if (v == null) this.removeAttributeNS(fullname.space, fullname.local);
            else this.setAttributeNS(fullname.space, fullname.local, v);
        };
    }

    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));
    }

    function defaultView(node) {
        return (node.ownerDocument && node.ownerDocument.defaultView) // node is a Node
            || (node.document && node) // node is a Window
            || node.defaultView; // node is a Document
    }

    function styleRemove(name) {
        return function () {
            this.style.removeProperty(name);
        };
    }

    function styleConstant(name, value, priority) {
        return function () {
            this.style.setProperty(name, value, priority);
        };
    }

    function styleFunction(name, value, priority) {
        return function () {
            var v = value.apply(this, arguments);
            if (v == null) this.style.removeProperty(name);
            else this.style.setProperty(name, v, priority);
        };
    }

    function selection_style(name, value, priority) {
        return arguments.length > 1
            ? this.each((value == null
                ? styleRemove : typeof value === "function"
                    ? styleFunction
                    : styleConstant)(name, value, priority == null ? "" : priority))
            : styleValue(this.node(), name);
    }

    function styleValue(node, name) {
        return node.style.getPropertyValue(name)
            || defaultView(node).getComputedStyle(node, null).getPropertyValue(name);
    }

    function propertyRemove(name) {
        return function () {
            delete this[name];
        };
    }

    function propertyConstant(name, value) {
        return function () {
            this[name] = value;
        };
    }

    function propertyFunction(name, value) {
        return function () {
            var v = value.apply(this, arguments);
            if (v == null) delete this[name];
            else this[name] = v;
        };
    }

    function selection_property(name, value) {
        return arguments.length > 1
            ? this.each((value == null
                ? propertyRemove : typeof value === "function"
                    ? propertyFunction
                    : propertyConstant)(name, value))
            : this.node()[name];
    }

    function classArray(string) {
        return string.trim().split(/^|\s+/);
    }

    function classList(node) {
        return node.classList || new ClassList(node);
    }

    function ClassList(node) {
        this._node = node;
        this._names = classArray(node.getAttribute("class") || "");
    }

    ClassList.prototype = {
        add: function (name) {
            var i = this._names.indexOf(name);
            if (i < 0) {
                this._names.push(name);
                this._node.setAttribute("class", this._names.join(" "));
            }
        },
        remove: function (name) {
            var i = this._names.indexOf(name);
            if (i >= 0) {
                this._names.splice(i, 1);
                this._node.setAttribute("class", this._names.join(" "));
            }
        },
        contains: function (name) {
            return this._names.indexOf(name) >= 0;
        }
    };

    function classedAdd(node, names) {
        var list = classList(node), i = -1, n = names.length;
        while (++i < n) list.add(names[i]);
    }

    function classedRemove(node, names) {
        var list = classList(node), i = -1, n = names.length;
        while (++i < n) list.remove(names[i]);
    }

    function classedTrue(names) {
        return function () {
            classedAdd(this, names);
        };
    }

    function classedFalse(names) {
        return function () {
            classedRemove(this, names);
        };
    }

    function classedFunction(names, value) {
        return function () {
            (value.apply(this, arguments) ? classedAdd : classedRemove)(this, names);
        };
    }

    function selection_classed(name, value) {
        var names = classArray(name + "");

        if (arguments.length < 2) {
            var list = classList(this.node()), i = -1, n = names.length;
            while (++i < n) if (!list.contains(names[i])) return false;
            return true;
        }

        return this.each((typeof value === "function"
            ? classedFunction : value
                ? classedTrue
                : classedFalse)(names, value));
    }

    function textRemove() {
        this.textContent = "";
    }

    function textConstant(value) {
        return function () {
            this.textContent = value;
        };
    }

    function textFunction(value) {
        return function () {
            var v = value.apply(this, arguments);
            this.textContent = v == null ? "" : v;
        };
    }

    function selection_text(value) {
        return arguments.length
            ? this.each(value == null
                ? textRemove : (typeof value === "function"
                    ? textFunction
                    : textConstant)(value))
            : this.node().textContent;
    }

    function htmlRemove() {
        this.innerHTML = "";
    }

    function htmlConstant(value) {
        return function () {
            this.innerHTML = value;
        };
    }

    function htmlFunction(value) {
        return function () {
            var v = value.apply(this, arguments);
            this.innerHTML = v == null ? "" : v;
        };
    }

    function selection_html(value) {
        return arguments.length
            ? this.each(value == null
                ? htmlRemove : (typeof value === "function"
                    ? htmlFunction
                    : htmlConstant)(value))
            : this.node().innerHTML;
    }

    function raise() {
        if (this.nextSibling) this.parentNode.appendChild(this);
    }

    function selection_raise() {
        return this.each(raise);
    }

    function lower() {
        if (this.previousSibling) this.parentNode.insertBefore(this, this.parentNode.firstChild);
    }

    function selection_lower() {
        return this.each(lower);
    }

    function selection_append(name) {
        var create = typeof name === "function" ? name : creator(name);
        return this.select(function () {
            return this.appendChild(create.apply(this, arguments));
        });
    }

    function constantNull() {
        return null;
    }

    function selection_insert(name, before) {
        var create = typeof name === "function" ? name : creator(name),
            select = before == null ? constantNull : typeof before === "function" ? before : selector(before);
        return this.select(function () {
            return this.insertBefore(create.apply(this, arguments), select.apply(this, arguments) || null);
        });
    }

    function remove() {
        var parent = this.parentNode;
        if (parent) parent.removeChild(this);
    }

    function selection_remove() {
        return this.each(remove);
    }

    function selection_cloneShallow() {
        var clone = this.cloneNode(false), parent = this.parentNode;
        return parent ? parent.insertBefore(clone, this.nextSibling) : clone;
    }

    function selection_cloneDeep() {
        var clone = this.cloneNode(true), parent = this.parentNode;
        return parent ? parent.insertBefore(clone, this.nextSibling) : clone;
    }

    function selection_clone(deep) {
        return this.select(deep ? selection_cloneDeep : selection_cloneShallow);
    }

    function selection_datum(value) {
        return arguments.length
            ? this.property("__data__", value)
            : this.node().__data__;
    }

    var filterEvents = {};

    var event = null;

    if (typeof document !== "undefined") {
        var element = document.documentElement;
        if (!("onmouseenter" in element)) {
            filterEvents = {mouseenter: "mouseover", mouseleave: "mouseout"};
        }
    }

    function filterContextListener(listener, index, group) {
        listener = contextListener(listener, index, group);
        return function (event) {
            var related = event.relatedTarget;
            if (!related || (related !== this && !(related.compareDocumentPosition(this) & 8))) {
                listener.call(this, event);
            }
        };
    }

    function contextListener(listener, index, group) {
        return function (event1) {
            var event0 = event; // Events can be reentrant (e.g., focus).
            event = event1;
            try {
                listener.call(this, this.__data__, index, group);
            } finally {
                event = event0;
            }
        };
    }

    function parseTypenames(typenames) {
        return typenames.trim().split(/^|\s+/).map(function (t) {
            var name = "", i = t.indexOf(".");
            if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i);
            return {type: t, name: name};
        });
    }

    function onRemove(typename) {
        return function () {
            var on = this.__on;
            if (!on) return;
            for (var j = 0, i = -1, m = on.length, o; j < m; ++j) {
                if (o = on[j], (!typename.type || o.type === typename.type) && o.name === typename.name) {
                    this.removeEventListener(o.type, o.listener, o.capture);
                } else {
                    on[++i] = o;
                }
            }
            if (++i) on.length = i;
            else delete this.__on;
        };
    }

    function onAdd(typename, value, capture) {
        var wrap = filterEvents.hasOwnProperty(typename.type) ? filterContextListener : contextListener;
        return function (d, i, group) {
            var on = this.__on, o, listener = wrap(value, i, group);
            if (on) for (var j = 0, m = on.length; j < m; ++j) {
                if ((o = on[j]).type === typename.type && o.name === typename.name) {
                    this.removeEventListener(o.type, o.listener, o.capture);
                    this.addEventListener(o.type, o.listener = listener, o.capture = capture);
                    o.value = value;
                    return;
                }
            }
            this.addEventListener(typename.type, listener, capture);
            o = {type: typename.type, name: typename.name, value: value, listener: listener, capture: capture};
            if (!on) this.__on = [o];
            else on.push(o);
        };
    }

    function selection_on(typename, value, capture) {
        var typenames = parseTypenames(typename + ""), i, n = typenames.length, t;

        if (arguments.length < 2) {
            var on = this.node().__on;
            if (on) for (var j = 0, m = on.length, o; j < m; ++j) {
                for (i = 0, o = on[j]; i < n; ++i) {
                    if ((t = typenames[i]).type === o.type && t.name === o.name) {
                        return o.value;
                    }
                }
            }
            return;
        }

        on = value ? onAdd : onRemove;
        if (capture == null) capture = false;
        for (i = 0; i < n; ++i) this.each(on(typenames[i], value, capture));
        return this;
    }

    function customEvent(event1, listener, that, args) {
        var event0 = event;
        event1.sourceEvent = event;
        event = event1;
        try {
            return listener.apply(that, args);
        } finally {
            event = event0;
        }
    }

    function dispatchEvent(node, type, params) {
        var window = defaultView(node),
            event = window.CustomEvent;

        if (typeof event === "function") {
            event = new event(type, params);
        } else {
            event = window.document.createEvent("Event");
            if (params) event.initEvent(type, params.bubbles, params.cancelable), event.detail = params.detail;
            else event.initEvent(type, false, false);
        }

        node.dispatchEvent(event);
    }

    function dispatchConstant(type, params) {
        return function () {
            return dispatchEvent(this, type, params);
        };
    }

    function dispatchFunction(type, params) {
        return function () {
            return dispatchEvent(this, type, params.apply(this, arguments));
        };
    }

    function selection_dispatch(type, params) {
        return this.each((typeof params === "function"
            ? dispatchFunction
            : dispatchConstant)(type, params));
    }

    var root = [null];

    function Selection(groups, parents) {
        this._groups = groups;
        this._parents = parents;
    }

    function selection() {
        return new Selection([[document.documentElement]], root);
    }

    Selection.prototype = selection.prototype = {
        constructor: Selection,
        select: selection_select,
        selectAll: selection_selectAll,
        filter: selection_filter,
        data: selection_data,
        enter: selection_enter,
        exit: selection_exit,
        join: selection_join,
        merge: selection_merge,
        order: selection_order,
        sort: selection_sort,
        call: selection_call,
        nodes: selection_nodes,
        node: selection_node,
        size: selection_size,
        empty: selection_empty,
        each: selection_each,
        attr: selection_attr,
        style: selection_style,
        property: selection_property,
        classed: selection_classed,
        text: selection_text,
        html: selection_html,
        raise: selection_raise,
        lower: selection_lower,
        append: selection_append,
        insert: selection_insert,
        remove: selection_remove,
        clone: selection_clone,
        datum: selection_datum,
        on: selection_on,
        dispatch: selection_dispatch
    };

    function d3Select(selector) {
        return typeof selector === "string"
            ? new Selection([[document.querySelector(selector)]], [document.documentElement])
            : new Selection([[selector]], root);
    }

    function sourceEvent() {
        var current = event, source;
        while (source = current.sourceEvent) current = source;
        return current;
    }

    function point$1(node, event) {
        var svg = node.ownerSVGElement || node;

        if (svg.createSVGPoint) {
            var point = svg.createSVGPoint();
            point.x = event.clientX, point.y = event.clientY;
            point = point.matrixTransform(node.getScreenCTM().inverse());
            return [point.x, point.y];
        }

        var rect = node.getBoundingClientRect();
        return [event.clientX - rect.left - node.clientLeft, event.clientY - rect.top - node.clientTop];
    }

    function d3Mouse(node) {
        var event = sourceEvent();
        if (event.changedTouches) event = event.changedTouches[0];
        return point$1(node, event);
    }

    function touch(node, touches, identifier) {
        if (arguments.length < 3) identifier = touches, touches = sourceEvent().changedTouches;

        for (var i = 0, n = touches ? touches.length : 0, touch; i < n; ++i) {
            if ((touch = touches[i]).identifier === identifier) {
                return point$1(node, touch);
            }
        }

        return null;
    }

    var prefix = "$";

    function Map$1() {
    }

    Map$1.prototype = map$1.prototype = {
        constructor: Map$1,
        has: function (key) {
            return (prefix + key) in this;
        },
        get: function (key) {
            return this[prefix + key];
        },
        set: function (key, value) {
            this[prefix + key] = value;
            return this;
        },
        remove: function (key) {
            var property = prefix + key;
            return property in this && delete this[property];
        },
        clear: function () {
            for (var property in this) if (property[0] === prefix) delete this[property];
        },
        keys: function () {
            var keys = [];
            for (var property in this) if (property[0] === prefix) keys.push(property.slice(1));
            return keys;
        },
        values: function () {
            var values = [];
            for (var property in this) if (property[0] === prefix) values.push(this[property]);
            return values;
        },
        entries: function () {
            var entries = [];
            for (var property in this) if (property[0] === prefix) entries.push({
                key: property.slice(1),
                value: this[property]
            });
            return entries;
        },
        size: function () {
            var size = 0;
            for (var property in this) if (property[0] === prefix) ++size;
            return size;
        },
        empty: function () {
            for (var property in this) if (property[0] === prefix) return false;
            return true;
        },
        each: function (f) {
            for (var property in this) if (property[0] === prefix) f(this[property], property.slice(1), this);
        }
    };

    function map$1(object, f) {
        var map = new Map$1;

        // Copy constructor.
        if (object instanceof Map$1) object.each(function (value, key) {
            map.set(key, value);
        });

        // Index array by numeric index or specified key function.
        else if (Array.isArray(object)) {
            var i = -1,
                n = object.length,
                o;

            if (f == null) while (++i < n) map.set(i, object[i]);
            else while (++i < n) map.set(f(o = object[i], i, object), o);
        }

        // Convert object to map.
        else if (object) for (var key in object) map.set(key, object[key]);

        return map;
    }

    function Set() {
    }

    var proto = map$1.prototype;

    Set.prototype = set.prototype = {
        constructor: Set,
        has: proto.has,
        add: function (value) {
            value += "";
            this[prefix + value] = value;
            return this;
        },
        remove: proto.remove,
        clear: proto.clear,
        values: proto.keys,
        size: proto.size,
        empty: proto.empty,
        each: proto.each
    };

    function set(object, f) {
        var set = new Set;

        // Copy constructor.
        if (object instanceof Set) object.each(function (value) {
            set.add(value);
        });

        // Otherwise, assume it’s an array.
        else if (object) {
            var i = -1, n = object.length;
            if (f == null) while (++i < n) set.add(object[i]);
            else while (++i < n) set.add(f(object[i], i, object));
        }

        return set;
    }

    /**
     * d3.tip
     * Copyright (c) 2013-2017 Justin Palmer
     *
     * Tooltips for d3.js SVG visualizations
     */
    // Public - constructs a new tooltip
    //
    // Returns a tip
    function d3Tip() {
        var direction = d3TipDirection,
            offset = d3TipOffset,
            html = d3TipHTML,
            rootElement = document.body,
            node = initNode(),
            svg = null,
            point = null,
            target = null;

        function tip(vis) {
            svg = getSVGNode(vis);
            if (!svg) return
            point = svg.createSVGPoint();
            rootElement.appendChild(node);
        }

        // Public - show the tooltip on the screen
        //
        // Returns a tip
        tip.show = function () {
            var args = Array.prototype.slice.call(arguments);
            if (args[args.length - 1] instanceof SVGElement) target = args.pop();

            var content = html.apply(this, args),
                poffset = offset.apply(this, args),
                dir = direction.apply(this, args),
                nodel = getNodeEl(),
                i = directions.length,
                coords,
                scrollTop = document.documentElement.scrollTop ||
                    rootElement.scrollTop,
                scrollLeft = document.documentElement.scrollLeft ||
                    rootElement.scrollLeft;

            nodel.html(content)
                .style('opacity', 1).style('pointer-events', 'all');

            while (i--) nodel.classed(directions[i], false);
            coords = directionCallbacks.get(dir).apply(this);
            nodel.classed(dir, true)
                .style('top', (coords.top + poffset[0]) + scrollTop + 'px')
                .style('left', (coords.left + poffset[1]) + scrollLeft + 'px');

            return tip
        };

        // Public - hide the tooltip
        //
        // Returns a tip
        tip.hide = function () {
            var nodel = getNodeEl();
            nodel.style('opacity', 0).style('pointer-events', 'none');
            return tip
        };

        // Public: Proxy attr calls to the d3 tip container.
        // Sets or gets attribute value.
        //
        // n - name of the attribute
        // v - value of the attribute
        //
        // Returns tip or attribute value
        // eslint-disable-next-line no-unused-vars
        tip.attr = function (n, v) {
            if (arguments.length < 2 && typeof n === 'string') {
                return getNodeEl().attr(n)
            }

            var args = Array.prototype.slice.call(arguments);
            selection.prototype.attr.apply(getNodeEl(), args);
            return tip
        };

        // Public: Proxy style calls to the d3 tip container.
        // Sets or gets a style value.
        //
        // n - name of the property
        // v - value of the property
        //
        // Returns tip or style property value
        // eslint-disable-next-line no-unused-vars
        tip.style = function (n, v) {
            if (arguments.length < 2 && typeof n === 'string') {
                return getNodeEl().style(n)
            }

            var args = Array.prototype.slice.call(arguments);
            selection.prototype.style.apply(getNodeEl(), args);
            return tip
        };

        // Public: Set or get the direction of the tooltip
        //
        // v - One of n(north), s(south), e(east), or w(west), nw(northwest),
        //     sw(southwest), ne(northeast) or se(southeast)
        //
        // Returns tip or direction
        tip.direction = function (v) {
            if (!arguments.length) return direction
            direction = v == null ? v : functor(v);

            return tip
        };

        // Public: Sets or gets the offset of the tip
        //
        // v - Array of [x, y] offset
        //
        // Returns offset or
        tip.offset = function (v) {
            if (!arguments.length) return offset
            offset = v == null ? v : functor(v);

            return tip
        };

        // Public: sets or gets the html value of the tooltip
        //
        // v - String value of the tip
        //
        // Returns html value or tip
        tip.html = function (v) {
            if (!arguments.length) return html
            html = v == null ? v : functor(v);

            return tip
        };

        // Public: sets or gets the root element anchor of the tooltip
        //
        // v - root element of the tooltip
        //
        // Returns root node of tip
        tip.rootElement = function (v) {
            if (!arguments.length) return rootElement
            rootElement = v == null ? v : functor(v);

            return tip
        };

        // Public: destroys the tooltip and removes it from the DOM
        //
        // Returns a tip
        tip.destroy = function () {
            if (node) {
                getNodeEl().remove();
                node = null;
            }
            return tip
        };

        function d3TipDirection() {
            return 'n'
        }

        function d3TipOffset() {
            return [0, 0]
        }

        function d3TipHTML() {
            return ' '
        }

        var directionCallbacks = map$1({
                n: directionNorth,
                s: directionSouth,
                e: directionEast,
                w: directionWest,
                nw: directionNorthWest,
                ne: directionNorthEast,
                sw: directionSouthWest,
                se: directionSouthEast
            }),
            directions = directionCallbacks.keys();

        function directionNorth() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.n.y - node.offsetHeight,
                left: bbox.n.x - node.offsetWidth / 2
            }
        }

        function directionSouth() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.s.y,
                left: bbox.s.x - node.offsetWidth / 2
            }
        }

        function directionEast() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.e.y - node.offsetHeight / 2,
                left: bbox.e.x
            }
        }

        function directionWest() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.w.y - node.offsetHeight / 2,
                left: bbox.w.x - node.offsetWidth
            }
        }

        function directionNorthWest() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.nw.y - node.offsetHeight,
                left: bbox.nw.x - node.offsetWidth
            }
        }

        function directionNorthEast() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.ne.y - node.offsetHeight,
                left: bbox.ne.x
            }
        }

        function directionSouthWest() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.sw.y,
                left: bbox.sw.x - node.offsetWidth
            }
        }

        function directionSouthEast() {
            var bbox = getScreenBBox(this);
            return {
                top: bbox.se.y,
                left: bbox.se.x
            }
        }

        function initNode() {
            var div = d3Select(document.createElement('div'));
            div
                .style('position', 'absolute')
                .style('top', 0)
                .style('opacity', 0)
                .style('pointer-events', 'none')
                .style('box-sizing', 'border-box');

            return div.node()
        }

        function getSVGNode(element) {
            var svgNode = element.node();
            if (!svgNode) return null
            if (svgNode.tagName.toLowerCase() === 'svg') return svgNode
            return svgNode.ownerSVGElement
        }

        function getNodeEl() {
            if (node == null) {
                node = initNode();
                // re-add node to DOM
                rootElement.appendChild(node);
            }
            return d3Select(node)
        }

        // Private - gets the screen coordinates of a shape
        //
        // Given a shape on the screen, will return an SVGPoint for the directions
        // n(north), s(south), e(east), w(west), ne(northeast), se(southeast),
        // nw(northwest), sw(southwest).
        //
        //    +-+-+
        //    |   |
        //    +   +
        //    |   |
        //    +-+-+
        //
        // Returns an Object {n, s, e, w, nw, sw, ne, se}
        function getScreenBBox(targetShape) {
            var targetel = target || targetShape;

            while (targetel.getScreenCTM == null && targetel.parentNode != null) {
                targetel = targetel.parentNode;
            }

            var bbox = {},
                matrix = targetel.getScreenCTM(),
                tbbox = targetel.getBBox(),
                width = tbbox.width,
                height = tbbox.height,
                x = tbbox.x,
                y = tbbox.y;

            point.x = x;
            point.y = y;
            bbox.nw = point.matrixTransform(matrix);
            point.x += width;
            bbox.ne = point.matrixTransform(matrix);
            point.y += height;
            bbox.se = point.matrixTransform(matrix);
            point.x -= width;
            bbox.sw = point.matrixTransform(matrix);
            point.y -= height / 2;
            bbox.w = point.matrixTransform(matrix);
            point.x += width;
            bbox.e = point.matrixTransform(matrix);
            point.x -= width / 2;
            point.y -= height / 2;
            bbox.n = point.matrixTransform(matrix);
            point.y += height;
            bbox.s = point.matrixTransform(matrix);

            return bbox
        }

        // Private - replace D3JS 3.X d3.functor() function
        function functor(v) {
            return typeof v === 'function' ? v : function () {
                return v
            }
        }

        return tip
    }

    function colors(specifier) {
        var n = specifier.length / 6 | 0, colors = new Array(n), i = 0;
        while (i < n) colors[i] = "#" + specifier.slice(i * 6, ++i * 6);
        return colors;
    }

    var schemeCategory10 = colors("1f77b4ff7f0e2ca02cd627289467bd8c564be377c27f7f7fbcbd2217becf");

    var schemeSet3 = colors("8dd3c7ffffb3bebadafb807280b1d3fdb462b3de69fccde5d9d9d9bc80bdccebc5ffed6f");

    function ramp(scheme) {
        return rgbBasis(scheme[scheme.length - 1]);
    }

    var scheme = new Array(3).concat(
        "fc8d59ffffbf91bfdb",
        "d7191cfdae61abd9e92c7bb6",
        "d7191cfdae61ffffbfabd9e92c7bb6",
        "d73027fc8d59fee090e0f3f891bfdb4575b4",
        "d73027fc8d59fee090ffffbfe0f3f891bfdb4575b4",
        "d73027f46d43fdae61fee090e0f3f8abd9e974add14575b4",
        "d73027f46d43fdae61fee090ffffbfe0f3f8abd9e974add14575b4",
        "a50026d73027f46d43fdae61fee090e0f3f8abd9e974add14575b4313695",
        "a50026d73027f46d43fdae61fee090ffffbfe0f3f8abd9e974add14575b4313695"
    ).map(colors);

    var interpolateRdYlBu = ramp(scheme);

    var moveToFront = index({
        init: function init(el) {
            el.parentNode.appendChild(el);
        }
    });

    var gradient = index({
        props: {
            id: {},
            // Use with: .attr('fill', 'url(#<gradId>)');
            colorScale: {
                "default": linear$1().range(['black', 'white'])
            },
            angle: {
                "default": 0
            } // 0 (left-right), 90 (down-up))

        },
        init: function init(el, state) {
            state.id = "areaGradient".concat(Math.round(Math.random() * 10000));
            state.gradient = d3Select(el).append('linearGradient');
        },
        update: function update(state) {
            var rad = Math.PI * state.angle / 180;
            state.gradient.attr('y1', Math.round(100 * Math.max(0, Math.sin(rad))) + '%').attr('y2', Math.round(100 * Math.max(0, -Math.sin(rad))) + '%').attr('x1', Math.round(100 * Math.max(0, -Math.cos(rad))) + '%').attr('x2', Math.round(100 * Math.max(0, Math.cos(rad))) + '%').attr('id', state.id);
            var stopsScale = linear$1().domain([0, 100]).range(state.colorScale.domain());
            var colorStops = state.gradient.selectAll('stop').data(d3Range(0, 100.01, 20)); // 11 stops is sufficient to cover all noticeable color nuances

            colorStops.exit().remove();
            colorStops.merge(colorStops.enter().append('stop')).attr('offset', function (d) {
                return "".concat(d, "%");
            }).attr('stop-color', function (d) {
                return state.colorScale(stopsScale(d));
            });
        }
    });

    var dropshadow = index({
        // Use with: .attr('filter', 'url(#<shadowId>)'))
        props: {
            id: {
                "default": "areaGradient".concat(Math.round(Math.random() * 10000))
            }
        },
        init: function init(el, state) {
            state.filter = d3Select(el).append('defs').append('filter').attr('height', '130%');
            state.filter.append('feGaussianBlur').attr('in', 'SourceAlpha').attr('stdDeviation', 3);
            state.filter.append('feOffset').attr('dx', 2).attr('dy', 2).attr('result', 'offsetblur');
            var feMerge = state.filter.append('feMerge');
            feMerge.append('feMergeNode');
            feMerge.append('feMergeNode').attr('in', 'SourceGraphic');
        },
        update: function update(state) {
            state.filter.attr('id', state.id);
        }
    });

    var throbber = index({
        props: {
            x: {
                "default": 0
            },
            y: {
                "default": 0
            },
            r: {
                "default": 8
            },
            color: {
                "default": 'darkblue'
            },
            duration: {
                "default": 0.7
            },
            angleFull: {
                "default": 120
            }
        },
        init: function init(el, state) {
            el = d3Select(el);
            state.path = el.append('path');
            state.transform = state.path.append('animateTransform').attr('attributeName', 'transform').attr('attributeType', 'XML').attr('type', 'rotate').attr('begin', '0s').attr('fill', 'freeze').attr('repeatCount', 'indefinite');
        },
        update: function update(state) {
            state.path.attr('d', genDonutSlice(state.x, state.y, state.r, state.r / 3, 0, state.angleFull)).attr('fill', state.color);
            state.transform.attr('from', '0 ' + state.x + ' ' + state.y).attr('to', '360 ' + state.x + ' ' + state.y).attr('dur', state.duration + 's'); //

            function genDonutSlice(cx, cy, r, thickness, startAngle, endAngle) {
                startAngle = startAngle / 180 * Math.PI;
                endAngle = endAngle / 180 * Math.PI;
                var outerR = r;
                var innerR = r - thickness;
                var p = [[cx + outerR * Math.cos(startAngle), cy + outerR * Math.sin(startAngle)], [cx + outerR * Math.cos(endAngle), cy + outerR * Math.sin(endAngle)], [cx + innerR * Math.cos(endAngle), cy + innerR * Math.sin(endAngle)], [cx + innerR * Math.cos(startAngle), cy + innerR * Math.sin(startAngle)]];
                var angleDiff = endAngle - startAngle;
                var largeArc = angleDiff % (Math.PI * 2) > Math.PI ? 1 : 0;
                var path = [];
                path.push('M' + p[0].join());
                path.push('A' + [outerR, outerR, 0, largeArc, 1, p[1]].join());
                path.push('L' + p[2].join());
                path.push('A' + [innerR, innerR, 0, largeArc, 0, p[3]].join());
                path.push('z');
                return path.join(' ');
            }
        }
    });

    var image = index({
        props: {
            imgUrl: {},
            x: {
                "default": 0
            },
            y: {
                "default": 0
            },
            maxWidth: {
                "default": 20
            },
            maxHeight: {
                "default": 20
            },
            svgAlign: {
                "default": 'xMidYMid'
            }
        },
        methods: {
            show: function show(state) {
                state.img.attr('width', state.maxWidth).attr('height', state.maxHeight);
                return this;
            },
            hide: function hide(state) {
                state.img.attr('width', 0).attr('height', 0);
                return this;
            }
        },
        init: function init(el, state) {
            state.img = d3Select(el).append('image');
        },
        update: function update(state) {
            state.img.attr('xlink:href', state.imgUrl).attr('x', state.x).attr('y', state.y).attr('width', state.maxW).attr('height', state.maxH).attr('preserveAspectRatio', state.svgAlign + ' meet');
        }
    });

    var zoomOverviewArea = index({
        props: {
            selection: {
                "default": {
                    x: [null, null],
                    // [start, end]
                    y: [null, null] // [start, end]

                }
            },
            xDomain: {
                onChange: function onChange(xDomain, state) {
                    state.xScale.domain(xDomain);
                }
            },
            yDomain: {
                onChange: function onChange(yDomain, state) {
                    state.yScale.domain(yDomain);
                }
            },
            transitionDuration: 700
        },
        stateInit: {
            xScale: linear$1(),
            yScale: linear$1()
        },
        init: function init(el, state, _ref) {
            var width = _ref.width,
                height = _ref.height,
                _ref$margin = _ref.margin,
                margin = _ref$margin === void 0 ? {
                    top: 2,
                    right: 2,
                    bottom: 2,
                    left: 2
                } : _ref$margin;
            state.xScale.range([margin.left, width - state.margin.right]);
            state.yScale.range([margin.top, height - state.margin.bottom]); // Build dom

            state.svg = d3Select(el).append('svg').attr('width', width).attr('height', height);
            state.outerBox = state.svg.append('rect').attr('x', state.xScale.range()[0]).attr('y', state.yScale.range()[0]).attr('rx', 2).attr('ry', 2).attr('width', state.xScale.range()[1]).attr('height', state.yScale.range()[1]).style('fill', '#EEE').style('stroke', 'grey');
            state.selectionBox = state.svg.append('rect').attr('rx', 1).attr('ry', 1).attr('width', 0).attr('height', 0).style('stroke', 'blue').style('stroke-opacity', 0.6).style('fill', 'blue').style('fill-opacity', 0.3);
        },
        update: function update(state) {
            state.outerBox.attr('x', state.xScale.range()[0]).attr('y', state.yScale.range()[0]).attr('width', state.xScale.range()[1]).attr('height', state.yScale.range()[1]);
            state.selectionBox.attr('x', state.xScale(state.selection.x[0])).attr('y', state.yScale(state.selection.y[0])).attr('width', state.xScale(state.selection.x[1] - state.selection.x[0])).attr('height', state.yScale(state.selection.y[1] - state.selection.y[0]));
        }
    });

    function _toConsumableArray$1(arr) {
        return _arrayWithoutHoles$1(arr) || _iterableToArray$1(arr) || _nonIterableSpread$1();
    }

    function _arrayWithoutHoles$1(arr) {
        if (Array.isArray(arr)) {
            for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];

            return arr2;
        }
    }

    function _iterableToArray$1(iter) {
        if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
    }

    function _nonIterableSpread$1() {
        throw new TypeError("Invalid attempt to spread non-iterable instance");
    }

    var fitToBox = index({
        props: {
            bbox: {
                "default": {
                    width: null,
                    height: null
                }
            },
            passes: {
                "default": 3
            }
        },
        init: function init(el, state) {
            state.el = el;
        },
        update: function update(state) {
            _toConsumableArray$1(Array(state.passes).keys()).some(function () {
                var startSize = parseInt(state.el.style['font-size'].split('px')[0]) || 20;
                var bbox = state.el.getBBox();
                var newSize = Math.floor(startSize * Math.min(state.bbox.width / bbox.width, state.bbox.height / bbox.height));
                if (newSize === startSize) return true; // Shortcut out

                state.el.style['font-size'] = newSize + 'px';
            });
        }
    });

    var ellipsis = index({
        props: {
            maxWidth: {
                "default": Infinity
            }
        },
        init: function init(el, state) {
            state.el = el;
        },
        update: function update(state) {
            var origTxt = state.el.textContent;
            var nChars = Math.round(origTxt.length * state.maxWidth / state.el.getBBox().width * 1.2); // Start above

            while (--nChars && state.maxWidth / state.el.getBBox().width < 1) {
                state.el.textContent = abbreviateText(origTxt, nChars);
            } //


            function abbreviateText(txt, maxChars) {
                return txt.length <= maxChars
                    ? txt
                    : txt.substring(0, maxChars * 2 / 3)
                    + '...'
                    + txt.substring(txt.length - maxChars / 3, txt.length);
            }
        }
    });

    function createCommonjsModule(fn, module) {
        return module = {exports: {}}, fn(module, module.exports), module.exports;
    }

    var tinycolor = createCommonjsModule(function (module) {
        // TinyColor v1.4.1
        // https://github.com/bgrins/TinyColor
        // Brian Grinstead, MIT License

        (function (Math) {

            var trimLeft = /^\s+/,
                trimRight = /\s+$/,
                tinyCounter = 0,
                mathRound = Math.round,
                mathMin = Math.min,
                mathMax = Math.max,
                mathRandom = Math.random;

            function tinycolor(color, opts) {

                color = (color) ? color : '';
                opts = opts || {};

                // If input is already a tinycolor, return itself
                if (color instanceof tinycolor) {
                    return color;
                }
                // If we are called as a function, call using new instead
                if (!(this instanceof tinycolor)) {
                    return new tinycolor(color, opts);
                }

                var rgb = inputToRGB(color);
                this._originalInput = color,
                    this._r = rgb.r,
                    this._g = rgb.g,
                    this._b = rgb.b,
                    this._a = rgb.a,
                    this._roundA = mathRound(100 * this._a) / 100,
                    this._format = opts.format || rgb.format;
                this._gradientType = opts.gradientType;

                // Don't let the range of [0,255] come back in [0,1].
                // Potentially lose a little bit of precision here, but will fix issues where
                // .5 gets interpreted as half of the total, instead of half of 1
                // If it was supposed to be 128, this was already taken care of by `inputToRgb`
                if (this._r < 1) {
                    this._r = mathRound(this._r);
                }
                if (this._g < 1) {
                    this._g = mathRound(this._g);
                }
                if (this._b < 1) {
                    this._b = mathRound(this._b);
                }

                this._ok = rgb.ok;
                this._tc_id = tinyCounter++;
            }

            tinycolor.prototype = {
                isDark: function () {
                    return this.getBrightness() < 128;
                },
                isLight: function () {
                    return !this.isDark();
                },
                isValid: function () {
                    return this._ok;
                },
                getOriginalInput: function () {
                    return this._originalInput;
                },
                getFormat: function () {
                    return this._format;
                },
                getAlpha: function () {
                    return this._a;
                },
                getBrightness: function () {
                    //http://www.w3.org/TR/AERT#color-contrast
                    var rgb = this.toRgb();
                    return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
                },
                getLuminance: function () {
                    //http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
                    var rgb = this.toRgb();
                    var RsRGB, GsRGB, BsRGB, R, G, B;
                    RsRGB = rgb.r / 255;
                    GsRGB = rgb.g / 255;
                    BsRGB = rgb.b / 255;

                    if (RsRGB <= 0.03928) {
                        R = RsRGB / 12.92;
                    } else {
                        R = Math.pow(((RsRGB + 0.055) / 1.055), 2.4);
                    }
                    if (GsRGB <= 0.03928) {
                        G = GsRGB / 12.92;
                    } else {
                        G = Math.pow(((GsRGB + 0.055) / 1.055), 2.4);
                    }
                    if (BsRGB <= 0.03928) {
                        B = BsRGB / 12.92;
                    } else {
                        B = Math.pow(((BsRGB + 0.055) / 1.055), 2.4);
                    }
                    return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);
                },
                setAlpha: function (value) {
                    this._a = boundAlpha(value);
                    this._roundA = mathRound(100 * this._a) / 100;
                    return this;
                },
                toHsv: function () {
                    var hsv = rgbToHsv(this._r, this._g, this._b);
                    return {h: hsv.h * 360, s: hsv.s, v: hsv.v, a: this._a};
                },
                toHsvString: function () {
                    var hsv = rgbToHsv(this._r, this._g, this._b);
                    var h = mathRound(hsv.h * 360), s = mathRound(hsv.s * 100), v = mathRound(hsv.v * 100);
                    return (this._a == 1) ?
                        "hsv(" + h + ", " + s + "%, " + v + "%)" :
                        "hsva(" + h + ", " + s + "%, " + v + "%, " + this._roundA + ")";
                },
                toHsl: function () {
                    var hsl = rgbToHsl(this._r, this._g, this._b);
                    return {h: hsl.h * 360, s: hsl.s, l: hsl.l, a: this._a};
                },
                toHslString: function () {
                    var hsl = rgbToHsl(this._r, this._g, this._b);
                    var h = mathRound(hsl.h * 360), s = mathRound(hsl.s * 100), l = mathRound(hsl.l * 100);
                    return (this._a == 1) ?
                        "hsl(" + h + ", " + s + "%, " + l + "%)" :
                        "hsla(" + h + ", " + s + "%, " + l + "%, " + this._roundA + ")";
                },
                toHex: function (allow3Char) {
                    return rgbToHex(this._r, this._g, this._b, allow3Char);
                },
                toHexString: function (allow3Char) {
                    return '#' + this.toHex(allow3Char);
                },
                toHex8: function (allow4Char) {
                    return rgbaToHex(this._r, this._g, this._b, this._a, allow4Char);
                },
                toHex8String: function (allow4Char) {
                    return '#' + this.toHex8(allow4Char);
                },
                toRgb: function () {
                    return {r: mathRound(this._r), g: mathRound(this._g), b: mathRound(this._b), a: this._a};
                },
                toRgbString: function () {
                    return (this._a == 1) ?
                        "rgb(" + mathRound(this._r) + ", " + mathRound(this._g) + ", " + mathRound(this._b) + ")" :
                        "rgba(" + mathRound(this._r) + ", " + mathRound(this._g) + ", " + mathRound(this._b) + ", " + this._roundA + ")";
                },
                toPercentageRgb: function () {
                    return {
                        r: mathRound(bound01(this._r, 255) * 100) + "%",
                        g: mathRound(bound01(this._g, 255) * 100) + "%",
                        b: mathRound(bound01(this._b, 255) * 100) + "%",
                        a: this._a
                    };
                },
                toPercentageRgbString: function () {
                    return (this._a == 1) ?
                        "rgb(" + mathRound(bound01(this._r, 255) * 100) + "%, " + mathRound(bound01(this._g, 255) * 100) + "%, " + mathRound(bound01(this._b, 255) * 100) + "%)" :
                        "rgba(" + mathRound(bound01(this._r, 255) * 100) + "%, " + mathRound(bound01(this._g, 255) * 100) + "%, " + mathRound(bound01(this._b, 255) * 100) + "%, " + this._roundA + ")";
                },
                toName: function () {
                    if (this._a === 0) {
                        return "transparent";
                    }

                    if (this._a < 1) {
                        return false;
                    }

                    return hexNames[rgbToHex(this._r, this._g, this._b, true)] || false;
                },
                toFilter: function (secondColor) {
                    var hex8String = '#' + rgbaToArgbHex(this._r, this._g, this._b, this._a);
                    var secondHex8String = hex8String;
                    var gradientType = this._gradientType ? "GradientType = 1, " : "";

                    if (secondColor) {
                        var s = tinycolor(secondColor);
                        secondHex8String = '#' + rgbaToArgbHex(s._r, s._g, s._b, s._a);
                    }

                    return "progid:DXImageTransform.Microsoft.gradient(" + gradientType + "startColorstr=" + hex8String + ",endColorstr=" + secondHex8String + ")";
                },
                toString: function (format) {
                    var formatSet = !!format;
                    format = format || this._format;

                    var formattedString = false;
                    var hasAlpha = this._a < 1 && this._a >= 0;
                    var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "hex4" || format === "hex8" || format === "name");

                    if (needsAlphaFormat) {
                        // Special case for "transparent", all other non-alpha formats
                        // will return rgba when there is transparency.
                        if (format === "name" && this._a === 0) {
                            return this.toName();
                        }
                        return this.toRgbString();
                    }
                    if (format === "rgb") {
                        formattedString = this.toRgbString();
                    }
                    if (format === "prgb") {
                        formattedString = this.toPercentageRgbString();
                    }
                    if (format === "hex" || format === "hex6") {
                        formattedString = this.toHexString();
                    }
                    if (format === "hex3") {
                        formattedString = this.toHexString(true);
                    }
                    if (format === "hex4") {
                        formattedString = this.toHex8String(true);
                    }
                    if (format === "hex8") {
                        formattedString = this.toHex8String();
                    }
                    if (format === "name") {
                        formattedString = this.toName();
                    }
                    if (format === "hsl") {
                        formattedString = this.toHslString();
                    }
                    if (format === "hsv") {
                        formattedString = this.toHsvString();
                    }

                    return formattedString || this.toHexString();
                },
                clone: function () {
                    return tinycolor(this.toString());
                },

                _applyModification: function (fn, args) {
                    var color = fn.apply(null, [this].concat([].slice.call(args)));
                    this._r = color._r;
                    this._g = color._g;
                    this._b = color._b;
                    this.setAlpha(color._a);
                    return this;
                },
                lighten: function () {
                    return this._applyModification(lighten, arguments);
                },
                brighten: function () {
                    return this._applyModification(brighten, arguments);
                },
                darken: function () {
                    return this._applyModification(darken, arguments);
                },
                desaturate: function () {
                    return this._applyModification(desaturate, arguments);
                },
                saturate: function () {
                    return this._applyModification(saturate, arguments);
                },
                greyscale: function () {
                    return this._applyModification(greyscale, arguments);
                },
                spin: function () {
                    return this._applyModification(spin, arguments);
                },

                _applyCombination: function (fn, args) {
                    return fn.apply(null, [this].concat([].slice.call(args)));
                },
                analogous: function () {
                    return this._applyCombination(analogous, arguments);
                },
                complement: function () {
                    return this._applyCombination(complement, arguments);
                },
                monochromatic: function () {
                    return this._applyCombination(monochromatic, arguments);
                },
                splitcomplement: function () {
                    return this._applyCombination(splitcomplement, arguments);
                },
                triad: function () {
                    return this._applyCombination(triad, arguments);
                },
                tetrad: function () {
                    return this._applyCombination(tetrad, arguments);
                }
            };

            // If input is an object, force 1 into "1.0" to handle ratios properly
            // String input requires "1.0" as input, so 1 will be treated as 1
            tinycolor.fromRatio = function (color, opts) {
                if (typeof color == "object") {
                    var newColor = {};
                    for (var i in color) {
                        if (color.hasOwnProperty(i)) {
                            if (i === "a") {
                                newColor[i] = color[i];
                            } else {
                                newColor[i] = convertToPercentage(color[i]);
                            }
                        }
                    }
                    color = newColor;
                }

                return tinycolor(color, opts);
            };

            // Given a string or object, convert that input to RGB
            // Possible string inputs:
            //
            //     "red"
            //     "#f00" or "f00"
            //     "#ff0000" or "ff0000"
            //     "#ff000000" or "ff000000"
            //     "rgb 255 0 0" or "rgb (255, 0, 0)"
            //     "rgb 1.0 0 0" or "rgb (1, 0, 0)"
            //     "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1"
            //     "rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1"
            //     "hsl(0, 100%, 50%)" or "hsl 0 100% 50%"
            //     "hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1"
            //     "hsv(0, 100%, 100%)" or "hsv 0 100% 100%"
            //
            function inputToRGB(color) {

                var rgb = {r: 0, g: 0, b: 0};
                var a = 1;
                var s = null;
                var v = null;
                var l = null;
                var ok = false;
                var format = false;

                if (typeof color == "string") {
                    color = stringInputToObject(color);
                }

                if (typeof color == "object") {
                    if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) {
                        rgb = rgbToRgb(color.r, color.g, color.b);
                        ok = true;
                        format = String(color.r).substr(-1) === "%" ? "prgb" : "rgb";
                    } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) {
                        s = convertToPercentage(color.s);
                        v = convertToPercentage(color.v);
                        rgb = hsvToRgb(color.h, s, v);
                        ok = true;
                        format = "hsv";
                    } else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) {
                        s = convertToPercentage(color.s);
                        l = convertToPercentage(color.l);
                        rgb = hslToRgb(color.h, s, l);
                        ok = true;
                        format = "hsl";
                    }

                    if (color.hasOwnProperty("a")) {
                        a = color.a;
                    }
                }

                a = boundAlpha(a);

                return {
                    ok: ok,
                    format: color.format || format,
                    r: mathMin(255, mathMax(rgb.r, 0)),
                    g: mathMin(255, mathMax(rgb.g, 0)),
                    b: mathMin(255, mathMax(rgb.b, 0)),
                    a: a
                };
            }


            // Conversion Functions
            // --------------------

            // `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from:
            // <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>

            // `rgbToRgb`
            // Handle bounds / percentage checking to conform to CSS color spec
            // <http://www.w3.org/TR/css3-color/>
            // *Assumes:* r, g, b in [0, 255] or [0, 1]
            // *Returns:* { r, g, b } in [0, 255]
            function rgbToRgb(r, g, b) {
                return {
                    r: bound01(r, 255) * 255,
                    g: bound01(g, 255) * 255,
                    b: bound01(b, 255) * 255
                };
            }

            // `rgbToHsl`
            // Converts an RGB color value to HSL.
            // *Assumes:* r, g, and b are contained in [0, 255] or [0, 1]
            // *Returns:* { h, s, l } in [0,1]
            function rgbToHsl(r, g, b) {

                r = bound01(r, 255);
                g = bound01(g, 255);
                b = bound01(b, 255);

                var max = mathMax(r, g, b), min = mathMin(r, g, b);
                var h, s, l = (max + min) / 2;

                if (max == min) {
                    h = s = 0; // achromatic
                } else {
                    var d = max - min;
                    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
                    switch (max) {
                        case r:
                            h = (g - b) / d + (g < b ? 6 : 0);
                            break;
                        case g:
                            h = (b - r) / d + 2;
                            break;
                        case b:
                            h = (r - g) / d + 4;
                            break;
                    }

                    h /= 6;
                }

                return {h: h, s: s, l: l};
            }

            // `hslToRgb`
            // Converts an HSL color value to RGB.
            // *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100]
            // *Returns:* { r, g, b } in the set [0, 255]
            function hslToRgb(h, s, l) {
                var r, g, b;

                h = bound01(h, 360);
                s = bound01(s, 100);
                l = bound01(l, 100);

                function hue2rgb(p, q, t) {
                    if (t < 0) t += 1;
                    if (t > 1) t -= 1;
                    if (t < 1 / 6) return p + (q - p) * 6 * t;
                    if (t < 1 / 2) return q;
                    if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
                    return p;
                }

                if (s === 0) {
                    r = g = b = l; // achromatic
                } else {
                    var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
                    var p = 2 * l - q;
                    r = hue2rgb(p, q, h + 1 / 3);
                    g = hue2rgb(p, q, h);
                    b = hue2rgb(p, q, h - 1 / 3);
                }

                return {r: r * 255, g: g * 255, b: b * 255};
            }

            // `rgbToHsv`
            // Converts an RGB color value to HSV
            // *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1]
            // *Returns:* { h, s, v } in [0,1]
            function rgbToHsv(r, g, b) {

                r = bound01(r, 255);
                g = bound01(g, 255);
                b = bound01(b, 255);

                var max = mathMax(r, g, b), min = mathMin(r, g, b);
                var h, s, v = max;

                var d = max - min;
                s = max === 0 ? 0 : d / max;

                if (max == min) {
                    h = 0; // achromatic
                } else {
                    switch (max) {
                        case r:
                            h = (g - b) / d + (g < b ? 6 : 0);
                            break;
                        case g:
                            h = (b - r) / d + 2;
                            break;
                        case b:
                            h = (r - g) / d + 4;
                            break;
                    }
                    h /= 6;
                }
                return {h: h, s: s, v: v};
            }

            // `hsvToRgb`
            // Converts an HSV color value to RGB.
            // *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
            // *Returns:* { r, g, b } in the set [0, 255]
            function hsvToRgb(h, s, v) {

                h = bound01(h, 360) * 6;
                s = bound01(s, 100);
                v = bound01(v, 100);

                var i = Math.floor(h),
                    f = h - i,
                    p = v * (1 - s),
                    q = v * (1 - f * s),
                    t = v * (1 - (1 - f) * s),
                    mod = i % 6,
                    r = [v, q, p, p, t, v][mod],
                    g = [t, v, v, q, p, p][mod],
                    b = [p, p, t, v, v, q][mod];

                return {r: r * 255, g: g * 255, b: b * 255};
            }

            // `rgbToHex`
            // Converts an RGB color to hex
            // Assumes r, g, and b are contained in the set [0, 255]
            // Returns a 3 or 6 character hex
            function rgbToHex(r, g, b, allow3Char) {

                var hex = [
                    pad2(mathRound(r).toString(16)),
                    pad2(mathRound(g).toString(16)),
                    pad2(mathRound(b).toString(16))
                ];

                // Return a 3 character hex if possible
                if (allow3Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1)) {
                    return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0);
                }

                return hex.join("");
            }

            // `rgbaToHex`
            // Converts an RGBA color plus alpha transparency to hex
            // Assumes r, g, b are contained in the set [0, 255] and
            // a in [0, 1]. Returns a 4 or 8 character rgba hex
            function rgbaToHex(r, g, b, a, allow4Char) {

                var hex = [
                    pad2(mathRound(r).toString(16)),
                    pad2(mathRound(g).toString(16)),
                    pad2(mathRound(b).toString(16)),
                    pad2(convertDecimalToHex(a))
                ];

                // Return a 4 character hex if possible
                if (allow4Char && hex[0].charAt(0) == hex[0].charAt(1) && hex[1].charAt(0) == hex[1].charAt(1) && hex[2].charAt(0) == hex[2].charAt(1) && hex[3].charAt(0) == hex[3].charAt(1)) {
                    return hex[0].charAt(0) + hex[1].charAt(0) + hex[2].charAt(0) + hex[3].charAt(0);
                }

                return hex.join("");
            }

            // `rgbaToArgbHex`
            // Converts an RGBA color to an ARGB Hex8 string
            // Rarely used, but required for "toFilter()"
            function rgbaToArgbHex(r, g, b, a) {

                var hex = [
                    pad2(convertDecimalToHex(a)),
                    pad2(mathRound(r).toString(16)),
                    pad2(mathRound(g).toString(16)),
                    pad2(mathRound(b).toString(16))
                ];

                return hex.join("");
            }

            // `equals`
            // Can be called with any tinycolor input
            tinycolor.equals = function (color1, color2) {
                if (!color1 || !color2) {
                    return false;
                }
                return tinycolor(color1).toRgbString() == tinycolor(color2).toRgbString();
            };

            tinycolor.random = function () {
                return tinycolor.fromRatio({
                    r: mathRandom(),
                    g: mathRandom(),
                    b: mathRandom()
                });
            };


            // Modification Functions
            // ----------------------
            // Thanks to less.js for some of the basics here
            // <https://github.com/cloudhead/less.js/blob/master/lib/less/functions.js>

            function desaturate(color, amount) {
                amount = (amount === 0) ? 0 : (amount || 10);
                var hsl = tinycolor(color).toHsl();
                hsl.s -= amount / 100;
                hsl.s = clamp01(hsl.s);
                return tinycolor(hsl);
            }

            function saturate(color, amount) {
                amount = (amount === 0) ? 0 : (amount || 10);
                var hsl = tinycolor(color).toHsl();
                hsl.s += amount / 100;
                hsl.s = clamp01(hsl.s);
                return tinycolor(hsl);
            }

            function greyscale(color) {
                return tinycolor(color).desaturate(100);
            }

            function lighten(color, amount) {
                amount = (amount === 0) ? 0 : (amount || 10);
                var hsl = tinycolor(color).toHsl();
                hsl.l += amount / 100;
                hsl.l = clamp01(hsl.l);
                return tinycolor(hsl);
            }

            function brighten(color, amount) {
                amount = (amount === 0) ? 0 : (amount || 10);
                var rgb = tinycolor(color).toRgb();
                rgb.r = mathMax(0, mathMin(255, rgb.r - mathRound(255 * -(amount / 100))));
                rgb.g = mathMax(0, mathMin(255, rgb.g - mathRound(255 * -(amount / 100))));
                rgb.b = mathMax(0, mathMin(255, rgb.b - mathRound(255 * -(amount / 100))));
                return tinycolor(rgb);
            }

            function darken(color, amount) {
                amount = (amount === 0) ? 0 : (amount || 10);
                var hsl = tinycolor(color).toHsl();
                hsl.l -= amount / 100;
                hsl.l = clamp01(hsl.l);
                return tinycolor(hsl);
            }

            // Spin takes a positive or negative amount within [-360, 360] indicating the change of hue.
            // Values outside of this range will be wrapped into this range.
            function spin(color, amount) {
                var hsl = tinycolor(color).toHsl();
                var hue = (hsl.h + amount) % 360;
                hsl.h = hue < 0 ? 360 + hue : hue;
                return tinycolor(hsl);
            }

            // Combination Functions
            // ---------------------
            // Thanks to jQuery xColor for some of the ideas behind these
            // <https://github.com/infusion/jQuery-xcolor/blob/master/jquery.xcolor.js>

            function complement(color) {
                var hsl = tinycolor(color).toHsl();
                hsl.h = (hsl.h + 180) % 360;
                return tinycolor(hsl);
            }

            function triad(color) {
                var hsl = tinycolor(color).toHsl();
                var h = hsl.h;
                return [
                    tinycolor(color),
                    tinycolor({h: (h + 120) % 360, s: hsl.s, l: hsl.l}),
                    tinycolor({h: (h + 240) % 360, s: hsl.s, l: hsl.l})
                ];
            }

            function tetrad(color) {
                var hsl = tinycolor(color).toHsl();
                var h = hsl.h;
                return [
                    tinycolor(color),
                    tinycolor({h: (h + 90) % 360, s: hsl.s, l: hsl.l}),
                    tinycolor({h: (h + 180) % 360, s: hsl.s, l: hsl.l}),
                    tinycolor({h: (h + 270) % 360, s: hsl.s, l: hsl.l})
                ];
            }

            function splitcomplement(color) {
                var hsl = tinycolor(color).toHsl();
                var h = hsl.h;
                return [
                    tinycolor(color),
                    tinycolor({h: (h + 72) % 360, s: hsl.s, l: hsl.l}),
                    tinycolor({h: (h + 216) % 360, s: hsl.s, l: hsl.l})
                ];
            }

            function analogous(color, results, slices) {
                results = results || 6;
                slices = slices || 30;

                var hsl = tinycolor(color).toHsl();
                var part = 360 / slices;
                var ret = [tinycolor(color)];

                for (hsl.h = ((hsl.h - (part * results >> 1)) + 720) % 360; --results;) {
                    hsl.h = (hsl.h + part) % 360;
                    ret.push(tinycolor(hsl));
                }
                return ret;
            }

            function monochromatic(color, results) {
                results = results || 6;
                var hsv = tinycolor(color).toHsv();
                var h = hsv.h, s = hsv.s, v = hsv.v;
                var ret = [];
                var modification = 1 / results;

                while (results--) {
                    ret.push(tinycolor({h: h, s: s, v: v}));
                    v = (v + modification) % 1;
                }

                return ret;
            }

            // Utility Functions
            // ---------------------

            tinycolor.mix = function (color1, color2, amount) {
                amount = (amount === 0) ? 0 : (amount || 50);

                var rgb1 = tinycolor(color1).toRgb();
                var rgb2 = tinycolor(color2).toRgb();

                var p = amount / 100;

                var rgba = {
                    r: ((rgb2.r - rgb1.r) * p) + rgb1.r,
                    g: ((rgb2.g - rgb1.g) * p) + rgb1.g,
                    b: ((rgb2.b - rgb1.b) * p) + rgb1.b,
                    a: ((rgb2.a - rgb1.a) * p) + rgb1.a
                };

                return tinycolor(rgba);
            };


            // Readability Functions
            // ---------------------
            // <http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef (WCAG Version 2)

            // `contrast`
            // Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
            tinycolor.readability = function (color1, color2) {
                var c1 = tinycolor(color1);
                var c2 = tinycolor(color2);
                return (Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) / (Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05);
            };

            // `isReadable`
            // Ensure that foreground and background color combinations meet WCAG2 guidelines.
            // The third argument is an optional Object.
            //      the 'level' property states 'AA' or 'AAA' - if missing or invalid, it defaults to 'AA';
            //      the 'size' property states 'large' or 'small' - if missing or invalid, it defaults to 'small'.
            // If the entire object is absent, isReadable defaults to {level:"AA",size:"small"}.

            // *Example*
            //    tinycolor.isReadable("#000", "#111") => false
            //    tinycolor.isReadable("#000", "#111",{level:"AA",size:"large"}) => false
            tinycolor.isReadable = function (color1, color2, wcag2) {
                var readability = tinycolor.readability(color1, color2);
                var wcag2Parms, out;

                out = false;

                wcag2Parms = validateWCAG2Parms(wcag2);
                switch (wcag2Parms.level + wcag2Parms.size) {
                    case "AAsmall":
                    case "AAAlarge":
                        out = readability >= 4.5;
                        break;
                    case "AAlarge":
                        out = readability >= 3;
                        break;
                    case "AAAsmall":
                        out = readability >= 7;
                        break;
                }
                return out;

            };

            // `mostReadable`
            // Given a base color and a list of possible foreground or background
            // colors for that base, returns the most readable color.
            // Optionally returns Black or White if the most readable color is unreadable.
            // *Example*
            //    tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255"
            //    tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString();  // "#ffffff"
            //    tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(); // "#faf3f3"
            //    tinycolor.mostReadable("#a8015a", ["#faf3f3"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(); // "#ffffff"
            tinycolor.mostReadable = function (baseColor, colorList, args) {
                var bestColor = null;
                var bestScore = 0;
                var readability;
                var includeFallbackColors, level, size;
                args = args || {};
                includeFallbackColors = args.includeFallbackColors;
                level = args.level;
                size = args.size;

                for (var i = 0; i < colorList.length; i++) {
                    readability = tinycolor.readability(baseColor, colorList[i]);
                    if (readability > bestScore) {
                        bestScore = readability;
                        bestColor = tinycolor(colorList[i]);
                    }
                }

                if (tinycolor.isReadable(baseColor, bestColor, {
                    "level": level,
                    "size": size
                }) || !includeFallbackColors) {
                    return bestColor;
                } else {
                    args.includeFallbackColors = false;
                    return tinycolor.mostReadable(baseColor, ["#fff", "#000"], args);
                }
            };


            // Big List of Colors
            // ------------------
            // <http://www.w3.org/TR/css3-color/#svg-color>
            var names = tinycolor.names = {
                aliceblue: "f0f8ff",
                antiquewhite: "faebd7",
                aqua: "0ff",
                aquamarine: "7fffd4",
                azure: "f0ffff",
                beige: "f5f5dc",
                bisque: "ffe4c4",
                black: "000",
                blanchedalmond: "ffebcd",
                blue: "00f",
                blueviolet: "8a2be2",
                brown: "a52a2a",
                burlywood: "deb887",
                burntsienna: "ea7e5d",
                cadetblue: "5f9ea0",
                chartreuse: "7fff00",
                chocolate: "d2691e",
                coral: "ff7f50",
                cornflowerblue: "6495ed",
                cornsilk: "fff8dc",
                crimson: "dc143c",
                cyan: "0ff",
                darkblue: "00008b",
                darkcyan: "008b8b",
                darkgoldenrod: "b8860b",
                darkgray: "a9a9a9",
                darkgreen: "006400",
                darkgrey: "a9a9a9",
                darkkhaki: "bdb76b",
                darkmagenta: "8b008b",
                darkolivegreen: "556b2f",
                darkorange: "ff8c00",
                darkorchid: "9932cc",
                darkred: "8b0000",
                darksalmon: "e9967a",
                darkseagreen: "8fbc8f",
                darkslateblue: "483d8b",
                darkslategray: "2f4f4f",
                darkslategrey: "2f4f4f",
                darkturquoise: "00ced1",
                darkviolet: "9400d3",
                deeppink: "ff1493",
                deepskyblue: "00bfff",
                dimgray: "696969",
                dimgrey: "696969",
                dodgerblue: "1e90ff",
                firebrick: "b22222",
                floralwhite: "fffaf0",
                forestgreen: "228b22",
                fuchsia: "f0f",
                gainsboro: "dcdcdc",
                ghostwhite: "f8f8ff",
                gold: "ffd700",
                goldenrod: "daa520",
                gray: "808080",
                green: "008000",
                greenyellow: "adff2f",
                grey: "808080",
                honeydew: "f0fff0",
                hotpink: "ff69b4",
                indianred: "cd5c5c",
                indigo: "4b0082",
                ivory: "fffff0",
                khaki: "f0e68c",
                lavender: "e6e6fa",
                lavenderblush: "fff0f5",
                lawngreen: "7cfc00",
                lemonchiffon: "fffacd",
                lightblue: "add8e6",
                lightcoral: "f08080",
                lightcyan: "e0ffff",
                lightgoldenrodyellow: "fafad2",
                lightgray: "d3d3d3",
                lightgreen: "90ee90",
                lightgrey: "d3d3d3",
                lightpink: "ffb6c1",
                lightsalmon: "ffa07a",
                lightseagreen: "20b2aa",
                lightskyblue: "87cefa",
                lightslategray: "789",
                lightslategrey: "789",
                lightsteelblue: "b0c4de",
                lightyellow: "ffffe0",
                lime: "0f0",
                limegreen: "32cd32",
                linen: "faf0e6",
                magenta: "f0f",
                maroon: "800000",
                mediumaquamarine: "66cdaa",
                mediumblue: "0000cd",
                mediumorchid: "ba55d3",
                mediumpurple: "9370db",
                mediumseagreen: "3cb371",
                mediumslateblue: "7b68ee",
                mediumspringgreen: "00fa9a",
                mediumturquoise: "48d1cc",
                mediumvioletred: "c71585",
                midnightblue: "191970",
                mintcream: "f5fffa",
                mistyrose: "ffe4e1",
                moccasin: "ffe4b5",
                navajowhite: "ffdead",
                navy: "000080",
                oldlace: "fdf5e6",
                olive: "808000",
                olivedrab: "6b8e23",
                orange: "ffa500",
                orangered: "ff4500",
                orchid: "da70d6",
                palegoldenrod: "eee8aa",
                palegreen: "98fb98",
                paleturquoise: "afeeee",
                palevioletred: "db7093",
                papayawhip: "ffefd5",
                peachpuff: "ffdab9",
                peru: "cd853f",
                pink: "ffc0cb",
                plum: "dda0dd",
                powderblue: "b0e0e6",
                purple: "800080",
                rebeccapurple: "663399",
                red: "f00",
                rosybrown: "bc8f8f",
                royalblue: "4169e1",
                saddlebrown: "8b4513",
                salmon: "fa8072",
                sandybrown: "f4a460",
                seagreen: "2e8b57",
                seashell: "fff5ee",
                sienna: "a0522d",
                silver: "c0c0c0",
                skyblue: "87ceeb",
                slateblue: "6a5acd",
                slategray: "708090",
                slategrey: "708090",
                snow: "fffafa",
                springgreen: "00ff7f",
                steelblue: "4682b4",
                tan: "d2b48c",
                teal: "008080",
                thistle: "d8bfd8",
                tomato: "ff6347",
                turquoise: "40e0d0",
                violet: "ee82ee",
                wheat: "f5deb3",
                white: "fff",
                whitesmoke: "f5f5f5",
                yellow: "ff0",
                yellowgreen: "9acd32"
            };

            // Make it easy to access colors via `hexNames[hex]`
            var hexNames = tinycolor.hexNames = flip(names);


            // Utilities
            // ---------

            // `{ 'name1': 'val1' }` becomes `{ 'val1': 'name1' }`
            function flip(o) {
                var flipped = {};
                for (var i in o) {
                    if (o.hasOwnProperty(i)) {
                        flipped[o[i]] = i;
                    }
                }
                return flipped;
            }

            // Return a valid alpha value [0,1] with all invalid values being set to 1
            function boundAlpha(a) {
                a = parseFloat(a);

                if (isNaN(a) || a < 0 || a > 1) {
                    a = 1;
                }

                return a;
            }

            // Take input from [0, n] and return it as [0, 1]
            function bound01(n, max) {
                if (isOnePointZero(n)) {
                    n = "100%";
                }

                var processPercent = isPercentage(n);
                n = mathMin(max, mathMax(0, parseFloat(n)));

                // Automatically convert percentage into number
                if (processPercent) {
                    n = parseInt(n * max, 10) / 100;
                }

                // Handle floating point rounding errors
                if ((Math.abs(n - max) < 0.000001)) {
                    return 1;
                }

                // Convert into [0, 1] range if it isn't already
                return (n % max) / parseFloat(max);
            }

            // Force a number between 0 and 1
            function clamp01(val) {
                return mathMin(1, mathMax(0, val));
            }

            // Parse a base-16 hex value into a base-10 integer
            function parseIntFromHex(val) {
                return parseInt(val, 16);
            }

            // Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
            // <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
            function isOnePointZero(n) {
                return typeof n == "string" && n.indexOf('.') != -1 && parseFloat(n) === 1;
            }

            // Check to see if string passed in is a percentage
            function isPercentage(n) {
                return typeof n === "string" && n.indexOf('%') != -1;
            }

            // Force a hex value to have 2 characters
            function pad2(c) {
                return c.length == 1 ? '0' + c : '' + c;
            }

            // Replace a decimal with it's percentage value
            function convertToPercentage(n) {
                if (n <= 1) {
                    n = (n * 100) + "%";
                }

                return n;
            }

            // Converts a decimal to a hex value
            function convertDecimalToHex(d) {
                return Math.round(parseFloat(d) * 255).toString(16);
            }

            // Converts a hex value to a decimal
            function convertHexToDecimal(h) {
                return (parseIntFromHex(h) / 255);
            }

            var matchers = (function () {

                // <http://www.w3.org/TR/css3-values/#integers>
                var CSS_INTEGER = "[-\\+]?\\d+%?";

                // <http://www.w3.org/TR/css3-values/#number-value>
                var CSS_NUMBER = "[-\\+]?\\d*\\.\\d+%?";

                // Allow positive/negative integer/number.  Don't capture the either/or, just the entire outcome.
                var CSS_UNIT = "(?:" + CSS_NUMBER + ")|(?:" + CSS_INTEGER + ")";

                // Actual matching.
                // Parentheses and commas are optional, but not required.
                // Whitespace can take the place of commas or opening paren
                var PERMISSIVE_MATCH3 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?";
                var PERMISSIVE_MATCH4 = "[\\s|\\(]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")[,|\\s]+(" + CSS_UNIT + ")\\s*\\)?";

                return {
                    CSS_UNIT: new RegExp(CSS_UNIT),
                    rgb: new RegExp("rgb" + PERMISSIVE_MATCH3),
                    rgba: new RegExp("rgba" + PERMISSIVE_MATCH4),
                    hsl: new RegExp("hsl" + PERMISSIVE_MATCH3),
                    hsla: new RegExp("hsla" + PERMISSIVE_MATCH4),
                    hsv: new RegExp("hsv" + PERMISSIVE_MATCH3),
                    hsva: new RegExp("hsva" + PERMISSIVE_MATCH4),
                    hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
                    hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
                    hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
                    hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/
                };
            })();

            // `isValidCSSUnit`
            // Take in a single string / number and check to see if it looks like a CSS unit
            // (see `matchers` above for definition).
            function isValidCSSUnit(color) {
                return !!matchers.CSS_UNIT.exec(color);
            }

            // `stringInputToObject`
            // Permissive string parsing.  Take in a number of formats, and output an object
            // based on detected format.  Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}`
            function stringInputToObject(color) {

                color = color.replace(trimLeft, '').replace(trimRight, '').toLowerCase();
                var named = false;
                if (names[color]) {
                    color = names[color];
                    named = true;
                } else if (color == 'transparent') {
                    return {r: 0, g: 0, b: 0, a: 0, format: "name"};
                }

                // Try to match string input using regular expressions.
                // Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
                // Just return an object and let the conversion functions handle that.
                // This way the result will be the same whether the tinycolor is initialized with string or object.
                var match;
                if ((match = matchers.rgb.exec(color))) {
                    return {r: match[1], g: match[2], b: match[3]};
                }
                if ((match = matchers.rgba.exec(color))) {
                    return {r: match[1], g: match[2], b: match[3], a: match[4]};
                }
                if ((match = matchers.hsl.exec(color))) {
                    return {h: match[1], s: match[2], l: match[3]};
                }
                if ((match = matchers.hsla.exec(color))) {
                    return {h: match[1], s: match[2], l: match[3], a: match[4]};
                }
                if ((match = matchers.hsv.exec(color))) {
                    return {h: match[1], s: match[2], v: match[3]};
                }
                if ((match = matchers.hsva.exec(color))) {
                    return {h: match[1], s: match[2], v: match[3], a: match[4]};
                }
                if ((match = matchers.hex8.exec(color))) {
                    return {
                        r: parseIntFromHex(match[1]),
                        g: parseIntFromHex(match[2]),
                        b: parseIntFromHex(match[3]),
                        a: convertHexToDecimal(match[4]),
                        format: named ? "name" : "hex8"
                    };
                }
                if ((match = matchers.hex6.exec(color))) {
                    return {
                        r: parseIntFromHex(match[1]),
                        g: parseIntFromHex(match[2]),
                        b: parseIntFromHex(match[3]),
                        format: named ? "name" : "hex"
                    };
                }
                if ((match = matchers.hex4.exec(color))) {
                    return {
                        r: parseIntFromHex(match[1] + '' + match[1]),
                        g: parseIntFromHex(match[2] + '' + match[2]),
                        b: parseIntFromHex(match[3] + '' + match[3]),
                        a: convertHexToDecimal(match[4] + '' + match[4]),
                        format: named ? "name" : "hex8"
                    };
                }
                if ((match = matchers.hex3.exec(color))) {
                    return {
                        r: parseIntFromHex(match[1] + '' + match[1]),
                        g: parseIntFromHex(match[2] + '' + match[2]),
                        b: parseIntFromHex(match[3] + '' + match[3]),
                        format: named ? "name" : "hex"
                    };
                }

                return false;
            }

            function validateWCAG2Parms(parms) {
                // return valid WCAG2 parms for isReadable.
                // If input parms are invalid, return {"level":"AA", "size":"small"}
                var level, size;
                parms = parms || {"level": "AA", "size": "small"};
                level = (parms.level || "AA").toUpperCase();
                size = (parms.size || "small").toLowerCase();
                if (level !== "AA" && level !== "AAA") {
                    level = "AA";
                }
                if (size !== "small" && size !== "large") {
                    size = "small";
                }
                return {"level": level, "size": size};
            }

            // Node: Export function
            if (module.exports) {
                module.exports = tinycolor;
            }
            // AMD/requirejs: Define the module
            else {
                window.tinycolor = tinycolor;
            }

        })(Math);
    });

    var ContinuousLegend = index({
        props: {
            width: {},
            height: {},
            scale: {},
            label: {}
        },
        init: function init(el, state) {
            state.gradient = gradient()(el);
            state.el = d3Select(el); // Build dom

            state.box = state.el.append('rect').attr('x', 0).attr('y', 0).attr('rx', 3).attr('ry', 3).attr('stroke', 'black').attr('stroke-width', 0.5);
            state.unitLabel = state.el.append('text').attr('class', 'legendText').style('text-anchor', 'middle').style('dominant-baseline', 'central');
            state.labelFitText = fitToBox()(state.unitLabel.node());
            state.startLabel = state.el.append('text').style('text-anchor', 'start').style('dominant-baseline', 'central');
            state.startLabelFitText = fitToBox()(state.startLabel.node());
            state.endLabel = state.el.append('text').style('text-anchor', 'end').style('dominant-baseline', 'central');
            state.endLabelFitText = fitToBox()(state.endLabel.node());
        },
        update: function update(state) {
            state.gradient.colorScale(state.scale);
            state.box.attr('width', state.width).attr('height', state.height).style('fill', "url(#".concat(state.gradient.id(), ")"));
            state.unitLabel.text(state.label).attr('x', state.width * 0.5).attr('y', state.height * 0.5).style('text-anchor', 'middle').style('dominant-baseline', 'central').style('fill', tinycolor(state.scale((state.scale.domain()[state.scale.domain().length - 1] - state.scale.domain()[0]) / 2)).isLight() ? '#444' : '#CCC');
            state.labelFitText.bbox({
                width: state.width * 0.8,
                height: state.height * 0.9
            });
            state.startLabel.text(state.scale.domain()[0]).attr('x', state.width * 0.02).attr('y', state.height * 0.5).style('fill', tinycolor(state.scale(state.scale.domain()[0])).isLight() ? '#444' : '#CCC');
            state.startLabelFitText.bbox({
                width: state.width * 0.3,
                height: state.height * 0.7
            });
            state.endLabel.text(state.scale.domain()[state.scale.domain().length - 1]).attr('x', state.width * 0.98).attr('y', state.height * 0.5).style('fill', tinycolor(state.scale(state.scale.domain()[state.scale.domain().length - 1])).isLight() ? '#444' : '#CCC');
            state.endLabelFitText.bbox({
                width: state.width * 0.3,
                height: state.height * 0.7
            });
        }
    });

    var DiscreteLegend = index({
        props: {
            width: {},
            height: {},
            scale: {},
            label: {}
        },
        init: function init(el, state) {
            state.el = d3Select(el);
        },
        update: function update(state) {
            var colorBinWidth = state.width / state.scale.domain().length;
            var slot = state.el.selectAll('.color-slot').data(state.scale.domain());
            slot.exit().remove();
            var newSlot = slot.enter().append('g').attr('class', 'color-slot');
            newSlot.append('rect').attr('y', 0).attr('rx', 0).attr('ry', 0).attr('stroke-width', 0);
            newSlot.append('text').style('text-anchor', 'middle').style('dominant-baseline', 'central');
            newSlot.append('title'); // Update

            slot = slot.merge(newSlot);
            slot.select('rect').attr('width', colorBinWidth).attr('height', state.height).attr('x', function (d, i) {
                return colorBinWidth * i;
            }).attr('fill', function (d) {
                return state.scale(d);
            });
            slot.select('text').text(function (d) {
                return d;
            }).attr('x', function (d, i) {
                return colorBinWidth * (i + .5);
            }).attr('y', state.height * 0.5).style('fill', function (d) {
                return tinycolor(state.scale(d)).isLight() ? '#333' : '#DDD';
            }).each(function (d) {
                fitToBox().bbox({
                    width: colorBinWidth * 0.9,
                    height: state.height * 0.8
                })(this);
            });
            slot.select('title').text(function (d) {
                return "".concat(d, " ").concat(state.label);
            });
        }
    });

    var legend = index({
        props: {
            width: {},
            height: {},
            scale: {},
            label: {}
        },
        init: function init(el, state) {
            state.legend = d3Select(el).append('g').attr('class', 'legend');
        },
        update: function update(state) {
            if (!state.scale) return; // Check if ordinal or continuous scale

            var ordinal = state.scale.hasOwnProperty('unknown'); // Only ordinal scales have the unknown method (for auto-assigning new categories)

            state.legend.html(''); // Wipe it

            (ordinal ? DiscreteLegend : ContinuousLegend)().width(state.width).height(state.height).scale(state.scale).label(state.label)(state.legend.node());
        }
    });

    var noop = {
        value: function () {
        }
    };

    function dispatch() {
        for (var i = 0, n = arguments.length, _ = {}, t; i < n; ++i) {
            if (!(t = arguments[i] + "") || (t in _) || /[\s.]/.test(t)) throw new Error("illegal type: " + t);
            _[t] = [];
        }
        return new Dispatch(_);
    }

    function Dispatch(_) {
        this._ = _;
    }

    function parseTypenames$1(typenames, types) {
        return typenames.trim().split(/^|\s+/).map(function (t) {
            var name = "", i = t.indexOf(".");
            if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i);
            if (t && !types.hasOwnProperty(t)) throw new Error("unknown type: " + t);
            return {type: t, name: name};
        });
    }

    Dispatch.prototype = dispatch.prototype = {
        constructor: Dispatch,
        on: function (typename, callback) {
            var _ = this._,
                T = parseTypenames$1(typename + "", _),
                t,
                i = -1,
                n = T.length;

            // If no callback was specified, return the callback of the given type and name.
            if (arguments.length < 2) {
                while (++i < n) if ((t = (typename = T[i]).type) && (t = get(_[t], typename.name))) return t;
                return;
            }

            // If a type was specified, set the callback for the given type and name.
            // Otherwise, if a null callback was specified, remove callbacks of the given name.
            if (callback != null && typeof callback !== "function") throw new Error("invalid callback: " + callback);
            while (++i < n) {
                if (t = (typename = T[i]).type) _[t] = set$1(_[t], typename.name, callback);
                else if (callback == null) for (t in _) _[t] = set$1(_[t], typename.name, null);
            }

            return this;
        },
        copy: function () {
            var copy = {}, _ = this._;
            for (var t in _) copy[t] = _[t].slice();
            return new Dispatch(copy);
        },
        call: function (type, that) {
            if ((n = arguments.length - 2) > 0) for (var args = new Array(n), i = 0, n, t; i < n; ++i) args[i] = arguments[i + 2];
            if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
            for (t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
        },
        apply: function (type, that, args) {
            if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
            for (var t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
        }
    };

    function get(type, name) {
        for (var i = 0, n = type.length, c; i < n; ++i) {
            if ((c = type[i]).name === name) {
                return c.value;
            }
        }
    }

    function set$1(type, name, callback) {
        for (var i = 0, n = type.length; i < n; ++i) {
            if (type[i].name === name) {
                type[i] = noop, type = type.slice(0, i).concat(type.slice(i + 1));
                break;
            }
        }
        if (callback != null) type.push({name: name, value: callback});
        return type;
    }

    function noevent() {
        event.preventDefault();
        event.stopImmediatePropagation();
    }

    function dragDisable(view) {
        var root = view.document.documentElement,
            selection = d3Select(view).on("dragstart.drag", noevent, true);
        if ("onselectstart" in root) {
            selection.on("selectstart.drag", noevent, true);
        } else {
            root.__noselect = root.style.MozUserSelect;
            root.style.MozUserSelect = "none";
        }
    }

    function yesdrag(view, noclick) {
        var root = view.document.documentElement,
            selection = d3Select(view).on("dragstart.drag", null);
        if (noclick) {
            selection.on("click.drag", noevent, true);
            setTimeout(function () {
                selection.on("click.drag", null);
            }, 0);
        }
        if ("onselectstart" in root) {
            selection.on("selectstart.drag", null);
        } else {
            root.style.MozUserSelect = root.__noselect;
            delete root.__noselect;
        }
    }

    var frame = 0, // is an animation frame pending?
        timeout = 0, // is a timeout pending?
        interval = 0, // are any timers active?
        pokeDelay = 1000, // how frequently we check for clock skew
        taskHead,
        taskTail,
        clockLast = 0,
        clockNow = 0,
        clockSkew = 0,
        clock = typeof performance === "object" && performance.now ? performance : Date,
        setFrame = typeof window === "object" && window.requestAnimationFrame ? window.requestAnimationFrame.bind(window) : function (f) {
            setTimeout(f, 17);
        };

    function now() {
        return clockNow || (setFrame(clearNow), clockNow = clock.now() + clockSkew);
    }

    function clearNow() {
        clockNow = 0;
    }

    function Timer() {
        this._call =
            this._time =
                this._next = null;
    }

    Timer.prototype = timer.prototype = {
        constructor: Timer,
        restart: function (callback, delay, time) {
            if (typeof callback !== "function") throw new TypeError("callback is not a function");
            time = (time == null ? now() : +time) + (delay == null ? 0 : +delay);
            if (!this._next && taskTail !== this) {
                if (taskTail) taskTail._next = this;
                else taskHead = this;
                taskTail = this;
            }
            this._call = callback;
            this._time = time;
            sleep();
        },
        stop: function () {
            if (this._call) {
                this._call = null;
                this._time = Infinity;
                sleep();
            }
        }
    };

    function timer(callback, delay, time) {
        var t = new Timer;
        t.restart(callback, delay, time);
        return t;
    }

    function timerFlush() {
        now(); // Get the current time, if not already set.
        ++frame; // Pretend we’ve set an alarm, if we haven’t already.
        var t = taskHead, e;
        while (t) {
            if ((e = clockNow - t._time) >= 0) t._call.call(null, e);
            t = t._next;
        }
        --frame;
    }

    function wake() {
        clockNow = (clockLast = clock.now()) + clockSkew;
        frame = timeout = 0;
        try {
            timerFlush();
        } finally {
            frame = 0;
            nap();
            clockNow = 0;
        }
    }

    function poke() {
        var now = clock.now(), delay = now - clockLast;
        if (delay > pokeDelay) clockSkew -= delay, clockLast = now;
    }

    function nap() {
        var t0, t1 = taskHead, t2, time = Infinity;
        while (t1) {
            if (t1._call) {
                if (time > t1._time) time = t1._time;
                t0 = t1, t1 = t1._next;
            } else {
                t2 = t1._next, t1._next = null;
                t1 = t0 ? t0._next = t2 : taskHead = t2;
            }
        }
        taskTail = t0;
        sleep(time);
    }

    function sleep(time) {
        if (frame) return; // Soonest alarm already set, or will be.
        if (timeout) timeout = clearTimeout(timeout);
        var delay = time - clockNow; // Strictly less than if we recomputed clockNow.
        if (delay > 24) {
            if (time < Infinity) timeout = setTimeout(wake, time - clock.now() - clockSkew);
            if (interval) interval = clearInterval(interval);
        } else {
            if (!interval) clockLast = clock.now(), interval = setInterval(poke, pokeDelay);
            frame = 1, setFrame(wake);
        }
    }

    function timeout$1(callback, delay, time) {
        var t = new Timer;
        delay = delay == null ? 0 : +delay;
        t.restart(function (elapsed) {
            t.stop();
            callback(elapsed + delay);
        }, delay, time);
        return t;
    }

    var emptyOn = dispatch("start", "end", "cancel", "interrupt");
    var emptyTween = [];

    var CREATED = 0;
    var SCHEDULED = 1;
    var STARTING = 2;
    var STARTED = 3;
    var RUNNING = 4;
    var ENDING = 5;
    var ENDED = 6;

    function schedule(node, name, id, index, group, timing) {
        var schedules = node.__transition;
        if (!schedules) node.__transition = {};
        else if (id in schedules) return;
        create(node, id, {
            name: name,
            index: index, // For context during callback.
            group: group, // For context during callback.
            on: emptyOn,
            tween: emptyTween,
            time: timing.time,
            delay: timing.delay,
            duration: timing.duration,
            ease: timing.ease,
            timer: null,
            state: CREATED
        });
    }

    function init(node, id) {
        var schedule = get$1(node, id);
        if (schedule.state > CREATED) throw new Error("too late; already scheduled");
        return schedule;
    }

    function set$2(node, id) {
        var schedule = get$1(node, id);
        if (schedule.state > STARTED) throw new Error("too late; already running");
        return schedule;
    }

    function get$1(node, id) {
        var schedule = node.__transition;
        if (!schedule || !(schedule = schedule[id])) throw new Error("transition not found");
        return schedule;
    }

    function create(node, id, self) {
        var schedules = node.__transition,
            tween;

        // Initialize the self timer when the transition is created.
        // Note the actual delay is not known until the first callback!
        schedules[id] = self;
        self.timer = timer(schedule, 0, self.time);

        function schedule(elapsed) {
            self.state = SCHEDULED;
            self.timer.restart(start, self.delay, self.time);

            // If the elapsed delay is less than our first sleep, start immediately.
            if (self.delay <= elapsed) start(elapsed - self.delay);
        }

        function start(elapsed) {
            var i, j, n, o;

            // If the state is not SCHEDULED, then we previously errored on start.
            if (self.state !== SCHEDULED) return stop();

            for (i in schedules) {
                o = schedules[i];
                if (o.name !== self.name) continue;

                // While this element already has a starting transition during this frame,
                // defer starting an interrupting transition until that transition has a
                // chance to tick (and possibly end); see d3/d3-transition#54!
                if (o.state === STARTED) return timeout$1(start);

                // Interrupt the active transition, if any.
                if (o.state === RUNNING) {
                    o.state = ENDED;
                    o.timer.stop();
                    o.on.call("interrupt", node, node.__data__, o.index, o.group);
                    delete schedules[i];
                }

                // Cancel any pre-empted transitions.
                else if (+i < id) {
                    o.state = ENDED;
                    o.timer.stop();
                    o.on.call("cancel", node, node.__data__, o.index, o.group);
                    delete schedules[i];
                }
            }

            // Defer the first tick to end of the current frame; see d3/d3#1576.
            // Note the transition may be canceled after start and before the first tick!
            // Note this must be scheduled before the start event; see d3/d3-transition#16!
            // Assuming this is successful, subsequent callbacks go straight to tick.
            timeout$1(function () {
                if (self.state === STARTED) {
                    self.state = RUNNING;
                    self.timer.restart(tick, self.delay, self.time);
                    tick(elapsed);
                }
            });

            // Dispatch the start event.
            // Note this must be done before the tween are initialized.
            self.state = STARTING;
            self.on.call("start", node, node.__data__, self.index, self.group);
            if (self.state !== STARTING) return; // interrupted
            self.state = STARTED;

            // Initialize the tween, deleting null tween.
            tween = new Array(n = self.tween.length);
            for (i = 0, j = -1; i < n; ++i) {
                if (o = self.tween[i].value.call(node, node.__data__, self.index, self.group)) {
                    tween[++j] = o;
                }
            }
            tween.length = j + 1;
        }

        function tick(elapsed) {
            var t = elapsed < self.duration ? self.ease.call(null, elapsed / self.duration) : (self.timer.restart(stop), self.state = ENDING, 1),
                i = -1,
                n = tween.length;

            while (++i < n) {
                tween[i].call(node, t);
            }

            // Dispatch the end event.
            if (self.state === ENDING) {
                self.on.call("end", node, node.__data__, self.index, self.group);
                stop();
            }
        }

        function stop() {
            self.state = ENDED;
            self.timer.stop();
            delete schedules[id];
            for (var i in schedules) return; // eslint-disable-line no-unused-vars
            delete node.__transition;
        }
    }

    function interrupt(node, name) {
        var schedules = node.__transition,
            schedule,
            active,
            empty = true,
            i;

        if (!schedules) return;

        name = name == null ? null : name + "";

        for (i in schedules) {
            if ((schedule = schedules[i]).name !== name) {
                empty = false;
                continue;
            }
            active = schedule.state > STARTING && schedule.state < ENDING;
            schedule.state = ENDED;
            schedule.timer.stop();
            schedule.on.call(active ? "interrupt" : "cancel", node, node.__data__, schedule.index, schedule.group);
            delete schedules[i];
        }

        if (empty) delete node.__transition;
    }

    function selection_interrupt(name) {
        return this.each(function () {
            interrupt(this, name);
        });
    }

    function tweenRemove(id, name) {
        var tween0, tween1;
        return function () {
            var schedule = set$2(this, id),
                tween = schedule.tween;

            // If this node shared tween with the previous node,
            // just assign the updated shared tween and we’re done!
            // Otherwise, copy-on-write.
            if (tween !== tween0) {
                tween1 = tween0 = tween;
                for (var i = 0, n = tween1.length; i < n; ++i) {
                    if (tween1[i].name === name) {
                        tween1 = tween1.slice();
                        tween1.splice(i, 1);
                        break;
                    }
                }
            }

            schedule.tween = tween1;
        };
    }

    function tweenFunction(id, name, value) {
        var tween0, tween1;
        if (typeof value !== "function") throw new Error;
        return function () {
            var schedule = set$2(this, id),
                tween = schedule.tween;

            // If this node shared tween with the previous node,
            // just assign the updated shared tween and we’re done!
            // Otherwise, copy-on-write.
            if (tween !== tween0) {
                tween1 = (tween0 = tween).slice();
                for (var t = {name: name, value: value}, i = 0, n = tween1.length; i < n; ++i) {
                    if (tween1[i].name === name) {
                        tween1[i] = t;
                        break;
                    }
                }
                if (i === n) tween1.push(t);
            }

            schedule.tween = tween1;
        };
    }

    function transition_tween(name, value) {
        var id = this._id;

        name += "";

        if (arguments.length < 2) {
            var tween = get$1(this.node(), id).tween;
            for (var i = 0, n = tween.length, t; i < n; ++i) {
                if ((t = tween[i]).name === name) {
                    return t.value;
                }
            }
            return null;
        }

        return this.each((value == null ? tweenRemove : tweenFunction)(id, name, value));
    }

    function tweenValue(transition, name, value) {
        var id = transition._id;

        transition.each(function () {
            var schedule = set$2(this, id);
            (schedule.value || (schedule.value = {}))[name] = value.apply(this, arguments);
        });

        return function (node) {
            return get$1(node, id).value[name];
        };
    }

    function interpolate$1(a, b) {
        var c;
        return (typeof b === "number" ? interpolateNumber
            : b instanceof color ? interpolateRgb
                : (c = color(b)) ? (b = c, interpolateRgb)
                    : interpolateString)(a, b);
    }

    function attrRemove$1(name) {
        return function () {
            this.removeAttribute(name);
        };
    }

    function attrRemoveNS$1(fullname) {
        return function () {
            this.removeAttributeNS(fullname.space, fullname.local);
        };
    }

    function attrConstant$1(name, interpolate, value1) {
        var string00,
            string1 = value1 + "",
            interpolate0;
        return function () {
            var string0 = this.getAttribute(name);
            return string0 === string1 ? null
                : string0 === string00 ? interpolate0
                    : interpolate0 = interpolate(string00 = string0, value1);
        };
    }

    function attrConstantNS$1(fullname, interpolate, value1) {
        var string00,
            string1 = value1 + "",
            interpolate0;
        return function () {
            var string0 = this.getAttributeNS(fullname.space, fullname.local);
            return string0 === string1 ? null
                : string0 === string00 ? interpolate0
                    : interpolate0 = interpolate(string00 = string0, value1);
        };
    }

    function attrFunction$1(name, interpolate, value) {
        var string00,
            string10,
            interpolate0;
        return function () {
            var string0, value1 = value(this), string1;
            if (value1 == null) return void this.removeAttribute(name);
            string0 = this.getAttribute(name);
            string1 = value1 + "";
            return string0 === string1 ? null
                : string0 === string00 && string1 === string10 ? interpolate0
                    : (string10 = string1, interpolate0 = interpolate(string00 = string0, value1));
        };
    }

    function attrFunctionNS$1(fullname, interpolate, value) {
        var string00,
            string10,
            interpolate0;
        return function () {
            var string0, value1 = value(this), string1;
            if (value1 == null) return void this.removeAttributeNS(fullname.space, fullname.local);
            string0 = this.getAttributeNS(fullname.space, fullname.local);
            string1 = value1 + "";
            return string0 === string1 ? null
                : string0 === string00 && string1 === string10 ? interpolate0
                    : (string10 = string1, interpolate0 = interpolate(string00 = string0, value1));
        };
    }

    function transition_attr(name, value) {
        var fullname = namespace(name), i = fullname === "transform" ? interpolateTransformSvg : interpolate$1;
        return this.attrTween(name, typeof value === "function"
            ? (fullname.local ? attrFunctionNS$1 : attrFunction$1)(fullname, i, tweenValue(this, "attr." + name, value))
            : value == null ? (fullname.local ? attrRemoveNS$1 : attrRemove$1)(fullname)
                : (fullname.local ? attrConstantNS$1 : attrConstant$1)(fullname, i, value));
    }

    function attrInterpolate(name, i) {
        return function (t) {
            this.setAttribute(name, i.call(this, t));
        };
    }

    function attrInterpolateNS(fullname, i) {
        return function (t) {
            this.setAttributeNS(fullname.space, fullname.local, i.call(this, t));
        };
    }

    function attrTweenNS(fullname, value) {
        var t0, i0;

        function tween() {
            var i = value.apply(this, arguments);
            if (i !== i0) t0 = (i0 = i) && attrInterpolateNS(fullname, i);
            return t0;
        }

        tween._value = value;
        return tween;
    }

    function attrTween(name, value) {
        var t0, i0;

        function tween() {
            var i = value.apply(this, arguments);
            if (i !== i0) t0 = (i0 = i) && attrInterpolate(name, i);
            return t0;
        }

        tween._value = value;
        return tween;
    }

    function transition_attrTween(name, value) {
        var key = "attr." + name;
        if (arguments.length < 2) return (key = this.tween(key)) && key._value;
        if (value == null) return this.tween(key, null);
        if (typeof value !== "function") throw new Error;
        var fullname = namespace(name);
        return this.tween(key, (fullname.local ? attrTweenNS : attrTween)(fullname, value));
    }

    function delayFunction(id, value) {
        return function () {
            init(this, id).delay = +value.apply(this, arguments);
        };
    }

    function delayConstant(id, value) {
        return value = +value, function () {
            init(this, id).delay = value;
        };
    }

    function transition_delay(value) {
        var id = this._id;

        return arguments.length
            ? this.each((typeof value === "function"
                ? delayFunction
                : delayConstant)(id, value))
            : get$1(this.node(), id).delay;
    }

    function durationFunction(id, value) {
        return function () {
            set$2(this, id).duration = +value.apply(this, arguments);
        };
    }

    function durationConstant(id, value) {
        return value = +value, function () {
            set$2(this, id).duration = value;
        };
    }

    function transition_duration(value) {
        var id = this._id;

        return arguments.length
            ? this.each((typeof value === "function"
                ? durationFunction
                : durationConstant)(id, value))
            : get$1(this.node(), id).duration;
    }

    function easeConstant(id, value) {
        if (typeof value !== "function") throw new Error;
        return function () {
            set$2(this, id).ease = value;
        };
    }

    function transition_ease(value) {
        var id = this._id;

        return arguments.length
            ? this.each(easeConstant(id, value))
            : get$1(this.node(), id).ease;
    }

    function transition_filter(match) {
        if (typeof match !== "function") match = matcher(match);

        for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, subgroup = subgroups[j] = [], node, i = 0; i < n; ++i) {
                if ((node = group[i]) && match.call(node, node.__data__, i, group)) {
                    subgroup.push(node);
                }
            }
        }

        return new Transition(subgroups, this._parents, this._name, this._id);
    }

    function transition_merge(transition) {
        if (transition._id !== this._id) throw new Error;

        for (var groups0 = this._groups, groups1 = transition._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
            for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
                if (node = group0[i] || group1[i]) {
                    merge[i] = node;
                }
            }
        }

        for (; j < m0; ++j) {
            merges[j] = groups0[j];
        }

        return new Transition(merges, this._parents, this._name, this._id);
    }

    function start(name) {
        return (name + "").trim().split(/^|\s+/).every(function (t) {
            var i = t.indexOf(".");
            if (i >= 0) t = t.slice(0, i);
            return !t || t === "start";
        });
    }

    function onFunction(id, name, listener) {
        var on0, on1, sit = start(name) ? init : set$2;
        return function () {
            var schedule = sit(this, id),
                on = schedule.on;

            // If this node shared a dispatch with the previous node,
            // just assign the updated shared dispatch and we’re done!
            // Otherwise, copy-on-write.
            if (on !== on0) (on1 = (on0 = on).copy()).on(name, listener);

            schedule.on = on1;
        };
    }

    function transition_on(name, listener) {
        var id = this._id;

        return arguments.length < 2
            ? get$1(this.node(), id).on.on(name)
            : this.each(onFunction(id, name, listener));
    }

    function removeFunction(id) {
        return function () {
            var parent = this.parentNode;
            for (var i in this.__transition) if (+i !== id) return;
            if (parent) parent.removeChild(this);
        };
    }

    function transition_remove() {
        return this.on("end.remove", removeFunction(this._id));
    }

    function transition_select(select) {
        var name = this._name,
            id = this._id;

        if (typeof select !== "function") select = selector(select);

        for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, subgroup = subgroups[j] = new Array(n), node, subnode, i = 0; i < n; ++i) {
                if ((node = group[i]) && (subnode = select.call(node, node.__data__, i, group))) {
                    if ("__data__" in node) subnode.__data__ = node.__data__;
                    subgroup[i] = subnode;
                    schedule(subgroup[i], name, id, i, subgroup, get$1(node, id));
                }
            }
        }

        return new Transition(subgroups, this._parents, name, id);
    }

    function transition_selectAll(select) {
        var name = this._name,
            id = this._id;

        if (typeof select !== "function") select = selectorAll(select);

        for (var groups = this._groups, m = groups.length, subgroups = [], parents = [], j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
                if (node = group[i]) {
                    for (var children = select.call(node, node.__data__, i, group), child, inherit = get$1(node, id), k = 0, l = children.length; k < l; ++k) {
                        if (child = children[k]) {
                            schedule(child, name, id, k, children, inherit);
                        }
                    }
                    subgroups.push(children);
                    parents.push(node);
                }
            }
        }

        return new Transition(subgroups, parents, name, id);
    }

    var Selection$1 = selection.prototype.constructor;

    function transition_selection() {
        return new Selection$1(this._groups, this._parents);
    }

    function styleNull(name, interpolate) {
        var string00,
            string10,
            interpolate0;
        return function () {
            var string0 = styleValue(this, name),
                string1 = (this.style.removeProperty(name), styleValue(this, name));
            return string0 === string1 ? null
                : string0 === string00 && string1 === string10 ? interpolate0
                    : interpolate0 = interpolate(string00 = string0, string10 = string1);
        };
    }

    function styleRemove$1(name) {
        return function () {
            this.style.removeProperty(name);
        };
    }

    function styleConstant$1(name, interpolate, value1) {
        var string00,
            string1 = value1 + "",
            interpolate0;
        return function () {
            var string0 = styleValue(this, name);
            return string0 === string1 ? null
                : string0 === string00 ? interpolate0
                    : interpolate0 = interpolate(string00 = string0, value1);
        };
    }

    function styleFunction$1(name, interpolate, value) {
        var string00,
            string10,
            interpolate0;
        return function () {
            var string0 = styleValue(this, name),
                value1 = value(this),
                string1 = value1 + "";
            if (value1 == null) string1 = value1 = (this.style.removeProperty(name), styleValue(this, name));
            return string0 === string1 ? null
                : string0 === string00 && string1 === string10 ? interpolate0
                    : (string10 = string1, interpolate0 = interpolate(string00 = string0, value1));
        };
    }

    function styleMaybeRemove(id, name) {
        var on0, on1, listener0, key = "style." + name, event = "end." + key, remove;
        return function () {
            var schedule = set$2(this, id),
                on = schedule.on,
                listener = schedule.value[key] == null ? remove || (remove = styleRemove$1(name)) : undefined;

            // If this node shared a dispatch with the previous node,
            // just assign the updated shared dispatch and we’re done!
            // Otherwise, copy-on-write.
            if (on !== on0 || listener0 !== listener) (on1 = (on0 = on).copy()).on(event, listener0 = listener);

            schedule.on = on1;
        };
    }

    function transition_style(name, value, priority) {
        var i = (name += "") === "transform" ? interpolateTransformCss : interpolate$1;
        return value == null ? this
                .styleTween(name, styleNull(name, i))
                .on("end.style." + name, styleRemove$1(name))
            : typeof value === "function" ? this
                    .styleTween(name, styleFunction$1(name, i, tweenValue(this, "style." + name, value)))
                    .each(styleMaybeRemove(this._id, name))
                : this
                    .styleTween(name, styleConstant$1(name, i, value), priority)
                    .on("end.style." + name, null);
    }

    function styleInterpolate(name, i, priority) {
        return function (t) {
            this.style.setProperty(name, i.call(this, t), priority);
        };
    }

    function styleTween(name, value, priority) {
        var t, i0;

        function tween() {
            var i = value.apply(this, arguments);
            if (i !== i0) t = (i0 = i) && styleInterpolate(name, i, priority);
            return t;
        }

        tween._value = value;
        return tween;
    }

    function transition_styleTween(name, value, priority) {
        var key = "style." + (name += "");
        if (arguments.length < 2) return (key = this.tween(key)) && key._value;
        if (value == null) return this.tween(key, null);
        if (typeof value !== "function") throw new Error;
        return this.tween(key, styleTween(name, value, priority == null ? "" : priority));
    }

    function textConstant$1(value) {
        return function () {
            this.textContent = value;
        };
    }

    function textFunction$1(value) {
        return function () {
            var value1 = value(this);
            this.textContent = value1 == null ? "" : value1;
        };
    }

    function transition_text(value) {
        return this.tween("text", typeof value === "function"
            ? textFunction$1(tweenValue(this, "text", value))
            : textConstant$1(value == null ? "" : value + ""));
    }

    function textInterpolate(i) {
        return function (t) {
            this.textContent = i.call(this, t);
        };
    }

    function textTween(value) {
        var t0, i0;

        function tween() {
            var i = value.apply(this, arguments);
            if (i !== i0) t0 = (i0 = i) && textInterpolate(i);
            return t0;
        }

        tween._value = value;
        return tween;
    }

    function transition_textTween(value) {
        var key = "text";
        if (arguments.length < 1) return (key = this.tween(key)) && key._value;
        if (value == null) return this.tween(key, null);
        if (typeof value !== "function") throw new Error;
        return this.tween(key, textTween(value));
    }

    function transition_transition() {
        var name = this._name,
            id0 = this._id,
            id1 = newId();

        for (var groups = this._groups, m = groups.length, j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
                if (node = group[i]) {
                    var inherit = get$1(node, id0);
                    schedule(node, name, id1, i, group, {
                        time: inherit.time + inherit.delay + inherit.duration,
                        delay: 0,
                        duration: inherit.duration,
                        ease: inherit.ease
                    });
                }
            }
        }

        return new Transition(groups, this._parents, name, id1);
    }

    function transition_end() {
        var on0, on1, that = this, id = that._id, size = that.size();
        return new Promise(function (resolve, reject) {
            var cancel = {value: reject},
                end = {
                    value: function () {
                        if (--size === 0) resolve();
                    }
                };

            that.each(function () {
                var schedule = set$2(this, id),
                    on = schedule.on;

                // If this node shared a dispatch with the previous node,
                // just assign the updated shared dispatch and we’re done!
                // Otherwise, copy-on-write.
                if (on !== on0) {
                    on1 = (on0 = on).copy();
                    on1._.cancel.push(cancel);
                    on1._.interrupt.push(cancel);
                    on1._.end.push(end);
                }

                schedule.on = on1;
            });
        });
    }

    var id = 0;

    function Transition(groups, parents, name, id) {
        this._groups = groups;
        this._parents = parents;
        this._name = name;
        this._id = id;
    }

    function transition(name) {
        return selection().transition(name);
    }

    function newId() {
        return ++id;
    }

    var selection_prototype = selection.prototype;

    Transition.prototype = transition.prototype = {
        constructor: Transition,
        select: transition_select,
        selectAll: transition_selectAll,
        filter: transition_filter,
        merge: transition_merge,
        selection: transition_selection,
        transition: transition_transition,
        call: selection_prototype.call,
        nodes: selection_prototype.nodes,
        node: selection_prototype.node,
        size: selection_prototype.size,
        empty: selection_prototype.empty,
        each: selection_prototype.each,
        on: transition_on,
        attr: transition_attr,
        attrTween: transition_attrTween,
        style: transition_style,
        styleTween: transition_styleTween,
        text: transition_text,
        textTween: transition_textTween,
        remove: transition_remove,
        tween: transition_tween,
        delay: transition_delay,
        duration: transition_duration,
        ease: transition_ease,
        end: transition_end
    };

    function cubicInOut(t) {
        return ((t *= 2) <= 1 ? t * t * t : (t -= 2) * t * t + 2) / 2;
    }

    var defaultTiming = {
        time: null, // Set on use.
        delay: 0,
        duration: 250,
        ease: cubicInOut
    };

    function inherit(node, id) {
        var timing;
        while (!(timing = node.__transition) || !(timing = timing[id])) {
            if (!(node = node.parentNode)) {
                return defaultTiming.time = now(), defaultTiming;
            }
        }
        return timing;
    }

    function selection_transition(name) {
        var id,
            timing;

        if (name instanceof Transition) {
            id = name._id, name = name._name;
        } else {
            id = newId(), (timing = defaultTiming).time = now(), name = name == null ? null : name + "";
        }

        for (var groups = this._groups, m = groups.length, j = 0; j < m; ++j) {
            for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
                if (node = group[i]) {
                    schedule(node, name, id, i, group, timing || inherit(node, id));
                }
            }
        }

        return new Transition(groups, this._parents, name, id);
    }

    selection.prototype.interrupt = selection_interrupt;
    selection.prototype.transition = selection_transition;

    function constant$3(x) {
        return function () {
            return x;
        };
    }

    function BrushEvent(target, type, selection) {
        this.target = target;
        this.type = type;
        this.selection = selection;
    }

    function nopropagation() {
        event.stopImmediatePropagation();
    }

    function noevent$1() {
        event.preventDefault();
        event.stopImmediatePropagation();
    }

    var MODE_DRAG = {name: "drag"},
        MODE_SPACE = {name: "space"},
        MODE_HANDLE = {name: "handle"},
        MODE_CENTER = {name: "center"};

    function number1(e) {
        return [+e[0], +e[1]];
    }

    function number2(e) {
        return [number1(e[0]), number1(e[1])];
    }

    function toucher(identifier) {
        return function (target) {
            return touch(target, event.touches, identifier);
        };
    }

    var X = {
        name: "x",
        handles: ["w", "e"].map(type),
        input: function (x, e) {
            return x == null ? null : [[+x[0], e[0][1]], [+x[1], e[1][1]]];
        },
        output: function (xy) {
            return xy && [xy[0][0], xy[1][0]];
        }
    };

    var Y = {
        name: "y",
        handles: ["n", "s"].map(type),
        input: function (y, e) {
            return y == null ? null : [[e[0][0], +y[0]], [e[1][0], +y[1]]];
        },
        output: function (xy) {
            return xy && [xy[0][1], xy[1][1]];
        }
    };

    var cursors = {
        overlay: "crosshair",
        selection: "move",
        n: "ns-resize",
        e: "ew-resize",
        s: "ns-resize",
        w: "ew-resize",
        nw: "nwse-resize",
        ne: "nesw-resize",
        se: "nwse-resize",
        sw: "nesw-resize"
    };

    var flipX = {
        e: "w",
        w: "e",
        nw: "ne",
        ne: "nw",
        se: "sw",
        sw: "se"
    };

    var flipY = {
        n: "s",
        s: "n",
        nw: "sw",
        ne: "se",
        se: "ne",
        sw: "nw"
    };

    var signsX = {
        overlay: +1,
        selection: +1,
        n: null,
        e: +1,
        s: null,
        w: -1,
        nw: -1,
        ne: +1,
        se: +1,
        sw: -1
    };

    var signsY = {
        overlay: +1,
        selection: +1,
        n: -1,
        e: null,
        s: +1,
        w: null,
        nw: -1,
        ne: -1,
        se: +1,
        sw: +1
    };

    function type(t) {
        return {type: t};
    }

    // Ignore right-click, since that should open the context menu.
    function defaultFilter() {
        return !event.ctrlKey && !event.button;
    }

    function defaultExtent() {
        var svg = this.ownerSVGElement || this;
        if (svg.hasAttribute("viewBox")) {
            svg = svg.viewBox.baseVal;
            return [[svg.x, svg.y], [svg.x + svg.width, svg.y + svg.height]];
        }
        return [[0, 0], [svg.width.baseVal.value, svg.height.baseVal.value]];
    }

    function defaultTouchable() {
        return navigator.maxTouchPoints || ("ontouchstart" in this);
    }

    // Like d3.local, but with the name “__brush” rather than auto-generated.
    function local(node) {
        while (!node.__brush) if (!(node = node.parentNode)) return;
        return node.__brush;
    }

    function empty$1(extent) {
        return extent[0][0] === extent[1][0]
            || extent[0][1] === extent[1][1];
    }

    function brushX() {
        return brush(X);
    }

    function brush(dim) {
        var extent = defaultExtent,
            filter = defaultFilter,
            touchable = defaultTouchable,
            keys = true,
            listeners = dispatch("start", "brush", "end"),
            handleSize = 6,
            touchending;

        function brush(group) {
            var overlay = group
                .property("__brush", initialize)
                .selectAll(".overlay")
                .data([type("overlay")]);

            overlay.enter().append("rect")
                .attr("class", "overlay")
                .attr("pointer-events", "all")
                .attr("cursor", cursors.overlay)
                .merge(overlay)
                .each(function () {
                    var extent = local(this).extent;
                    d3Select(this)
                        .attr("x", extent[0][0])
                        .attr("y", extent[0][1])
                        .attr("width", extent[1][0] - extent[0][0])
                        .attr("height", extent[1][1] - extent[0][1]);
                });

            group.selectAll(".selection")
                .data([type("selection")])
                .enter().append("rect")
                .attr("class", "selection")
                .attr("cursor", cursors.selection)
                .attr("fill", "#777")
                .attr("fill-opacity", 0.3)
                .attr("stroke", "#fff")
                .attr("shape-rendering", "crispEdges");

            var handle = group.selectAll(".handle")
                .data(dim.handles, function (d) {
                    return d.type;
                });

            handle.exit().remove();

            handle.enter().append("rect")
                .attr("class", function (d) {
                    return "handle handle--" + d.type;
                })
                .attr("cursor", function (d) {
                    return cursors[d.type];
                });

            group
                .each(redraw)
                .attr("fill", "none")
                .attr("pointer-events", "all")
                .on("mousedown.brush", started)
                .filter(touchable)
                .on("touchstart.brush", started)
                .on("touchmove.brush", touchmoved)
                .on("touchend.brush touchcancel.brush", touchended)
                .style("touch-action", "none")
                .style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
        }

        brush.move = function (group, selection) {
            if (group.selection) {
                group
                    .on("start.brush", function () {
                        emitter(this, arguments).beforestart().start();
                    })
                    .on("interrupt.brush end.brush", function () {
                        emitter(this, arguments).end();
                    })
                    .tween("brush", function () {
                        var that = this,
                            state = that.__brush,
                            emit = emitter(that, arguments),
                            selection0 = state.selection,
                            selection1 = dim.input(typeof selection === "function" ? selection.apply(this, arguments) : selection, state.extent),
                            i = interpolate(selection0, selection1);

                        function tween(t) {
                            state.selection = t === 1 && selection1 === null ? null : i(t);
                            redraw.call(that);
                            emit.brush();
                        }

                        return selection0 !== null && selection1 !== null ? tween : tween(1);
                    });
            } else {
                group
                    .each(function () {
                        var that = this,
                            args = arguments,
                            state = that.__brush,
                            selection1 = dim.input(typeof selection === "function" ? selection.apply(that, args) : selection, state.extent),
                            emit = emitter(that, args).beforestart();

                        interrupt(that);
                        state.selection = selection1 === null ? null : selection1;
                        redraw.call(that);
                        emit.start().brush().end();
                    });
            }
        };

        brush.clear = function (group) {
            brush.move(group, null);
        };

        function redraw() {
            var group = d3Select(this),
                selection = local(this).selection;

            if (selection) {
                group.selectAll(".selection")
                    .style("display", null)
                    .attr("x", selection[0][0])
                    .attr("y", selection[0][1])
                    .attr("width", selection[1][0] - selection[0][0])
                    .attr("height", selection[1][1] - selection[0][1]);

                group.selectAll(".handle")
                    .style("display", null)
                    .attr("x", function (d) {
                        return d.type[d.type.length - 1] === "e" ? selection[1][0] - handleSize / 2 : selection[0][0] - handleSize / 2;
                    })
                    .attr("y", function (d) {
                        return d.type[0] === "s" ? selection[1][1] - handleSize / 2 : selection[0][1] - handleSize / 2;
                    })
                    .attr("width", function (d) {
                        return d.type === "n" || d.type === "s" ? selection[1][0] - selection[0][0] + handleSize : handleSize;
                    })
                    .attr("height", function (d) {
                        return d.type === "e" || d.type === "w" ? selection[1][1] - selection[0][1] + handleSize : handleSize;
                    });
            } else {
                group.selectAll(".selection,.handle")
                    .style("display", "none")
                    .attr("x", null)
                    .attr("y", null)
                    .attr("width", null)
                    .attr("height", null);
            }
        }

        function emitter(that, args, clean) {
            return (!clean && that.__brush.emitter) || new Emitter(that, args);
        }

        function Emitter(that, args) {
            this.that = that;
            this.args = args;
            this.state = that.__brush;
            this.active = 0;
        }

        Emitter.prototype = {
            beforestart: function () {
                if (++this.active === 1) this.state.emitter = this, this.starting = true;
                return this;
            },
            start: function () {
                if (this.starting) this.starting = false, this.emit("start");
                else this.emit("brush");
                return this;
            },
            brush: function () {
                this.emit("brush");
                return this;
            },
            end: function () {
                if (--this.active === 0) delete this.state.emitter, this.emit("end");
                return this;
            },
            emit: function (type) {
                customEvent(new BrushEvent(brush, type, dim.output(this.state.selection)), listeners.apply, listeners, [type, this.that, this.args]);
            }
        };

        function started() {
            if (touchending && !event.touches) return;
            if (!filter.apply(this, arguments)) return;

            var that = this,
                type = event.target.__data__.type,
                mode = (keys && event.metaKey ? type = "overlay" : type) === "selection" ? MODE_DRAG : (keys && event.altKey ? MODE_CENTER : MODE_HANDLE),
                signX = dim === Y ? null : signsX[type],
                signY = dim === X ? null : signsY[type],
                state = local(that),
                extent = state.extent,
                selection = state.selection,
                W = extent[0][0], w0, w1,
                N = extent[0][1], n0, n1,
                E = extent[1][0], e0, e1,
                S = extent[1][1], s0, s1,
                dx = 0,
                dy = 0,
                moving,
                shifting = signX && signY && keys && event.shiftKey,
                lockX,
                lockY,
                pointer = event.touches ? toucher(event.changedTouches[0].identifier) : d3Mouse,
                point0 = pointer(that),
                point = point0,
                emit = emitter(that, arguments, true).beforestart();

            if (type === "overlay") {
                if (selection) moving = true;
                state.selection = selection = [
                    [w0 = dim === Y ? W : point0[0], n0 = dim === X ? N : point0[1]],
                    [e0 = dim === Y ? E : w0, s0 = dim === X ? S : n0]
                ];
            } else {
                w0 = selection[0][0];
                n0 = selection[0][1];
                e0 = selection[1][0];
                s0 = selection[1][1];
            }

            w1 = w0;
            n1 = n0;
            e1 = e0;
            s1 = s0;

            var group = d3Select(that)
                .attr("pointer-events", "none");

            var overlay = group.selectAll(".overlay")
                .attr("cursor", cursors[type]);

            if (event.touches) {
                emit.moved = moved;
                emit.ended = ended;
            } else {
                var view = d3Select(event.view)
                    .on("mousemove.brush", moved, true)
                    .on("mouseup.brush", ended, true);
                if (keys) view
                    .on("keydown.brush", keydowned, true)
                    .on("keyup.brush", keyupped, true);

                dragDisable(event.view);
            }

            nopropagation();
            interrupt(that);
            redraw.call(that);
            emit.start();

            function moved() {
                var point1 = pointer(that);
                if (shifting && !lockX && !lockY) {
                    if (Math.abs(point1[0] - point[0]) > Math.abs(point1[1] - point[1])) lockY = true;
                    else lockX = true;
                }
                point = point1;
                moving = true;
                noevent$1();
                move();
            }

            function move() {
                var t;

                dx = point[0] - point0[0];
                dy = point[1] - point0[1];

                switch (mode) {
                    case MODE_SPACE:
                    case MODE_DRAG: {
                        if (signX) dx = Math.max(W - w0, Math.min(E - e0, dx)), w1 = w0 + dx, e1 = e0 + dx;
                        if (signY) dy = Math.max(N - n0, Math.min(S - s0, dy)), n1 = n0 + dy, s1 = s0 + dy;
                        break;
                    }
                    case MODE_HANDLE: {
                        if (signX < 0) dx = Math.max(W - w0, Math.min(E - w0, dx)), w1 = w0 + dx, e1 = e0;
                        else if (signX > 0) dx = Math.max(W - e0, Math.min(E - e0, dx)), w1 = w0, e1 = e0 + dx;
                        if (signY < 0) dy = Math.max(N - n0, Math.min(S - n0, dy)), n1 = n0 + dy, s1 = s0;
                        else if (signY > 0) dy = Math.max(N - s0, Math.min(S - s0, dy)), n1 = n0, s1 = s0 + dy;
                        break;
                    }
                    case MODE_CENTER: {
                        if (signX) w1 = Math.max(W, Math.min(E, w0 - dx * signX)), e1 = Math.max(W, Math.min(E, e0 + dx * signX));
                        if (signY) n1 = Math.max(N, Math.min(S, n0 - dy * signY)), s1 = Math.max(N, Math.min(S, s0 + dy * signY));
                        break;
                    }
                }

                if (e1 < w1) {
                    signX *= -1;
                    t = w0, w0 = e0, e0 = t;
                    t = w1, w1 = e1, e1 = t;
                    if (type in flipX) overlay.attr("cursor", cursors[type = flipX[type]]);
                }

                if (s1 < n1) {
                    signY *= -1;
                    t = n0, n0 = s0, s0 = t;
                    t = n1, n1 = s1, s1 = t;
                    if (type in flipY) overlay.attr("cursor", cursors[type = flipY[type]]);
                }

                if (state.selection) selection = state.selection; // May be set by brush.move!
                if (lockX) w1 = selection[0][0], e1 = selection[1][0];
                if (lockY) n1 = selection[0][1], s1 = selection[1][1];

                if (selection[0][0] !== w1
                    || selection[0][1] !== n1
                    || selection[1][0] !== e1
                    || selection[1][1] !== s1) {
                    state.selection = [[w1, n1], [e1, s1]];
                    redraw.call(that);
                    emit.brush();
                }
            }

            function ended() {
                nopropagation();
                if (event.touches) {
                    if (event.touches.length) return;
                    if (touchending) clearTimeout(touchending);
                    touchending = setTimeout(function () {
                        touchending = null;
                    }, 500); // Ghost clicks are delayed!
                } else {
                    yesdrag(event.view, moving);
                    view.on("keydown.brush keyup.brush mousemove.brush mouseup.brush", null);
                }
                group.attr("pointer-events", "all");
                overlay.attr("cursor", cursors.overlay);
                if (state.selection) selection = state.selection; // May be set by brush.move (on start)!
                if (empty$1(selection)) state.selection = null, redraw.call(that);
                emit.end();
            }

            function keydowned() {
                switch (event.keyCode) {
                    case 16: { // SHIFT
                        shifting = signX && signY;
                        break;
                    }
                    case 18: { // ALT
                        if (mode === MODE_HANDLE) {
                            if (signX) e0 = e1 - dx * signX, w0 = w1 + dx * signX;
                            if (signY) s0 = s1 - dy * signY, n0 = n1 + dy * signY;
                            mode = MODE_CENTER;
                            move();
                        }
                        break;
                    }
                    case 32: { // SPACE; takes priority over ALT
                        if (mode === MODE_HANDLE || mode === MODE_CENTER) {
                            if (signX < 0) e0 = e1 - dx; else if (signX > 0) w0 = w1 - dx;
                            if (signY < 0) s0 = s1 - dy; else if (signY > 0) n0 = n1 - dy;
                            mode = MODE_SPACE;
                            overlay.attr("cursor", cursors.selection);
                            move();
                        }
                        break;
                    }
                    default:
                        return;
                }
                noevent$1();
            }

            function keyupped() {
                switch (event.keyCode) {
                    case 16: { // SHIFT
                        if (shifting) {
                            lockX = lockY = shifting = false;
                            move();
                        }
                        break;
                    }
                    case 18: { // ALT
                        if (mode === MODE_CENTER) {
                            if (signX < 0) e0 = e1; else if (signX > 0) w0 = w1;
                            if (signY < 0) s0 = s1; else if (signY > 0) n0 = n1;
                            mode = MODE_HANDLE;
                            move();
                        }
                        break;
                    }
                    case 32: { // SPACE
                        if (mode === MODE_SPACE) {
                            if (event.altKey) {
                                if (signX) e0 = e1 - dx * signX, w0 = w1 + dx * signX;
                                if (signY) s0 = s1 - dy * signY, n0 = n1 + dy * signY;
                                mode = MODE_CENTER;
                            } else {
                                if (signX < 0) e0 = e1; else if (signX > 0) w0 = w1;
                                if (signY < 0) s0 = s1; else if (signY > 0) n0 = n1;
                                mode = MODE_HANDLE;
                            }
                            overlay.attr("cursor", cursors[type]);
                            move();
                        }
                        break;
                    }
                    default:
                        return;
                }
                noevent$1();
            }
        }

        function touchmoved() {
            emitter(this, arguments).moved();
        }

        function touchended() {
            emitter(this, arguments).ended();
        }

        function initialize() {
            var state = this.__brush || {selection: null};
            state.extent = number2(extent.apply(this, arguments));
            state.dim = dim;
            return state;
        }

        brush.extent = function (_) {
            return arguments.length ? (extent = typeof _ === "function" ? _ : constant$3(number2(_)), brush) : extent;
        };

        brush.filter = function (_) {
            return arguments.length ? (filter = typeof _ === "function" ? _ : constant$3(!!_), brush) : filter;
        };

        brush.touchable = function (_) {
            return arguments.length ? (touchable = typeof _ === "function" ? _ : constant$3(!!_), brush) : touchable;
        };

        brush.handleSize = function (_) {
            return arguments.length ? (handleSize = +_, brush) : handleSize;
        };

        brush.keyModifiers = function (_) {
            return arguments.length ? (keys = !!_, brush) : keys;
        };

        brush.on = function () {
            var value = listeners.on.apply(listeners, arguments);
            return value === listeners ? brush : value;
        };

        return brush;
    }

    var TimeOverview = index({
        props: {
            width: {
                "default": 300
            },
            height: {
                "default": 20
            },
            margins: {
                "default": {
                    top: 0,
                    right: 0,
                    bottom: 20,
                    left: 0
                }
            },
            scale: {},
            domainRange: {},
            currentSelection: {},
            tickFormat: {},
            onChange: {
                "default": function _default(selectionStart, selectionEnd) {
                }
            }
        },
        init: function init(el, state) {
            state.xGrid = axisBottom().tickFormat('');
            state.xAxis = axisBottom().tickPadding(0);
            state.brush = brushX().handleSize(24).on('end', function () {
                if (!event.sourceEvent) return;
                var selection = event.selection ? event.selection.map(state.scale.invert) : state.scale.domain();
                state.onChange.apply(state, _toConsumableArray(selection));
            }); // Build dom

            state.svg = d3Select(el).append('svg').attr('class', 'brusher');
            var brusher = state.svg.append('g').attr('class', 'brusher-margins');
            brusher.append('rect').attr('class', 'grid-background');
            brusher.append('g').attr('class', 'x grid');
            brusher.append('g').attr('class', 'x axis');
            brusher.append('g').attr('class', 'brush');
        },
        update: function update(state) {
            if (state.domainRange[1] <= state.domainRange[0]) return;
            var brushWidth = state.width - state.margins.left - state.margins.right,
                brushHeight = state.height - state.margins.top - state.margins.bottom;
            state.scale.domain(state.domainRange).range([0, brushWidth]);
            state.xAxis.scale(state.scale).tickFormat(state.tickFormat);
            state.xGrid.scale(state.scale).tickSize(-brushHeight);
            state.svg.attr('width', state.width).attr('height', state.height);
            state.svg.select('.brusher-margins').attr('transform', "translate(".concat(state.margins.left, ",").concat(state.margins.top, ")"));
            state.svg.select('.grid-background').attr('width', brushWidth).attr('height', brushHeight);
            state.svg.select('.x.grid').attr('transform', 'translate(0,' + brushHeight + ')').call(state.xGrid);
            state.svg.select('.x.axis').attr("transform", "translate(0," + brushHeight + ")").call(state.xAxis).selectAll('text').attr('y', 8);
            state.svg.select('.brush').call(state.brush.extent([[0, 0], [brushWidth, brushHeight]])).call(state.brush.move, state.currentSelection.map(state.scale));
        }
    });

    function alphaNumCmp(a, b) {
        var alist = a.split(/(\d+)/),
            blist = b.split(/(\d+)/);
        alist.length && alist[alist.length - 1] == '' ? alist.pop() : null; // remove the last element if empty

        blist.length && blist[blist.length - 1] == '' ? blist.pop() : null; // remove the last element if empty

        for (var i = 0, len = Math.max(alist.length, blist.length); i < len; i++) {
            if (alist.length == i || blist.length == i) {
                // Out of bounds for one of the sides
                return alist.length - blist.length;
            }

            if (alist[i] != blist[i]) {
                // find the first non-equal part
                if (alist[i].match(/\d/)) // if numeric
                {
                    return +alist[i] - +blist[i]; // compare as number
                } else {
                    return alist[i].toLowerCase() > blist[i].toLowerCase() ? 1 : -1; // compare as string
                }
            }
        }

        return 0;
    }

    var timelines = index({
        props: {
            data: {
                "default": [],
                onChange: function onChange(data, state) {
                    parseData(data);
                    state.zoomX = [min(state.completeFlatData, function (d) {
                        return d.timeRange[0];
                    }), max(state.completeFlatData, function (d) {
                        return d.timeRange[1];
                    })];
                    state.zoomY = [null, null];

                    if (state.overviewArea) {
                        state.overviewArea.domainRange(state.zoomX).currentSelection(state.zoomX);
                    } //


                    function parseData(rawData) {
                        state.completeStructData = [];
                        state.completeFlatData = [];
                        state.totalNLines = 0;

                        for (var i = 0, ilen = rawData.length; i < ilen; i++) {
                            var group = rawData[i].group;
                            state.completeStructData.push({
                                group: group,
                                lines: rawData[i].data.map(function (d) {
                                    return d.label;
                                })
                            });

                            for (var j = 0, jlen = rawData[i].data.length; j < jlen; j++) {
                                for (var k = 0, klen = rawData[i].data[j].data.length; k < klen; k++) {
                                    state.completeFlatData.push({
                                        group: group,
                                        label: rawData[i].data[j].label,
                                        timeRange: rawData[i].data[j].data[k].timeRange.map(function (d) {
                                            return new Date(d);
                                        }),
                                        val: rawData[i].data[j].data[k].val,
                                        labelVal: rawData[i].data[j].data[k][rawData[i].data[j].data[k].hasOwnProperty('labelVal') ? 'labelVal' : 'val']
                                    });
                                }

                                state.totalNLines++;
                            }
                        }
                    }
                }
            },
            width: {
                "default": window.innerWidth
            },
            maxHeight: {
                "default": 640
            },
            maxLineHeight: {
                "default": 12
            },
            leftMargin: {
                "default": 90
            },
            rightMargin: {
                "default": 100
            },
            topMargin: {
                "default": 26
            },
            bottomMargin: {
                "default": 30
            },
            useUtc: {
                "default": false
            },
            xTickFormat: {},
            dateMarker: {},
            timeFormat: {
                //"default": '%Y-%m-%d %-I:%M:%S %p',
                "default": '%Y-%m-%d',
                triggerUpdate: false
            },
            zoomX: {
                // Which time-range to show (null = min/max)
                "default": [null, null],
                onChange: function onChange(zoomX, state) {
                    if (state.svg) state.svg.dispatch('zoom', {
                        detail: {
                            zoomX: zoomX,
                            zoomY: null,
                            redraw: false
                        }
                    });
                }
            },
            zoomY: {
                // Which lines to show (null = min/max) [0 indexed]
                "default": [null, null],
                onChange: function onChange(zoomY, state) {
                    if (state.svg) state.svg.dispatch('zoom', {
                        detail: {
                            zoomX: null,
                            zoomY: zoomY,
                            redraw: false
                        }
                    });
                }
            },
            minSegmentDuration: {},
            zColorScale: {
                "default": sequential(interpolateRdYlBu)
            },
            plotInterface: {
                "default": null
            },
            zQualitative: {
                "default": false,
                onChange: function onChange(discrete, state) {
                    state.zColorScale = discrete ? ordinal([].concat(_toConsumableArray(schemeCategory10), _toConsumableArray(schemeSet3))) : sequential(interpolateRdYlBu); // alt: d3.interpolateInferno
                }
            },
            zDataLabel: {
                "default": '',
                triggerUpdate: false
            },
            // Units of z data. Used in the tooltip descriptions
            zScaleLabel: {
                "default": '',
                triggerUpdate: false
            },
            // Units of colorScale. Used in the legend label
            enableOverview: {
                "default": true
            },
            // True/False
            enableAnimations: {
                "default": true,
                onChange: function onChange(val, state) {
                    state.transDuration = val ? 700 : 0;
                }
            },
            // Callbacks
            onZoom: {},
            // When user zooms in / resets zoom. Returns ([startX, endX], [startY, endY])
            onLabelClick: {},
            // When user clicks on a group or y label. Returns (group) or (label, group) respectively
            onSegmentClick: {} // When user clicks on a segment. Returns (segment object) respectively

        },
        methods: {
            getNLines: function getNLines(s) {
                return s.nLines;
            },
            getTotalNLines: function getTotalNLines(s) {
                return s.totalNLines;
            },
            getVisibleStructure: function getVisibleStructure(s) {
                return s.structData;
            },
            getSvg: function getSvg(s) {
                return d3Select(s.svg.node().parentNode).html();
            },
            zoomYLabels: function zoomYLabels(state, _) {
                if (!_) {
                    return [y2Label(state.zoomY[0]), y2Label(state.zoomY[1])];
                }

                return this.zoomY([label2Y(_[0], true), label2Y(_[1], false)]); //

                function y2Label(y) {
                    if (y == null) return y;
                    var cntDwn = y;

                    for (var i = 0, len = state.completeStructData.length; i < len; i++) {
                        if (state.completeStructData[i].lines.length > cntDwn) return getIdxLine(state.completeStructData[i], cntDwn);
                        cntDwn -= state.completeStructData[i].lines.length;
                    } // y larger than all lines, return last


                    return getIdxLine(state.completeStructData[state.completeStructData.length - 1], state.completeStructData[state.completeStructData.length - 1].lines.length - 1); //

                    function getIdxLine(grpData, idx) {
                        return {
                            'group': grpData.group,
                            'label': grpData.lines[idx]
                        };
                    }
                }

                function label2Y(label, useIdxAfterIfNotFound) {
                    useIdxAfterIfNotFound = useIdxAfterIfNotFound || false;
                    var subIdxNotFound = useIdxAfterIfNotFound ? 0 : 1;
                    if (label == null) return label;
                    var idx = 0;

                    for (var i = 0, lenI = state.completeStructData.length; i < lenI; i++) {
                        var grpCmp = state.grpCmpFunction(label.group, state.completeStructData[i].group);
                        if (grpCmp < 0) break;

                        if (grpCmp == 0 && label.group == state.completeStructData[i].group) {
                            for (var j = 0, lenJ = state.completeStructData[i].lines.length; j < lenJ; j++) {
                                var cmpRes = state.labelCmpFunction(label.label, state.completeStructData[i].lines[j]);

                                if (cmpRes < 0) {
                                    return idx + j - subIdxNotFound;
                                }

                                if (cmpRes == 0 && label.label == state.completeStructData[i].lines[j]) {
                                    return idx + j;
                                }
                            }

                            return idx + state.completeStructData[i].lines.length - subIdxNotFound;
                        }

                        idx += state.completeStructData[i].lines.length;
                    }

                    return idx - subIdxNotFound;
                }
            },
            sort: function sort(state, labelCmpFunction, grpCmpFunction) {
                if (labelCmpFunction == null) {
                    labelCmpFunction = state.labelCmpFunction;
                }

                if (grpCmpFunction == null) {
                    grpCmpFunction = state.grpCmpFunction;
                }

                state.labelCmpFunction = labelCmpFunction;
                state.grpCmpFunction = grpCmpFunction;
                state.completeStructData.sort(function (a, b) {
                    return grpCmpFunction(a.group, b.group);
                });

                for (var i = 0, len = state.completeStructData.length; i < len; i++) {
                    state.completeStructData[i].lines.sort(labelCmpFunction);
                }

                state._rerender();

                return this;
            },
            sortAlpha: function sortAlpha(state, asc) {
                if (asc == null) {
                    asc = true;
                }

                var alphaCmp = function alphaCmp(a, b) {
                    return alphaNumCmp(asc ? a : b, asc ? b : a);
                };

                return this.sort(alphaCmp, alphaCmp);
            },
            sortChrono: function sortChrono(state, asc) {
                if (asc == null) {
                    asc = true;
                }

                function buildIdx(accessFunction) {
                    var idx = {};

                    var _loop = function _loop(i, len) {
                        var key = accessFunction(state.completeFlatData[i]);

                        if (idx.hasOwnProperty(key)) {
                            return "continue";
                        }

                        var itmList = state.completeFlatData.filter(function (d) {
                            return key == accessFunction(d);
                        });
                        idx[key] = [min(itmList, function (d) {
                            return d.timeRange[0];
                        }), max(itmList, function (d) {
                            return d.timeRange[1];
                        })];
                    };

                    for (var i = 0, len = state.completeFlatData.length; i < len; i++) {
                        var _ret = _loop(i);

                        if (_ret === "continue") continue;
                    }

                    return idx;
                }

                var timeCmp = function timeCmp(a, b) {
                    var aT = a[1],
                        bT = b[1];
                    if (!aT || !bT) return null; // One of the two vals is null

                    if (aT[1].getTime() == bT[1].getTime()) {
                        if (aT[0].getTime() == bT[0].getTime()) {
                            return alphaNumCmp(a[0], b[0]); // If first and last is same, use alphaNum
                        }

                        return aT[0] - bT[0]; // If last is same, earliest first wins
                    }

                    return bT[1] - aT[1]; // latest last wins
                };

                function getCmpFunction(accessFunction, asc) {
                    return function (a, b) {
                        return timeCmp(accessFunction(asc ? a : b), accessFunction(asc ? b : a));
                    };
                }

                var grpIdx = buildIdx(function (d) {
                    return d.group;
                });
                var lblIdx = buildIdx(function (d) {
                    return d.label;
                });
                var grpCmp = getCmpFunction(function (d) {
                    return [d, grpIdx[d] || null];
                }, asc);
                var lblCmp = getCmpFunction(function (d) {
                    return [d, lblIdx[d] || null];
                }, asc);
                return this.sort(lblCmp, grpCmp);
            },
            overviewDomain: function overviewDomain(state, _) {
                if (!state.enableOverview) {
                    return null;
                }

                if (!_) {
                    return state.overviewArea.domainRange();
                }

                state.overviewArea.domainRange(_);
                return this;
            },
            refresh: function refresh(state) {
                state._rerender();

                return this;
            }
        },
        stateInit: {
            height: null,
            overviewHeight: 20,
            // Height of overview section in bottom
            minLabelFont: 2,
            groupBkgGradient: ['#FAFAFA', '#E0E0E0'],
            yScale: null,
            grpScale: null,
            xAxis: null,
            xGrid: null,
            yAxis: null,
            //grpAxis: null,
            dateMarkerLine: null,
            svg: null,
            graph: null,
            overviewAreaElem: null,
            overviewArea: null,
            graphW: null,
            graphH: null,
            completeStructData: null,
            structData: null,
            completeFlatData: null,
            flatData: null,
            totalNLines: null,
            nLines: null,
            minSegmentDuration: 0,
            // ms
            transDuration: 700,
            // ms for transition duration
            labelCmpFunction: alphaNumCmp,
            grpCmpFunction: alphaNumCmp
        },
        init: function init(el, state) {
            var elem = d3Select(el).attr('class', 'timelines-chart');
            state.svg = elem.append('svg');
            state.overviewAreaElem = elem.append('div'); // Initialize scales and axes

            state.yScale = point();
            state.grpScale = ordinal();
            state.xAxis = axisBottom();
            state.xGrid = axisTop();
            state.yAxis = axisRight();
            //state.grpAxis = axisLeft();
            buildDomStructure();
            addTooltips();
            addZoomSelection();
            setEvents(); //

            function buildDomStructure() {
                state.yScale.invert = invertOrdinal;
                state.grpScale.invert = invertOrdinal;
                state.groupGradId = gradient().colorScale(linear$1().domain([0, 1]).range(state.groupBkgGradient)).angle(-90)(state.svg.node()).id();
                var axises = state.svg.append('g').attr('class', 'axises');
                axises.append('g').attr('class', 'x-axis');
                axises.append('g').attr('class', 'x-grid');
                axises.append('g').attr('class', 'y-axis');
                axises.append('g').attr('class', 'grp-axis');
                state.yAxis.scale(state.yScale).tickSize(0);
                //state.grpAxis.scale(state.grpScale).tickSize(0);
                state.colorLegend = legend()(state.svg.append('g').attr('class', 'legendG').node());
                state.graph = state.svg.append('g');
                state.dateMarkerLine = state.svg.append('line').attr('class', 'x-axis-date-marker');

                if (state.enableOverview) {
                    addOverviewArea();
                } // Applies to ordinal scales (invert not supported in d3)


                function invertOrdinal(val, cmpFunc) {
                    cmpFunc = cmpFunc || function (a, b) {
                        return a >= b;
                    };

                    var scDomain = this.domain();
                    var scRange = this.range();

                    if (scRange.length === 2 && scDomain.length !== 2) {
                        // Special case, interpolate range vals
                        scRange = d3Range(scRange[0], scRange[1], (scRange[1] - scRange[0]) / scDomain.length);
                    }

                    var bias = scRange[0];

                    for (var i = 0, len = scRange.length; i < len; i++) {
                        if (cmpFunc(scRange[i] + bias, val)) {
                            return scDomain[Math.round(i * scDomain.length / scRange.length)];
                        }
                    }

                    return this.domain()[this.domain().length - 1];
                }

                function addOverviewArea() {
                    state.overviewArea = TimeOverview().margins({
                        top: 1,
                        right: 20,
                        bottom: 20,
                        left: 20
                    }).onChange(function (startTime, endTime) {
                        state.svg.dispatch('zoom', {
                            detail: {
                                zoomX: [startTime, endTime],
                                zoomY: null
                            }
                        });
                    }).domainRange(state.zoomX).currentSelection(state.zoomX)(state.overviewAreaElem.node());
                    state.svg.on('zoomScent', function () {
                        var zoomX = event.detail.zoomX;
                        if (!state.overviewArea || !zoomX) return; // Out of overview bounds > extend it

                        if (zoomX[0] < state.overviewArea.domainRange()[0] || zoomX[1] > state.overviewArea.domainRange()[1]) {
                            state.overviewArea.domainRange([new Date(Math.min(zoomX[0], state.overviewArea.domainRange()[0])), new Date(Math.max(zoomX[1], state.overviewArea.domainRange()[1]))]);
                        }

                        state.overviewArea.currentSelection(zoomX);
                    });
                }
            }

            function addTooltips() {
              console.log("XXXX")
                state.groupTooltip = d3Tip().attr('class', 'chart-tooltip group-tooltip').direction('w').offset([0, 0]).html(function (d) {
                    var leftPush = d.hasOwnProperty('timeRange') ? state.xScale(d.timeRange[0]) : 0;
                    var topPush = d.hasOwnProperty('label') ? state.grpScale(d.group) - state.yScale(d.group + '+&+' + d.label) : 0;
                    state.groupTooltip.offset([topPush, -leftPush]);
                    return d.group;
                });
                state.svg.call(state.groupTooltip);
                state.lineTooltip = d3Tip().attr('class', 'chart-tooltip line-tooltip').direction('e').offset([0, 0]).html(function (d) {
                    var rightPush = d.hasOwnProperty('timeRange') ? state.xScale.range()[1] - state.xScale(d.timeRange[1]) : 0;
                    state.lineTooltip.offset([0, rightPush]);
                    return d.label;
                });
                state.svg.call(state.lineTooltip);
              //return state.zDataLabel + (normVal ? ' (<strong>' + Math.round((d.val - state.zColorScale.domain()[0]) / normVal * 100 * 100) / 100 + '%</strong>)' : '') + '<br>' + '<strong>From: </strong>' + dateFormat(d.timeRange[0]) + '<br>' + '<strong>To: </strong>' + dateFormat(d.timeRange[1]);
              state.segmentTooltip = d3Tip()
                    .attr('class', 'chart-tooltip segment-tooltip')
                    .direction('s').offset([5, 0]).html(function (d) {
                        var normVal = state.zColorScale.domain()[
                            state.zColorScale.domain().length - 1
                            ] - state.zColorScale.domain()[0];
                        console.log("XXX"); console.log(state.useUtc); console.log(timeFormat); console.log(d.timeRange[0]);
                        console.log(timeFormat(d.timeRange[0]))
                        var dateFormat = (state.useUtc ? utcFormat : timeFormat
                        )("".concat(state.timeFormat).concat(state.useUtc ? ' (UTC)' : ''));
                        //return '<strong>' + d.labelVal + ' </strong>' + state.zDataLabel + (normVal ? ' (<strong>' + Math.round((d.val - state.zColorScale.domain()[0]) / normVal * 100 * 100) / 100 + '%</strong>)' : '') + '<br>' + '<strong>From: </strong>' + dateFormat(d.timeRange[0]) + '<br>' + '<strong>To: </strong>' + dateFormat(d.timeRange[1]);
                        return state.zDataLabel
                            //+ (normVal ? ' (<strong>' + Math.round((d.val - state.zColorScale.domain()[0])
                            //    / normVal * 100 * 100) / 100 + '%</strong>)' : '')
                            //+ '<br>'
                            + '<strong>From: </strong>'
                            + dateFormat(d.timeRange[0]) + '<br>'
                            + '<strong>To: </strong>'
                            + dateFormat(d.timeRange[1]);

                    });
              state.svg.call(state.segmentTooltip);
            }

            function addZoomSelection() {
                state.graph.on('mousedown', function () {
                    if (d3Select(window).on('mousemove.zoomRect') != null) // Selection already active
                        return;
                    var e = this;
                    if (d3Mouse(e)[0] < 0 || d3Mouse(e)[0] > state.graphW || d3Mouse(e)[1] < 0 || d3Mouse(e)[1] > state.graphH) return;
                    state.disableHover = true;
                    var rect = state.graph.append('rect').attr('class', 'chart-zoom-selection');
                    var startCoords = d3Mouse(e);
                    d3Select(window).on('mousemove.zoomRect', function () {
                        event.stopPropagation();
                        var newCoords = [Math.max(0, Math.min(state.graphW, d3Mouse(e)[0])), Math.max(0, Math.min(state.graphH, d3Mouse(e)[1]))];
                        rect.attr('x', Math.min(startCoords[0], newCoords[0])).attr('y', Math.min(startCoords[1], newCoords[1])).attr('width', Math.abs(newCoords[0] - startCoords[0])).attr('height', Math.abs(newCoords[1] - startCoords[1]));
                        state.svg.dispatch('zoomScent', {
                            detail: {
                                zoomX: [startCoords[0], newCoords[0]].sort(d3Ascending).map(state.xScale.invert),
                                zoomY: [startCoords[1], newCoords[1]].sort(d3Ascending).map(function (d) {
                                    return state.yScale.domain().indexOf(state.yScale.invert(d)) + (state.zoomY && state.zoomY[0] ? state.zoomY[0] : 0);
                                })
                            }
                        });
                    }).on('mouseup.zoomRect', function () {
                        d3Select(window).on('mousemove.zoomRect', null).on('mouseup.zoomRect', null);
                        d3Select('body').classed('stat-noselect', false);
                        rect.remove();
                        state.disableHover = false;
                        var endCoords = [Math.max(0, Math.min(state.graphW, d3Mouse(e)[0])), Math.max(0, Math.min(state.graphH, d3Mouse(e)[1]))];
                        if (startCoords[0] == endCoords[0] && startCoords[1] == endCoords[1]) return;
                        var newDomainX = [startCoords[0], endCoords[0]].sort(d3Ascending).map(state.xScale.invert);
                        var newDomainY = [startCoords[1], endCoords[1]].sort(d3Ascending).map(function (d) {
                            return state.yScale.domain().indexOf(state.yScale.invert(d)) + (state.zoomY && state.zoomY[0] ? state.zoomY[0] : 0);
                        });
                        var changeX = newDomainX[1] - newDomainX[0] > 60 * 1000; // Zoom damper

                        var changeY = newDomainY[0] != state.zoomY[0] || newDomainY[1] != state.zoomY[1];

                        if (changeX || changeY) {
                            state.svg.dispatch('zoom', {
                                detail: {
                                    zoomX: changeX ? newDomainX : null,
                                    zoomY: changeY ? newDomainY : null
                                }
                            });
                        }
                    }, true);
                    event.stopPropagation();
                });
                state.resetBtn = state.svg.append('text').attr('class', 'reset-zoom-btn').text('Reset Zoom').style('text-anchor', 'end').on('mouseup', function () {
                    state.svg.dispatch('resetZoom');
                }).on('mouseover', function () {
                    d3Select(this).style('opacity', 1);
                }).on('mouseout', function () {
                    d3Select(this).style('opacity', .6);
                });
            }

            function setEvents() {
                state.svg.on('zoom', function () {
                    var evData = event.detail,
                        zoomX = evData.zoomX,
                        zoomY = evData.zoomY,
                        redraw = evData.redraw == null ? true : evData.redraw;
                    if (!zoomX && !zoomY) return;
                    if (zoomX) state.zoomX = zoomX;
                    if (zoomY) state.zoomY = zoomY;
                    state.svg.dispatch('zoomScent', {
                        detail: {
                            zoomX: zoomX,
                            zoomY: zoomY
                        }
                    });
                    if (!redraw) return;

                    state._rerender();

                    if (state.onZoom) state.onZoom(state.zoomX, state.zoomY);
                });
                state.svg.on('resetZoom', function () {
                    var prevZoomX = state.zoomX;
                    var prevZoomY = state.zoomY || [null, null];
                    var newZoomX = state.enableOverview ? state.overviewArea.domainRange() : [min(state.flatData, function (d) {
                            return d.timeRange[0];
                        }), max(state.flatData, function (d) {
                            return d.timeRange[1];
                        })],
                        newZoomY = [null, null];

                    if (prevZoomX[0] < newZoomX[0] || prevZoomX[1] > newZoomX[1] || prevZoomY[0] != newZoomY[0] || prevZoomY[1] != newZoomX[1]) {
                        state.zoomX = [new Date(Math.min(prevZoomX[0], newZoomX[0])), new Date(Math.max(prevZoomX[1], newZoomX[1]))];
                        state.zoomY = newZoomY;
                        state.svg.dispatch('zoomScent', {
                            detail: {
                                zoomX: state.zoomX,
                                zoomY: state.zoomY
                            }
                        });

                        state._rerender();
                    }

                    if (state.onZoom) state.onZoom(null, null);
                });
            }
        },
        update: function update(state) {
            applyFilters();
            setupDimensions();
            adjustXScale();
            adjustYScale();
            adjustGrpScale();
            renderAxises();
            renderGroups();
            renderTimelines();
            adjustLegend(); //

            function applyFilters() {
                // Flat data based on segment length
                state.flatData = state.minSegmentDuration > 0 ? state.completeFlatData.filter(function (d) {
                    return d.timeRange[1] - d.timeRange[0] >= state.minSegmentDuration;
                }) : state.completeFlatData; // zoomY

                if (state.zoomY == null || state.zoomY == [null, null]) {
                    state.structData = state.completeStructData;
                    state.nLines = 0;

                    for (var i = 0, len = state.structData.length; i < len; i++) {
                        state.nLines += state.structData[i].lines.length;
                    }

                    return;
                }

                state.structData = [];
                var cntDwn = [state.zoomY[0] == null ? 0 : state.zoomY[0]]; // Initial threshold

                cntDwn.push(Math.max(0, (state.zoomY[1] == null ? state.totalNLines : state.zoomY[1] + 1) - cntDwn[0])); // Number of lines

                state.nLines = cntDwn[1];

                var _loop3 = function _loop3(_i, _len) {
                    var validLines = state.completeStructData[_i].lines;

                    if (state.minSegmentDuration > 0) {
                        // Use only non-filtered (due to segment length) groups/labels
                        if (!state.flatData.some(function (d) {
                            return d.group == state.completeStructData[_i].group;
                        })) {
                            return "continue"; // No data for this group
                        }

                        validLines = state.completeStructData[_i].lines.filter(function (d) {
                            return state.flatData.some(function (dd) {
                                return dd.group == state.completeStructData[_i].group && dd.label == d;
                            });
                        });
                    }

                    if (cntDwn[0] >= validLines.length) {
                        // Ignore whole group (before start)
                        cntDwn[0] -= validLines.length;
                        return "continue";
                    }

                    var groupData = {
                        group: state.completeStructData[_i].group,
                        lines: null
                    };

                    if (validLines.length - cntDwn[0] >= cntDwn[1]) {
                        // Last (or first && last) group (partial)
                        groupData.lines = validLines.slice(cntDwn[0], cntDwn[1] + cntDwn[0]);
                        state.structData.push(groupData);
                        cntDwn[1] = 0;
                        return "break";
                    }

                    if (cntDwn[0] > 0) {
                        // First group (partial)
                        groupData.lines = validLines.slice(cntDwn[0]);
                        cntDwn[0] = 0;
                    } else {
                        // Middle group (full fit)
                        groupData.lines = validLines;
                    }

                    state.structData.push(groupData);
                    cntDwn[1] -= groupData.lines.length;
                };

                _loop2: for (var _i = 0, _len = state.completeStructData.length; _i < _len; _i++) {
                    var _ret2 = _loop3(_i);

                    switch (_ret2) {
                        case "continue":
                            continue;

                        case "break":
                            break _loop2;
                    }
                }

                state.nLines -= cntDwn[1];
            }

            function setupDimensions() {
                state.graphW = state.width - state.leftMargin - state.rightMargin;
                state.graphH = min([state.nLines * state.maxLineHeight, state.maxHeight - state.topMargin - state.bottomMargin]);
                state.height = state.graphH + state.topMargin + state.bottomMargin;
                state.svg.transition().duration(state.transDuration).attr('width', state.width).attr('height', state.height);
                state.graph.attr('transform', 'translate(' + state.leftMargin + ',' + state.topMargin + ')');

                if (state.overviewArea) {
                    state.overviewArea.width(state.width * 0.8).height(state.overviewHeight + state.overviewArea.margins().top + state.overviewArea.margins().bottom);
                }
            }

            function adjustXScale() {
                state.zoomX[0] = state.zoomX[0] || min(state.flatData, function (d) {
                    return d.timeRange[0];
                });
                state.zoomX[1] = state.zoomX[1] || max(state.flatData, function (d) {
                    return d.timeRange[1];
                });
                state.xScale = (state.useUtc ? d3ScaleUtc : d3ScaleTime)().domain(state.zoomX).range([0, state.graphW]).clamp(true);

                if (state.overviewArea) {
                    state.overviewArea.scale(state.xScale.copy()).tickFormat(state.xTickFormat);
                }
            }

            function adjustYScale() {
                var labels = [];

                var _loop4 = function _loop4(i, len) {
                    labels = labels.concat(state.structData[i].lines.map(function (d) {
                        return state.structData[i].group + '+&+' + d;
                    }));
                };

                for (var i = 0, len = state.structData.length; i < len; i++) {
                    _loop4(i);
                }

                state.yScale.domain(labels);
                state.yScale.range([state.graphH / labels.length * 0.5, state.graphH * (1 - 0.5 / labels.length)]);
            }

            function adjustGrpScale() {
                state.grpScale.domain(state.structData.map(function (d) {
                    return d.group;
                }));
                var cntLines = 0;
                state.grpScale.range(state.structData.map(function (d) {
                    var pos = (cntLines + d.lines.length / 2) / state.nLines * state.graphH;
                    cntLines += d.lines.length;
                    return pos;
                }));
            }

            function adjustLegend() {
                state.svg.select('.legendG').transition().duration(state.transDuration).attr('transform', "translate(".concat(state.leftMargin + state.graphW * 0.05, ",2)"));
                //state.colorLegend.width(Math.max(120, state.graphW / 3 * (state.zQualitative ? 2 : 1))).height(state.topMargin * .6).scale(state.zColorScale).label(state.zScaleLabel);
                state.resetBtn.transition().duration(state.transDuration).attr('x', state.leftMargin + state.graphW * .99).attr('y', state.topMargin * .8);
                fitToBox().bbox({
                    width: state.graphW * .4,
                    height: Math.min(13, state.topMargin * .8)
                })(state.resetBtn.node());
            }

            function renderAxises() {
                state.svg.select('.axises').attr('transform', 'translate(' + state.leftMargin + ',' + state.topMargin + ')'); // X

                var nXTicks = Math.max(2, Math.min(12, Math.round(state.graphW * 0.012)));
                state.xAxis.scale(state.xScale).ticks(nXTicks).tickFormat(state.xTickFormat);
                state.xGrid.scale(state.xScale).ticks(nXTicks).tickFormat('');
                state.svg.select('g.x-axis').style('stroke-opacity', 0).style('fill-opacity', 0).attr('transform', 'translate(0,' + state.graphH + ')').transition().duration(state.transDuration).call(state.xAxis).style('stroke-opacity', 1).style('fill-opacity', 1);
                /* Angled x axis labels
         state.svg.select('g.x-axis').selectAll('text')
         .style('text-anchor', 'end')
         .attr('transform', 'translate(-10, 3) rotate(-60)');
         */

                state.xGrid.tickSize(state.graphH);
                state.svg.select('g.x-grid').attr('transform', 'translate(0,' + state.graphH + ')').transition().duration(state.transDuration).call(state.xGrid);

                if (state.dateMarker && state.dateMarker >= state.xScale.domain()[0] && state.dateMarker <= state.xScale.domain()[1]) {
                    state.dateMarkerLine.style('display', 'block').transition().duration(state.transDuration).attr('x1', state.xScale(state.dateMarker) + state.leftMargin).attr('x2', state.xScale(state.dateMarker) + state.leftMargin).attr('y1', state.topMargin + 1).attr('y2', state.graphH + state.topMargin);
                } else {
                    state.dateMarkerLine.style('display', 'none');
                } // Y


                var fontVerticalMargin = 0.6;
                var labelDisplayRatio = Math.ceil(state.nLines * state.minLabelFont / Math.sqrt(2) / state.graphH / fontVerticalMargin);
                var tickVals = state.yScale.domain().filter(function (d, i) {
                    return !(i % labelDisplayRatio);
                });
                var fontSize = Math.min(12, state.graphH / tickVals.length * fontVerticalMargin * Math.sqrt(2));
                var maxChars = Math.ceil(state.rightMargin / (fontSize / Math.sqrt(2)));
                state.yAxis.tickValues(tickVals);
                state.yAxis.tickFormat(function (d) {
                    return reduceLabel(d.split('+&+')[1], 100) // maxChars);
                });
                state.svg.select('g.y-axis').transition().duration(state.transDuration).attr('transform', 'translate(' + state.graphW + ', 0)').style('font-size', fontSize + 'px').call(state.yAxis); // Grp

                var minHeight = min(state.grpScale.range(), function (d, i) {
                    return i > 0 ? d - state.grpScale.range()[i - 1] : d * 2;
                });
                fontSize = Math.min(14, minHeight * fontVerticalMargin * Math.sqrt(2));
                maxChars = Math.floor(state.leftMargin / (fontSize / Math.sqrt(2)));
                /*state.grpAxis.tickFormat(function (d) {
          return reduceLabel(d, maxChars);
        });
        state.svg.select('g.grp-axis').transition().duration(state.transDuration).style('font-size', fontSize + 'px').call(state.grpAxis); // Make Axises clickable
         */

                if (state.onLabelClick) {
                    //state.svg.selectAll('g.y-axis,g.grp-axis').selectAll('text').style('cursor', 'pointer').on('click', function (d) {
                    state.svg.selectAll('g.y-axis').selectAll('text').style('cursor', 'pointer').on('click', function (d) {
                        var segms = d.split('+&+');
                        state.onLabelClick.apply(state, _toConsumableArray(segms.reverse()));
                    });
                } //


                function reduceLabel(label, maxChars) {
                    return label.length <= maxChars ? label : label.substring(0, maxChars * 2 / 3) + '...' + label.substring(label.length - maxChars / 3, label.length);
                }
            }

            function renderGroups() {
                var groups = state.graph.selectAll('rect.series-group').data(state.structData, function (d) {
                    return d.group;
                });
                groups.exit().transition().duration(state.transDuration).style('stroke-opacity', 0).style('fill-opacity', 0).remove();
                var newGroups = groups
                        .enter().append('rect').attr('class', 'series-group')
                        .attr('x', 0).attr('y', 0).attr('height', 0)
                        .style('fill', 'url(#' + state.groupGradId + ')')
                    //.on('mouseover', state.groupTooltip.show)
                    //.on('mouseout', state.groupTooltip.hide)
                ;
                newGroups.append('title').text('click-drag to zoom in');
                groups = groups.merge(newGroups);
                groups.transition().duration(state.transDuration).attr('width', state.graphW).attr('height', function (d) {
                    return state.graphH * d.lines.length / state.nLines;
                }).attr('y', function (d) {
                    return state.grpScale(d.group) - state.graphH * d.lines.length / state.nLines / 2;
                });
            }

            function renderTimelines(maxElems) {
                if (maxElems < 0) maxElems = null;
                var hoverEnlargeRatio = .4;

                var dataFilter = function dataFilter(d, i) {
                    return (maxElems == null || i < maxElems) && state.grpScale.domain().indexOf(d.group) + 1 && d.timeRange[1] >= state.xScale.domain()[0] && d.timeRange[0] <= state.xScale.domain()[1] && state.yScale.domain().indexOf(d.group + '+&+' + d.label) + 1;
                };

                state.lineHeight = state.graphH / state.nLines * 0.8;
                var timelines = state.graph.selectAll('rect.series-segment').data(state.flatData.filter(dataFilter), function (d) {
                    return d.group + d.label + d.timeRange[0];
                });
                timelines.exit().transition().duration(state.transDuration).style('fill-opacity', 0).remove();
                var newSegments = timelines.enter().append('rect').attr('class', 'series-segment').attr('rx', 1).attr('ry', 1).attr('x', state.graphW / 2).attr('y', state.graphH / 2).attr('width', 0).attr('height', 0).style('fill', function (d) {
                    return state.zColorScale(d.val);
                }).style('fill-opacity', 0)
                //.on('mouseover.groupTooltip', state.groupTooltip.show)
                //.on('mouseout.groupTooltip', state.groupTooltip.hide)
                    .on('mouseover.lineTooltip', state.lineTooltip.show)
                    .on('mouseout.lineTooltip', state.lineTooltip.hide)
                    .on('mouseover.segmentTooltip', function(e) {
                        state.plotInterface.showTooltipSimple(e['label']);
                        state.segmentTooltip.show(e)
                    })
                    .on('mouseout.segmentTooltip', function(e) {
                        state.plotInterface.tooltip.transition().style('opacity', 0);
                        state.segmentTooltip.hide(e)
                    })
                    .on('click.segmentTooltip', function(e) {
                        //console.log(e); alert(state.plotInterface)
                        console.log('AAA')
                        console.log(e);
                        state.plotInterface.handleSearchTerm(e['label'], true)
                    })
                ;
                newSegments.on('mouseover', function () {
                    if ('disableHover' in state && state.disableHover) return;
                    moveToFront()(this);
                    var hoverEnlarge = state.lineHeight * hoverEnlargeRatio;
                    d3Select(this).transition().duration(70).attr('x', function (d) {
                        return state.xScale(d.timeRange[0]) - hoverEnlarge / 2;
                    }).attr('width', function (d) {
                        return max([1, state.xScale(d.timeRange[1]) - state.xScale(d.timeRange[0])]) + hoverEnlarge;
                    }).attr('y', function (d) {
                        return state.yScale(d.group + '+&+' + d.label) - (state.lineHeight + hoverEnlarge) / 2;
                    }).attr('height', state.lineHeight + hoverEnlarge).style('fill-opacity', 1);
                }).on('mouseout', function () {
                    d3Select(this).transition().duration(250).attr('x', function (d) {
                        return state.xScale(d.timeRange[0]);
                    }).attr('width', function (d) {
                        return max([1, state.xScale(d.timeRange[1]) - state.xScale(d.timeRange[0])]);
                    }).attr('y', function (d) {
                        return state.yScale(d.group + '+&+' + d.label) - state.lineHeight / 2;
                    }).attr('height', state.lineHeight).style('fill-opacity', .8);
                }).on('click', function (s) {
                    if (state.onSegmentClick) state.onSegmentClick(s);
                });
                timelines = timelines.merge(newSegments);
                timelines.transition().duration(state.transDuration).attr('x', function (d) {
                    return state.xScale(d.timeRange[0]);
                }).attr('width', function (d) {
                    return max([1, state.xScale(d.timeRange[1]) - state.xScale(d.timeRange[0])]);
                }).attr('y', function (d) {
                    return state.yScale(d.group + '+&+' + d.label) - state.lineHeight / 2;
                }).attr('height', state.lineHeight).style('fill-opacity', .8);
            }
        }
    });

    return timelines;

})));
//# sourceMappingURL=timelines-chart.js.map
