OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ======= |
| 3 PlotKit is a collection of Javascript classes that allows |
| 4 you to quickly visualise data using different types of charts. |
| 5 |
| 6 http://www.liquidx.net/plotkit/ |
| 7 |
| 8 See LICENSE file for copyright information. |
| 9 */ |
| 10 |
| 11 // -------------------------------------------------------------------- |
| 12 // Check required components |
| 13 // -------------------------------------------------------------------- |
| 14 |
| 15 try { |
| 16 if (typeof(MochiKit.Base) == 'undefined' || |
| 17 typeof(MochiKit.DOM) == 'undefined' || |
| 18 typeof(MochiKit.Color) == 'undefined' || |
| 19 typeof(MochiKit.Format) == 'undefined') |
| 20 { |
| 21 throw ""; |
| 22 } |
| 23 } |
| 24 catch (e) { |
| 25 throw "PlotKit depends on MochiKit.{Base,Color,DOM,Format}" |
| 26 } |
| 27 |
| 28 // ------------------------------------------------------------------- |
| 29 // Inject Common Shortcuts we use into MochiKit.Color.Color |
| 30 // ------------------------------------------------------------------- |
| 31 |
| 32 MochiKit.Base.update(MochiKit.Color.Color.prototype, { |
| 33 asFillColor: function() { |
| 34 return this.lighterColorWithLevel(0.3); |
| 35 }, |
| 36 |
| 37 asStrokeColor: function() { |
| 38 return this.darkerColorWithLevel(0.1); |
| 39 }, |
| 40 |
| 41 asPointColor: function() { |
| 42 return this.lighterColorWithLevel(0.1); |
| 43 } |
| 44 }); |
| 45 |
| 46 |
| 47 // ------------------------------------------------------------------- |
| 48 // Define our own PlotKit namespace |
| 49 // ------------------------------------------------------------------- |
| 50 |
| 51 if (typeof(PlotKit) == 'undefined') { |
| 52 PlotKit = {}; |
| 53 } |
| 54 |
| 55 PlotKit.NAME = "PlotKit"; |
| 56 PlotKit.VERSION = "0.8"; |
| 57 PlotKit.__repr__ = function() { |
| 58 return "[" + this.NAME + " " + this.VERSION + "]"; |
| 59 }; |
| 60 |
| 61 PlotKit.toString = function() { |
| 62 return this.__repr__(); |
| 63 } |
| 64 |
| 65 // ------------------------------------------------------------------- |
| 66 // Encapsulate all our utility function into it's own namespace. |
| 67 // ------------------------------------------------------------------- |
| 68 |
| 69 if (typeof(PlotKit.Base) == 'undefined') { |
| 70 PlotKit.Base = {}; |
| 71 } |
| 72 |
| 73 PlotKit.Base.NAME = 'PlotKit.Base'; |
| 74 PlotKit.Base.VERSION = PlotKit.VERSION; |
| 75 |
| 76 PlotKit.Base.__repr__ = function() { |
| 77 return "[" + this.NAME + " " + this.VERSION + "]"; |
| 78 }; |
| 79 |
| 80 PlotKit.Base.toString = function() { |
| 81 return this.__repr__(); |
| 82 } |
| 83 |
| 84 |
| 85 // Detect whether we are using prototype.js |
| 86 PlotKit.Base.usingPrototype = function() { |
| 87 try { |
| 88 return (typeof(Object.extend) == 'function'); |
| 89 } |
| 90 catch (e) { |
| 91 return false; |
| 92 } |
| 93 } |
| 94 |
| 95 |
| 96 MochiKit.Base.update(PlotKit.Base, { |
| 97 roundInterval: function(range, intervals, precision) { |
| 98 // We want to make the interval look regular, |
| 99 var trunc = MochiKit.Format.roundToFixed; |
| 100 var sep = range/intervals; |
| 101 return parseFloat(trunc(sep, precision)); |
| 102 }, |
| 103 |
| 104 collapse: function(lst) { |
| 105 var m = MochiKit.Base; |
| 106 var biggerList = new Array(); |
| 107 for (var i = 0; i < lst.length; i++) { |
| 108 biggerList = m.concat(biggerList, lst[i]); |
| 109 } |
| 110 if (PlotKit.Base.usingPrototype()) { |
| 111 delete biggerList.extend; |
| 112 delete biggerList.from; |
| 113 delete biggerList.inspect; |
| 114 } |
| 115 |
| 116 return biggerList; |
| 117 }, |
| 118 |
| 119 uniq: function(sortedList) { |
| 120 // get unique elements in list, exactly the same as unix shell's uniq. |
| 121 var m = MochiKit.Base; |
| 122 |
| 123 if (!m.isArrayLike(sortedList) || (sortedList.length < 1)) |
| 124 return new Array(); |
| 125 |
| 126 var uniq = new Array(); |
| 127 var lastElem = sortedList[0]; |
| 128 uniq.push(sortedList[0]); |
| 129 for (var i = 1; i < sortedList.length; i++) { |
| 130 if (m.compare(sortedList[i], lastElem) != 0) { |
| 131 lastElem = sortedList[i]; |
| 132 uniq.push(sortedList[i]); |
| 133 } |
| 134 } |
| 135 return uniq; |
| 136 }, |
| 137 |
| 138 colorScheme: function() { |
| 139 var mb = MochiKit.Base; |
| 140 var mc = MochiKit.Color |
| 141 var scheme = ["red", "orange", "yellow", "green", "cyan", "blue", "purpl
e", "magenta"]; |
| 142 |
| 143 var makeColor = function(name) { |
| 144 return mc.Color[name + "Color"]() |
| 145 }; |
| 146 |
| 147 return mb.map(makeColor, scheme); |
| 148 }, |
| 149 |
| 150 baseDarkPrimaryColors: function () { |
| 151 var hexColor = MochiKit.Color.Color.fromHexString; |
| 152 return [hexColor("#ad3f40"), |
| 153 hexColor("#ddac2c"), |
| 154 hexColor("#dfdd0c"), |
| 155 hexColor("#5276c4"), |
| 156 hexColor("#739c5a")]; |
| 157 }, |
| 158 |
| 159 basePrimaryColors: function () { |
| 160 var hexColor = MochiKit.Color.Color.fromHexString; |
| 161 return [hexColor("#d24c4d"), |
| 162 hexColor("#f2b32f"), |
| 163 hexColor("#ece90e"), |
| 164 hexColor("#5d83da"), |
| 165 hexColor("#78a15d")]; |
| 166 }, |
| 167 |
| 168 baseBlueColors: function () { |
| 169 var hexColor = MochiKit.Color.Color.fromHexString; |
| 170 return [hexColor("#4b6b94"), hexColor("#5d81b4"), hexColor("#acbad2")]; |
| 171 }, |
| 172 |
| 173 palette: function(baseColor, fromLevel, toLevel, increment) { |
| 174 var isNil = MochiKit.Base.isUndefinedOrNull; |
| 175 var fractions = new Array(); |
| 176 if (isNil(increment)) |
| 177 increment = 0.1; |
| 178 if (isNil(toLevel)) |
| 179 toLevel = 0.4; |
| 180 if (isNil(fromLevel)) |
| 181 fromLevel = -0.2; |
| 182 |
| 183 var level = fromLevel; |
| 184 while (level <= toLevel) { |
| 185 fractions.push(level); |
| 186 level += increment; |
| 187 } |
| 188 |
| 189 var makeColor = function(color, fraction) { |
| 190 return color.lighterColorWithLevel(fraction); |
| 191 }; |
| 192 return MochiKit.Base.map(partial(makeColor, baseColor), fractions); |
| 193 }, |
| 194 |
| 195 excanvasSupported: function() { |
| 196 if (/MSIE/.test(navigator.userAgent) && !window.opera) { |
| 197 return true; |
| 198 } |
| 199 return false; |
| 200 }, |
| 201 |
| 202 // The following functions are from quirksmode.org |
| 203 // http://www.quirksmode.org/js/findpos.html |
| 204 |
| 205 findPosX: function(obj) { |
| 206 var curleft = 0; |
| 207 if (obj.offsetParent) { |
| 208 while (obj.offsetParent) { |
| 209 curleft += obj.offsetLeft |
| 210 obj = obj.offsetParent; |
| 211 } |
| 212 } |
| 213 else if (obj.x) |
| 214 curleft += obj.x; |
| 215 return curleft; |
| 216 }, |
| 217 |
| 218 findPosY: function(obj) { |
| 219 var curtop = 0; |
| 220 if (obj.offsetParent) { |
| 221 while (obj.offsetParent) { |
| 222 curtop += obj.offsetTop |
| 223 obj = obj.offsetParent; |
| 224 } |
| 225 } |
| 226 else if (obj.y) |
| 227 curtop += obj.y; |
| 228 return curtop; |
| 229 }, |
| 230 |
| 231 isFuncLike: function(obj) { |
| 232 return (typeof(obj) == 'function'); |
| 233 } |
| 234 }); |
| 235 |
| 236 // |
| 237 // Prototype.js aware (crippled) versions of map and items. |
| 238 // |
| 239 |
| 240 PlotKit.Base.map = function(fn, lst) { |
| 241 if (PlotKit.Base.usingPrototype()) { |
| 242 var rval = []; |
| 243 for (var x in lst) { |
| 244 if (typeof(lst[x]) == 'function') continue; |
| 245 rval.push(fn(lst[x])); |
| 246 } |
| 247 return rval; |
| 248 } |
| 249 else { |
| 250 return MochiKit.Base.map(fn, lst); |
| 251 } |
| 252 }; |
| 253 |
| 254 PlotKit.Base.items = function(lst) { |
| 255 if (PlotKit.Base.usingPrototype()) { |
| 256 var rval = []; |
| 257 for (var x in lst) { |
| 258 if (typeof(lst[x]) == 'function') continue; |
| 259 rval.push([x, lst[x]]); |
| 260 } |
| 261 return rval; |
| 262 } |
| 263 else { |
| 264 return MochiKit.Base.items(lst); |
| 265 } |
| 266 }; |
| 267 |
| 268 PlotKit.Base.keys = function(lst) { |
| 269 if (PlotKit.Base.usingPrototype()) { |
| 270 var rval = []; |
| 271 for (var x in lst) { |
| 272 if (typeof(lst[x]) == 'function') continue; |
| 273 rval.push(x); |
| 274 } |
| 275 return rval; |
| 276 } |
| 277 else { |
| 278 return MochiKit.Base.keys(lst); |
| 279 } |
| 280 }; |
| 281 |
| 282 // |
| 283 // colour schemes |
| 284 // |
| 285 |
| 286 PlotKit.Base.baseColors = function () { |
| 287 var hexColor = MochiKit.Color.Color.fromHexString; |
| 288 return [hexColor("#476fb2"), |
| 289 hexColor("#be2c2b"), |
| 290 hexColor("#85b730"), |
| 291 hexColor("#734a99"), |
| 292 hexColor("#26a1c5"), |
| 293 hexColor("#fb8707"), |
| 294 hexColor("#000000")]; |
| 295 }; |
| 296 |
| 297 PlotKit.Base.officeBaseStyle = { |
| 298 "axisLineWidth": 2.0, |
| 299 "axisLabelColor": Color.grayColor(), |
| 300 "axisLineColor": Color.whiteColor(), |
| 301 "padding": {top: 5, bottom: 10, left: 30, right: 30} |
| 302 }; |
| 303 |
| 304 MochiKit.Base.update(PlotKit.Base,{ |
| 305 officeBlue: function() { |
| 306 var r = { |
| 307 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), |
| 308 "backgroundColor": PlotKit.Base.baseColors()[0].lighterColorWithLevel(0.
45) |
| 309 }; |
| 310 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 311 return r; |
| 312 }, |
| 313 officeRed: function() { |
| 314 var r = { |
| 315 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[1]), |
| 316 "backgroundColor": PlotKit.Base.baseColors()[1].lighterColorWithLevel(0.
5) |
| 317 }; |
| 318 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 319 return r; |
| 320 }, |
| 321 officeGreen: function() { |
| 322 var r = { |
| 323 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[2]), |
| 324 "backgroundColor": PlotKit.Base.baseColors()[2].lighterColorWithLevel(0.
5) |
| 325 }; |
| 326 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 327 return r; |
| 328 }, |
| 329 officePurple: function() { |
| 330 var r = { |
| 331 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[3]), |
| 332 "backgroundColor": PlotKit.Base.baseColors()[3].lighterColorWithLevel(0.
5) |
| 333 }; |
| 334 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 335 return r; |
| 336 }, |
| 337 |
| 338 officeCyan: function() { |
| 339 var r = { |
| 340 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[4]), |
| 341 "backgroundColor": PlotKit.Base.baseColors()[4].lighterColorWithLeve
l(0.5) |
| 342 }; |
| 343 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 344 return r; |
| 345 }, |
| 346 |
| 347 officeOrange: function() { |
| 348 var r = { |
| 349 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[5]), |
| 350 "backgroundColor": PlotKit.Base.baseColors()[5].lighterColorWithLeve
l(0.4) |
| 351 }; |
| 352 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 353 return r; |
| 354 }, |
| 355 |
| 356 officeBlack: function() { |
| 357 var r = { |
| 358 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[6], 0.0, 0
.6), |
| 359 "backgroundColor": PlotKit.Base.baseColors()[6].lighterColorWithLevel(0.
9) |
| 360 }; |
| 361 MochiKit.Base.update(r, PlotKit.Base.officeBaseStyle); |
| 362 return r; |
| 363 } |
| 364 }); |
| 365 |
| 366 |
| 367 PlotKit.Base.EXPORT = [ |
| 368 "baseColors", |
| 369 "collapse", |
| 370 "colorScheme", |
| 371 "findPosX", |
| 372 "findPosY", |
| 373 "officeBaseStyle", |
| 374 "officeBlue", |
| 375 "officeRed", |
| 376 "officeGreen", |
| 377 "officePurple", |
| 378 "officeCyan", |
| 379 "officeOrange", |
| 380 "officeBlack", |
| 381 "roundInterval", |
| 382 "uniq", |
| 383 "isFuncLike", |
| 384 "excanvasSupported" |
| 385 ]; |
| 386 |
| 387 PlotKit.Base.EXPORT_OK = []; |
| 388 |
| 389 PlotKit.Base.__new__ = function() { |
| 390 var m = MochiKit.Base; |
| 391 |
| 392 m.nameFunctions(this); |
| 393 |
| 394 this.EXPORT_TAGS = { |
| 395 ":common": this.EXPORT, |
| 396 ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
| 397 }; |
| 398 }; |
| 399 |
| 400 PlotKit.Base.__new__(); |
| 401 MochiKit.Base._exportSymbols(this, PlotKit.Base); |
OLD | NEW |