2 var nv = window.nv || {};
5 nv.version = '1.1.13b';
6 nv.dev = true //set false when in production
10 nv.tooltip = {}; // For the tooltip system
11 nv.utils = nv.utils || {}; // Utility subsystem
12 nv.models = {}; //stores all the possible models/components
13 nv.charts = {}; //stores all the ready to use charts
14 nv.graphs = []; //stores all the graphs currently on the page
15 nv.logs = {}; //stores some statistics and potential error messages
17 nv.dispatch = d3.dispatch('render_start', 'render_end');
19 // *************************************************************************
20 // Development render timers - disabled if dev = false
23 nv.dispatch.on('render_start', function(e) {
24 nv.logs.startTime = +new Date();
27 nv.dispatch.on('render_end', function(e) {
28 nv.logs.endTime = +new Date();
29 nv.logs.totalTime = nv.logs.endTime - nv.logs.startTime;
30 nv.log('total', nv.logs.totalTime); // used for development, to keep track of graph generation times
34 // ********************************************
35 // Public Core NV functions
37 // Logs all arguments, and returns the last so you can test things in place
38 // Note: in IE8 console.log is an object not a function, and if modernizr is used
39 // then calling Function.prototype.bind with with anything other than a function
40 // causes a TypeError to be thrown.
42 if (nv.dev && console.log && console.log.apply)
43 console.log.apply(console, arguments)
44 else if (nv.dev && typeof console.log == "function" && Function.prototype.bind) {
45 var log = Function.prototype.bind.call(console.log, console);
46 log.apply(console, arguments);
48 return arguments[arguments.length - 1];
52 nv.render = function render(step) {
53 step = step || 1; // number of graphs to generate in each timeout loop
55 nv.render.active = true;
56 nv.dispatch.render_start();
58 setTimeout(function() {
61 for (var i = 0; i < step && (graph = nv.render.queue[i]); i++) {
62 chart = graph.generate();
63 if (typeof graph.callback == typeof(Function)) graph.callback(chart);
64 nv.graphs.push(chart);
67 nv.render.queue.splice(0, i);
69 if (nv.render.queue.length) setTimeout(arguments.callee, 0);
70 else { nv.render.active = false; nv.dispatch.render_end(); }
74 nv.render.active = false;
77 nv.addGraph = function(obj) {
78 if (typeof arguments[0] === typeof(Function))
79 obj = {generate: arguments[0], callback: arguments[1]};
81 nv.render.queue.push(obj);
83 if (!nv.render.active) nv.render();
86 nv.identity = function(d) { return d; };
88 nv.strip = function(s) { return s.replace(/(\s|&)/g,''); };
90 function daysInMonth(month,year) {
91 return (new Date(year, month+1, 0)).getDate();
94 function d3_time_range(floor, step, number) {
95 return function(t0, t1, dt) {
96 var time = floor(t0), times = [];
97 if (time < t0) step(time);
100 var date = new Date(+time);
101 if ((number(date) % dt === 0)) times.push(date);
105 while (time < t1) { times.push(new Date(+time)); step(time); }
111 d3.time.monthEnd = function(date) {
112 return new Date(date.getFullYear(), date.getMonth(), 0);
115 d3.time.monthEnds = d3_time_range(d3.time.monthEnd, function(date) {
116 date.setUTCDate(date.getUTCDate() + 1);
117 date.setDate(daysInMonth(date.getMonth() + 1, date.getFullYear()));
119 return date.getMonth();