annotate webapp/src/main/webapp/jquery/svg/jquery.svgplot.js @ 1151:139ad480333b

update jquery svg plugin to ver. 1.4.6
author hertzhaft
date Mon, 26 Nov 2012 23:49:41 +0100
parents 301ef9bf1965
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
756
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
1 /* http://keith-wood.name/svg.html
1151
139ad480333b update jquery svg plugin to ver. 1.4.6
hertzhaft
parents: 1050
diff changeset
2 SVG plotting extension for jQuery v1.4.5.
756
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
3 Written by Keith Wood (kbwood{at}iinet.com.au) December 2008.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
4 Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
6 Please attribute the author if you use it. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
7
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
8 (function($) { // Hide scope, no $ conflict
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
9
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
10 $.svg.addExtension('plot', SVGPlot);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
11
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
12 /* Extension point for SVG plotting.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
13 Access through svg.plot. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
14 function SVGPlot(wrapper) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
15 this._wrapper = wrapper; // The attached SVG wrapper object
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
16 this._drawNow = false; // True for immediate update, false to wait for redraw call
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
17 // The plot title and settings
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
18 this._title = {value: '', offset: 25, settings: {textAnchor: 'middle'}};
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
19 this._area = [0.1, 0.1, 0.8, 0.9]; // The chart area: left, top, right, bottom,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
20 // > 1 in pixels, <= 1 as proportion
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
21 this._areaFormat = {fill: 'none', stroke: 'black'}; // The formatting for the plot area
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
22 this._gridlines = []; // The formatting of the x- and y-gridlines
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
23 this._equalXY = true; // True for equal-sized x- and y-units, false to fill available space
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
24 this._functions = []; // The functions to be plotted, each is an object
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
25 this._onstatus = null; // The callback function for status updates
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
26 this._uuid = new Date().getTime();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
27 this._plotCont = this._wrapper.svg(0, 0, 0, 0, {class_: 'svg-plot'}); // The main container for the plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
28
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
29 this.xAxis = new SVGPlotAxis(this); // The main x-axis
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
30 this.xAxis.title('X', 20);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
31 this.yAxis = new SVGPlotAxis(this); // The main y-axis
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
32 this.yAxis.title('Y', 20);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
33 this.legend = new SVGPlotLegend(this); // The plot legend
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
34 this._drawNow = true;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
35 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
36
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
37 $.extend(SVGPlot.prototype, {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
38
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
39 /* Useful indexes. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
40 X: 0,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
41 Y: 1,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
42 W: 2,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
43 H: 3,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
44 L: 0,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
45 T: 1,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
46 R: 2,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
47 B: 3,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
48
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
49 /* Set or retrieve the container for the plot.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
50 @param cont (SVG element) the container for the plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
51 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
52 (SVG element) the current container (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
53 container: function(cont) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
54 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
55 return this._plotCont;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
56 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
57 this._plotCont = cont;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
58 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
59 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
60
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
61 /* Set or retrieve the main plotting area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
62 @param left (number) > 1 is pixels, <= 1 is proportion of width or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
63 (number[4]) for left, top, right, bottom
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
64 @param top (number) > 1 is pixels, <= 1 is proportion of height
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
65 @param right (number) > 1 is pixels, <= 1 is proportion of width
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
66 @param bottom (number) > 1 is pixels, <= 1 is proportion of height
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
67 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
68 (number[4]) the plotting area: left, top, right, bottom (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
69 area: function(left, top, right, bottom) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
70 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
71 return this._area;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
72 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
73 this._area = (isArray(left) ? left : [left, top, right, bottom]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
74 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
75 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
76 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
77
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
78 /* Set or retrieve the background of the plot area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
79 @param fill (string) how to fill the area background
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
80 @param stroke (string) the colour of the outline (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
81 @param settings (object) additional formatting for the area background (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
82 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
83 (object) the area format (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
84 format: function(fill, stroke, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
85 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
86 return this._areaFormat;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
87 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
88 if (typeof stroke == 'object') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
89 settings = stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
90 stroke = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
91 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
92 this._areaFormat = $.extend({fill: fill},
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
93 (stroke ? {stroke: stroke} : {}), settings || {});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
94 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
95 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
96 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
97
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
98 /* Set or retrieve the gridlines formatting for the plot area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
99 @param xSettings (string) the colour of the gridlines along the x-axis, or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
100 (object) formatting for the gridlines along the x-axis, or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
101 null for none
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
102 @param ySettings (string) the colour of the gridlines along the y-axis, or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
103 (object) formatting for the gridlines along the y-axis, or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
104 null for none
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
105 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
106 (object[2]) the gridlines formatting (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
107 gridlines: function(xSettings, ySettings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
108 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
109 return this._gridlines;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
110 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
111 this._gridlines = [(typeof xSettings == 'string' ? {stroke: xSettings} : xSettings),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
112 (typeof ySettings == 'string' ? {stroke: ySettings} : ySettings)];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
113 if (this._gridlines[0] == null && this._gridlines[1] == null) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
114 this._gridlines = [];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
115 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
116 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
117 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
118 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
119
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
120 /* Set or retrieve the equality of the x- and y-axes.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
121 @param value (boolean) true for equal x- and y-units, false to fill the available space
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
122 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
123 (boolean) the current setting (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
124 equalXY: function(value) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
125 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
126 return this._equalXY;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
127 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
128 this._equalXY = value;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
129 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
130 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
131
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
132 /* Set or retrieve the title of the plot and its formatting.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
133 @param value (string) the title
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
134 @param offset (number) the vertical positioning of the title
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
135 > 1 is pixels, <= 1 is proportion of width (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
136 @param colour (string) the colour of the title (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
137 @param settings (object) formatting for the title (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
138 @return (SVGPlot) this plot object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
139 (object) value, offset, and settings for the title (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
140 title: function(value, offset, colour, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
141 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
142 return this._title;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
143 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
144 if (typeof offset != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
145 settings = colour;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
146 colour = offset;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
147 offset = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
148 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
149 if (typeof colour != 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
150 settings = colour;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
151 colour = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
152 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
153 this._title = {value: value, offset: offset || this._title.offset,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
154 settings: $.extend({textAnchor: 'middle'},
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
155 (colour ? {fill: colour} : {}), settings || {})};
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
156 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
157 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
158 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
159
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
160 /* Add a function to be plotted on the plot.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
161 @param name (string) the name of this series (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
162 @param fn (function) the function to be plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
163 @param range (number[2]) the range of values to plot (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
164 @param points (number) the number of points to plot within this range (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
165 @param stroke (string) the colour of the plotted lines (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
166 @param strokeWidth (number) the width of the plotted lines (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
167 @param settings (object) additional settings for the plotted values (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
168 @return (SVGPlot) this plot object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
169 addFunction: function(name, fn, range, points, stroke, strokeWidth, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
170 this._functions.push(new SVGPlotFunction(
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
171 this, name, fn, range, points, stroke, strokeWidth, settings));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
172 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
173 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
174 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
175
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
176 /* Retrieve the function wrappers.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
177 @param i (number) the function index (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
178 @return (SVGPlotFunction) the specified function or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
179 (SVGPlotFunction[]) the list of functions */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
180 functions: function(i) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
181 return (arguments.length > 0 ? this._functions[i] : null) || this._functions;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
182 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
183
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
184 /* Suppress drawing of the plot until redraw() is called.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
185 @return (SVGPlot) this plot object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
186 noDraw: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
187 this._drawNow = false;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
188 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
189 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
190
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
191 /* Redraw the entire plot with the current settings and values.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
192 @return (SVGPlot) this plot object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
193 redraw: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
194 this._drawNow = true;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
195 this._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
196 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
197 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
198
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
199 /* Set the callback function for status updates.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
200 @param onstatus (function) the callback function
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
201 @return (SVGPlot) this plot object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
202 status: function(onstatus) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
203 this._onstatus = onstatus;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
204 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
205 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
206
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
207 /* Actually draw the plot (if allowed). */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
208 _drawPlot: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
209 if (!this._drawNow) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
210 return;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
211 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
212 while (this._plotCont.firstChild) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
213 this._plotCont.removeChild(this._plotCont.firstChild);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
214 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
215 if (!this._plotCont.parent) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
216 this._wrapper._svg.appendChild(this._plotCont);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
217 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
218 // Set sizes if not already there
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
219 if (!this._plotCont.width) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
220 this._plotCont.setAttribute('width',
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
221 parseInt(this._plotCont.getAttribute('width'), 10) || this._wrapper._width());
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
222 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
223 else if (this._plotCont.width.baseVal) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
224 this._plotCont.width.baseVal.value =
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
225 this._plotCont.width.baseVal.value || this._wrapper._width();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
226 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
227 else {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
228 this._plotCont.width = this._plotCont.width || this._wrapper._width();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
229 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
230 if (!this._plotCont.height) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
231 this._plotCont.setAttribute('height',
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
232 parseInt(this._plotCont.getAttribute('height'), 10) || this._wrapper._height());
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
233 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
234 else if (this._plotCont.height.baseVal) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
235 this._plotCont.height.baseVal.value =
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
236 this._plotCont.height.baseVal.value || this._wrapper._height();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
237 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
238 else {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
239 this._plotCont.height = this._plotCont.height || this._wrapper._height();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
240 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
241 this._drawChartBackground();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
242 var dims = this._getDims();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
243 var clip = this._wrapper.other(this._plotCont, 'clipPath', {id: 'clip' + this._uuid});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
244 this._wrapper.rect(clip, dims[this.X], dims[this.Y], dims[this.W], dims[this.H]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
245 this._plot = this._wrapper.group(this._plotCont,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
246 {class_: 'foreground', clipPath: 'url(#clip' + this._uuid + ')'});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
247 this._drawAxis(true);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
248 this._drawAxis(false);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
249 for (var i = 0; i < this._functions.length; i++) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
250 this._plotFunction(this._functions[i], i);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
251 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
252 this._drawTitle();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
253 this._drawLegend();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
254 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
255
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
256 /* Decode an attribute value.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
257 @param node the node to examine
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
258 @param name the attribute name
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
259 @return the actual value */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
260 _getValue: function(node, name) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
261 return (!node[name] ? parseInt(node.getAttribute(name), 10) :
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
262 (node[name].baseVal ? node[name].baseVal.value : node[name]));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
263 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
264
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
265 /* Calculate the actual dimensions of the plot area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
266 @param area (number[4]) the area values to evaluate (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
267 @return (number[4]) an array of dimension values: left, top, width, height */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
268 _getDims: function(area) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
269 var otherArea = (area != null);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
270 area = area || this._area;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
271 var availWidth = this._getValue(this._plotCont, 'width');
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
272 var availHeight = this._getValue(this._plotCont, 'height');
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
273 var left = (area[this.L] > 1 ? area[this.L] : availWidth * area[this.L]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
274 var top = (area[this.T] > 1 ? area[this.T] : availHeight * area[this.T]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
275 var width = (area[this.R] > 1 ? area[this.R] : availWidth * area[this.R]) - left;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
276 var height = (area[this.B] > 1 ? area[this.B] : availHeight * area[this.B]) - top;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
277 if (this._equalXY && !otherArea) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
278 var scale = Math.min(width / (this.xAxis._scale.max - this.xAxis._scale.min),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
279 height / (this.yAxis._scale.max - this.yAxis._scale.min));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
280 width = scale * (this.xAxis._scale.max - this.xAxis._scale.min);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
281 height = scale * (this.yAxis._scale.max - this.yAxis._scale.min);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
282 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
283 return [left, top, width, height];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
284 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
285
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
286 /* Calculate the scaling factors for the plot area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
287 @return (number[2]) the x- and y-scaling factors */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
288 _getScales: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
289 var dims = this._getDims();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
290 return [dims[this.W] / (this.xAxis._scale.max - this.xAxis._scale.min),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
291 dims[this.H] / (this.yAxis._scale.max - this.yAxis._scale.min)];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
292 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
293
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
294 /* Draw the chart background, including gridlines.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
295 @param noXGrid (boolean) true to suppress the x-gridlines, false to draw them (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
296 @param noYGrid (boolean) true to suppress the y-gridlines, false to draw them (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
297 @return (element) the background group element */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
298 _drawChartBackground: function(noXGrid, noYGrid) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
299 var bg = this._wrapper.group(this._plotCont, {class_: 'background'});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
300 var dims = this._getDims();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
301 this._wrapper.rect(bg, dims[this.X], dims[this.Y], dims[this.W], dims[this.H], this._areaFormat);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
302 if (this._gridlines[0] && this.yAxis._ticks.major && !noYGrid) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
303 this._drawGridlines(bg, true, this._gridlines[0], dims);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
304 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
305 if (this._gridlines[1] && this.xAxis._ticks.major && !noXGrid) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
306 this._drawGridlines(bg, false, this._gridlines[1], dims);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
307 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
308 return bg;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
309 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
310
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
311 /* Draw one set of gridlines.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
312 @param bg (element) the background group element
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
313 @param horiz (boolean) true if horizontal, false if vertical
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
314 @param format (object) additional settings for the gridlines */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
315 _drawGridlines: function(bg, horiz, format, dims) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
316 var g = this._wrapper.group(bg, format);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
317 var axis = (horiz ? this.yAxis : this.xAxis);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
318 var scales = this._getScales();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
319 var major = Math.floor(axis._scale.min / axis._ticks.major) * axis._ticks.major;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
320 major += (major <= axis._scale.min ? axis._ticks.major : 0);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
321 while (major < axis._scale.max) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
322 var v = (horiz ? axis._scale.max - major : major - axis._scale.min) *
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
323 scales[horiz ? 1 : 0] + (horiz ? dims[this.Y] : dims[this.X]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
324 this._wrapper.line(g, (horiz ? dims[this.X] : v), (horiz ? v : dims[this.Y]),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
325 (horiz ? dims[this.X] + dims[this.W] : v), (horiz ? v : dims[this.Y] + dims[this.H]));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
326 major += axis._ticks.major;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
327 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
328 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
329
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
330 /* Draw an axis, its tick marks, and title.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
331 @param horiz (boolean) true for x-axis, false for y-axis */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
332 _drawAxis: function(horiz) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
333 var id = (horiz ? 'x' : 'y') + 'Axis';
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
334 var axis = (horiz ? this.xAxis : this.yAxis);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
335 var axis2 = (horiz ? this.yAxis : this.xAxis);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
336 var dims = this._getDims();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
337 var scales = this._getScales();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
338 var gl = this._wrapper.group(this._plot, $.extend({class_: id}, axis._lineFormat));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
339 var gt = this._wrapper.group(this._plot, $.extend({class_: id + 'Labels',
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
340 textAnchor: (horiz ? 'middle' : 'end')}, axis._labelFormat));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
341 var zero = (horiz ? axis2._scale.max : -axis2._scale.min) *
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
342 scales[horiz ? 1 : 0] + (horiz ? dims[this.Y] : dims[this.X]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
343 this._wrapper.line(gl, (horiz ? dims[this.X] : zero), (horiz ? zero : dims[this.Y]),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
344 (horiz ? dims[this.X] + dims[this.W] : zero),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
345 (horiz ? zero : dims[this.Y] + dims[this.H]));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
346 if (axis._ticks.major) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
347 var size = axis._ticks.size;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
348 var major = Math.floor(axis._scale.min / axis._ticks.major) * axis._ticks.major;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
349 major = (major < axis._scale.min ? major + axis._ticks.major : major);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
350 var minor = (!axis._ticks.minor ? axis._scale.max + 1 :
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
351 Math.floor(axis._scale.min / axis._ticks.minor) * axis._ticks.minor);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
352 minor = (minor < axis._scale.min ? minor + axis._ticks.minor : minor);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
353 var offsets = [(axis._ticks.position == 'nw' || axis._ticks.position == 'both' ? -1 : 0),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
354 (axis._ticks.position == 'se' || axis._ticks.position == 'both' ? +1 : 0)];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
355 while (major <= axis._scale.max || minor <= axis._scale.max) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
356 var cur = Math.min(major, minor);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
357 var len = (cur == major ? size : size / 2);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
358 var xy = (horiz ? cur - axis._scale.min : axis._scale.max - cur) *
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
359 scales[horiz ? 0 : 1] + (horiz ? dims[this.X] : dims[this.Y]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
360 this._wrapper.line(gl, (horiz ? xy : zero + len * offsets[0]),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
361 (horiz ? zero + len * offsets[0] : xy),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
362 (horiz ? xy : zero + len * offsets[1]),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
363 (horiz ? zero + len * offsets[1] : xy));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
364 if (cur == major && cur != 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
365 this._wrapper.text(gt, (horiz ? xy : zero - size),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
366 (horiz ? zero - size : xy), '' + cur);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
367 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
368 major += (cur == major ? axis._ticks.major : 0);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
369 minor += (cur == minor ? axis._ticks.minor : 0);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
370 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
371 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
372 if (axis._title) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
373 if (horiz) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
374 this._wrapper.text(this._plotCont, dims[this.X] - axis._titleOffset,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
375 zero, axis._title, $.extend({textAnchor: 'end'}, axis._titleFormat || {}));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
376 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
377 else {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
378 this._wrapper.text(this._plotCont, zero,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
379 dims[this.Y] + dims[this.H] + axis._titleOffset,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
380 axis._title, $.extend({textAnchor : 'middle'}, axis._titleFormat || {}));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
381 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
382 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
383 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
384
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
385 /* Plot an individual function. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
386 _plotFunction: function(fn, cur) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
387 var dims = this._getDims();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
388 var scales = this._getScales();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
389 var path = this._wrapper.createPath();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
390 var range = fn._range || [this.xAxis._scale.min, this.xAxis._scale.max];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
391 var xScale = (range[1] - range[0]) / fn._points;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
392 var first = true;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
393 for (var i = 0; i <= fn._points; i++) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
394 var x = range[0] + i * xScale;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
395 if (x > this.xAxis._scale.max + xScale) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
396 break;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
397 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
398 if (x < this.xAxis._scale.min - xScale) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
399 continue;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
400 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
401 var px = (x - this.xAxis._scale.min) * scales[0] + dims[this.X];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
402 var py = dims[this.H] - ((fn._fn(x) - this.yAxis._scale.min) * scales[1]) + dims[this.Y];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
403 path[(first ? 'move' : 'line') + 'To'](px, py);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
404 first = false;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
405 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
406 var p = this._wrapper.path(this._plot, path,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
407 $.extend({class_: 'fn' + cur, fill: 'none', stroke: fn._stroke,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
408 strokeWidth: fn._strokeWidth}, fn._settings || {}));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
409 this._showStatus(p, fn._name);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
410 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
411
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
412 /* Draw the plot title - centred. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
413 _drawTitle: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
414 this._wrapper.text(this._plotCont, this._getValue(this._plotCont, 'width') / 2,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
415 this._title.offset, this._title.value, this._title.settings);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
416 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
417
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
418 /* Draw the chart legend. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
419 _drawLegend: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
420 if (!this.legend._show) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
421 return;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
422 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
423 var g = this._wrapper.group(this._plotCont, {class_: 'legend'});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
424 var dims = this._getDims(this.legend._area);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
425 this._wrapper.rect(g, dims[this.X], dims[this.Y], dims[this.W], dims[this.H],
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
426 this.legend._bgSettings);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
427 var horiz = dims[this.W] > dims[this.H];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
428 var numFn = this._functions.length;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
429 var offset = (horiz ? dims[this.W] : dims[this.H]) / numFn;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
430 var xBase = dims[this.X] + 5;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
431 var yBase = dims[this.Y] + ((horiz ? dims[this.H] : offset) + this.legend._sampleSize) / 2;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
432 for (var i = 0; i < numFn; i++) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
433 var fn = this._functions[i];
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
434 this._wrapper.rect(g, xBase + (horiz ? i * offset : 0),
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
435 yBase + (horiz ? 0 : i * offset) - this.legend._sampleSize,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
436 this.legend._sampleSize, this.legend._sampleSize, {fill: fn._stroke});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
437 this._wrapper.text(g, xBase + (horiz ? i * offset : 0) + this.legend._sampleSize + 5,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
438 yBase + (horiz ? 0 : i * offset), fn._name, this.legend._textSettings);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
439 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
440 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
441
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
442 /* Show the current value status on hover. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
443 _showStatus: function(elem, label) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
444 var status = this._onstatus;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
445 if (this._onstatus) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
446 $(elem).hover(function(evt) { status.apply(this, [label]); },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
447 function() { status.apply(this, ['']); });
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
448 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
449 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
450 });
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
451
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
452 /* Details about each plot function.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
453 @param plot (SVGPlot) the owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
454 @param name (string) the name of this function (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
455 @param fn (function) the function to be plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
456 @param range (number[2]) the range of values to be plotted (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
457 @param points (number) the number of points to plot within this range (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
458 @param stroke (string) the colour of the (out)line for the plot (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
459 @param strokeWidth (number) the width of the (out)line for the plot (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
460 @param settings (object) additional formatting settings (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
461 @return (SVGPlotFunction) the new plot function object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
462 function SVGPlotFunction(plot, name, fn, range, points, stroke, strokeWidth, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
463 if (typeof name != 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
464 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
465 strokeWidth = stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
466 stroke = points;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
467 points = range;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
468 range = fn;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
469 fn = name;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
470 name = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
471 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
472 if (!isArray(range)) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
473 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
474 strokeWidth = stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
475 stroke = points;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
476 points = range;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
477 range = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
478 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
479 if (typeof points != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
480 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
481 strokeWidth = stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
482 stroke = points;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
483 points = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
484 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
485 if (typeof stroke != 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
486 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
487 strokeWidth = stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
488 stroke = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
489 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
490 if (typeof strokeWidth != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
491 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
492 strokeWidth = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
493 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
494 this._plot = plot; // The owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
495 this._name = name || ''; // Display name
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
496 this._fn = fn || identity; // The actual function: y = fn(x)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
497 this._range = range; // The range of values plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
498 this._points = points || 100; // The number of points plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
499 this._stroke = stroke || 'black'; // The line colour
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
500 this._strokeWidth = strokeWidth || 1; // The line width
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
501 this._settings = settings || {}; // Any other settings
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
502 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
503
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
504 $.extend(SVGPlotFunction.prototype, {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
505
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
506 /* Set or retrieve the name for this function.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
507 @param name (string) the function's name
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
508 @return (SVGPlotFunction) this plot function object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
509 (string) the function name (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
510 name: function(name) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
511 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
512 return this._name;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
513 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
514 this._name = name;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
515 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
516 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
517 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
518
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
519 /* Set or retrieve the function to be plotted.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
520 @param name (string) the function's name (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
521 @param fn (function) the function to be ploted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
522 @return (SVGPlotFunction) this plot function object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
523 (function) the actual function (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
524 fn: function(name, fn) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
525 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
526 return this._fn;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
527 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
528 if (typeof name == 'function') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
529 fn = name;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
530 name = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
531 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
532 this._name = name || this._name;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
533 this._fn = fn;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
534 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
535 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
536 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
537
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
538 /* Set or retrieve the range of values to be plotted.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
539 @param min (number) the minimum value to be plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
540 @param max (number) the maximum value to be plotted
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
541 @return (SVGPlotFunction) this plot function object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
542 (number[2]) the value range (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
543 range: function(min, max) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
544 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
545 return this._range;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
546 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
547 this._range = (min == null ? null : [min, max]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
548 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
549 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
550 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
551
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
552 /* Set or retrieve the number of points to be plotted.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
553 @param value (number) the number of points to plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
554 @return (SVGPlotFunction) this plot function object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
555 (number) the number of points (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
556 points: function(value) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
557 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
558 return this._points;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
559 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
560 this._points = value;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
561 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
562 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
563 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
564
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
565 /* Set or retrieve the formatting for this function.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
566 @param stroke (string) the (out)line colour
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
567 @param strokeWidth (number) the line's width (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
568 @param settings (object) additional formatting settings for the function (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
569 @return (SVGPlotFunction) this plot function object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
570 (object) formatting settings (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
571 format: function(stroke, strokeWidth, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
572 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
573 return $.extend({stroke: this._stroke,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
574 strokeWidth: this._strokeWidth}, this._settings);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
575 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
576 if (typeof strokeWidth != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
577 settings = strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
578 strokeWidth = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
579 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
580 this._stroke = stroke || this._stroke;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
581 this._strokeWidth = strokeWidth || this._strokeWidth;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
582 $.extend(this._settings, settings || {});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
583 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
584 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
585 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
586
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
587 /* Return to the parent plot. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
588 end: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
589 return this._plot;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
590 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
591 });
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
592
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
593 /* Default function to plot.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
594 @param x (number) the input value
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
595 @return (number) the same value */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
596 function identity(x) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
597 return x;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
598 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
599
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
600 /* Details about each plot axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
601 @param plot (SVGPlot) the owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
602 @param title (string) the title of the axis
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
603 @param min (number) the minimum value displayed on this axis
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
604 @param max (number) the maximum value displayed on this axis
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
605 @param major (number) the distance between major ticks
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
606 @param minor (number) the distance between minor ticks (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
607 @return (SVGPlotAxis) the new axis object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
608 function SVGPlotAxis(plot, title, min, max, major, minor) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
609 this._plot = plot; // The owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
610 this._title = title || ''; // The plot's title
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
611 this._titleFormat = {}; // Formatting settings for the title
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
612 this._titleOffset = 0; // The offset for positioning the title
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
613 this._labelFormat = {}; // Formatting settings for the labels
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
614 this._lineFormat = {stroke: 'black', strokeWidth: 1}; // Formatting settings for the axis lines
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
615 this._ticks = {major: major || 10, minor: minor || 0, size: 10, position: 'both'}; // Tick mark options
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
616 this._scale = {min: min || 0, max: max || 100}; // Axis scale settings
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
617 this._crossAt = 0; // Where this axis crosses the other one. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
618 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
619
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
620 $.extend(SVGPlotAxis.prototype, {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
621
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
622 /* Set or retrieve the scale for this axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
623 @param min (number) the minimum value shown
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
624 @param max (number) the maximum value shown
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
625 @return (SVGPlotAxis) this axis object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
626 (object) min and max values (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
627 scale: function(min, max) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
628 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
629 return this._scale;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
630 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
631 this._scale.min = min;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
632 this._scale.max = max;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
633 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
634 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
635 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
636
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
637 /* Set or retrieve the ticks for this axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
638 @param major (number) the distance between major ticks
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
639 @param minor (number) the distance between minor ticks
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
640 @param size (number) the length of the major ticks (minor are half) (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
641 @param position (string) the location of the ticks:
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
642 'nw', 'se', 'both' (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
643 @return (SVGPlotAxis) this axis object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
644 (object) major, minor, size, and position values (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
645 ticks: function(major, minor, size, position) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
646 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
647 return this._ticks;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
648 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
649 if (typeof size == 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
650 position = size;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
651 size = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
652 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
653 this._ticks.major = major;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
654 this._ticks.minor = minor;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
655 this._ticks.size = size || this._ticks.size;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
656 this._ticks.position = position || this._ticks.position;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
657 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
658 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
659 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
660
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
661 /* Set or retrieve the title for this axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
662 @param title (string) the title text
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
663 @param offset (number) the distance to offset the title position (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
664 @param colour (string) how to colour the title (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
665 @param format (object) formatting settings for the title (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
666 @return (SVGPlotAxis) this axis object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
667 (object) title, offset, and format values (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
668 title: function(title, offset, colour, format) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
669 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
670 return {title: this._title, offset: this._titleOffset, format: this._titleFormat};
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
671 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
672 if (typeof offset != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
673 format = colour;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
674 colour = offset;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
675 offset = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
676 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
677 if (typeof colour != 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
678 format = colour;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
679 colour = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
680 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
681 this._title = title;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
682 this._titleOffset = (offset != null ? offset : this._titleOffset);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
683 if (colour || format) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
684 this._titleFormat = $.extend(format || {}, (colour ? {fill: colour} : {}));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
685 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
686 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
687 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
688 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
689
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
690 /* Set or retrieve the label format for this axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
691 @param colour (string) how to colour the labels (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
692 @param format (object) formatting settings for the labels (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
693 @return (SVGPlotAxis) this axis object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
694 (object) format values (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
695 format: function(colour, format) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
696 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
697 return this._labelFormat;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
698 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
699 if (typeof colour != 'string') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
700 format = colour;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
701 colour = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
702 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
703 this._labelFormat = $.extend(format || {}, (colour ? {fill: colour} : {}));
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
704 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
705 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
706 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
707
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
708 /* Set or retrieve the line formatting for this axis.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
709 @param colour (string) the line's colour
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
710 @param width (number) the line's width (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
711 @param settings (object) additional formatting settings for the line (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
712 @return (SVGPlotAxis) this axis object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
713 (object) line formatting values (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
714 line: function(colour, width, settings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
715 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
716 return this._lineFormat;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
717 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
718 if (typeof width != 'number') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
719 settings = width;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
720 width = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
721 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
722 $.extend(this._lineFormat, {stroke: colour, strokeWidth:
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
723 width || this._lineFormat.strokeWidth}, settings || {});
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
724 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
725 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
726 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
727
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
728 /* Return to the parent plot. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
729 end: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
730 return this._plot;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
731 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
732 });
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
733
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
734 /* Details about the plot legend.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
735 @param plot (SVGPlot) the owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
736 @param bgSettings (object) additional formatting settings for the legend background (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
737 @param textSettings (object) additional formatting settings for the legend text (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
738 @return (SVGPlotLegend) the new legend object */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
739 function SVGPlotLegend(plot, bgSettings, textSettings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
740 this._plot = plot; // The owning plot
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
741 this._show = true; // Show the legend?
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
742 this._area = [0.9, 0.1, 1.0, 0.9]; // The legend area: left, top, right, bottom,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
743 // > 1 in pixels, <= 1 as proportion
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
744 this._sampleSize = 15; // Size of sample box
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
745 this._bgSettings = bgSettings || {stroke: 'gray'}; // Additional formatting settings for the legend background
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
746 this._textSettings = textSettings || {}; // Additional formatting settings for the text
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
747 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
748
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
749 $.extend(SVGPlotLegend.prototype, {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
750
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
751 /* Set or retrieve whether the legend should be shown.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
752 @param show (boolean) true to display it, false to hide it
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
753 @return (SVGPlotLegend) this legend object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
754 (boolean) show the legend? (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
755 show: function(show) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
756 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
757 return this._show;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
758 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
759 this._show = show;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
760 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
761 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
762 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
763
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
764 /* Set or retrieve the legend area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
765 @param left (number) > 1 is pixels, <= 1 is proportion of width or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
766 (number[4]) for left, top, right, bottom
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
767 @param top (number) > 1 is pixels, <= 1 is proportion of height
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
768 @param right (number) > 1 is pixels, <= 1 is proportion of width
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
769 @param bottom (number) > 1 is pixels, <= 1 is proportion of height
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
770 @return (SVGPlotLegend) this legend object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
771 (number[4]) the legend area: left, top, right, bottom (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
772 area: function(left, top, right, bottom) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
773 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
774 return this._area;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
775 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
776 this._area = (isArray(left) ? left : [left, top, right, bottom]);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
777 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
778 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
779 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
780
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
781 /* Set or retrieve additional settings for the legend area.
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
782 @param sampleSize (number) the size of the sample box to display (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
783 @param bgSettings (object) additional formatting settings for the legend background
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
784 @param textSettings (object) additional formatting settings for the legend text (optional)
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
785 @return (SVGPlotLegend) this legend object or
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
786 (object) bgSettings and textSettings for the legend (if no parameters) */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
787 settings: function(sampleSize, bgSettings, textSettings) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
788 if (arguments.length == 0) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
789 return {sampleSize: this._sampleSize, bgSettings: this._bgSettings,
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
790 textSettings: this._textSettings};
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
791 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
792 if (typeof sampleSize == 'object') {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
793 textSettings = bgSettings;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
794 bgSettings = sampleSize;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
795 sampleSize = null;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
796 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
797 this._sampleSize = sampleSize || this._sampleSize;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
798 this._bgSettings = bgSettings;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
799 this._textSettings = textSettings || this._textSettings;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
800 this._plot._drawPlot();
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
801 return this;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
802 },
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
803
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
804 /* Return to the parent plot. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
805 end: function() {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
806 return this._plot;
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
807 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
808 });
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
809
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
810 //==============================================================================
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
811
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
812 /* Determine whether an object is an array. */
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
813 function isArray(a) {
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
814 return (a && a.constructor == Array);
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
815 }
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
816
ccf67eaf97ee added jQuery ui and svg javascripts
hertzhaft
parents:
diff changeset
817 })(jQuery)