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' ||
23 typeof(MochiKit
.Format
) == 'undefined')
29 throw "PlotKit depends on MochiKit.{Base,Color,DOM,Format}"
32 // -------------------------------------------------------------------
33 // Inject Common Shortcuts we use into MochiKit.Color.Color
34 // -------------------------------------------------------------------
36 MochiKit
.Base
.update(MochiKit
.Color
.Color
.prototype, {
37 asFillColor
: function() {
38 return this.lighterColorWithLevel(0.3);
41 asStrokeColor
: function() {
42 return this.darkerColorWithLevel(0.1);
45 asPointColor
: function() {
46 return this.lighterColorWithLevel(0.1);
51 // -------------------------------------------------------------------
52 // Define our own PlotKit namespace
53 // -------------------------------------------------------------------
55 if (typeof(PlotKit
) == 'undefined') {
59 PlotKit
.NAME
= "PlotKit";
60 PlotKit
.VERSION
= "0.8";
61 PlotKit
.__repr__
= function() {
62 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
65 PlotKit
.toString
= function() {
66 return this.__repr__();
69 // -------------------------------------------------------------------
70 // Encapsulate all our utility function into it's own namespace.
71 // -------------------------------------------------------------------
73 if (typeof(PlotKit
.Base
) == 'undefined') {
77 PlotKit
.Base
.NAME
= 'PlotKit.Base';
78 PlotKit
.Base
.VERSION
= PlotKit
.VERSION
;
80 PlotKit
.Base
.__repr__
= function() {
81 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
84 PlotKit
.Base
.toString
= function() {
85 return this.__repr__();
89 // Detect whether we are using prototype.js
90 PlotKit
.Base
.usingPrototype
= function() {
92 return (typeof(Object
.extend
) == 'function');
100 MochiKit
.Base
.update(PlotKit
.Base
, {
101 roundInterval
: function(range
, intervals
, precision
) {
102 // We want to make the interval look regular,
103 var trunc
= MochiKit
.Format
.roundToFixed
;
104 var sep
= range
/intervals
;
105 return parseFloat(trunc(sep
, precision
));
108 collapse
: function(lst
) {
109 var m
= MochiKit
.Base
;
110 var biggerList
= new Array();
111 for (var i
= 0; i
< lst
.length
; i
++) {
112 biggerList
= m
.concat(biggerList
, lst
[i
]);
114 if (PlotKit
.Base
.usingPrototype()) {
115 delete biggerList
.extend
;
116 delete biggerList
.from
;
117 delete biggerList
.inspect
;
123 uniq
: function(sortedList
) {
124 // get unique elements in list, exactly the same as unix shell's uniq.
125 var m
= MochiKit
.Base
;
127 if (!m
.isArrayLike(sortedList
) || (sortedList
.length
< 1))
130 var uniq
= new Array();
131 var lastElem
= sortedList
[0];
132 uniq
.push(sortedList
[0]);
133 for (var i
= 1; i
< sortedList
.length
; i
++) {
134 if (m
.compare(sortedList
[i
], lastElem
) != 0) {
135 lastElem
= sortedList
[i
];
136 uniq
.push(sortedList
[i
]);
143 palette
: function(baseColor
, fromLevel
, toLevel
, increment
) {
144 var isNil
= MochiKit
.Base
.isUndefinedOrNull
;
145 var fractions
= new Array();
146 if (isNil(increment
))
150 if (isNil(fromLevel
))
153 var level
= fromLevel
;
154 while (level
<= toLevel
) {
155 fractions
.push(level
);
159 var makeColor
= function(color
, fraction
) {
160 return color
.lighterColorWithLevel(fraction
);
162 return MochiKit
.Base
.map(partial(makeColor
, baseColor
), fractions
);
165 excanvasSupported
: function() {
166 if (/MSIE/.test(navigator
.userAgent
) && !window
.opera
) {
172 // The following functions are from quirksmode.org
173 // http://www.quirksmode.org/js
/findpos
.html
175 findPosX
: function(obj
) {
177 if (obj
.offsetParent
) {
178 while (obj
.offsetParent
) {
179 curleft
+= obj
.offsetLeft
180 obj
= obj
.offsetParent
;
188 findPosY
: function(obj
) {
190 if (obj
.offsetParent
) {
191 while (obj
.offsetParent
) {
192 curtop
+= obj
.offsetTop
193 obj
= obj
.offsetParent
;
201 isFuncLike
: function(obj
) {
202 return (typeof(obj
) == 'function');
207 // Prototype.js aware (crippled) versions of map and items.
210 PlotKit
.Base
.map
= function(fn
, lst
) {
211 if (PlotKit
.Base
.usingPrototype()) {
214 if (typeof(lst
[x
]) == 'function') continue;
215 rval
.push(fn(lst
[x
]));
220 return MochiKit
.Base
.map(fn
, lst
);
224 PlotKit
.Base
.items
= function(lst
) {
225 if (PlotKit
.Base
.usingPrototype()) {
228 if (typeof(lst
[x
]) == 'function') continue;
229 rval
.push([x
, lst
[x
]]);
234 return MochiKit
.Base
.items(lst
);
238 PlotKit
.Base
.keys
= function(lst
) {
239 if (PlotKit
.Base
.usingPrototype()) {
242 if (typeof(lst
[x
]) == 'function') continue;
248 return MochiKit
.Base
.keys(lst
);
255 PlotKit
.Base
.baseColors
= function () {
256 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
257 return [hexColor("#476fb2"),
263 hexColor("#000000")];
266 PlotKit
.Base
.EXPORT
= [
277 PlotKit
.Base
.EXPORT_OK
= [];
279 PlotKit
.Base
.__new__
= function() {
280 var m
= MochiKit
.Base
;
282 m
.nameFunctions(this);
285 ":common": this.EXPORT
,
286 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
290 PlotKit
.Base
.__new__();
291 MochiKit
.Base
._exportSymbols(this, PlotKit
.Base
);