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 // Inject Common Shortcuts we use into MochiKit.Color.Color
33 // -------------------------------------------------------------------
35 MochiKit
.Base
.update(MochiKit
.Color
.Color
.prototype, {
36 asFillColor
: function() {
37 return this.lighterColorWithLevel(0.3);
40 asStrokeColor
: function() {
41 return this.darkerColorWithLevel(0.1);
44 asPointColor
: function() {
45 return this.lighterColorWithLevel(0.1);
50 // -------------------------------------------------------------------
51 // Define our own PlotKit namespace
52 // -------------------------------------------------------------------
54 if (typeof(PlotKit
) == 'undefined') {
58 PlotKit
.NAME
= "PlotKit";
59 PlotKit
.VERSION
= "0.8";
60 PlotKit
.__repr__
= function() {
61 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
64 PlotKit
.toString
= function() {
65 return this.__repr__();
68 // -------------------------------------------------------------------
69 // Encapsulate all our utility function into it's own namespace.
70 // -------------------------------------------------------------------
72 if (typeof(PlotKit
.Base
) == 'undefined') {
76 PlotKit
.Base
.NAME
= 'PlotKit.Base';
77 PlotKit
.Base
.VERSION
= PlotKit
.VERSION
;
79 PlotKit
.Base
.__repr__
= function() {
80 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
83 PlotKit
.Base
.toString
= function() {
84 return this.__repr__();
88 // Detect whether we are using prototype.js
89 PlotKit
.Base
.usingPrototype
= function() {
91 return (typeof(Object
.extend
) == 'function');
99 MochiKit
.Base
.update(PlotKit
.Base
, {
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
]);
106 if (PlotKit
.Base
.usingPrototype()) {
107 delete biggerList
.extend
;
108 delete biggerList
.from
;
109 delete biggerList
.inspect
;
115 uniq
: function(sortedList
) {
116 // get unique elements in list, exactly the same as unix shell's uniq.
117 var m
= MochiKit
.Base
;
119 if (!m
.isArrayLike(sortedList
) || (sortedList
.length
< 1))
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
]);
135 palette
: function(baseColor
, fromLevel
, toLevel
, increment
) {
136 var isNil
= MochiKit
.Base
.isUndefinedOrNull
;
137 var fractions
= new Array();
138 if (isNil(increment
))
142 if (isNil(fromLevel
))
145 var level
= fromLevel
;
146 while (level
<= toLevel
) {
147 fractions
.push(level
);
151 var makeColor
= function(color
, fraction
) {
152 return color
.lighterColorWithLevel(fraction
);
154 return MochiKit
.Base
.map(partial(makeColor
, baseColor
), fractions
);
157 excanvasSupported
: function() {
158 if (/MSIE/.test(navigator
.userAgent
) && !window
.opera
) {
164 // The following functions are from quirksmode.org
165 // http://www.quirksmode.org/js
/findpos
.html
167 findPosX
: function(obj
) {
169 if (obj
.offsetParent
) {
170 while (obj
.offsetParent
) {
171 curleft
+= obj
.offsetLeft
172 obj
= obj
.offsetParent
;
180 findPosY
: function(obj
) {
182 if (obj
.offsetParent
) {
183 while (obj
.offsetParent
) {
184 curtop
+= obj
.offsetTop
185 obj
= obj
.offsetParent
;
193 isFuncLike
: function(obj
) {
194 return (typeof(obj
) == 'function');
199 // Prototype.js aware (crippled) versions of map and items.
202 PlotKit
.Base
.map
= function(fn
, lst
) {
203 if (PlotKit
.Base
.usingPrototype()) {
206 if (typeof(lst
[x
]) == 'function') continue;
207 rval
.push(fn(lst
[x
]));
212 return MochiKit
.Base
.map(fn
, lst
);
216 PlotKit
.Base
.items
= function(lst
) {
217 if (PlotKit
.Base
.usingPrototype()) {
220 if (typeof(lst
[x
]) == 'function') continue;
221 rval
.push([x
, lst
[x
]]);
226 return MochiKit
.Base
.items(lst
);
230 PlotKit
.Base
.keys
= function(lst
) {
231 if (PlotKit
.Base
.usingPrototype()) {
234 if (typeof(lst
[x
]) == 'function') continue;
240 return MochiKit
.Base
.keys(lst
);
247 PlotKit
.Base
.baseColors
= function () {
248 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
249 return [hexColor("#476fb2"),
255 hexColor("#000000")];
258 PlotKit
.Base
.EXPORT
= [
268 PlotKit
.Base
.EXPORT_OK
= [];
270 PlotKit
.Base
.__new__
= function() {
271 var m
= MochiKit
.Base
;
273 m
.nameFunctions(this);
276 ":common": this.EXPORT
,
277 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
281 PlotKit
.Base
.__new__();
282 MochiKit
.Base
._exportSymbols(this, PlotKit
.Base
);