1
|
|
|
/** |
2
|
|
|
* Utility functions. |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
jsondash = jsondash || {util: {}}; |
|
|
|
|
6
|
|
|
|
7
|
|
|
jsondash.util.serializeToJSON = function(arr) { |
8
|
|
|
// Convert form data to a proper json value |
9
|
|
|
var json = {}; |
10
|
|
|
$.each(arr, function(_, pair){ |
11
|
|
|
json[pair.name] = pair.value; |
12
|
|
|
}); |
13
|
|
|
return json; |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
jsondash.util.isOverride = function(config) { |
17
|
|
|
return config.override && config.override === true; |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
// Credit: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript |
21
|
|
|
jsondash.util.s4 = function() { |
22
|
|
|
return Math.floor((1 + Math.random()) * 0x10000) |
23
|
|
|
.toString(16) |
24
|
|
|
.substring(1); |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
jsondash.util.guid = function() { |
28
|
|
|
var s4 = jsondash.util.s4; |
29
|
|
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + |
30
|
|
|
s4() + '-' + s4() + s4() + s4(); |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
jsondash.util.polygon = function(d) { |
34
|
|
|
return "M" + d.join("L") + "Z"; |
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
jsondash.util.scaleStr = function(x, y) { |
38
|
|
|
return 'scale(' + x + ',' + y + ')'; |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
jsondash.util.translateStr = function(x, y) { |
42
|
|
|
return 'translate(' + x + ',' + y + ')'; |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
jsondash.util.isD3Subtype = function(config) { |
46
|
|
|
// Handle specific D3 types that aren't necessarily referenced under |
47
|
|
|
// the D3 namespace in a select field. |
48
|
|
|
if(config.type === 'dendrogram') return true; |
|
|
|
|
49
|
|
|
if(config.type === 'voronoi') return true; |
|
|
|
|
50
|
|
|
if(config.type === 'circlepack') return true; |
|
|
|
|
51
|
|
|
if(config.type === 'treemap') return true; |
|
|
|
|
52
|
|
|
if(config.type === 'radial-dendrogram') return true; |
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
jsondash.util.isSparkline = function(type) { |
57
|
|
|
return type.substr(0, 10) === 'sparklines'; |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* [getDigitSize return a d3 scale for adjusting font size based |
62
|
|
|
* on digits and width of container.] |
63
|
|
|
*/ |
64
|
|
|
jsondash.util.getDigitSize = function() { |
65
|
|
|
var BOX_PADDING = 20; |
|
|
|
|
66
|
|
|
// scale value is reversed, since we want |
67
|
|
|
// the font-size to get smaller as the number gets longer. |
68
|
|
|
var scale = d3.scale.linear() |
|
|
|
|
69
|
|
|
.clamp(true) |
70
|
|
|
.domain([2, 14]) // min/max digits length: $0 - $999,999,999.00 |
71
|
|
|
.range([90, 30]); // max/min font-size |
72
|
|
|
window.scale = scale; |
73
|
|
|
return scale; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
77
|
|
|
module.exports = jsondash; |
78
|
|
|
} |
79
|
|
|
|