Commit | Line | Data |
---|---|---|
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 | ||
20 | if (typeof(PlotKit.Layout) == 'undefined') { | |
21 | PlotKit.Layout = {}; | |
22 | } | |
23 | ||
6a1aa64f DV |
24 | // -------------------------------------------------------------------- |
25 | // Start of Layout definition | |
26 | // -------------------------------------------------------------------- | |
27 | ||
28 | PlotKit.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 | ||
43 | PlotKit.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 | ||
51 | PlotKit.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 | ||
62 | PlotKit.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 |
82 | PlotKit.Layout.prototype._evaluateLineCharts = function() { | |
6a1aa64f DV |
83 | // add all the rects |
84 | this.points = new Array(); | |
6a1aa64f DV |
85 | for (var setName in this.datasets) { |
86 | var dataset = this.datasets[setName]; | |
6a1aa64f DV |
87 | for (var j = 0; j < dataset.length; j++) { |
88 | var item = dataset[j]; | |
89 | var point = { | |
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]), | |
94 | name: setName | |
95 | }; | |
96 | ||
97 | // limit the x, y values so they do not overdraw | |
98 | if (point.y <= 0.0) { | |
99 | point.y = 0.0; | |
100 | } | |
101 | if (point.y >= 1.0) { | |
102 | point.y = 1.0; | |
103 | } | |
104 | if ((point.x >= 0.0) && (point.x <= 1.0)) { | |
105 | this.points.push(point); | |
106 | } | |
107 | } | |
6a1aa64f DV |
108 | } |
109 | }; | |
110 | ||
6a1aa64f | 111 | PlotKit.Layout.prototype._evaluateLineTicks = function() { |
450370ba DV |
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]); | |
119 | } | |
120 | } | |
121 | ||
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]); | |
129 | } | |
130 | } | |
6a1aa64f DV |
131 | }; |
132 |