shrink Layout more and more
[dygraphs.git] / plotkit_v091 / PlotKit / Layout.js
CommitLineData
6a1aa64f
DV
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
6a1aa64f
DV
16// --------------------------------------------------------------------
17// Start of Layout definition
18// --------------------------------------------------------------------
19
20if (typeof(PlotKit.Layout) == 'undefined') {
21 PlotKit.Layout = {};
22}
23
6a1aa64f
DV
24// --------------------------------------------------------------------
25// Start of Layout definition
26// --------------------------------------------------------------------
27
28PlotKit.Layout = function(style, options) {
450370ba 29 this.options = { };
6a1aa64f
DV
30
31 // valid external options : TODO: input verification
6a1aa64f
DV
32 MochiKit.Base.update(this.options, options ? options : {});
33
6a1aa64f
DV
34 // internal states
35 this.datasets = new Array();
6a1aa64f
DV
36};
37
38// --------------------------------------------------------------------
39// Dataset Manipulation
40// --------------------------------------------------------------------
41
42
43PlotKit.Layout.prototype.addDataset = function(setname, set_xy) {
44 this.datasets[setname] = set_xy;
45};
46
6a1aa64f
DV
47// --------------------------------------------------------------------
48// Evaluates the layout for the current data and style.
49// --------------------------------------------------------------------
50
51PlotKit.Layout.prototype.evaluate = function() {
52 this._evaluateLimits();
0bece959
DV
53 this._evaluateLineCharts();
54 this._evaluateLineTicks();
6a1aa64f
DV
55};
56
57
6a1aa64f
DV
58// --------------------------------------------------------------------
59// START Internal Functions
60// --------------------------------------------------------------------
61
62PlotKit.Layout.prototype._evaluateLimits = function() {
450370ba
DV
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 }
6a1aa64f 72 this.xrange = this.maxxval - this.minxval;
0bece959 73 this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0);
6a1aa64f 74
0bece959
DV
75 this.minyval = this.options.yAxis[0];
76 this.maxyval = this.options.yAxis[1];
6a1aa64f 77 this.yrange = this.maxyval - this.minyval;
0bece959 78 this.yscale = (this.yrange != 0 ? 1/this.yrange : 1.0);
6a1aa64f
DV
79};
80
6a1aa64f
DV
81// Create the line charts
82PlotKit.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();
6a1aa64f
DV
89 for (var setName in this.datasets) {
90 var dataset = this.datasets[setName];
6a1aa64f
DV
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 }
6a1aa64f
DV
112 }
113};
114
6a1aa64f 115PlotKit.Layout.prototype._evaluateLineTicks = function() {
450370ba
DV
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 }
6a1aa64f
DV
135};
136