13b2de0fc203014adbdf3517ce5bb7432469e79e
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() {
84 this.points
= new Array();
85 for (var setName
in this.datasets
) {
86 var dataset
= this.datasets
[setName
];
87 for (var j
= 0; j
< dataset
.length
; j
++) {
88 var item
= dataset
[j
];
90 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
91 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
92 xval
: parseFloat(item
[0]),
93 yval
: parseFloat(item
[1]),
97 // limit the x, y values so they do not overdraw
101 if (point
.y
>= 1.0) {
104 if ((point
.x
>= 0.0) && (point
.x
<= 1.0)) {
105 this.points
.push(point
);
111 PlotKit
.Layout
.prototype._evaluateLineTicks
= function() {
112 this.xticks
= new Array();
113 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
114 var tick
= this.options
.xTicks
[i
];
115 var label
= tick
.label
;
116 var pos
= this.xscale
* (tick
.v
- this.minxval
);
117 if ((pos
>= 0.0) && (pos
<= 1.0)) {
118 this.xticks
.push([pos
, label
]);
122 this.yticks
= new Array();
123 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
124 var tick
= this.options
.yTicks
[i
];
125 var label
= tick
.label
;
126 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
127 if ((pos
>= 0.0) && (pos
<= 1.0)) {
128 this.yticks
.push([pos
, label
]);