d3rrc
Version:
Reusable D3 Charts for React.js
1,644 lines (1,529 loc) • 401 kB
JavaScript
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "http://localhost:8090/";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
var Charts = {
ChartTitle: __webpack_require__(1).ChartTitle,
VBarChart: __webpack_require__(5).VBarChart,
HBarChart: __webpack_require__(5).HBarChart,
LineChart: __webpack_require__(7).LineChart,
PieChart: __webpack_require__(8).PieChart,
PieChart: __webpack_require__(8).PieChart,
TimeSeriesChart: __webpack_require__(9).TimeSeriesChart,
TimeSeries2Chart: __webpack_require__(10).TimeSeries2Chart,
ScatterChart: __webpack_require__(11).ScatterChart,
d3: __webpack_require__(6),
//React: require('react/addons')
};
module.exports = Charts;
if(typeof(window)==='object'){
window.D3RRC = Charts;
}
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
/** @jsx React.DOM */var React = __webpack_require__(2);
var Support = __webpack_require__(3);
var ChartTitle = React.createClass({displayName: "ChartTitle",
render: function(){
var classNames = Support.classList(this, {
'chart-title': true
});
var title = this.props.children;
return (
React.createElement("h2", React.__spread({}, props, {className: classNames}), title)
);
}
});
module.exports = {
ChartTitle: ChartTitle
};
/***/ },
/* 2 */
/***/ function(module, exports) {
module.exports = React;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
var React = __webpack_require__(2);
var classNames = __webpack_require__(4);
var types = {
Object: function(def){
return function(src, props, prefix, key){
var reMatchKey = new RegExp('^'+prefix+'-'+key+'-');
var res = {}, extended = false;
if(def){
Object.keys(def).forEach(function(key){
res[key]=def[key]();
});
}
if(src && typeof(src)==='object'){
Object.keys(src).forEach(function(key){
extended = true;
if(def[key]){
return res[key]=def[key](src[key]);
}
res[key]=src[key];
});
}
Object.keys(props).forEach(function(key){
var seg = key.split('-').slice(2).join('-');
if(seg){
extended = true;
val = props[key];
if(def[seg]){
return res[seg]=def[seg](val);
}
res[seg]=val;
}
});
return extended?res:(def?res:def);
};
},
Number:function(def){
return function(src){
if(!src){
return def;
}
return parseFloat(src);
};
},
Function:function(def){
return function(src){
if(!src){
return def;
}
return src;
};
},
Boolean:function(def){
return function(src){
if(typeof(src)==='undefined'){
return def;
}
return !!src;
};
},
String:function(def){
return function(src){
if(!src){
return def;
}
return ''+src;
};
},
};
module.exports = {
types: types,
el: function(src, sel){
if(!sel){
sel = src;
src = document;
}
return src.querySelector(sel);
},
els: function(src, sel){
if(!sel){
sel = src;
src = document;
}
return Array.prototype.slice.call(src.querySelectorAll(sel));
},
val: function(from){
return from.value||from.getAttribute('value')||from.innerText||from.innerHTML;
},
toHyphens: function(s){
return s.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
},
toCamelCase: function(s){
return s.toLowerCase().replace(/-(.)/g, function(match, group){
return group.toUpperCase();
});
},
paramByName: function(name, defaultValue) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? defaultValue : decodeURIComponent(results[1].replace(/\+/g, " "));
},
filterParams: function(filter, defaultValues){
var params = this.params(defaultValues),
results = {},
keys = Object.keys(params);
keys.forEach(function(key){
if(key.match(filter)){
results[key] = params[key];
}
});
return results;
},
param: function(val, defaultValue){
var result = defaultValue,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function(item){
tmp = item.split("=");
if (tmp[0] === val){
result = decodeURIComponent(tmp[1].replace(/\+/g, " "));
}
});
return result;
},
reTrue: /^(true|t|yes|y|1)$/i,
reFalse: /^(false|f|no|n|0)$/i,
isTrue: function(value){
return !!this.reTrue.exec(''+value);
},
isFalse: function(value){
return !!this.reFalse.exec(''+value);
},
isNumeric: function(n){
return !isNaN(parseFloat(n)) && isFinite(n);
},
isFraction: function(n){
return n.match(/^(\d+\W\d+\/\d+|\d+\/\d+)$/);
},
decodeValue: function(value){
if(this.isNumeric(value)){
return +value;
}else if(this.isTrue(value)){
return true;
}else if(this.isFalse(value)){
return false;
}else{
return value;
}
},
hashParams: function(defaults){
var q = location.hash.split('?'),
d = function(s){
return decodeURIComponent(s.replace(/\+/g, " "));
},
r = /([^&=]+)=?([^&]*)/g,
r2= /([^&=\[]+)\[(.*)\]/,
r3= /([^&=\[]+)\]\[(.+)/,
urlParams = {},
key, value, e, e2, elem;
q.shift();
q = q.join('?');
for(key in (defaults || {})){
urlParams[key] = defaults[key];
}
while (e = r.exec(q)) {
if(e[1].indexOf("[") === -1){
// simple match, no [] identifiers
urlParams[d(e[1])] = d(e[2]);
}else{
value = e[2];
key = e[1];
elem = urlParams;
if(key.indexOf('][')===-1){
while(e2=r2.exec(key)){
key = e2[1];
elem = elem[key] = elem[key] || {};
key = e2[2];
}
}else{
key = key.replace(/\]$/i, '');
while(e2 = r3.exec(key)){
key = e2[1];
elem = elem[key] = elem[key] || {};
if(e2[2]){
key = e2[2];
}
}
}
if(!key){
key = elem.length = elem.length||0;
elem.length++;
}
elem[key] = value;
}
}
return urlParams;
},
params: function(defaults){
return this.parseParams(window.location.search.replace(/^\?/,''), true, defaults);
},
parseParams: function(paramStr, decodeValues, defaults){
var self = this,
q = paramStr||'',
d = function (s) {
var value = decodeURIComponent(s.replace(/\+/g, " "));
if(decodeValues){
value = self.decodeValue(value);
}
return value;
},
r = /([^&=]+)=?([^&]*)/g,
r2= /([^&=\[]+)\[(.*)\]/,
r3= /([^&=\[]+)\]\[(.+)/,
urlParams = {},
key, value, e, e2, elem;
if(typeof(decodeValues)==='object'){
defaults = decodeValues;
decodeValues = false;
}
for(key in (defaults || {})){
urlParams[key] = defaults[key];
}
while (e = r.exec(q)) {
if(e[1].indexOf("[") === -1){
// simple match, no [] identifiers
urlParams[d(e[1])] = d(e[2]);
}else{
value = e[2];
key = e[1];
elem = urlParams;
if(key.indexOf('][')===-1){
while(e2=r2.exec(key)){
key = e2[1];
elem = elem[key] = elem[key] || {};
key = e2[2];
}
}else{
key = key.replace(/\]$/i, '');
while(e2 = r3.exec(key)){
key = e2[1];
elem = elem[key] = elem[key] || {};
if(e2[2]){
key = e2[2];
}
}
}
if(!key){
key = elem.length = elem.length||0;
elem.length++;
}
elem[key] = value;
}
}
return urlParams;
},
pkg: function(from){
var result = {};
from.forEach(function(e){
result[e.getAttribute('name')] = val(e);
});
return result;
},
classList: function(component, defaults){
var result = defaults;
var keys = (component.props.className||'').split(/[ \t]+/g);
keys.forEach(function(key){
result[key]=true;
});
return classNames(result);
},
getAttrs: function(props, ex){
var keys = Object.keys(props||{});
var exclude = ['children'].concat(ex || []);
var res = {};
keys.forEach(function(key){
if(exclude.indexOf(key)===-1){
res[key] = props[key];
}
});
return res;
},
getProps: function(component, prefix, handlers){
var res = {}, keys = Object.keys(handlers);
keys.forEach(function(key){
try{
res[key] = handlers[key](component.props[prefix+'-'+key], component.props, prefix, key);
}catch(e){
console.error('ERROR with Props key '+key);
console.error(e.stack||e);
throw e;
}
});
return res;
},
};
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_RESULT__;/*!
Copyright (c) 2015 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
(function () {
'use strict';
function classNames () {
var classes = '';
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!arg) continue;
var argType = typeof arg;
if ('string' === argType || 'number' === argType) {
classes += ' ' + arg;
} else if (Array.isArray(arg)) {
classes += ' ' + classNames.apply(null, arg);
} else if ('object' === argType) {
for (var key in arg) {
if (arg.hasOwnProperty(key) && arg[key]) {
classes += ' ' + key;
}
}
}
}
return classes.substr(1);
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = classNames;
} else if (true){
// AMD. Register as an anonymous module.
!(__WEBPACK_AMD_DEFINE_RESULT__ = function () {
return classNames;
}.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
} else {
window.classNames = classNames;
}
}());
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
/** @jsx React.DOM */var React = __webpack_require__(2);
var Support = __webpack_require__(3);
var d3 = __webpack_require__(6);
var VBarChart = React.createClass({displayName: "VBarChart",
chart: function (selection) {
var $__0=
Support.getProps(this, 'chart', {
margin: Support.types.Object({
top: Support.types.Number(10),
right: Support.types.Number(10),
bottom: Support.types.Number(20),
left: Support.types.Number(50)
}),
width: Support.types.Number(-1),
height: Support.types.Number(420),
xRoundBands: Support.types.Number(0.2),
names: Support.types.Function(function (d) {
return d[0];
}),
values: Support.types.Function(function (d) {
return d[1];
}),
duration: Support.types.Number(500),
color: Support.types.Function(false),
enterBar: Support.types.Function(function (bar) {
bar = bar.append('rect')
.attr('class', function (d, i) {
return values(d) < 0 ? 'negative' : 'positive';
});
if (color) {
bar.style('fill', function (d) {
return color(names(d));
});
}
}),
updateBar: Support.types.Function(function (bar) {
bar.select('rect')
.attr('class', function (d) {
return values(d) < 0 ? 'negative' : 'positive';
})
.attr('x', function (d) {
return X(d);
})
.attr('y', function (d) {
return values(d) < 0 ? Y0() : Y(d);
})
.attr('width', xScale.rangeBand())
.attr('height', function (d, i) {
return Math.abs(Y(d) - Y0());
});
}),
exitBar: Support.types.Function(function (bar) {}),
style: Support.types.Object(false)
}),margin=$__0.margin,width=$__0.width,height=$__0.height,xRoundBands=$__0.xRoundBands,names=$__0.names,values=$__0.values,xAxisBottom=$__0.xAxisBottom,duration=$__0.duration,color=$__0.color,enterBar=$__0.enterBar,updateBar=$__0.updateBar,exitBar=$__0.exitBar,style=$__0.style;
var X = function (d) {
return xScale(names(d));
};
var Y0 = function () {
return yScale(0);
};
var Y = function (d) {
return yScale(values(d));
};
var scaleO = xScale = d3.scale.ordinal();
var scaleL = yScale = d3.scale.linear();
var yAxis = d3.svg.axis().scale(scaleL).orient('left');
var xAxis = d3.svg.axis().scale(scaleO);
var xAxisBottom = d3.svg.axis().scale(scaleO);
height = parseInt(height);
width = parseInt(width);
duration = parseInt(duration);
selection.each(function (data) {
var w = width === -1 ? this.offsetWidth : width;
// Update the x-scale.
xScale
.domain(data.map(names))
.rangeRoundBands([0, w - margin.left - margin.right], parseFloat(xRoundBands));
var ys = d3.extent(data.map(values));
if (ys[0] > 0) {
ys[0] = 0;
}
// Update the y-scale.
yScale
.domain(ys)
.range([height - margin.top - margin.bottom, 0])
.nice();
// Select the svg element, if it exists.
var svg = d3.select(this).selectAll('svg').data([data]);
// Otherwise, create the skeletal chart.
var gEnter = svg.enter().append('svg').append('g');
gEnter.append('g').attr('class', 'bars');
gEnter.append('g').attr('class', 'y axis');
gEnter.append('g').attr('class', 'x axis bottom');
gEnter.append('g').attr('class', 'x axis zero');
// Update the outer dimensions.
svg.attr('width', w)
.attr('height', height);
// Update the inner dimensions.
var g = svg.select('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Update the bars.
var bars = svg.select('.bars'); //.selectAll('.bar');//.data(data);
var bar = bars.selectAll('g.bar').data(data);
var barEnter = bar.enter()
.append('g')
.attr('class', 'bar');
enterBar(barEnter);
updateBar(bar.transition().duration(duration));
var barExit = bar.exit()
.remove();
exitBar(barExit);
// x axis at the bottom of the chart
g.select('.x.axis.bottom')
.attr('transform', 'translate(0,' + (height - margin.top - margin.bottom) + ')')
.call(xAxisBottom.orient('bottom'));
// zero line
g.select('.x.axis.zero')
.attr('transform', 'translate(0,' + Y0() + ')')
.call(xAxis.tickFormat('').tickSize(0));
// Update the y-axis.
g.select('.y.axis')
.call(yAxis);
g.selectAll('.axis line')
.attr('style', 'shape-rendering: crispEdges; stroke: black; fill: none;');
g.selectAll('.axis path')
.attr('style', 'shape-rendering: crispEdges; stroke: black; fill: none;');
if (style) {
Object.keys(style).forEach(function (key) {
svg.selectAll(key).attr('style', style[key]);
});
}
});
},
renderChart: function (data) {
if (!data) {
return;
}
var container = this.getDOMNode();
d3.select(container)
.datum(data)
.call(this.chart);
},
componentDidMount: function () {
return this.renderChart(this.props.data);
},
componentDidUpdate: function () {
return this.renderChart(this.props.data);
},
render: function () {
var classNames = Support.classList(this, {
'bar-chart': true
});
return React.createElement("div", {className: classNames});
}
});
var HBarChart = React.createClass({displayName: "HBarChart",
chart: function (selection) {
var $__0=
Support.getProps(this, 'chart', {
margin: Support.types.Object({
top: Support.types.Number(0),
right: Support.types.Number(10),
bottom: Support.types.Number(20),
left: Support.types.Number(50)
}),
width: Support.types.Number(-1),
height: Support.types.Number(420),
names: Support.types.Function(function (d) {
return d[0];
}),
values: Support.types.Function(function (d) {
return d[1];
}),
duration: Support.types.Number(500),
color: Support.types.Function(d3.scale.category10()),
enterBar: Support.types.Function(function (bar) {
bar = bar.append('rect')
.attr('class', 'bar');
if (color) {
bar.style('fill', function (d) {
return color(names(d));
});
}
}),
updateBar: Support.types.Function(function (bar) {
bar.select('rect')
.attr('x', function (d) {
return margin.left;
})
.attr('y', function (d) {
return Y(d);
})
.attr('width', function (d) {
return X(d);
})
.attr('height', yScale.rangeBand());
}),
exitBar: Support.types.Function(function (bar) {}),
style: Support.types.Object(false)
}),margin=$__0.margin,width=$__0.width,height=$__0.height,names=$__0.names,values=$__0.values,duration=$__0.duration,color=$__0.color,enterBar=$__0.enterBar,updateBar=$__0.updateBar,exitBar=$__0.exitBar,style=$__0.style;
var xScale = d3.scale.linear();
var yScale = d3.scale.ordinal();
var X = function (d) {
return xScale(values(d));
};
var Y = function (d) {
return yScale(names(d)) + margin.top;
};
selection.each(function (data) {
var w = width === -1 ? this.offsetWidth : width;
var maxChartHeight = height - margin.top - margin.bottom;
if (color && color.domain) {
var cnames = data.map(names);
color.domain(cnames);
}
var svg = d3.select(this).selectAll('svg').data([data]);
svg.enter().append('svg')
.attr('width', w)
.attr('height', height);
var maxValue = d3.max(data, values);
yScale.domain(data.map(names));
xScale.range([0, w - margin.left - margin.right]).domain([0, maxValue]).nice();
yScale.rangeBands([0, maxChartHeight], 0.1, 0.25);
var yAxis = d3.svg.axis().scale(yScale).orient('left');
var xAxis = d3.svg.axis().scale(xScale).orient('bottom');
var bars = svg.selectAll('.bar')
.data(data);
var barEnter = bars.enter()
.append('g')
.attr('class', 'bar');
enterBar(barEnter);
updateBar(bars.transition().duration(duration));
var barExit = bars.exit()
.remove();
exitBar(barExit);
//append x axis
svg.append("g")
.attr("transform", "translate(" + margin.left + ", " + (maxChartHeight + margin.top) + ")")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.style("stroke", "black")
.style("fill", "none")
.style("stroke-width", 1)
.style("shape-rendering", "crispEdges")
.call(xAxis)
.selectAll("text")
.attr("stroke", "none")
.attr("fill", "black");
//append y axis
svg.append("g")
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.style("stroke", "black")
.style("fill", "none")
.style("stroke-width", 1)
.style("shape-rendering", "crispEdges")
.call(yAxis)
.selectAll("text")
.attr("stroke", "none")
.attr("fill", "black");
if (style) {
Object.keys(style).forEach(function (key) {
svg.selectAll(key).attr('style', style[key]);
});
}
});
},
renderChart: function (data) {
if (!data) {
return;
}
var container = this.getDOMNode();
d3.select(container)
.datum(data)
.call(this.chart);
},
componentDidMount: function () {
return this.renderChart(this.props.data);
},
componentDidUpdate: function () {
return this.renderChart(this.props.data);
},
componentWillReceiveProps: function(props) {
console.log('got new props');
return this.renderChart(props.data);
},
render: function () {
var classNames = Support.classList(this, {
'bar-chart': true
});
return React.createElement("div", {className: classNames});
}
});
module.exports={
VBarChart: VBarChart,
HBarChart: HBarChart
};
/***/ },
/* 6 */
/***/ function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;!function() {
var d3 = {
version: "3.5.6"
};
var d3_arraySlice = [].slice, d3_array = function(list) {
return d3_arraySlice.call(list);
};
var d3_document = this.document;
function d3_documentElement(node) {
return node && (node.ownerDocument || node.document || node).documentElement;
}
function d3_window(node) {
return node && (node.ownerDocument && node.ownerDocument.defaultView || node.document && node || node.defaultView);
}
if (d3_document) {
try {
d3_array(d3_document.documentElement.childNodes)[0].nodeType;
} catch (e) {
d3_array = function(list) {
var i = list.length, array = new Array(i);
while (i--) array[i] = list[i];
return array;
};
}
}
if (!Date.now) Date.now = function() {
return +new Date();
};
if (d3_document) {
try {
d3_document.createElement("DIV").style.setProperty("opacity", 0, "");
} catch (error) {
var d3_element_prototype = this.Element.prototype, d3_element_setAttribute = d3_element_prototype.setAttribute, d3_element_setAttributeNS = d3_element_prototype.setAttributeNS, d3_style_prototype = this.CSSStyleDeclaration.prototype, d3_style_setProperty = d3_style_prototype.setProperty;
d3_element_prototype.setAttribute = function(name, value) {
d3_element_setAttribute.call(this, name, value + "");
};
d3_element_prototype.setAttributeNS = function(space, local, value) {
d3_element_setAttributeNS.call(this, space, local, value + "");
};
d3_style_prototype.setProperty = function(name, value, priority) {
d3_style_setProperty.call(this, name, value + "", priority);
};
}
}
d3.ascending = d3_ascending;
function d3_ascending(a, b) {
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
}
d3.descending = function(a, b) {
return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
};
d3.min = function(array, f) {
var i = -1, n = array.length, a, b;
if (arguments.length === 1) {
while (++i < n) if ((b = array[i]) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
} else {
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
}
return a;
};
d3.max = function(array, f) {
var i = -1, n = array.length, a, b;
if (arguments.length === 1) {
while (++i < n) if ((b = array[i]) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
} else {
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
a = b;
break;
}
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
}
return a;
};
d3.extent = function(array, f) {
var i = -1, n = array.length, a, b, c;
if (arguments.length === 1) {
while (++i < n) if ((b = array[i]) != null && b >= b) {
a = c = b;
break;
}
while (++i < n) if ((b = array[i]) != null) {
if (a > b) a = b;
if (c < b) c = b;
}
} else {
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b >= b) {
a = c = b;
break;
}
while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
if (a > b) a = b;
if (c < b) c = b;
}
}
return [ a, c ];
};
function d3_number(x) {
return x === null ? NaN : +x;
}
function d3_numeric(x) {
return !isNaN(x);
}
d3.sum = function(array, f) {
var s = 0, n = array.length, a, i = -1;
if (arguments.length === 1) {
while (++i < n) if (d3_numeric(a = +array[i])) s += a;
} else {
while (++i < n) if (d3_numeric(a = +f.call(array, array[i], i))) s += a;
}
return s;
};
d3.mean = function(array, f) {
var s = 0, n = array.length, a, i = -1, j = n;
if (arguments.length === 1) {
while (++i < n) if (d3_numeric(a = d3_number(array[i]))) s += a; else --j;
} else {
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) s += a; else --j;
}
if (j) return s / j;
};
d3.quantile = function(values, p) {
var H = (values.length - 1) * p + 1, h = Math.floor(H), v = +values[h - 1], e = H - h;
return e ? v + e * (values[h] - v) : v;
};
d3.median = function(array, f) {
var numbers = [], n = array.length, a, i = -1;
if (arguments.length === 1) {
while (++i < n) if (d3_numeric(a = d3_number(array[i]))) numbers.push(a);
} else {
while (++i < n) if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) numbers.push(a);
}
if (numbers.length) return d3.quantile(numbers.sort(d3_ascending), .5);
};
d3.variance = function(array, f) {
var n = array.length, m = 0, a, d, s = 0, i = -1, j = 0;
if (arguments.length === 1) {
while (++i < n) {
if (d3_numeric(a = d3_number(array[i]))) {
d = a - m;
m += d / ++j;
s += d * (a - m);
}
}
} else {
while (++i < n) {
if (d3_numeric(a = d3_number(f.call(array, array[i], i)))) {
d = a - m;
m += d / ++j;
s += d * (a - m);
}
}
}
if (j > 1) return s / (j - 1);
};
d3.deviation = function() {
var v = d3.variance.apply(this, arguments);
return v ? Math.sqrt(v) : v;
};
function d3_bisector(compare) {
return {
left: function(a, x, lo, hi) {
if (arguments.length < 3) lo = 0;
if (arguments.length < 4) 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 (arguments.length < 3) lo = 0;
if (arguments.length < 4) 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;
}
};
}
var d3_bisect = d3_bisector(d3_ascending);
d3.bisectLeft = d3_bisect.left;
d3.bisect = d3.bisectRight = d3_bisect.right;
d3.bisector = function(f) {
return d3_bisector(f.length === 1 ? function(d, x) {
return d3_ascending(f(d), x);
} : f);
};
d3.shuffle = function(array, i0, i1) {
if ((m = arguments.length) < 3) {
i1 = array.length;
if (m < 2) i0 = 0;
}
var m = i1 - i0, t, i;
while (m) {
i = Math.random() * m-- | 0;
t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t;
}
return array;
};
d3.permute = function(array, indexes) {
var i = indexes.length, permutes = new Array(i);
while (i--) permutes[i] = array[indexes[i]];
return permutes;
};
d3.pairs = function(array) {
var i = 0, n = array.length - 1, p0, p1 = array[0], pairs = new Array(n < 0 ? 0 : n);
while (i < n) pairs[i] = [ p0 = p1, p1 = array[++i] ];
return pairs;
};
d3.zip = function() {
if (!(n = arguments.length)) return [];
for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m; ) {
for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n; ) {
zip[j] = arguments[j][i];
}
}
return zips;
};
function d3_zipLength(d) {
return d.length;
}
d3.transpose = function(matrix) {
return d3.zip.apply(d3, matrix);
};
d3.keys = function(map) {
var keys = [];
for (var key in map) keys.push(key);
return keys;
};
d3.values = function(map) {
var values = [];
for (var key in map) values.push(map[key]);
return values;
};
d3.entries = function(map) {
var entries = [];
for (var key in map) entries.push({
key: key,
value: map[key]
});
return entries;
};
d3.merge = function(arrays) {
var n = arrays.length, m, i = -1, j = 0, merged, array;
while (++i < n) j += arrays[i].length;
merged = new Array(j);
while (--n >= 0) {
array = arrays[n];
m = array.length;
while (--m >= 0) {
merged[--j] = array[m];
}
}
return merged;
};
var abs = Math.abs;
d3.range = function(start, stop, step) {
if (arguments.length < 3) {
step = 1;
if (arguments.length < 2) {
stop = start;
start = 0;
}
}
if ((stop - start) / step === Infinity) throw new Error("infinite range");
var range = [], k = d3_range_integerScale(abs(step)), i = -1, j;
start *= k, stop *= k, step *= k;
if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k); else while ((j = start + step * ++i) < stop) range.push(j / k);
return range;
};
function d3_range_integerScale(x) {
var k = 1;
while (x * k % 1) k *= 10;
return k;
}
function d3_class(ctor, properties) {
for (var key in properties) {
Object.defineProperty(ctor.prototype, key, {
value: properties[key],
enumerable: false
});
}
}
d3.map = function(object, f) {
var map = new d3_Map();
if (object instanceof d3_Map) {
object.forEach(function(key, value) {
map.set(key, value);
});
} else if (Array.isArray(object)) {
var i = -1, n = object.length, o;
if (arguments.length === 1) while (++i < n) map.set(i, object[i]); else while (++i < n) map.set(f.call(object, o = object[i], i), o);
} else {
for (var key in object) map.set(key, object[key]);
}
return map;
};
function d3_Map() {
this._ = Object.create(null);
}
var d3_map_proto = "__proto__", d3_map_zero = "\x00";
d3_class(d3_Map, {
has: d3_map_has,
get: function(key) {
return this._[d3_map_escape(key)];
},
set: function(key, value) {
return this._[d3_map_escape(key)] = value;
},
remove: d3_map_remove,
keys: d3_map_keys,
values: function() {
var values = [];
for (var key in this._) values.push(this._[key]);
return values;
},
entries: function() {
var entries = [];
for (var key in this._) entries.push({
key: d3_map_unescape(key),
value: this._[key]
});
return entries;
},
size: d3_map_size,
empty: d3_map_empty,
forEach: function(f) {
for (var key in this._) f.call(this, d3_map_unescape(key), this._[key]);
}
});
function d3_map_escape(key) {
return (key += "") === d3_map_proto || key[0] === d3_map_zero ? d3_map_zero + key : key;
}
function d3_map_unescape(key) {
return (key += "")[0] === d3_map_zero ? key.slice(1) : key;
}
function d3_map_has(key) {
return d3_map_escape(key) in this._;
}
function d3_map_remove(key) {
return (key = d3_map_escape(key)) in this._ && delete this._[key];
}
function d3_map_keys() {
var keys = [];
for (var key in this._) keys.push(d3_map_unescape(key));
return keys;
}
function d3_map_size() {
var size = 0;
for (var key in this._) ++size;
return size;
}
function d3_map_empty() {
for (var key in this._) return false;
return true;
}
d3.nest = function() {
var nest = {}, keys = [], sortKeys = [], sortValues, rollup;
function map(mapType, array, depth) {
if (depth >= keys.length) return rollup ? rollup.call(nest, array) : sortValues ? array.sort(sortValues) : array;
var i = -1, n = array.length, key = keys[depth++], keyValue, object, setter, valuesByKey = new d3_Map(), values;
while (++i < n) {
if (values = valuesByKey.get(keyValue = key(object = array[i]))) {
values.push(object);
} else {
valuesByKey.set(keyValue, [ object ]);
}
}
if (mapType) {
object = mapType();
setter = function(keyValue, values) {
object.set(keyValue, map(mapType, values, depth));
};
} else {
object = {};
setter = function(keyValue, values) {
object[keyValue] = map(mapType, values, depth);
};
}
valuesByKey.forEach(setter);
return object;
}
function entries(map, depth) {
if (depth >= keys.length) return map;
var array = [], sortKey = sortKeys[depth++];
map.forEach(function(key, keyMap) {
array.push({
key: key,
values: entries(keyMap, depth)
});
});
return sortKey ? array.sort(function(a, b) {
return sortKey(a.key, b.key);
}) : array;
}
nest.map = function(array, mapType) {
return map(mapType, array, 0);
};
nest.entries = function(array) {
return entries(map(d3.map, array, 0), 0);
};
nest.key = function(d) {
keys.push(d);
return nest;
};
nest.sortKeys = function(order) {
sortKeys[keys.length - 1] = order;
return nest;
};
nest.sortValues = function(order) {
sortValues = order;
return nest;
};
nest.rollup = function(f) {
rollup = f;
return nest;
};
return nest;
};
d3.set = function(array) {
var set = new d3_Set();
if (array) for (var i = 0, n = array.length; i < n; ++i) set.add(array[i]);
return set;
};
function d3_Set() {
this._ = Object.create(null);
}
d3_class(d3_Set, {
has: d3_map_has,
add: function(key) {
this._[d3_map_escape(key += "")] = true;
return key;
},
remove: d3_map_remove,
values: d3_map_keys,
size: d3_map_size,
empty: d3_map_empty,
forEach: function(f) {
for (var key in this._) f.call(this, d3_map_unescape(key));
}
});
d3.behavior = {};
function d3_identity(d) {
return d;
}
d3.rebind = function(target, source) {
var i = 1, n = arguments.length, method;
while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
return target;
};
function d3_rebind(target, source, method) {
return function() {
var value = method.apply(source, arguments);
return value === source ? target : value;
};
}
function d3_vendorSymbol(object, name) {
if (name in object) return name;
name = name.charAt(0).toUpperCase() + name.slice(1);
for (var i = 0, n = d3_vendorPrefixes.length; i < n; ++i) {
var prefixName = d3_vendorPrefixes[i] + name;
if (prefixName in object) return prefixName;
}
}
var d3_vendorPrefixes = [ "webkit", "ms", "moz", "Moz", "o", "O" ];
function d3_noop() {}
d3.dispatch = function() {
var dispatch = new d3_dispatch(), i = -1, n = arguments.length;
while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
return dispatch;
};
function d3_dispatch() {}
d3_dispatch.prototype.on = function(type, listener) {
var i = type.indexOf("."), name = "";
if (i >= 0) {
name = type.slice(i + 1);
type = type.slice(0, i);
}
if (type) return arguments.length < 2 ? this[type].on(name) : this[type].on(name, listener);
if (arguments.length === 2) {
if (listener == null) for (type in this) {
if (this.hasOwnProperty(type)) this[type].on(name, null);
}
return this;
}
};
function d3_dispatch_event(dispatch) {
var listeners = [], listenerByName = new d3_Map();
function event() {
var z = listeners, i = -1, n = z.length, l;
while (++i < n) if (l = z[i].on) l.apply(this, arguments);
return dispatch;
}
event.on = function(name, listener) {
var l = listenerByName.get(name), i;
if (arguments.length < 2) return l && l.on;
if (l) {
l.on = null;
listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
listenerByName.remove(name);
}
if (listener) listeners.push(listenerByName.set(name, {
on: listener
}));
return dispatch;
};
return event;
}
d3.event = null;
function d3_eventPreventDefault() {
d3.event.preventDefault();
}
function d3_eventSource() {
var e = d3.event, s;
while (s = e.sourceEvent) e = s;
return e;
}
function d3_eventDispatch(target) {
var dispatch = new d3_dispatch(), i = 0, n = arguments.length;
while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
dispatch.of = function(thiz, argumentz) {
return function(e1) {
try {
var e0 = e1.sourceEvent = d3.event;
e1.target = target;
d3.event = e1;
dispatch[e1.type].apply(thiz, argumentz);
} finally {
d3.event = e0;
}
};
};
return dispatch;
}
d3.requote = function(s) {
return s.replace(d3_requote_re, "\\$&");
};
var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
var d3_subclass = {}.__proto__ ? function(object, prototype) {
object.__proto__ = prototype;
} : function(object, prototype) {
for (var property in prototype) object[property] = prototype[property];
};
function d3_selection(groups) {
d3_subclass(groups, d3_selectionPrototype);
return groups;
}
var d3_select = function(s, n) {
return n.querySelector(s);
}, d3_selectAll = function(s, n) {
return n.querySelectorAll(s);
}, d3_selectMatches = function(n, s) {
var d3_selectMatcher = n.matches || n[d3_vendorSymbol(n, "matchesSelector")];
d3_selectMatches = function(n, s) {
return d3_selectMatcher.call(n, s);
};
return d3_selectMatches(n, s);
};
if (typeof Sizzle === "function") {
d3_select = function(s, n) {
return Sizzle(s, n)[0] || null;
};
d3_selectAll = Sizzle;
d3_selectMatches = Sizzle.matchesSelector;
}
d3.selection = function() {
return d3.select(d3_document.documentElement);
};
var d3_selectionPrototype = d3.selection.prototype = [];
d3_selectionPrototype.select = function(selector) {
var subgroups = [], subgroup, subnode, group, node;
selector = d3_selection_selector(selector);
for (var j = -1, m = this.length; ++j < m; ) {
subgroups.push(subgroup = []);
subgroup.parentNode = (group = this[j]).parentNode;
for (var i = -1, n = group.length; ++i < n; ) {
if (node = group[i]) {
subgroup.push(subnode = selector.call(node, node.__data__, i, j));
if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
} else {
subgroup.push(null);
}
}
}
return d3_selection(subgroups);
};
function d3_selection_selector(selector) {
return typeof selector === "function" ? selector : function() {
return d3_select(selector, this);
};
}
d3_selectionPrototype.selectAll = function(selector) {
var subgroups = [], subgroup, node;
selector = d3_selection_selectorAll(selector);
for (var j = -1, m = this.length; ++j < m; ) {
for (var group = this[j], i = -1, n = group.length; ++i < n; ) {
if (node = group[i]) {
subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j)));
subgroup.parentNode = node;
}
}
}
return d3_selection(subgroups);
};
function d3_selection_selectorAll(selector) {
return typeof selector === "function" ? selector : function() {
return d3_selectAll(selector, this);
};
}
var d3_nsPrefix = {
svg: "http://www.w3.org/2000/svg",
xhtml: "http://www.w3.org/1999/xhtml",
xlink: "http://www.w3.org/1999/xlink",
xml: "http://www.w3.org/XML/1998/namespace",
xmlns: "http://www.w3.org/2000/xmlns/"
};
d3.ns = {
prefix: d3_nsPrefix,
qualify: function(name) {
var i = name.indexOf(":"), prefix = name;
if (i >= 0) {
prefix = name.slice(0, i);
name = name.slice(i + 1);
}
return d3_nsPrefix.hasOwnProperty(prefix) ? {
space: d3_nsPrefix[prefix],
local: name
} : name;
}
};
d3_selectionPrototype.attr = function(name, value) {
if (arguments.length < 2) {
if (typeof name === "string") {
var node = this.node();
name = d3.ns.qualify(name);
return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name);
}
for (value in name) this.each(d3_selection_attr(value, name[value]));
return this;
}
return this.each(d3_selection_attr(name, value));
};
function d3_selection_attr(name, value) {
name = d3.ns.qualify(name);
function attrNull() {
this.removeAttribute(name);
}
function attrNullNS() {
this.removeAttributeNS(name.space, name.local);
}
function attrConstant() {
this.setAttribute(name, value);
}
function attrConstantNS() {
this.setAttributeNS(name.space, name.local, value);
}
function attrFunction() {
var x = value.apply(this, arguments);
if (x == null) this.removeAttribute(name); else this.setAttribute(name, x);
}
function attrFunctionNS() {
var x = value.apply(this, arguments);
if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x);
}
return value == null ? name.local ? attrNullNS : attrNull : typeof value === "function" ? name.local ? attrFunctionNS : attrFunction : name.local ? attrConstantNS : attrConstant;
}
function d3_collapse(s) {
return s.trim().replace(/\s+/g, " ");
}
d3_selectionPrototype.classed = function(name, value) {
if (arguments.length < 2) {
if (typeof name === "string") {
var node = this.node(), n = (name = d3_selection_classes(name)).length, i = -1;
if (value = node.classList) {
while (++i < n) if (!value.contains(name[i])) return false;
} else {
value = node.getAttribute("class");
while (++i < n) if (!d3_selection_classedRe(name[i]).test(value)) return false;
}
return true;
}
for (value in name) this.each(d3_selection_classed(value, name[value]));
return this;
}
return this.each(d3_selection_classed(name, value));
};
function d3_selection_classedRe(name) {
return new RegExp("(?:^|\\s+)" + d3.requote(name) + "(?:\\s+|$)", "g");
}
function d3_selection_classes(name) {
return (name + "").trim().split(/^|\s+/);
}
function d3_selection_classed(name, value) {
name = d3_selection_classes(name).map(d3_selection_classedName);
var n = name.length;
function classedConstant() {
var i = -1;
while (++i < n) name[i](this, value);
}
function classedFunction() {
var i = -1, x = value.apply(this, arguments);
while (++i < n) name[i](this, x);
}
return typeof value === "function" ? classedFunction : classedConstant;
}
function d3_selection_classedName(name) {
var re = d3_selection_classedRe(name);
return function(node, value) {
if (c = node.classList) return value ? c.add(name) : c.remove(name);
var c = node.getAttribute("class") || "";
if (value) {
re.lastIndex = 0;
if (!re.test(c)) node.setAttribute("class", d3_collapse(c + " " + name));
} else {
node.setAttribute("class", d3_collapse(c.replace(re, " ")));
}
};
}
d3_selectionPrototype.style = function(name, value, priority) {
var n = arguments.length;
if (n < 3) {
if (typeof name !== "string") {
if (n < 2) value = "";
for (priority in name) this.each(d3_selection_style(priority, name[priority], value));
return this;
}
if (n < 2) {
var node = this.node();
return d3_window(node).getComputedStyle(node, null).getPropertyValue(name);
}
priority = "";
}
return this.each(d3_selection_style(name, value, priority));
};
function d3_selection_style(name, value, priority) {
function styleNull() {
this.style.removeProperty(name);
}
function styleConstant() {
this.style.setProperty(name, value, priority);
}
function styleFunction() {
var x = value.apply(this, arguments);
if (x == null) this.style.removeProperty(name); else this.style.setProperty(name, x, priority);
}
return value == null ? styleNull : typeof value === "function" ? styleFunction : styleConstant;
}
d3_selectionPrototype.property = function(name, value) {
if (arguments.length < 2) {
if (typeof name === "string") return this.node()[name];
for (value in name) this.each(d3_selection_property(value, name[value]));
return this;
}
return this.each(d3_selection_property(name, value));
};
function d3_selection_property(name, value) {
function propertyNull() {
delete this[name];
}
function propertyConstant() {
this[name] = value;
}
function propertyFunction() {
var x = value.apply(this, arguments);
if (x == null) delete this[name]; else this[name] = x;
}
return value == null ? propertyNull : typeof value === "function" ? propertyFunction : propertyConstant;
}
d3_selectionPrototype.text = function(value) {
return arguments.length ? this.each(typeof value === "function" ? function() {
var v = value.apply(this, arguments);
this.textContent = v == null ? "" : v;
} : value == null ? function() {
this.textContent = "";
} : function() {
this.textContent = value;
}) : this.node().textContent;
};
d3_selectionPrototype.html = function(value) {
return arguments.length ? this.each(typeof value === "function" ? function() {
var v = value.apply(this, arguments);
this.innerHTML = v == null ? "" : v;
} : value == null ? function() {
this.innerHTML = "";
} : function() {
this.innerHTML = value;
}) : this.node().innerHTML;
};
d3_selectionPrototype.append = function(name) {
name = d3_selection_creator(name);
return this.select(function() {
return this.appendChild(name.apply(this, arguments));
});
};
fun