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
]);
142 colorScheme
: function() {
143 var mb
= MochiKit
.Base
;
144 var mc
= MochiKit
.Color
145 var scheme
= ["red", "orange", "yellow", "green", "cyan", "blue", "purple", "magenta"];
147 var makeColor
= function(name
) {
148 return mc
.Color
[name
+ "Color"]()
151 return mb
.map(makeColor
, scheme
);
154 baseDarkPrimaryColors
: function () {
155 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
156 return [hexColor("#ad3f40"),
160 hexColor("#739c5a")];
163 basePrimaryColors
: function () {
164 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
165 return [hexColor("#d24c4d"),
169 hexColor("#78a15d")];
172 baseBlueColors
: function () {
173 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
174 return [hexColor("#4b6b94"), hexColor("#5d81b4"), hexColor("#acbad2")];
177 palette
: function(baseColor
, fromLevel
, toLevel
, increment
) {
178 var isNil
= MochiKit
.Base
.isUndefinedOrNull
;
179 var fractions
= new Array();
180 if (isNil(increment
))
184 if (isNil(fromLevel
))
187 var level
= fromLevel
;
188 while (level
<= toLevel
) {
189 fractions
.push(level
);
193 var makeColor
= function(color
, fraction
) {
194 return color
.lighterColorWithLevel(fraction
);
196 return MochiKit
.Base
.map(partial(makeColor
, baseColor
), fractions
);
199 excanvasSupported
: function() {
200 if (/MSIE/.test(navigator
.userAgent
) && !window
.opera
) {
206 // The following functions are from quirksmode.org
207 // http://www.quirksmode.org/js
/findpos
.html
209 findPosX
: function(obj
) {
211 if (obj
.offsetParent
) {
212 while (obj
.offsetParent
) {
213 curleft
+= obj
.offsetLeft
214 obj
= obj
.offsetParent
;
222 findPosY
: function(obj
) {
224 if (obj
.offsetParent
) {
225 while (obj
.offsetParent
) {
226 curtop
+= obj
.offsetTop
227 obj
= obj
.offsetParent
;
235 isFuncLike
: function(obj
) {
236 return (typeof(obj
) == 'function');
241 // Prototype.js aware (crippled) versions of map and items.
244 PlotKit
.Base
.map
= function(fn
, lst
) {
245 if (PlotKit
.Base
.usingPrototype()) {
248 if (typeof(lst
[x
]) == 'function') continue;
249 rval
.push(fn(lst
[x
]));
254 return MochiKit
.Base
.map(fn
, lst
);
258 PlotKit
.Base
.items
= function(lst
) {
259 if (PlotKit
.Base
.usingPrototype()) {
262 if (typeof(lst
[x
]) == 'function') continue;
263 rval
.push([x
, lst
[x
]]);
268 return MochiKit
.Base
.items(lst
);
272 PlotKit
.Base
.keys
= function(lst
) {
273 if (PlotKit
.Base
.usingPrototype()) {
276 if (typeof(lst
[x
]) == 'function') continue;
282 return MochiKit
.Base
.keys(lst
);
290 PlotKit
.Base
.baseColors
= function () {
291 var hexColor
= MochiKit
.Color
.Color
.fromHexString
;
292 return [hexColor("#476fb2"),
298 hexColor("#000000")];
301 PlotKit
.Base
.officeBaseStyle
= {
302 "axisLineWidth": 2.0,
303 "axisLabelColor": Color
.grayColor(),
304 "axisLineColor": Color
.whiteColor(),
305 "padding": {top
: 5, bottom
: 10, left
: 30, right
: 30}
308 MochiKit
.Base
.update(PlotKit
.Base
,{
309 officeBlue
: function() {
311 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[0]),
312 "backgroundColor": PlotKit
.Base
.baseColors()[0].lighterColorWithLevel(0.45)
314 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
317 officeRed
: function() {
319 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[1]),
320 "backgroundColor": PlotKit
.Base
.baseColors()[1].lighterColorWithLevel(0.5)
322 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
325 officeGreen
: function() {
327 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[2]),
328 "backgroundColor": PlotKit
.Base
.baseColors()[2].lighterColorWithLevel(0.5)
330 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
333 officePurple
: function() {
335 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[3]),
336 "backgroundColor": PlotKit
.Base
.baseColors()[3].lighterColorWithLevel(0.5)
338 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
342 officeCyan
: function() {
344 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[4]),
345 "backgroundColor": PlotKit
.Base
.baseColors()[4].lighterColorWithLevel(0.5)
347 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
351 officeOrange
: function() {
353 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[5]),
354 "backgroundColor": PlotKit
.Base
.baseColors()[5].lighterColorWithLevel(0.4)
356 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
360 officeBlack
: function() {
362 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[6], 0.0, 0.6),
363 "backgroundColor": PlotKit
.Base
.baseColors()[6].lighterColorWithLevel(0.9)
365 MochiKit
.Base
.update(r
, PlotKit
.Base
.officeBaseStyle
);
371 PlotKit
.Base
.EXPORT
= [
391 PlotKit
.Base
.EXPORT_OK
= [];
393 PlotKit
.Base
.__new__
= function() {
394 var m
= MochiKit
.Base
;
396 m
.nameFunctions(this);
399 ":common": this.EXPORT
,
400 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
404 PlotKit
.Base
.__new__();
405 MochiKit
.Base
._exportSymbols(this, PlotKit
.Base
);