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