1 //Global utility functions 2 3 function $(_id) { 4 return document.getElementById(_id); 5 } 6 7 Array.prototype.max = function() { 8 var max = this[0]; 9 var len = this.length; 10 for (var i = 1; i < len; i++) if (this[i] > max) max = this[i]; 11 return max; 12 } 13 14 Array.prototype.min = function() { 15 var min = this[0]; 16 var len = this.length; 17 for (var i = 1; i < len; i++) if (this[i] < min) min = this[i]; 18 return min; 19 } 20 21 wso2vis.util.generateColors = function(count, scheme) { 22 function hexNumtoHexStr(n) { 23 function toHexStr(N) { 24 if (N==null) return "00"; 25 N=parseInt(N); if (N==0 || isNaN(N)) return "00"; 26 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N); 27 return "0123456789ABCDEF".charAt((N-N%16)/16) 28 + "0123456789ABCDEF".charAt(N%16); 29 } 30 31 return "#" + toHexStr((n & 0xFF0000)>>>16) + toHexStr((n & 0x00FF00)>>>8) + toHexStr((n & 0x0000FF)); 32 } 33 34 function generateInterpolatedColorArray(count, colors) { 35 function interpolateColors(color1, color2, f) { 36 if (f >= 1) 37 return color2; 38 if (f <= 0) 39 return color1; 40 var fb = 1 - f; 41 return ((((color2 & 0xFF0000) * f)+((color1 & 0xFF0000) * fb)) & 0xFF0000) 42 +((((color2 & 0x00FF00) * f)+((color1 & 0x00FF00) * fb)) & 0x00FF00) 43 +((((color2 & 0x0000FF) * f)+((color1 & 0x0000FF) * fb)) & 0x0000FF); 44 } 45 46 var len = colors.length; 47 var res = new Array(); 48 res.push(hexNumtoHexStr(colors[0])); 49 50 for (var i = 1; i < count; i++) { 51 var val = i * len / count; 52 var color1 = Math.floor(val); 53 var color2 = Math.ceil(val); 54 res.push(hexNumtoHexStr(interpolateColors(colors[color1], colors[color2], val - color1))); 55 } 56 57 return res; 58 } 59 60 if (count <= 0) 61 return null; 62 63 var a10 = [0x1f77b4, 0xff7f0e, 0x2ca02c, 0xd62728, 0x9467bd, 64 0x8c564b, 0xe377c2, 0x7f7f7f, 0xbcbd22, 0x17becf]; 65 var b20 = [0x1f77b4, 0xaec7e8, 0xff7f0e, 0xffbb78, 0x2ca02c, 66 0x98df8a, 0xd62728, 0xff9896, 0x9467bd, 0xc5b0d5, 67 0x8c564b, 0xc49c94, 0xe377c2, 0xf7b6d2, 0x7f7f7f, 68 0xc7c7c7, 0xbcbd22, 0xdbdb8d, 0x17becf, 0x9edae5]; 69 var c19 = [0x9c9ede, 0x7375b5, 0x4a5584, 0xcedb9c, 0xb5cf6b, 70 0x8ca252, 0x637939, 0xe7cb94, 0xe7ba52, 0xbd9e39, 71 0x8c6d31, 0xe7969c, 0xd6616b, 0xad494a, 0x843c39, 72 0xde9ed6, 0xce6dbd, 0xa55194, 0x7b4173]; 73 var colorScheme; 74 75 if (scheme == 20) { 76 colorScheme = b20; 77 } 78 else if (scheme == 10) { 79 colorScheme = a10; 80 } 81 else /* any ((scheme === undefined) || (scheme == 19))*/{ 82 colorScheme = c19; 83 } 84 85 if (count <= colorScheme.length) { 86 c = new Array(); 87 for (var i = 0; i < count; i++) 88 c.push(hexNumtoHexStr(colorScheme[i])); 89 return c; 90 } 91 92 return generateInterpolatedColorArray(count, colorScheme); 93 } 94 95