85082c97a01dc3526ac8b050204c7873a05d801f
[dygraphs.git] / plotkit_v091 / PlotKit / Layout.js
1 /*
2 PlotKit Layout
3 ==============
4
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.
8
9 Copyright
10 ---------
11 Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
12 For use under the BSD license. <http://www.liquidx.net/plotkit>
13
14 */
15
16 // --------------------------------------------------------------------
17 // Start of Layout definition
18 // --------------------------------------------------------------------
19
20 if (typeof(PlotKit.Layout) == 'undefined') {
21 PlotKit.Layout = {};
22 }
23
24 // --------------------------------------------------------------------
25 // Start of Layout definition
26 // --------------------------------------------------------------------
27
28 PlotKit.Layout = function(style, options) {
29 this.options = { };
30
31 // valid external options : TODO: input verification
32 MochiKit.Base.update(this.options, options ? options : {});
33
34 // internal states
35 this.datasets = new Array();
36 };
37
38 // --------------------------------------------------------------------
39 // Dataset Manipulation
40 // --------------------------------------------------------------------
41
42
43 PlotKit.Layout.prototype.addDataset = function(setname, set_xy) {
44 this.datasets[setname] = set_xy;
45 };
46
47 // --------------------------------------------------------------------
48 // Evaluates the layout for the current data and style.
49 // --------------------------------------------------------------------
50
51 PlotKit.Layout.prototype.evaluate = function() {
52 this._evaluateLimits();
53 this._evaluateLineCharts();
54 this._evaluateLineTicks();
55 };
56
57
58 // --------------------------------------------------------------------
59 // START Internal Functions
60 // --------------------------------------------------------------------
61
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;
68
69 var x2 = series[series.length - 1][0];
70 if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2;
71 }
72 this.xrange = this.maxxval - this.minxval;
73 this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0);
74
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);
79 };
80
81 // Create the line charts
82 PlotKit.Layout.prototype._evaluateLineCharts = function() {
83 var items = PlotKit.Base.items;
84
85 var setCount = items(this.datasets).length;
86
87 // add all the rects
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];
93 var point = {
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]),
98 name: setName
99 };
100
101 // limit the x, y values so they do not overdraw
102 if (point.y <= 0.0) {
103 point.y = 0.0;
104 }
105 if (point.y >= 1.0) {
106 point.y = 1.0;
107 }
108 if ((point.x >= 0.0) && (point.x <= 1.0)) {
109 this.points.push(point);
110 }
111 }
112 }
113 };
114
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]);
123 }
124 }
125
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]);
133 }
134 }
135 };
136