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