move custom_rhino out of plotkit
[dygraphs.git] / plotkit_v091 / PlotKit / Base.js
CommitLineData
6a1aa64f
DV
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
19try {
20 if (typeof(MochiKit.Base) == 'undefined' ||
21 typeof(MochiKit.DOM) == 'undefined' ||
223ddf66 22 typeof(MochiKit.Color) == 'undefined')
6a1aa64f
DV
23 {
24 throw "";
25 }
26}
27catch (e) {
223ddf66 28 throw "PlotKit depends on MochiKit.{Base,Color,DOM}"
6a1aa64f
DV
29}
30
31// -------------------------------------------------------------------
6a1aa64f
DV
32// Define our own PlotKit namespace
33// -------------------------------------------------------------------
34
35if (typeof(PlotKit) == 'undefined') {
36 PlotKit = {};
37}
38
39PlotKit.NAME = "PlotKit";
40PlotKit.VERSION = "0.8";
41PlotKit.__repr__ = function() {
42 return "[" + this.NAME + " " + this.VERSION + "]";
43};
44
45PlotKit.toString = function() {
46 return this.__repr__();
47}
48
49// -------------------------------------------------------------------
50// Encapsulate all our utility function into it's own namespace.
51// -------------------------------------------------------------------
52
53if (typeof(PlotKit.Base) == 'undefined') {
54 PlotKit.Base = {};
55}
56
57PlotKit.Base.NAME = 'PlotKit.Base';
58PlotKit.Base.VERSION = PlotKit.VERSION;
59
60PlotKit.Base.__repr__ = function() {
61 return "[" + this.NAME + " " + this.VERSION + "]";
62};
63
64PlotKit.Base.toString = function() {
65 return this.__repr__();
66}
67
68
69// Detect whether we are using prototype.js
70PlotKit.Base.usingPrototype = function() {
71 try {
72 return (typeof(Object.extend) == 'function');
73 }
74 catch (e) {
75 return false;
76 }
77}
78
79
80MochiKit.Base.update(PlotKit.Base, {
6a1aa64f
DV
81 collapse: function(lst) {
82 var m = MochiKit.Base;
83 var biggerList = new Array();
84 for (var i = 0; i < lst.length; i++) {
85 biggerList = m.concat(biggerList, lst[i]);
86 }
87 if (PlotKit.Base.usingPrototype()) {
88 delete biggerList.extend;
89 delete biggerList.from;
90 delete biggerList.inspect;
91 }
92
93 return biggerList;
94 },
95
96 uniq: function(sortedList) {
97 // get unique elements in list, exactly the same as unix shell's uniq.
98 var m = MochiKit.Base;
99
100 if (!m.isArrayLike(sortedList) || (sortedList.length < 1))
101 return new Array();
102
103 var uniq = new Array();
104 var lastElem = sortedList[0];
105 uniq.push(sortedList[0]);
106 for (var i = 1; i < sortedList.length; i++) {
107 if (m.compare(sortedList[i], lastElem) != 0) {
108 lastElem = sortedList[i];
109 uniq.push(sortedList[i]);
110 }
111 }
112 return uniq;
113 },
114
6a1aa64f
DV
115
116 palette: function(baseColor, fromLevel, toLevel, increment) {
117 var isNil = MochiKit.Base.isUndefinedOrNull;
118 var fractions = new Array();
119 if (isNil(increment))
120 increment = 0.1;
121 if (isNil(toLevel))
122 toLevel = 0.4;
123 if (isNil(fromLevel))
124 fromLevel = -0.2;
125
126 var level = fromLevel;
127 while (level <= toLevel) {
128 fractions.push(level);
129 level += increment;
130 }
131
132 var makeColor = function(color, fraction) {
133 return color.lighterColorWithLevel(fraction);
134 };
135 return MochiKit.Base.map(partial(makeColor, baseColor), fractions);
136 },
137
138 excanvasSupported: function() {
139 if (/MSIE/.test(navigator.userAgent) && !window.opera) {
140 return true;
141 }
142 return false;
143 },
144
145 // The following functions are from quirksmode.org
146 // http://www.quirksmode.org/js/findpos.html
147
148 findPosX: function(obj) {
149 var curleft = 0;
150 if (obj.offsetParent) {
151 while (obj.offsetParent) {
152 curleft += obj.offsetLeft
153 obj = obj.offsetParent;
154 }
155 }
156 else if (obj.x)
157 curleft += obj.x;
158 return curleft;
159 },
160
161 findPosY: function(obj) {
162 var curtop = 0;
163 if (obj.offsetParent) {
164 while (obj.offsetParent) {
165 curtop += obj.offsetTop
166 obj = obj.offsetParent;
167 }
168 }
169 else if (obj.y)
170 curtop += obj.y;
171 return curtop;
172 },
173
174 isFuncLike: function(obj) {
175 return (typeof(obj) == 'function');
176 }
177});
178
179//
180// Prototype.js aware (crippled) versions of map and items.
181//
182
183PlotKit.Base.map = function(fn, lst) {
184 if (PlotKit.Base.usingPrototype()) {
185 var rval = [];
186 for (var x in lst) {
187 if (typeof(lst[x]) == 'function') continue;
188 rval.push(fn(lst[x]));
189 }
190 return rval;
191 }
192 else {
193 return MochiKit.Base.map(fn, lst);
194 }
195};
196
197PlotKit.Base.items = function(lst) {
198 if (PlotKit.Base.usingPrototype()) {
199 var rval = [];
200 for (var x in lst) {
201 if (typeof(lst[x]) == 'function') continue;
202 rval.push([x, lst[x]]);
203 }
204 return rval;
205 }
206 else {
207 return MochiKit.Base.items(lst);
208 }
209};
210
211PlotKit.Base.keys = function(lst) {
212 if (PlotKit.Base.usingPrototype()) {
213 var rval = [];
214 for (var x in lst) {
215 if (typeof(lst[x]) == 'function') continue;
216 rval.push(x);
217 }
218 return rval;
219 }
220 else {
221 return MochiKit.Base.keys(lst);
222 }
223};
224
225//
226// colour schemes
227//
6a1aa64f
DV
228PlotKit.Base.baseColors = function () {
229 var hexColor = MochiKit.Color.Color.fromHexString;
230 return [hexColor("#476fb2"),
231 hexColor("#be2c2b"),
232 hexColor("#85b730"),
233 hexColor("#734a99"),
234 hexColor("#26a1c5"),
235 hexColor("#fb8707"),
236 hexColor("#000000")];
237};
238
6a1aa64f
DV
239PlotKit.Base.EXPORT = [
240 "baseColors",
241 "collapse",
6a1aa64f
DV
242 "findPosX",
243 "findPosY",
6a1aa64f
DV
244 "uniq",
245 "isFuncLike",
246 "excanvasSupported"
247];
248
249PlotKit.Base.EXPORT_OK = [];
250
251PlotKit.Base.__new__ = function() {
252 var m = MochiKit.Base;
253
254 m.nameFunctions(this);
255
256 this.EXPORT_TAGS = {
257 ":common": this.EXPORT,
258 ":all": m.concat(this.EXPORT, this.EXPORT_OK)
259 };
260};
261
262PlotKit.Base.__new__();
263MochiKit.Base._exportSymbols(this, PlotKit.Base);
264