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