2012-10-18 18:27:01 +00:00
|
|
|
|
|
|
|
var nv = window.nv || {};
|
|
|
|
|
2013-07-03 19:33:34 +00:00
|
|
|
nv.version = '1.0.0b';
|
2012-10-18 18:27:01 +00:00
|
|
|
nv.dev = true //set false when in production
|
2012-04-05 09:18:21 +00:00
|
|
|
|
2012-03-20 23:43:21 +00:00
|
|
|
window.nv = nv;
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
nv.tooltip = {}; // For the tooltip system
|
|
|
|
nv.utils = {}; // Utility subsystem
|
2012-04-05 05:24:39 +00:00
|
|
|
nv.models = {}; //stores all the possible models/components
|
|
|
|
nv.charts = {}; //stores all the ready to use charts
|
|
|
|
nv.graphs = []; //stores all the graphs currently on the page
|
2012-07-15 06:00:56 +00:00
|
|
|
nv.logs = {}; //stores some statistics and potential error messages
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-04-05 09:18:21 +00:00
|
|
|
nv.dispatch = d3.dispatch('render_start', 'render_end');
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-07-24 04:17:53 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// Development render timers - disabled if dev = false
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-07-24 04:17:53 +00:00
|
|
|
if (nv.dev) {
|
|
|
|
nv.dispatch.on('render_start', function(e) {
|
2012-07-24 18:28:35 +00:00
|
|
|
nv.logs.startTime = +new Date();
|
2012-07-24 04:17:53 +00:00
|
|
|
});
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2012-07-24 04:17:53 +00:00
|
|
|
nv.dispatch.on('render_end', function(e) {
|
2012-07-24 18:28:35 +00:00
|
|
|
nv.logs.endTime = +new Date();
|
|
|
|
nv.logs.totalTime = nv.logs.endTime - nv.logs.startTime;
|
|
|
|
nv.log('total', nv.logs.totalTime); // used for development, to keep track of graph generation times
|
2012-07-24 04:17:53 +00:00
|
|
|
});
|
|
|
|
}
|
2012-03-20 23:43:21 +00:00
|
|
|
|
|
|
|
// ********************************************
|
|
|
|
// Public Core NV functions
|
|
|
|
|
2012-07-15 06:00:56 +00:00
|
|
|
// Logs all arguments, and returns the last so you can test things in place
|
|
|
|
nv.log = function() {
|
2012-12-03 23:11:49 +00:00
|
|
|
if (nv.dev && console.log && console.log.apply)
|
|
|
|
console.log.apply(console, arguments)
|
|
|
|
else if (nv.dev && console.log && Function.prototype.bind) {
|
|
|
|
var log = Function.prototype.bind.call(console.log, console);
|
|
|
|
log.apply(console, arguments);
|
|
|
|
}
|
2012-07-15 06:00:56 +00:00
|
|
|
return arguments[arguments.length - 1];
|
2012-07-24 04:17:53 +00:00
|
|
|
};
|
2012-07-15 06:00:56 +00:00
|
|
|
|
|
|
|
|
2012-04-05 09:18:21 +00:00
|
|
|
nv.render = function render(step) {
|
2013-01-27 02:07:12 +00:00
|
|
|
step = step || 1; // number of graphs to generate in each timeout loop
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2013-01-30 06:54:28 +00:00
|
|
|
nv.render.active = true;
|
2012-03-20 23:43:21 +00:00
|
|
|
nv.dispatch.render_start();
|
|
|
|
|
2012-07-24 04:17:53 +00:00
|
|
|
setTimeout(function() {
|
2012-12-09 12:09:54 +00:00
|
|
|
var chart, graph;
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2013-01-30 06:54:28 +00:00
|
|
|
for (var i = 0; i < step && (graph = nv.render.queue[i]); i++) {
|
2012-03-20 23:43:21 +00:00
|
|
|
chart = graph.generate();
|
2012-07-24 04:17:53 +00:00
|
|
|
if (typeof graph.callback == typeof(Function)) graph.callback(chart);
|
2012-03-20 23:43:21 +00:00
|
|
|
nv.graphs.push(chart);
|
|
|
|
}
|
|
|
|
|
2013-01-30 06:54:28 +00:00
|
|
|
nv.render.queue.splice(0, i);
|
2012-03-20 23:43:21 +00:00
|
|
|
|
2013-01-30 06:54:28 +00:00
|
|
|
if (nv.render.queue.length) setTimeout(arguments.callee, 0);
|
2012-07-24 04:17:53 +00:00
|
|
|
else { nv.render.active = false; nv.dispatch.render_end(); }
|
2012-03-20 23:43:21 +00:00
|
|
|
}, 0);
|
|
|
|
};
|
2012-07-24 04:17:53 +00:00
|
|
|
|
2012-09-17 20:06:47 +00:00
|
|
|
nv.render.active = false;
|
2012-03-20 23:43:21 +00:00
|
|
|
nv.render.queue = [];
|
|
|
|
|
|
|
|
nv.addGraph = function(obj) {
|
2012-07-24 04:17:53 +00:00
|
|
|
if (typeof arguments[0] === typeof(Function))
|
2012-03-20 23:43:21 +00:00
|
|
|
obj = {generate: arguments[0], callback: arguments[1]};
|
|
|
|
|
|
|
|
nv.render.queue.push(obj);
|
|
|
|
|
|
|
|
if (!nv.render.active) nv.render();
|
|
|
|
};
|
|
|
|
|
2012-07-24 04:17:53 +00:00
|
|
|
nv.identity = function(d) { return d; };
|
2012-04-05 09:18:21 +00:00
|
|
|
|
2012-06-28 07:00:35 +00:00
|
|
|
nv.strip = function(s) { return s.replace(/(\s|&)/g,''); };
|
2012-03-27 20:55:15 +00:00
|
|
|
|
|
|
|
function daysInMonth(month,year) {
|
2012-06-28 06:35:02 +00:00
|
|
|
return (new Date(year, month+1, 0)).getDate();
|
2012-07-24 04:17:53 +00:00
|
|
|
}
|
2012-03-27 20:55:15 +00:00
|
|
|
|
|
|
|
function d3_time_range(floor, step, number) {
|
|
|
|
return function(t0, t1, dt) {
|
|
|
|
var time = floor(t0), times = [];
|
|
|
|
if (time < t0) step(time);
|
|
|
|
if (dt > 1) {
|
|
|
|
while (time < t1) {
|
|
|
|
var date = new Date(+time);
|
2012-07-24 04:17:53 +00:00
|
|
|
if ((number(date) % dt === 0)) times.push(date);
|
2012-03-27 20:55:15 +00:00
|
|
|
step(time);
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-24 04:17:53 +00:00
|
|
|
while (time < t1) { times.push(new Date(+time)); step(time); }
|
2012-03-27 20:55:15 +00:00
|
|
|
}
|
|
|
|
return times;
|
|
|
|
};
|
2012-07-24 04:17:53 +00:00
|
|
|
}
|
2012-03-27 20:55:15 +00:00
|
|
|
|
|
|
|
d3.time.monthEnd = function(date) {
|
|
|
|
return new Date(date.getFullYear(), date.getMonth(), 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
d3.time.monthEnds = d3_time_range(d3.time.monthEnd, function(date) {
|
|
|
|
date.setUTCDate(date.getUTCDate() + 1);
|
|
|
|
date.setDate(daysInMonth(date.getMonth() + 1, date.getFullYear()));
|
|
|
|
}, function(date) {
|
|
|
|
return date.getMonth();
|
|
|
|
}
|
|
|
|
);
|
2012-07-24 04:17:53 +00:00
|
|
|
|