fc1581720d964457892bad4be2c11c963f2e26fa
[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
143 palette: function(baseColor, fromLevel, toLevel, increment) {
144 var isNil = MochiKit.Base.isUndefinedOrNull;
145 var fractions = new Array();
146 if (isNil(increment))
147 increment = 0.1;
148 if (isNil(toLevel))
149 toLevel = 0.4;
150 if (isNil(fromLevel))
151 fromLevel = -0.2;
152
153 var level = fromLevel;
154 while (level <= toLevel) {
155 fractions.push(level);
156 level += increment;
157 }
158
159 var makeColor = function(color, fraction) {
160 return color.lighterColorWithLevel(fraction);
161 };
162 return MochiKit.Base.map(partial(makeColor, baseColor), fractions);
163 },
164
165 excanvasSupported: function() {
166 if (/MSIE/.test(navigator.userAgent) && !window.opera) {
167 return true;
168 }
169 return false;
170 },
171
172 // The following functions are from quirksmode.org
173 // http://www.quirksmode.org/js/findpos.html
174
175 findPosX: function(obj) {
176 var curleft = 0;
177 if (obj.offsetParent) {
178 while (obj.offsetParent) {
179 curleft += obj.offsetLeft
180 obj = obj.offsetParent;
181 }
182 }
183 else if (obj.x)
184 curleft += obj.x;
185 return curleft;
186 },
187
188 findPosY: function(obj) {
189 var curtop = 0;
190 if (obj.offsetParent) {
191 while (obj.offsetParent) {
192 curtop += obj.offsetTop
193 obj = obj.offsetParent;
194 }
195 }
196 else if (obj.y)
197 curtop += obj.y;
198 return curtop;
199 },
200
201 isFuncLike: function(obj) {
202 return (typeof(obj) == 'function');
203 }
204 });
205
206 //
207 // Prototype.js aware (crippled) versions of map and items.
208 //
209
210 PlotKit.Base.map = function(fn, lst) {
211 if (PlotKit.Base.usingPrototype()) {
212 var rval = [];
213 for (var x in lst) {
214 if (typeof(lst[x]) == 'function') continue;
215 rval.push(fn(lst[x]));
216 }
217 return rval;
218 }
219 else {
220 return MochiKit.Base.map(fn, lst);
221 }
222 };
223
224 PlotKit.Base.items = function(lst) {
225 if (PlotKit.Base.usingPrototype()) {
226 var rval = [];
227 for (var x in lst) {
228 if (typeof(lst[x]) == 'function') continue;
229 rval.push([x, lst[x]]);
230 }
231 return rval;
232 }
233 else {
234 return MochiKit.Base.items(lst);
235 }
236 };
237
238 PlotKit.Base.keys = function(lst) {
239 if (PlotKit.Base.usingPrototype()) {
240 var rval = [];
241 for (var x in lst) {
242 if (typeof(lst[x]) == 'function') continue;
243 rval.push(x);
244 }
245 return rval;
246 }
247 else {
248 return MochiKit.Base.keys(lst);
249 }
250 };
251
252 //
253 // colour schemes
254 //
255 PlotKit.Base.baseColors = function () {
256 var hexColor = MochiKit.Color.Color.fromHexString;
257 return [hexColor("#476fb2"),
258 hexColor("#be2c2b"),
259 hexColor("#85b730"),
260 hexColor("#734a99"),
261 hexColor("#26a1c5"),
262 hexColor("#fb8707"),
263 hexColor("#000000")];
264 };
265
266 PlotKit.Base.EXPORT = [
267 "baseColors",
268 "collapse",
269 "findPosX",
270 "findPosY",
271 "roundInterval",
272 "uniq",
273 "isFuncLike",
274 "excanvasSupported"
275 ];
276
277 PlotKit.Base.EXPORT_OK = [];
278
279 PlotKit.Base.__new__ = function() {
280 var m = MochiKit.Base;
281
282 m.nameFunctions(this);
283
284 this.EXPORT_TAGS = {
285 ":common": this.EXPORT,
286 ":all": m.concat(this.EXPORT, this.EXPORT_OK)
287 };
288 };
289
290 PlotKit.Base.__new__();
291 MochiKit.Base._exportSymbols(this, PlotKit.Base);
292