2 nv.utils.windowSize = function() {
4 var size = {width: 640, height: 480};
6 // Earlier IE uses Doc.body
7 if (document.body && document.body.offsetWidth) {
8 size.width = document.body.offsetWidth;
9 size.height = document.body.offsetHeight;
12 // IE can use depending on mode it is in
13 if (document.compatMode=='CSS1Compat' &&
14 document.documentElement &&
15 document.documentElement.offsetWidth ) {
16 size.width = document.documentElement.offsetWidth;
17 size.height = document.documentElement.offsetHeight;
20 // Most recent browsers use
21 if (window.innerWidth && window.innerHeight) {
22 size.width = window.innerWidth;
23 size.height = window.innerHeight;
30 // Easy way to bind multiple functions to window.onresize
31 // TODO: give a way to remove a function after its bound, other than removing all of them
32 nv.utils.windowResize = function(fun){
33 if (fun === undefined) return;
34 var oldresize = window.onresize;
36 window.onresize = function(e) {
37 if (typeof oldresize == 'function') oldresize(e);
42 // Backwards compatible way to implement more d3-like coloring of graphs.
43 // If passed an array, wrap it in a function which implements the old default
45 nv.utils.getColor = function(color) {
46 if (!arguments.length) return nv.utils.defaultColor(); //if you pass in nothing, get default colors back
48 if( Object.prototype.toString.call( color ) === '[object Array]' )
49 return function(d, i) { return d.color || color[i % color.length]; };
52 //can't really help it if someone passes rubbish as color
55 // Default color chooser uses the index of an object as before.
56 nv.utils.defaultColor = function() {
57 var colors = d3.scale.category20().range();
58 return function(d, i) { return d.color || colors[i % colors.length] };
62 // Returns a color function that takes the result of 'getKey' for each series and
63 // looks for a corresponding color from the dictionary,
64 nv.utils.customTheme = function(dictionary, getKey, defaultColors) {
65 getKey = getKey || function(series) { return series.key }; // use default series.key if getKey is undefined
66 defaultColors = defaultColors || d3.scale.category20().range(); //default color function
68 var defIndex = defaultColors.length; //current default color (going in reverse)
70 return function(series, index) {
71 var key = getKey(series);
73 if (!defIndex) defIndex = defaultColors.length; //used all the default colors, start over
75 if (typeof dictionary[key] !== "undefined")
76 return (typeof dictionary[key] === "function") ? dictionary[key]() : dictionary[key];
78 return defaultColors[--defIndex]; // no match in dictionary, use default color
84 // From the PJAX example on d3js.org, while this is not really directly needed
85 // it's a very cool method for doing pjax, I may expand upon it a little bit,
86 // open to suggestions on anything that may be useful
87 nv.utils.pjax = function(links, content) {
88 d3.selectAll(links).on("click", function() {
89 history.pushState(this.href, this.textContent, this.href);
91 d3.event.preventDefault();
95 d3.html(href, function(fragment) {
96 var target = d3.select(content).node();
97 target.parentNode.replaceChild(d3.select(fragment).select(content).node(), target);
98 nv.utils.pjax(links, content);
102 d3.select(window).on("popstate", function() {
103 if (d3.event.state) load(d3.event.state);
107 /* For situations where we want to approximate the width in pixels for an SVG:text element.
108 Most common instance is when the element is in a display:none; container.
109 Forumla is : text.length * font-size * constant_factor
111 nv.utils.calcApproxTextWidth = function (svgTextElem) {
112 if (svgTextElem instanceof d3.selection) {
113 var fontSize = parseInt(svgTextElem.style("font-size").replace("px",""));
114 var textLength = svgTextElem.text().length;
116 return textLength * fontSize * 0.5;
121 /* Numbers that are undefined, null or NaN, convert them to zeros.
123 nv.utils.NaNtoZero = function(n) {
124 if (typeof n !== 'number'
127 || n === Infinity) return 0;
133 Snippet of code you can insert into each nv.models.* to give you the ability to
140 To enable in the chart:
141 chart.options = nv.utils.optionsFunc.bind(chart);
143 nv.utils.optionsFunc = function(args) {
145 d3.map(args).forEach((function(key,value) {
146 if (typeof this[key] === "function") {