4 PlotKit is a collection of Javascript classes that allows
5 you to quickly visualise data using different types of charts.
7 For license/info/documentation: http://www.liquidx.net/plotkit/
11 Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
12 For use under the BSD license. <http://www.liquidx.net/plotkit>
15 // --------------------------------------------------------------------
16 // Check required components
17 // --------------------------------------------------------------------
20 if (typeof(MochiKit
.Base
) == 'undefined' ||
21 typeof(MochiKit
.DOM
) == 'undefined' ||
22 typeof(MochiKit
.Color
) == 'undefined')
28 throw "PlotKit depends on MochiKit.{Base,Color,DOM}"
31 // -------------------------------------------------------------------
32 // Define our own PlotKit namespace
33 // -------------------------------------------------------------------
35 if (typeof(PlotKit
) == 'undefined') {
39 PlotKit
.NAME
= "PlotKit";
40 PlotKit
.VERSION
= "0.8";
41 PlotKit
.__repr__
= function() {
42 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
45 PlotKit
.toString
= function() {
46 return this.__repr__();
49 // -------------------------------------------------------------------
50 // Encapsulate all our utility function into it's own namespace.
51 // -------------------------------------------------------------------
53 if (typeof(PlotKit
.Base
) == 'undefined') {
57 PlotKit
.Base
.NAME
= 'PlotKit.Base';
58 PlotKit
.Base
.VERSION
= PlotKit
.VERSION
;
60 PlotKit
.Base
.__repr__
= function() {
61 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
64 PlotKit
.Base
.toString
= function() {
65 return this.__repr__();
69 // Detect whether we are using prototype.js
70 PlotKit
.Base
.usingPrototype
= function() {
72 return (typeof(Object
.extend
) == 'function');
80 MochiKit
.Base
.update(PlotKit
.Base
, {
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
]);
87 if (PlotKit
.Base
.usingPrototype()) {
88 delete biggerList
.extend
;
89 delete biggerList
.from
;
90 delete biggerList
.inspect
;
96 uniq
: function(sortedList
) {
97 // get unique elements in list, exactly the same as unix shell's uniq.
98 var m
= MochiKit
.Base
;
100 if (!m
.isArrayLike(sortedList
) || (sortedList
.length
< 1))
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
]);
116 palette
: function(baseColor
, fromLevel
, toLevel
, increment
) {
117 var isNil
= MochiKit
.Base
.isUndefinedOrNull
;
118 var fractions
= new Array();
119 if (isNil(increment
))
123 if (isNil(fromLevel
))
126 var level
= fromLevel
;
127 while (level
<= toLevel
) {
128 fractions
.push(level
);
132 var makeColor
= function(color
, fraction
) {
133 return color
.lighterColorWithLevel(fraction
);
135 return MochiKit
.Base
.map(partial(makeColor
, baseColor
), fractions
);
138 excanvasSupported
: function() {
139 if (/MSIE/.test(navigator
.userAgent
) && !window
.opera
) {
145 // The following functions are from quirksmode.org
146 // http://www.quirksmode.org/js
/findpos
.html
148 findPosX
: function(obj
) {
150 if (obj
.offsetParent
) {
151 while (obj
.offsetParent
) {
152 curleft
+= obj
.offsetLeft
153 obj
= obj
.offsetParent
;
161 findPosY
: function(obj
) {
163 if (obj
.offsetParent
) {
164 while (obj
.offsetParent
) {
165 curtop
+= obj
.offsetTop
166 obj
= obj
.offsetParent
;
174 isFuncLike
: function(obj
) {
175 return (typeof(obj
) == 'function');
180 // Prototype.js aware (crippled) versions of map and items.
183 PlotKit
.Base
.map
= function(fn
, lst
) {
184 if (PlotKit
.Base
.usingPrototype()) {
187 if (typeof(lst
[x
]) == 'function') continue;
188 rval
.push(fn(lst
[x
]));
193 return MochiKit
.Base
.map(fn
, lst
);
197 PlotKit
.Base
.items
= function(lst
) {
198 if (PlotKit
.Base
.usingPrototype()) {
201 if (typeof(lst
[x
]) == 'function') continue;
202 rval
.push([x
, lst
[x
]]);
207 return MochiKit
.Base
.items(lst
);
211 PlotKit
.Base
.keys
= function(lst
) {
212 if (PlotKit
.Base
.usingPrototype()) {
215 if (typeof(lst
[x
]) == 'function') continue;
221 return MochiKit
.Base
.keys(lst
);
228 PlotKit
.Base
.baseColors
= function () {
229 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
230 return [hexColor("#476fb2"),
236 hexColor("#000000")];
239 PlotKit
.Base
.EXPORT
= [
249 PlotKit
.Base
.EXPORT_OK
= [];
251 PlotKit
.Base
.__new__
= function() {
252 var m
= MochiKit
.Base
;
254 m
.nameFunctions(this);
257 ":common": this.EXPORT
,
258 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
262 PlotKit
.Base
.__new__();
263 MochiKit
.Base
._exportSymbols(this, PlotKit
.Base
);