5 Handles laying out data on to a virtual canvas square canvas between 0.0
6 and 1.0. If you want to add new chart/plot types such as point plots,
7 you need to add them here.
11 Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
12 For use under the BSD license. <http://www.liquidx.net/plotkit>
16 // --------------------------------------------------------------------
17 // Start of Layout definition
18 // --------------------------------------------------------------------
20 if (typeof(PlotKit
.Layout
) == 'undefined') {
24 // --------------------------------------------------------------------
25 // Start of Layout definition
26 // --------------------------------------------------------------------
28 PlotKit
.Layout
= function(style
, options
) {
31 // valid external options : TODO: input verification
32 MochiKit
.Base
.update(this.options
, options
? options
: {});
35 this.datasets
= new Array();
38 // --------------------------------------------------------------------
39 // Dataset Manipulation
40 // --------------------------------------------------------------------
43 PlotKit
.Layout
.prototype.addDataset
= function(setname
, set_xy
) {
44 this.datasets
[setname
] = set_xy
;
47 // --------------------------------------------------------------------
48 // Evaluates the layout for the current data and style.
49 // --------------------------------------------------------------------
51 PlotKit
.Layout
.prototype.evaluate
= function() {
52 this._evaluateLimits();
53 this._evaluateLineCharts();
54 this._evaluateLineTicks();
58 // --------------------------------------------------------------------
59 // START Internal Functions
60 // --------------------------------------------------------------------
62 PlotKit
.Layout
.prototype._evaluateLimits
= function() {
63 this.minxval
= this.maxxval
= null;
64 for (var name
in this.datasets
) {
65 var series
= this.datasets
[name
];
66 var x1
= series
[0][0];
67 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
69 var x2
= series
[series
.length
- 1][0];
70 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
72 this.xrange
= this.maxxval
- this.minxval
;
73 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
75 this.minyval
= this.options
.yAxis
[0];
76 this.maxyval
= this.options
.yAxis
[1];
77 this.yrange
= this.maxyval
- this.minyval
;
78 this.yscale
= (this.yrange
!= 0 ? 1/this.yrange
: 1.0);
81 // Create the line charts
82 PlotKit
.Layout
.prototype._evaluateLineCharts
= function() {
83 var items
= PlotKit
.Base
.items
;
85 var setCount
= items(this.datasets
).length
;
88 this.points
= new Array();
89 for (var setName
in this.datasets
) {
90 var dataset
= this.datasets
[setName
];
91 for (var j
= 0; j
< dataset
.length
; j
++) {
92 var item
= dataset
[j
];
94 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
95 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
96 xval
: parseFloat(item
[0]),
97 yval
: parseFloat(item
[1]),
101 // limit the x, y values so they do not overdraw
102 if (point
.y
<= 0.0) {
105 if (point
.y
>= 1.0) {
108 if ((point
.x
>= 0.0) && (point
.x
<= 1.0)) {
109 this.points
.push(point
);
115 PlotKit
.Layout
.prototype._evaluateLineTicks
= function() {
116 this.xticks
= new Array();
117 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
118 var tick
= this.options
.xTicks
[i
];
119 var label
= tick
.label
;
120 var pos
= this.xscale
* (tick
.v
- this.minxval
);
121 if ((pos
>= 0.0) && (pos
<= 1.0)) {
122 this.xticks
.push([pos
, label
]);
126 this.yticks
= new Array();
127 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
128 var tick
= this.options
.yTicks
[i
];
129 var label
= tick
.label
;
130 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
131 if ((pos
>= 0.0) && (pos
<= 1.0)) {
132 this.yticks
.push([pos
, label
]);