annotate static/jquery.flot.js @ 3:942d4cfc2538

Add static alias. Correct typos in demo paths.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 15 Aug 2011 19:05:23 +0930
parents 2d9ee2b3ae82
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /* Javascript plotting library for jQuery, v. 0.6.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 * Released under the MIT license by IOLA, December 2007.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 */
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 // first an inline dependency, jquery.colorhelpers.js, we inline it here
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 // for convenience
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 /* Plugin for jQuery for working with colors.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 * Version 1.0.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 * Inspiration from jQuery color animation plugin by John Resig.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 * Released under the MIT license by Ole Laursen, October 2009.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 * Examples:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 * var c = $.color.extract($("#mydiv"), 'background-color');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 * console.log(c.r, c.g, c.b, c.a);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 *
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 * Note that .scale() and .add() work in-place instead of returning
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 * new objects.
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 */
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 (function(){jQuery.color={};jQuery.color.make=function(E,D,B,C){var F={};F.r=E||0;F.g=D||0;F.b=B||0;F.a=C!=null?C:1;F.add=function(I,H){for(var G=0;G<I.length;++G){F[I.charAt(G)]+=H}return F.normalize()};F.scale=function(I,H){for(var G=0;G<I.length;++G){F[I.charAt(G)]*=H}return F.normalize()};F.toString=function(){if(F.a>=1){return"rgb("+[F.r,F.g,F.b].join(",")+")"}else{return"rgba("+[F.r,F.g,F.b,F.a].join(",")+")"}};F.normalize=function(){function G(I,J,H){return J<I?I:(J>H?H:J)}F.r=G(0,parseInt(F.r),255);F.g=G(0,parseInt(F.g),255);F.b=G(0,parseInt(F.b),255);F.a=G(0,F.a,1);return F};F.clone=function(){return jQuery.color.make(F.r,F.b,F.g,F.a)};return F.normalize()};jQuery.color.extract=function(C,B){var D;do{D=C.css(B).toLowerCase();if(D!=""&&D!="transparent"){break}C=C.parent()}while(!jQuery.nodeName(C.get(0),"body"));if(D=="rgba(0, 0, 0, 0)"){D="transparent"}return jQuery.color.parse(D)};jQuery.color.parse=function(E){var D,B=jQuery.color.make;if(D=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(E)){return B(parseInt(D[1],10),parseInt(D[2],10),parseInt(D[3],10))}if(D=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(E)){return B(parseInt(D[1],10),parseInt(D[2],10),parseInt(D[3],10),parseFloat(D[4]))}if(D=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(E)){return B(parseFloat(D[1])*2.55,parseFloat(D[2])*2.55,parseFloat(D[3])*2.55)}if(D=/rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(E)){return B(parseFloat(D[1])*2.55,parseFloat(D[2])*2.55,parseFloat(D[3])*2.55,parseFloat(D[4]))}if(D=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(E)){return B(parseInt(D[1],16),parseInt(D[2],16),parseInt(D[3],16))}if(D=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(E)){return B(parseInt(D[1]+D[1],16),parseInt(D[2]+D[2],16),parseInt(D[3]+D[3],16))}var C=jQuery.trim(E).toLowerCase();if(C=="transparent"){return B(255,255,255,0)}else{D=A[C];return B(D[0],D[1],D[2])}};var A={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 // the actual Flot code
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 (function($) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 function Plot(placeholder, data_, options_, plugins) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 // data is on the form:
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 // [ series1, series2 ... ]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 // where series is either just the data as [ [x1, y1], [x2, y2], ... ]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 var series = [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 options = {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 // the color theme used for graphs
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 legend: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 show: true,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 noColumns: 1, // number of colums in legend table
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 labelFormatter: null, // fn: string -> string
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 labelBoxBorderColor: "#ccc", // border color for the little label boxes
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 container: null, // container (as jQuery object) to put legend in, null means default on top of graph
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 position: "ne", // position of default legend container within plot
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 margin: 5, // distance from grid edge to default legend container within plot
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 backgroundColor: null, // null means auto-detect
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 backgroundOpacity: 0.85 // set to 0 to avoid background
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 xaxis: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 mode: null, // null or "time"
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 transform: null, // null or f: number -> number to transform axis
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 inverseTransform: null, // if transform is set, this should be the inverse function
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 min: null, // min. value to show, null means set automatically
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 max: null, // max. value to show, null means set automatically
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 autoscaleMargin: null, // margin in % to add if auto-setting min/max
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 tickFormatter: null, // fn: number -> string
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 labelWidth: null, // size of tick labels in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 labelHeight: null,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 // mode specific options
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 tickDecimals: null, // no. of decimals, null means auto
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 tickSize: null, // number or [number, "unit"]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 minTickSize: null, // number or [number, "unit"]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 monthNames: null, // list of names of months
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 timeformat: null, // format string to use
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 twelveHourClock: false // 12 or 24 time in time mode
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 yaxis: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 autoscaleMargin: 0.02
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 x2axis: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 autoscaleMargin: null
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 y2axis: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 autoscaleMargin: 0.02
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 series: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 points: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 show: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 radius: 3,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 lineWidth: 2, // in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 fill: true,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 fillColor: "#ffffff"
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 lines: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 // we don't put in show: false so we can see
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 // whether lines were actively disabled
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 lineWidth: 2, // in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 fill: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 fillColor: null,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 steps: false
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 bars: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 show: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 lineWidth: 2, // in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 barWidth: 1, // in units of the x axis
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 fill: true,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 fillColor: null,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 align: "left", // or "center"
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 horizontal: false // when horizontal, left is now top
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 shadowSize: 3
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 grid: {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 show: true,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 aboveData: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 color: "#545454", // primary color used for outline and labels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 backgroundColor: null, // null for transparent, else color
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 tickColor: "rgba(0,0,0,0.15)", // color used for the ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 labelMargin: 5, // in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 borderWidth: 2, // in pixels
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 borderColor: null, // set if different from the grid color
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 markings: null, // array of ranges or fn: axes -> array of ranges
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 markingsColor: "#f4f4f4",
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 markingsLineWidth: 2,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 // interactive stuff
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 clickable: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 hoverable: false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 autoHighlight: true, // highlight in case mouse is near
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 mouseActiveRadius: 10 // how far the mouse can be away to activate an item
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 hooks: {}
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 canvas = null, // the canvas for the plot itself
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 overlay = null, // canvas for interactive stuff on top of plot
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 eventHolder = null, // jQuery object that events should be bound to
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 ctx = null, octx = null,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 axes = { xaxis: {}, yaxis: {}, x2axis: {}, y2axis: {} },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
134 plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 canvasWidth = 0, canvasHeight = 0,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 plotWidth = 0, plotHeight = 0,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 hooks = {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 processOptions: [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 processRawData: [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 processDatapoints: [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 draw: [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 bindEvents: [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 drawOverlay: []
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 plot = this;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 // public functions
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 plot.setData = setData;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 plot.setupGrid = setupGrid;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 plot.draw = draw;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 plot.getPlaceholder = function() { return placeholder; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 plot.getCanvas = function() { return canvas; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 plot.getPlotOffset = function() { return plotOffset; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 plot.width = function () { return plotWidth; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 plot.height = function () { return plotHeight; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 plot.offset = function () {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 var o = eventHolder.offset();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 o.left += plotOffset.left;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 o.top += plotOffset.top;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 return o;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 plot.getData = function() { return series; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 plot.getAxes = function() { return axes; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 plot.getOptions = function() { return options; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 plot.highlight = highlight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 plot.unhighlight = unhighlight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 plot.triggerRedrawOverlay = triggerRedrawOverlay;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 plot.pointOffset = function(point) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 return { left: parseInt(axisSpecToRealAxis(point, "xaxis").p2c(+point.x) + plotOffset.left),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 top: parseInt(axisSpecToRealAxis(point, "yaxis").p2c(+point.y) + plotOffset.top) };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 // public attributes
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 plot.hooks = hooks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 // initialize
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 initPlugins(plot);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 parseOptions(options_);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 constructCanvas();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 setData(data_);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 setupGrid();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 draw();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 bindEvents();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 function executeHooks(hook, args) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 args = [plot].concat(args);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 for (var i = 0; i < hook.length; ++i)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 hook[i].apply(this, args);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 function initPlugins() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 for (var i = 0; i < plugins.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 var p = plugins[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 p.init(plot);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 if (p.options)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 $.extend(true, options, p.options);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 function parseOptions(opts) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 $.extend(true, options, opts);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204 if (options.grid.borderColor == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 options.grid.borderColor = options.grid.color;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 // backwards compatibility, to be removed in future
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 if (options.xaxis.noTicks && options.xaxis.ticks == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208 options.xaxis.ticks = options.xaxis.noTicks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 if (options.yaxis.noTicks && options.yaxis.ticks == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 options.yaxis.ticks = options.yaxis.noTicks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211 if (options.grid.coloredAreas)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 options.grid.markings = options.grid.coloredAreas;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 if (options.grid.coloredAreasColor)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214 options.grid.markingsColor = options.grid.coloredAreasColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 if (options.lines)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 $.extend(true, options.series.lines, options.lines);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 if (options.points)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 $.extend(true, options.series.points, options.points);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 if (options.bars)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 $.extend(true, options.series.bars, options.bars);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 if (options.shadowSize)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 options.series.shadowSize = options.shadowSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 for (var n in hooks)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 if (options.hooks[n] && options.hooks[n].length)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 hooks[n] = hooks[n].concat(options.hooks[n]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 executeHooks(hooks.processOptions, [options]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 function setData(d) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 series = parseData(d);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 fillInSeriesOptions();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 processData();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 function parseData(d) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 var res = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 for (var i = 0; i < d.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 var s = $.extend(true, {}, options.series);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 if (d[i].data) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 s.data = d[i].data; // move the data instead of deep-copy
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 delete d[i].data;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246 $.extend(true, s, d[i]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 d[i].data = s.data;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 s.data = d[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 res.push(s);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 return res;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 function axisSpecToRealAxis(obj, attr) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 var a = obj[attr];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 if (!a || a == 1)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 return axes[attr];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 if (typeof a == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263 return axes[attr.charAt(0) + a + attr.slice(1)];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 return a; // assume it's OK
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 function fillInSeriesOptions() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 var i;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 // collect what we already got of colors
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 var neededColors = series.length,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 usedColors = [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 assignedColors = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 var sc = series[i].color;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 if (sc != null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 --neededColors;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 if (typeof sc == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 assignedColors.push(sc);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 usedColors.push($.color.parse(series[i].color));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285 // we might need to generate more colors if higher indices
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 // are assigned
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 for (i = 0; i < assignedColors.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288 neededColors = Math.max(neededColors, assignedColors[i] + 1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
290
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
291 // produce colors as needed
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
292 var colors = [], variation = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
293 i = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
294 while (colors.length < neededColors) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
295 var c;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
296 if (options.colors.length == i) // check degenerate case
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
297 c = $.color.make(100, 100, 100);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
298 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
299 c = $.color.parse(options.colors[i]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
300
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
301 // vary color if needed
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
302 var sign = variation % 2 == 1 ? -1 : 1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
303 c.scale('rgb', 1 + sign * Math.ceil(variation / 2) * 0.2)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
304
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
305 // FIXME: if we're getting to close to something else,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
306 // we should probably skip this one
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
307 colors.push(c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
308
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
309 ++i;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
310 if (i >= options.colors.length) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
311 i = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
312 ++variation;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
313 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
314 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
315
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
316 // fill in the options
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
317 var colori = 0, s;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
318 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
320
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 // assign colors
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 if (s.color == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323 s.color = colors[colori].toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 ++colori;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
325 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 else if (typeof s.color == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
327 s.color = colors[s.color].toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
328
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 // turn on lines automatically in case nothing is set
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
330 if (s.lines.show == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 var v, show = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
332 for (v in s)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 if (s[v].show) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334 show = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337 if (show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 s.lines.show = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
340
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 // setup axes
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
342 s.xaxis = axisSpecToRealAxis(s, "xaxis");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343 s.yaxis = axisSpecToRealAxis(s, "yaxis");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
345 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
346
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347 function processData() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 var topSentry = Number.POSITIVE_INFINITY,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 bottomSentry = Number.NEGATIVE_INFINITY,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350 i, j, k, m, length,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351 s, points, ps, x, y, axis, val, f, p;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
352
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353 for (axis in axes) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 axes[axis].datamin = topSentry;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
355 axes[axis].datamax = bottomSentry;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
356 axes[axis].used = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
357 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
359 function updateAxis(axis, min, max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
360 if (min < axis.datamin)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361 axis.datamin = min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
362 if (max > axis.datamax)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
363 axis.datamax = max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
364 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
365
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
366 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
367 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
368 s.datapoints = { points: [] };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
369
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
370 executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
371 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
372
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
373 // first pass: clean and copy data
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
374 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
375 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
376
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
377 var data = s.data, format = s.datapoints.format;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
378
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
379 if (!format) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
380 format = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
381 // find out how to copy
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
382 format.push({ x: true, number: true, required: true });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
383 format.push({ y: true, number: true, required: true });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
385 if (s.bars.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
386 format.push({ y: true, number: true, required: false, defaultValue: 0 });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
387
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
388 s.datapoints.format = format;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
389 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
390
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
391 if (s.datapoints.pointsize != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
392 continue; // already filled in
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
393
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
394 if (s.datapoints.pointsize == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
395 s.datapoints.pointsize = format.length;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
396
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
397 ps = s.datapoints.pointsize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
398 points = s.datapoints.points;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
400 insertSteps = s.lines.show && s.lines.steps;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401 s.xaxis.used = s.yaxis.used = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
402
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
403 for (j = k = 0; j < data.length; ++j, k += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
404 p = data[j];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
405
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
406 var nullify = p == null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
407 if (!nullify) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
408 for (m = 0; m < ps; ++m) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
409 val = p[m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
410 f = format[m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
411
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
412 if (f) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
413 if (f.number && val != null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
414 val = +val; // convert to number
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
415 if (isNaN(val))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
416 val = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
417 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
418
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
419 if (val == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
420 if (f.required)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
421 nullify = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
422
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
423 if (f.defaultValue != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
424 val = f.defaultValue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
425 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
426 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
427
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
428 points[k + m] = val;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
429 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
430 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
431
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
432 if (nullify) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
433 for (m = 0; m < ps; ++m) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
434 val = points[k + m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
435 if (val != null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
436 f = format[m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
437 // extract min/max info
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
438 if (f.x)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
439 updateAxis(s.xaxis, val, val);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
440 if (f.y)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
441 updateAxis(s.yaxis, val, val);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
442 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
443 points[k + m] = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
444 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
445 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
446 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
447 // a little bit of line specific stuff that
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
448 // perhaps shouldn't be here, but lacking
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
449 // better means...
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
450 if (insertSteps && k > 0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
451 && points[k - ps] != null
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
452 && points[k - ps] != points[k]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
453 && points[k - ps + 1] != points[k + 1]) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
454 // copy the point to make room for a middle point
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
455 for (m = 0; m < ps; ++m)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
456 points[k + ps + m] = points[k + m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
457
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
458 // middle point has same y
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
459 points[k + 1] = points[k - ps + 1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
460
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
461 // we've added a point, better reflect that
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
462 k += ps;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
463 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
464 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
465 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
466 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
467
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
468 // give the hooks a chance to run
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
469 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
470 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
471
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
472 executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
473 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
474
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
475 // second pass: find datamax/datamin for auto-scaling
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
476 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
477 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
478 points = s.datapoints.points,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
479 ps = s.datapoints.pointsize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
480
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
481 var xmin = topSentry, ymin = topSentry,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
482 xmax = bottomSentry, ymax = bottomSentry;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
483
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
484 for (j = 0; j < points.length; j += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
485 if (points[j] == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
486 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
487
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
488 for (m = 0; m < ps; ++m) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
489 val = points[j + m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
490 f = format[m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
491 if (!f)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
492 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
493
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
494 if (f.x) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
495 if (val < xmin)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
496 xmin = val;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
497 if (val > xmax)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
498 xmax = val;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
499 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
500 if (f.y) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
501 if (val < ymin)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
502 ymin = val;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
503 if (val > ymax)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
504 ymax = val;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
505 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
506 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
507 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
508
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
509 if (s.bars.show) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
510 // make sure we got room for the bar on the dancing floor
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
511 var delta = s.bars.align == "left" ? 0 : -s.bars.barWidth/2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
512 if (s.bars.horizontal) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
513 ymin += delta;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
514 ymax += delta + s.bars.barWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
515 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
516 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
517 xmin += delta;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
518 xmax += delta + s.bars.barWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
519 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
520 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
521
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
522 updateAxis(s.xaxis, xmin, xmax);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
523 updateAxis(s.yaxis, ymin, ymax);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
524 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
525
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
526 for (axis in axes) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
527 if (axes[axis].datamin == topSentry)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
528 axes[axis].datamin = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
529 if (axes[axis].datamax == bottomSentry)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
530 axes[axis].datamax = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
531 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
532 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
533
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
534 function constructCanvas() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
535 function makeCanvas(width, height) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
536 var c = document.createElement('canvas');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
537 c.width = width;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
538 c.height = height;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
539 if ($.browser.msie) // excanvas hack
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
540 c = window.G_vmlCanvasManager.initElement(c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
541 return c;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
542 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
543
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
544 canvasWidth = placeholder.width();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
545 canvasHeight = placeholder.height();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
546 placeholder.html(""); // clear placeholder
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
547 if (placeholder.css("position") == 'static')
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
548 placeholder.css("position", "relative"); // for positioning labels and overlay
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
549
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
550 if (canvasWidth <= 0 || canvasHeight <= 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
551 throw "Invalid dimensions for plot, width = " + canvasWidth + ", height = " + canvasHeight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
552
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
553 if ($.browser.msie) // excanvas hack
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
554 window.G_vmlCanvasManager.init_(document); // make sure everything is setup
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
555
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
556 // the canvas
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
557 canvas = $(makeCanvas(canvasWidth, canvasHeight)).appendTo(placeholder).get(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
558 ctx = canvas.getContext("2d");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
559
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
560 // overlay canvas for interactive features
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
561 overlay = $(makeCanvas(canvasWidth, canvasHeight)).css({ position: 'absolute', left: 0, top: 0 }).appendTo(placeholder).get(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
562 octx = overlay.getContext("2d");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
563 octx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
564 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
565
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
566 function bindEvents() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
567 // we include the canvas in the event holder too, because IE 7
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
568 // sometimes has trouble with the stacking order
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
569 eventHolder = $([overlay, canvas]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
570
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
571 // bind events
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
572 if (options.grid.hoverable)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
573 eventHolder.mousemove(onMouseMove);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
574
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
575 if (options.grid.clickable)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
576 eventHolder.click(onClick);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
577
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
578 executeHooks(hooks.bindEvents, [eventHolder]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
579 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
580
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
581 function setupGrid() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
582 function setTransformationHelpers(axis, o) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
583 function identity(x) { return x; }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
584
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
585 var s, m, t = o.transform || identity,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
586 it = o.inverseTransform;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
587
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
588 // add transformation helpers
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
589 if (axis == axes.xaxis || axis == axes.x2axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
590 // precompute how much the axis is scaling a point
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
591 // in canvas space
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
592 s = axis.scale = plotWidth / (t(axis.max) - t(axis.min));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
593 m = t(axis.min);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
594
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
595 // data point to canvas coordinate
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
596 if (t == identity) // slight optimization
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
597 axis.p2c = function (p) { return (p - m) * s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
598 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
599 axis.p2c = function (p) { return (t(p) - m) * s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
600 // canvas coordinate to data point
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
601 if (!it)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
602 axis.c2p = function (c) { return m + c / s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
603 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
604 axis.c2p = function (c) { return it(m + c / s); };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
605 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
606 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
607 s = axis.scale = plotHeight / (t(axis.max) - t(axis.min));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
608 m = t(axis.max);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
609
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
610 if (t == identity)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
611 axis.p2c = function (p) { return (m - p) * s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
612 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
613 axis.p2c = function (p) { return (m - t(p)) * s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
614 if (!it)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
615 axis.c2p = function (c) { return m - c / s; };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
616 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
617 axis.c2p = function (c) { return it(m - c / s); };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
618 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
619 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
620
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
621 function measureLabels(axis, axisOptions) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
622 var i, labels = [], l;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
623
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
624 axis.labelWidth = axisOptions.labelWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
625 axis.labelHeight = axisOptions.labelHeight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
626
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
627 if (axis == axes.xaxis || axis == axes.x2axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
628 // to avoid measuring the widths of the labels, we
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
629 // construct fixed-size boxes and put the labels inside
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
630 // them, we don't need the exact figures and the
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
631 // fixed-size box content is easy to center
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
632 if (axis.labelWidth == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
633 axis.labelWidth = canvasWidth / (axis.ticks.length > 0 ? axis.ticks.length : 1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
634
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
635 // measure x label heights
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
636 if (axis.labelHeight == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
637 labels = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
638 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
639 l = axis.ticks[i].label;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
640 if (l)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
641 labels.push('<div class="tickLabel" style="float:left;width:' + axis.labelWidth + 'px">' + l + '</div>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
642 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
643
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
644 if (labels.length > 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
645 var dummyDiv = $('<div style="position:absolute;top:-10000px;width:10000px;font-size:smaller">'
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
646 + labels.join("") + '<div style="clear:left"></div></div>').appendTo(placeholder);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
647 axis.labelHeight = dummyDiv.height();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
648 dummyDiv.remove();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
649 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
650 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
651 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
652 else if (axis.labelWidth == null || axis.labelHeight == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
653 // calculate y label dimensions
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
654 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
655 l = axis.ticks[i].label;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
656 if (l)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
657 labels.push('<div class="tickLabel">' + l + '</div>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
658 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
659
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
660 if (labels.length > 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
661 var dummyDiv = $('<div style="position:absolute;top:-10000px;font-size:smaller">'
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
662 + labels.join("") + '</div>').appendTo(placeholder);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
663 if (axis.labelWidth == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
664 axis.labelWidth = dummyDiv.width();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
665 if (axis.labelHeight == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
666 axis.labelHeight = dummyDiv.find("div").height();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
667 dummyDiv.remove();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
668 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
669
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
670 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
671
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
672 if (axis.labelWidth == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
673 axis.labelWidth = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
674 if (axis.labelHeight == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
675 axis.labelHeight = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
676 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
677
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
678 function setGridSpacing() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
679 // get the most space needed around the grid for things
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
680 // that may stick out
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
681 var maxOutset = options.grid.borderWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
682 for (i = 0; i < series.length; ++i)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
683 maxOutset = Math.max(maxOutset, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
684
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
685 plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = maxOutset;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
686
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
687 var margin = options.grid.labelMargin + options.grid.borderWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
688
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
689 if (axes.xaxis.labelHeight > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
690 plotOffset.bottom = Math.max(maxOutset, axes.xaxis.labelHeight + margin);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
691 if (axes.yaxis.labelWidth > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
692 plotOffset.left = Math.max(maxOutset, axes.yaxis.labelWidth + margin);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
693 if (axes.x2axis.labelHeight > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
694 plotOffset.top = Math.max(maxOutset, axes.x2axis.labelHeight + margin);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
695 if (axes.y2axis.labelWidth > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
696 plotOffset.right = Math.max(maxOutset, axes.y2axis.labelWidth + margin);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
697
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
698 plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
699 plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
700 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
701
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
702 var axis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
703 for (axis in axes)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
704 setRange(axes[axis], options[axis]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
705
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
706 if (options.grid.show) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
707 for (axis in axes) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
708 prepareTickGeneration(axes[axis], options[axis]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
709 setTicks(axes[axis], options[axis]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
710 measureLabels(axes[axis], options[axis]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
711 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
712
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
713 setGridSpacing();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
714 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
715 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
716 plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
717 plotWidth = canvasWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
718 plotHeight = canvasHeight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
719 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
720
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
721 for (axis in axes)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
722 setTransformationHelpers(axes[axis], options[axis]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
723
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
724 if (options.grid.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
725 insertLabels();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
726
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
727 insertLegend();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
728 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
729
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
730 function setRange(axis, axisOptions) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
731 var min = +(axisOptions.min != null ? axisOptions.min : axis.datamin),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
732 max = +(axisOptions.max != null ? axisOptions.max : axis.datamax),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
733 delta = max - min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
734
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
735 if (delta == 0.0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
736 // degenerate case
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
737 var widen = max == 0 ? 1 : 0.01;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
738
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
739 if (axisOptions.min == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
740 min -= widen;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
741 // alway widen max if we couldn't widen min to ensure we
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
742 // don't fall into min == max which doesn't work
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
743 if (axisOptions.max == null || axisOptions.min != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
744 max += widen;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
745 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
746 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
747 // consider autoscaling
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
748 var margin = axisOptions.autoscaleMargin;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
749 if (margin != null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
750 if (axisOptions.min == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
751 min -= delta * margin;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
752 // make sure we don't go below zero if all values
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
753 // are positive
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
754 if (min < 0 && axis.datamin != null && axis.datamin >= 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
755 min = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
756 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
757 if (axisOptions.max == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
758 max += delta * margin;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
759 if (max > 0 && axis.datamax != null && axis.datamax <= 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
760 max = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
761 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
762 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
763 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
764 axis.min = min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
765 axis.max = max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
766 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
767
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
768 function prepareTickGeneration(axis, axisOptions) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
769 // estimate number of ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
770 var noTicks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
771 if (typeof axisOptions.ticks == "number" && axisOptions.ticks > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
772 noTicks = axisOptions.ticks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
773 else if (axis == axes.xaxis || axis == axes.x2axis)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
774 // heuristic based on the model a*sqrt(x) fitted to
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
775 // some reasonable data points
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
776 noTicks = 0.3 * Math.sqrt(canvasWidth);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
777 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
778 noTicks = 0.3 * Math.sqrt(canvasHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
779
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
780 var delta = (axis.max - axis.min) / noTicks,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
781 size, generator, unit, formatter, i, magn, norm;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
782
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
783 if (axisOptions.mode == "time") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
784 // pretty handling of time
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
785
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
786 // map of app. size of time units in milliseconds
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
787 var timeUnitSize = {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
788 "second": 1000,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
789 "minute": 60 * 1000,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
790 "hour": 60 * 60 * 1000,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
791 "day": 24 * 60 * 60 * 1000,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
792 "month": 30 * 24 * 60 * 60 * 1000,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
793 "year": 365.2425 * 24 * 60 * 60 * 1000
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
794 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
795
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
796
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
797 // the allowed tick sizes, after 1 year we use
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
798 // an integer algorithm
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
799 var spec = [
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
800 [1, "second"], [2, "second"], [5, "second"], [10, "second"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
801 [30, "second"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
802 [1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
803 [30, "minute"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
804 [1, "hour"], [2, "hour"], [4, "hour"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
805 [8, "hour"], [12, "hour"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
806 [1, "day"], [2, "day"], [3, "day"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
807 [0.25, "month"], [0.5, "month"], [1, "month"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
808 [2, "month"], [3, "month"], [6, "month"],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
809 [1, "year"]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
810 ];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
811
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
812 var minSize = 0;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
813 if (axisOptions.minTickSize != null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
814 if (typeof axisOptions.tickSize == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
815 minSize = axisOptions.tickSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
816 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
817 minSize = axisOptions.minTickSize[0] * timeUnitSize[axisOptions.minTickSize[1]];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
818 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
819
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
820 for (i = 0; i < spec.length - 1; ++i)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
821 if (delta < (spec[i][0] * timeUnitSize[spec[i][1]]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
822 + spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
823 && spec[i][0] * timeUnitSize[spec[i][1]] >= minSize)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
824 break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
825 size = spec[i][0];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
826 unit = spec[i][1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
827
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
828 // special-case the possibility of several years
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
829 if (unit == "year") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
830 magn = Math.pow(10, Math.floor(Math.log(delta / timeUnitSize.year) / Math.LN10));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
831 norm = (delta / timeUnitSize.year) / magn;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
832 if (norm < 1.5)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
833 size = 1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
834 else if (norm < 3)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
835 size = 2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
836 else if (norm < 7.5)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
837 size = 5;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
838 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
839 size = 10;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
840
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
841 size *= magn;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
842 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
843
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
844 if (axisOptions.tickSize) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
845 size = axisOptions.tickSize[0];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
846 unit = axisOptions.tickSize[1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
847 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
848
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
849 generator = function(axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
850 var ticks = [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
851 tickSize = axis.tickSize[0], unit = axis.tickSize[1],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
852 d = new Date(axis.min);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
853
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
854 var step = tickSize * timeUnitSize[unit];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
855
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
856 if (unit == "second")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
857 d.setUTCSeconds(floorInBase(d.getUTCSeconds(), tickSize));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
858 if (unit == "minute")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
859 d.setUTCMinutes(floorInBase(d.getUTCMinutes(), tickSize));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
860 if (unit == "hour")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
861 d.setUTCHours(floorInBase(d.getUTCHours(), tickSize));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
862 if (unit == "month")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
863 d.setUTCMonth(floorInBase(d.getUTCMonth(), tickSize));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
864 if (unit == "year")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
865 d.setUTCFullYear(floorInBase(d.getUTCFullYear(), tickSize));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
866
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
867 // reset smaller components
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
868 d.setUTCMilliseconds(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
869 if (step >= timeUnitSize.minute)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
870 d.setUTCSeconds(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
871 if (step >= timeUnitSize.hour)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
872 d.setUTCMinutes(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
873 if (step >= timeUnitSize.day)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
874 d.setUTCHours(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
875 if (step >= timeUnitSize.day * 4)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
876 d.setUTCDate(1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
877 if (step >= timeUnitSize.year)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
878 d.setUTCMonth(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
879
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
880
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
881 var carry = 0, v = Number.NaN, prev;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
882 do {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
883 prev = v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
884 v = d.getTime();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
885 ticks.push({ v: v, label: axis.tickFormatter(v, axis) });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
886 if (unit == "month") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
887 if (tickSize < 1) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
888 // a bit complicated - we'll divide the month
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
889 // up but we need to take care of fractions
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
890 // so we don't end up in the middle of a day
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
891 d.setUTCDate(1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
892 var start = d.getTime();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
893 d.setUTCMonth(d.getUTCMonth() + 1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
894 var end = d.getTime();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
895 d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
896 carry = d.getUTCHours();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
897 d.setUTCHours(0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
898 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
899 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
900 d.setUTCMonth(d.getUTCMonth() + tickSize);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
901 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
902 else if (unit == "year") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
903 d.setUTCFullYear(d.getUTCFullYear() + tickSize);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
904 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
905 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
906 d.setTime(v + step);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
907 } while (v < axis.max && v != prev);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
908
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
909 return ticks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
910 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
911
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
912 formatter = function (v, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
913 var d = new Date(v);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
914
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
915 // first check global format
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
916 if (axisOptions.timeformat != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
917 return $.plot.formatDate(d, axisOptions.timeformat, axisOptions.monthNames);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
918
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
919 var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
920 var span = axis.max - axis.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
921 var suffix = (axisOptions.twelveHourClock) ? " %p" : "";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
922
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
923 if (t < timeUnitSize.minute)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
924 fmt = "%h:%M:%S" + suffix;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
925 else if (t < timeUnitSize.day) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
926 if (span < 2 * timeUnitSize.day)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
927 fmt = "%h:%M" + suffix;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
928 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
929 fmt = "%b %d %h:%M" + suffix;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
930 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
931 else if (t < timeUnitSize.month)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
932 fmt = "%b %d";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
933 else if (t < timeUnitSize.year) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
934 if (span < timeUnitSize.year)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
935 fmt = "%b";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
936 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
937 fmt = "%b %y";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
938 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
939 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
940 fmt = "%y";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
941
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
942 return $.plot.formatDate(d, fmt, axisOptions.monthNames);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
943 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
944 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
945 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
946 // pretty rounding of base-10 numbers
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
947 var maxDec = axisOptions.tickDecimals;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
948 var dec = -Math.floor(Math.log(delta) / Math.LN10);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
949 if (maxDec != null && dec > maxDec)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
950 dec = maxDec;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
951
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
952 magn = Math.pow(10, -dec);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
953 norm = delta / magn; // norm is between 1.0 and 10.0
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
954
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
955 if (norm < 1.5)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
956 size = 1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
957 else if (norm < 3) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
958 size = 2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
959 // special case for 2.5, requires an extra decimal
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
960 if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
961 size = 2.5;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
962 ++dec;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
963 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
964 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
965 else if (norm < 7.5)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
966 size = 5;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
967 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
968 size = 10;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
969
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
970 size *= magn;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
971
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
972 if (axisOptions.minTickSize != null && size < axisOptions.minTickSize)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
973 size = axisOptions.minTickSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
974
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
975 if (axisOptions.tickSize != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
976 size = axisOptions.tickSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
977
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
978 axis.tickDecimals = Math.max(0, (maxDec != null) ? maxDec : dec);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
979
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
980 generator = function (axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
981 var ticks = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
982
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
983 // spew out all possible ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
984 var start = floorInBase(axis.min, axis.tickSize),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
985 i = 0, v = Number.NaN, prev;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
986 do {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
987 prev = v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
988 v = start + i * axis.tickSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
989 ticks.push({ v: v, label: axis.tickFormatter(v, axis) });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
990 ++i;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
991 } while (v < axis.max && v != prev);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
992 return ticks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
993 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
994
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
995 formatter = function (v, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
996 return v.toFixed(axis.tickDecimals);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
997 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
998 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
999
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1000 axis.tickSize = unit ? [size, unit] : size;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1001 axis.tickGenerator = generator;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1002 if ($.isFunction(axisOptions.tickFormatter))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1003 axis.tickFormatter = function (v, axis) { return "" + axisOptions.tickFormatter(v, axis); };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1004 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1005 axis.tickFormatter = formatter;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1006 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1007
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1008 function setTicks(axis, axisOptions) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1009 axis.ticks = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1010
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1011 if (!axis.used)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1012 return;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1013
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1014 if (axisOptions.ticks == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1015 axis.ticks = axis.tickGenerator(axis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1016 else if (typeof axisOptions.ticks == "number") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1017 if (axisOptions.ticks > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1018 axis.ticks = axis.tickGenerator(axis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1019 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1020 else if (axisOptions.ticks) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1021 var ticks = axisOptions.ticks;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1022
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1023 if ($.isFunction(ticks))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1024 // generate the ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1025 ticks = ticks({ min: axis.min, max: axis.max });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1026
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1027 // clean up the user-supplied ticks, copy them over
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1028 var i, v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1029 for (i = 0; i < ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1030 var label = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1031 var t = ticks[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1032 if (typeof t == "object") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1033 v = t[0];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1034 if (t.length > 1)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1035 label = t[1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1036 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1037 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1038 v = t;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1039 if (label == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1040 label = axis.tickFormatter(v, axis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1041 axis.ticks[i] = { v: v, label: label };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1042 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1043 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1044
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1045 if (axisOptions.autoscaleMargin != null && axis.ticks.length > 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1046 // snap to ticks
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1047 if (axisOptions.min == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1048 axis.min = Math.min(axis.min, axis.ticks[0].v);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1049 if (axisOptions.max == null && axis.ticks.length > 1)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1050 axis.max = Math.max(axis.max, axis.ticks[axis.ticks.length - 1].v);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1051 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1052 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1053
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1054 function draw() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1055 ctx.clearRect(0, 0, canvasWidth, canvasHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1056
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1057 var grid = options.grid;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1058
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1059 if (grid.show && !grid.aboveData)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1060 drawGrid();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1061
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1062 for (var i = 0; i < series.length; ++i)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1063 drawSeries(series[i]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1064
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1065 executeHooks(hooks.draw, [ctx]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1066
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1067 if (grid.show && grid.aboveData)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1068 drawGrid();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1069 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1070
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1071 function extractRange(ranges, coord) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1072 var firstAxis = coord + "axis",
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1073 secondaryAxis = coord + "2axis",
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1074 axis, from, to, reverse;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1075
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1076 if (ranges[firstAxis]) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1077 axis = axes[firstAxis];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1078 from = ranges[firstAxis].from;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1079 to = ranges[firstAxis].to;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1080 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1081 else if (ranges[secondaryAxis]) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1082 axis = axes[secondaryAxis];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1083 from = ranges[secondaryAxis].from;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1084 to = ranges[secondaryAxis].to;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1085 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1086 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1087 // backwards-compat stuff - to be removed in future
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1088 axis = axes[firstAxis];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1089 from = ranges[coord + "1"];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1090 to = ranges[coord + "2"];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1091 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1092
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1093 // auto-reverse as an added bonus
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1094 if (from != null && to != null && from > to)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1095 return { from: to, to: from, axis: axis };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1096
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1097 return { from: from, to: to, axis: axis };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1098 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1099
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1100 function drawGrid() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1101 var i;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1102
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1103 ctx.save();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1104 ctx.translate(plotOffset.left, plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1105
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1106 // draw background, if any
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1107 if (options.grid.backgroundColor) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1108 ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1109 ctx.fillRect(0, 0, plotWidth, plotHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1110 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1111
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1112 // draw markings
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1113 var markings = options.grid.markings;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1114 if (markings) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1115 if ($.isFunction(markings))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1116 // xmin etc. are backwards-compatible, to be removed in future
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1117 markings = markings({ xmin: axes.xaxis.min, xmax: axes.xaxis.max, ymin: axes.yaxis.min, ymax: axes.yaxis.max, xaxis: axes.xaxis, yaxis: axes.yaxis, x2axis: axes.x2axis, y2axis: axes.y2axis });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1118
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1119 for (i = 0; i < markings.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1120 var m = markings[i],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1121 xrange = extractRange(m, "x"),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1122 yrange = extractRange(m, "y");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1123
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1124 // fill in missing
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1125 if (xrange.from == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1126 xrange.from = xrange.axis.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1127 if (xrange.to == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1128 xrange.to = xrange.axis.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1129 if (yrange.from == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1130 yrange.from = yrange.axis.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1131 if (yrange.to == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1132 yrange.to = yrange.axis.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1133
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1134 // clip
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1135 if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1136 yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1137 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1138
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1139 xrange.from = Math.max(xrange.from, xrange.axis.min);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1140 xrange.to = Math.min(xrange.to, xrange.axis.max);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1141 yrange.from = Math.max(yrange.from, yrange.axis.min);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1142 yrange.to = Math.min(yrange.to, yrange.axis.max);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1143
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1144 if (xrange.from == xrange.to && yrange.from == yrange.to)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1145 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1146
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1147 // then draw
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1148 xrange.from = xrange.axis.p2c(xrange.from);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1149 xrange.to = xrange.axis.p2c(xrange.to);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1150 yrange.from = yrange.axis.p2c(yrange.from);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1151 yrange.to = yrange.axis.p2c(yrange.to);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1152
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1153 if (xrange.from == xrange.to || yrange.from == yrange.to) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1154 // draw line
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1155 ctx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1156 ctx.strokeStyle = m.color || options.grid.markingsColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1157 ctx.lineWidth = m.lineWidth || options.grid.markingsLineWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1158 //ctx.moveTo(Math.floor(xrange.from), yrange.from);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1159 //ctx.lineTo(Math.floor(xrange.to), yrange.to);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1160 ctx.moveTo(xrange.from, yrange.from);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1161 ctx.lineTo(xrange.to, yrange.to);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1162 ctx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1163 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1164 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1165 // fill area
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1166 ctx.fillStyle = m.color || options.grid.markingsColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1167 ctx.fillRect(xrange.from, yrange.to,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1168 xrange.to - xrange.from,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1169 yrange.from - yrange.to);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1170 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1171 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1172 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1173
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1174 // draw the inner grid
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1175 ctx.lineWidth = 1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1176 ctx.strokeStyle = options.grid.tickColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1177 ctx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1178 var v, axis = axes.xaxis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1179 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1180 v = axis.ticks[i].v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1181 if (v <= axis.min || v >= axes.xaxis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1182 continue; // skip those lying on the axes
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1183
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1184 ctx.moveTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, 0);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1185 ctx.lineTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, plotHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1186 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1187
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1188 axis = axes.yaxis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1189 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1190 v = axis.ticks[i].v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1191 if (v <= axis.min || v >= axis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1192 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1193
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1194 ctx.moveTo(0, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1195 ctx.lineTo(plotWidth, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1196 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1197
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1198 axis = axes.x2axis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1199 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1200 v = axis.ticks[i].v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1201 if (v <= axis.min || v >= axis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1202 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1203
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1204 ctx.moveTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, -5);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1205 ctx.lineTo(Math.floor(axis.p2c(v)) + ctx.lineWidth/2, 5);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1206 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1207
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1208 axis = axes.y2axis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1209 for (i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1210 v = axis.ticks[i].v;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1211 if (v <= axis.min || v >= axis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1212 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1213
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1214 ctx.moveTo(plotWidth-5, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1215 ctx.lineTo(plotWidth+5, Math.floor(axis.p2c(v)) + ctx.lineWidth/2);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1216 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1217
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1218 ctx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1219
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1220 if (options.grid.borderWidth) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1221 // draw border
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1222 var bw = options.grid.borderWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1223 ctx.lineWidth = bw;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1224 ctx.strokeStyle = options.grid.borderColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1225 ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1226 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1227
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1228 ctx.restore();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1229 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1230
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1231 function insertLabels() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1232 placeholder.find(".tickLabels").remove();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1233
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1234 var html = ['<div class="tickLabels" style="font-size:smaller;color:' + options.grid.color + '">'];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1235
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1236 function addLabels(axis, labelGenerator) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1237 for (var i = 0; i < axis.ticks.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1238 var tick = axis.ticks[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1239 if (!tick.label || tick.v < axis.min || tick.v > axis.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1240 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1241 html.push(labelGenerator(tick, axis));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1242 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1243 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1244
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1245 var margin = options.grid.labelMargin + options.grid.borderWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1246
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1247 addLabels(axes.xaxis, function (tick, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1248 return '<div style="position:absolute;top:' + (plotOffset.top + plotHeight + margin) + 'px;left:' + Math.round(plotOffset.left + axis.p2c(tick.v) - axis.labelWidth/2) + 'px;width:' + axis.labelWidth + 'px;text-align:center" class="tickLabel">' + tick.label + "</div>";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1249 });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1250
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1251
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1252 addLabels(axes.yaxis, function (tick, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1253 return '<div style="position:absolute;top:' + Math.round(plotOffset.top + axis.p2c(tick.v) - axis.labelHeight/2) + 'px;right:' + (plotOffset.right + plotWidth + margin) + 'px;width:' + axis.labelWidth + 'px;text-align:right" class="tickLabel">' + tick.label + "</div>";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1254 });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1255
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1256 addLabels(axes.x2axis, function (tick, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1257 return '<div style="position:absolute;bottom:' + (plotOffset.bottom + plotHeight + margin) + 'px;left:' + Math.round(plotOffset.left + axis.p2c(tick.v) - axis.labelWidth/2) + 'px;width:' + axis.labelWidth + 'px;text-align:center" class="tickLabel">' + tick.label + "</div>";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1258 });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1259
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1260 addLabels(axes.y2axis, function (tick, axis) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1261 return '<div style="position:absolute;top:' + Math.round(plotOffset.top + axis.p2c(tick.v) - axis.labelHeight/2) + 'px;left:' + (plotOffset.left + plotWidth + margin) +'px;width:' + axis.labelWidth + 'px;text-align:left" class="tickLabel">' + tick.label + "</div>";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1262 });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1263
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1264 html.push('</div>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1265
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1266 placeholder.append(html.join(""));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1267 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1268
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1269 function drawSeries(series) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1270 if (series.lines.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1271 drawSeriesLines(series);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1272 if (series.bars.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1273 drawSeriesBars(series);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1274 if (series.points.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1275 drawSeriesPoints(series);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1276 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1277
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1278 function drawSeriesLines(series) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1279 function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1280 var points = datapoints.points,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1281 ps = datapoints.pointsize,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1282 prevx = null, prevy = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1283
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1284 ctx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1285 for (var i = ps; i < points.length; i += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1286 var x1 = points[i - ps], y1 = points[i - ps + 1],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1287 x2 = points[i], y2 = points[i + 1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1288
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1289 if (x1 == null || x2 == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1290 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1291
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1292 // clip with ymin
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1293 if (y1 <= y2 && y1 < axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1294 if (y2 < axisy.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1295 continue; // line segment is outside
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1296 // compute new intersection point
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1297 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1298 y1 = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1299 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1300 else if (y2 <= y1 && y2 < axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1301 if (y1 < axisy.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1302 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1303 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1304 y2 = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1305 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1306
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1307 // clip with ymax
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1308 if (y1 >= y2 && y1 > axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1309 if (y2 > axisy.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1310 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1311 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1312 y1 = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1313 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1314 else if (y2 >= y1 && y2 > axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1315 if (y1 > axisy.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1316 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1317 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1318 y2 = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1319 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1320
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1321 // clip with xmin
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1322 if (x1 <= x2 && x1 < axisx.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1323 if (x2 < axisx.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1324 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1325 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1326 x1 = axisx.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1327 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1328 else if (x2 <= x1 && x2 < axisx.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1329 if (x1 < axisx.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1330 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1331 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1332 x2 = axisx.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1333 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1334
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1335 // clip with xmax
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1336 if (x1 >= x2 && x1 > axisx.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1337 if (x2 > axisx.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1338 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1339 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1340 x1 = axisx.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1341 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1342 else if (x2 >= x1 && x2 > axisx.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1343 if (x1 > axisx.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1344 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1345 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1346 x2 = axisx.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1347 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1348
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1349 if (x1 != prevx || y1 != prevy)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1350 ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1351
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1352 prevx = x2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1353 prevy = y2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1354 ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1355 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1356 ctx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1357 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1358
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1359 function plotLineArea(datapoints, axisx, axisy) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1360 var points = datapoints.points,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1361 ps = datapoints.pointsize,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1362 bottom = Math.min(Math.max(0, axisy.min), axisy.max),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1363 top, lastX = 0, areaOpen = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1364
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1365 for (var i = ps; i < points.length; i += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1366 var x1 = points[i - ps], y1 = points[i - ps + 1],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1367 x2 = points[i], y2 = points[i + 1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1368
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1369 if (areaOpen && x1 != null && x2 == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1370 // close area
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1371 ctx.lineTo(axisx.p2c(lastX), axisy.p2c(bottom));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1372 ctx.fill();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1373 areaOpen = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1374 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1375 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1376
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1377 if (x1 == null || x2 == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1378 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1379
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1380 // clip x values
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1381
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1382 // clip with xmin
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1383 if (x1 <= x2 && x1 < axisx.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1384 if (x2 < axisx.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1385 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1386 y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1387 x1 = axisx.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1388 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1389 else if (x2 <= x1 && x2 < axisx.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1390 if (x1 < axisx.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1391 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1392 y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1393 x2 = axisx.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1394 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1395
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1396 // clip with xmax
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1397 if (x1 >= x2 && x1 > axisx.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1398 if (x2 > axisx.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1399 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1400 y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1401 x1 = axisx.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1402 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1403 else if (x2 >= x1 && x2 > axisx.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1404 if (x1 > axisx.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1405 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1406 y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1407 x2 = axisx.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1408 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1409
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1410 if (!areaOpen) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1411 // open area
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1412 ctx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1413 ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1414 areaOpen = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1415 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1416
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1417 // now first check the case where both is outside
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1418 if (y1 >= axisy.max && y2 >= axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1419 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1420 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1421 lastX = x2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1422 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1423 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1424 else if (y1 <= axisy.min && y2 <= axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1425 ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1426 ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1427 lastX = x2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1428 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1429 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1430
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1431 // else it's a bit more complicated, there might
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1432 // be two rectangles and two triangles we need to fill
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1433 // in; to find these keep track of the current x values
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1434 var x1old = x1, x2old = x2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1435
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1436 // and clip the y values, without shortcutting
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1437
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1438 // clip with ymin
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1439 if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1440 x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1441 y1 = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1442 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1443 else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1444 x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1445 y2 = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1446 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1447
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1448 // clip with ymax
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1449 if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1450 x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1451 y1 = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1452 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1453 else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1454 x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1455 y2 = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1456 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1457
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1458
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1459 // if the x value was changed we got a rectangle
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1460 // to fill
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1461 if (x1 != x1old) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1462 if (y1 <= axisy.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1463 top = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1464 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1465 top = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1466
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1467 ctx.lineTo(axisx.p2c(x1old), axisy.p2c(top));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1468 ctx.lineTo(axisx.p2c(x1), axisy.p2c(top));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1469 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1470
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1471 // fill the triangles
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1472 ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1473 ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1474
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1475 // fill the other rectangle if it's there
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1476 if (x2 != x2old) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1477 if (y2 <= axisy.min)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1478 top = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1479 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1480 top = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1481
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1482 ctx.lineTo(axisx.p2c(x2), axisy.p2c(top));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1483 ctx.lineTo(axisx.p2c(x2old), axisy.p2c(top));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1484 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1485
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1486 lastX = Math.max(x2, x2old);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1487 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1488
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1489 if (areaOpen) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1490 ctx.lineTo(axisx.p2c(lastX), axisy.p2c(bottom));
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1491 ctx.fill();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1492 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1493 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1494
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1495 ctx.save();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1496 ctx.translate(plotOffset.left, plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1497 ctx.lineJoin = "round";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1498
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1499 var lw = series.lines.lineWidth,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1500 sw = series.shadowSize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1501 // FIXME: consider another form of shadow when filling is turned on
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1502 if (lw > 0 && sw > 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1503 // draw shadow as a thick and thin line with transparency
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1504 ctx.lineWidth = sw;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1505 ctx.strokeStyle = "rgba(0,0,0,0.1)";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1506 // position shadow at angle from the mid of line
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1507 var angle = Math.PI/18;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1508 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1509 ctx.lineWidth = sw/2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1510 plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1511 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1512
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1513 ctx.lineWidth = lw;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1514 ctx.strokeStyle = series.color;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1515 var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1516 if (fillStyle) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1517 ctx.fillStyle = fillStyle;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1518 plotLineArea(series.datapoints, series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1519 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1520
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1521 if (lw > 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1522 plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1523 ctx.restore();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1524 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1525
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1526 function drawSeriesPoints(series) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1527 function plotPoints(datapoints, radius, fillStyle, offset, circumference, axisx, axisy) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1528 var points = datapoints.points, ps = datapoints.pointsize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1529
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1530 for (var i = 0; i < points.length; i += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1531 var x = points[i], y = points[i + 1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1532 if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1533 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1534
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1535 ctx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1536 ctx.arc(axisx.p2c(x), axisy.p2c(y) + offset, radius, 0, circumference, false);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1537 if (fillStyle) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1538 ctx.fillStyle = fillStyle;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1539 ctx.fill();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1540 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1541 ctx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1542 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1543 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1544
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1545 ctx.save();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1546 ctx.translate(plotOffset.left, plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1547
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1548 var lw = series.lines.lineWidth,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1549 sw = series.shadowSize,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1550 radius = series.points.radius;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1551 if (lw > 0 && sw > 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1552 // draw shadow in two steps
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1553 var w = sw / 2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1554 ctx.lineWidth = w;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1555 ctx.strokeStyle = "rgba(0,0,0,0.1)";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1556 plotPoints(series.datapoints, radius, null, w + w/2, Math.PI,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1557 series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1558
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1559 ctx.strokeStyle = "rgba(0,0,0,0.2)";
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1560 plotPoints(series.datapoints, radius, null, w/2, Math.PI,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1561 series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1562 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1563
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1564 ctx.lineWidth = lw;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1565 ctx.strokeStyle = series.color;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1566 plotPoints(series.datapoints, radius,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1567 getFillStyle(series.points, series.color), 0, 2 * Math.PI,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1568 series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1569 ctx.restore();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1570 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1571
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1572 function drawBar(x, y, b, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1573 var left, right, bottom, top,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1574 drawLeft, drawRight, drawTop, drawBottom,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1575 tmp;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1576
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1577 if (horizontal) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1578 drawBottom = drawRight = drawTop = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1579 drawLeft = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1580 left = b;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1581 right = x;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1582 top = y + barLeft;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1583 bottom = y + barRight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1584
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1585 // account for negative bars
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1586 if (right < left) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1587 tmp = right;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1588 right = left;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1589 left = tmp;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1590 drawLeft = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1591 drawRight = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1592 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1593 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1594 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1595 drawLeft = drawRight = drawTop = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1596 drawBottom = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1597 left = x + barLeft;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1598 right = x + barRight;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1599 bottom = b;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1600 top = y;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1601
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1602 // account for negative bars
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1603 if (top < bottom) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1604 tmp = top;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1605 top = bottom;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1606 bottom = tmp;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1607 drawBottom = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1608 drawTop = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1609 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1610 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1611
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1612 // clip
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1613 if (right < axisx.min || left > axisx.max ||
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1614 top < axisy.min || bottom > axisy.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1615 return;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1616
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1617 if (left < axisx.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1618 left = axisx.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1619 drawLeft = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1620 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1621
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1622 if (right > axisx.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1623 right = axisx.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1624 drawRight = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1625 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1626
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1627 if (bottom < axisy.min) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1628 bottom = axisy.min;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1629 drawBottom = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1630 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1631
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1632 if (top > axisy.max) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1633 top = axisy.max;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1634 drawTop = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1635 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1636
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1637 left = axisx.p2c(left);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1638 bottom = axisy.p2c(bottom);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1639 right = axisx.p2c(right);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1640 top = axisy.p2c(top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1641
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1642 // fill the bar
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1643 if (fillStyleCallback) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1644 c.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1645 c.moveTo(left, bottom);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1646 c.lineTo(left, top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1647 c.lineTo(right, top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1648 c.lineTo(right, bottom);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1649 c.fillStyle = fillStyleCallback(bottom, top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1650 c.fill();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1651 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1652
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1653 // draw outline
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1654 if (drawLeft || drawRight || drawTop || drawBottom) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1655 c.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1656
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1657 // FIXME: inline moveTo is buggy with excanvas
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1658 c.moveTo(left, bottom + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1659 if (drawLeft)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1660 c.lineTo(left, top + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1661 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1662 c.moveTo(left, top + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1663 if (drawTop)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1664 c.lineTo(right, top + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1665 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1666 c.moveTo(right, top + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1667 if (drawRight)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1668 c.lineTo(right, bottom + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1669 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1670 c.moveTo(right, bottom + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1671 if (drawBottom)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1672 c.lineTo(left, bottom + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1673 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1674 c.moveTo(left, bottom + offset);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1675 c.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1676 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1677 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1678
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1679 function drawSeriesBars(series) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1680 function plotBars(datapoints, barLeft, barRight, offset, fillStyleCallback, axisx, axisy) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1681 var points = datapoints.points, ps = datapoints.pointsize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1682
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1683 for (var i = 0; i < points.length; i += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1684 if (points[i] == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1685 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1686 drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1687 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1688 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1689
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1690 ctx.save();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1691 ctx.translate(plotOffset.left, plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1692
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1693 // FIXME: figure out a way to add shadows (for instance along the right edge)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1694 ctx.lineWidth = series.bars.lineWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1695 ctx.strokeStyle = series.color;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1696 var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1697 var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1698 plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1699 ctx.restore();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1700 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1701
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1702 function getFillStyle(filloptions, seriesColor, bottom, top) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1703 var fill = filloptions.fill;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1704 if (!fill)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1705 return null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1706
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1707 if (filloptions.fillColor)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1708 return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1709
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1710 var c = $.color.parse(seriesColor);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1711 c.a = typeof fill == "number" ? fill : 0.4;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1712 c.normalize();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1713 return c.toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1714 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1715
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1716 function insertLegend() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1717 placeholder.find(".legend").remove();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1718
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1719 if (!options.legend.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1720 return;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1721
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1722 var fragments = [], rowStarted = false,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1723 lf = options.legend.labelFormatter, s, label;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1724 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1725 s = series[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1726 label = s.label;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1727 if (!label)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1728 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1729
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1730 if (i % options.legend.noColumns == 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1731 if (rowStarted)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1732 fragments.push('</tr>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1733 fragments.push('<tr>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1734 rowStarted = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1735 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1736
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1737 if (lf)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1738 label = lf(label, s);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1739
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1740 fragments.push(
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1741 '<td class="legendColorBox"><div style="border:1px solid ' + options.legend.labelBoxBorderColor + ';padding:1px"><div style="width:4px;height:0;border:5px solid ' + s.color + ';overflow:hidden"></div></div></td>' +
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1742 '<td class="legendLabel">' + label + '</td>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1743 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1744 if (rowStarted)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1745 fragments.push('</tr>');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1746
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1747 if (fragments.length == 0)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1748 return;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1749
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1750 var table = '<table style="font-size:smaller;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1751 if (options.legend.container != null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1752 $(options.legend.container).html(table);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1753 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1754 var pos = "",
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1755 p = options.legend.position,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1756 m = options.legend.margin;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1757 if (m[0] == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1758 m = [m, m];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1759 if (p.charAt(0) == "n")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1760 pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1761 else if (p.charAt(0) == "s")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1762 pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1763 if (p.charAt(1) == "e")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1764 pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1765 else if (p.charAt(1) == "w")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1766 pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1767 var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +';') + '</div>').appendTo(placeholder);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1768 if (options.legend.backgroundOpacity != 0.0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1769 // put in the transparent background
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1770 // separately to avoid blended labels and
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1771 // label boxes
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1772 var c = options.legend.backgroundColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1773 if (c == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1774 c = options.grid.backgroundColor;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1775 if (c && typeof c == "string")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1776 c = $.color.parse(c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1777 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1778 c = $.color.extract(legend, 'background-color');
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1779 c.a = 1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1780 c = c.toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1781 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1782 var div = legend.children();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1783 $('<div style="position:absolute;width:' + div.width() + 'px;height:' + div.height() + 'px;' + pos +'background-color:' + c + ';"> </div>').prependTo(legend).css('opacity', options.legend.backgroundOpacity);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1784 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1785 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1786 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1787
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1788
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1789 // interactive features
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1790
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1791 var highlights = [],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1792 redrawTimeout = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1793
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1794 // returns the data item the mouse is over, or null if none is found
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1795 function findNearbyItem(mouseX, mouseY, seriesFilter) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1796 var maxDistance = options.grid.mouseActiveRadius,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1797 smallestDistance = maxDistance * maxDistance + 1,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1798 item = null, foundPoint = false, i, j;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1799
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1800 for (i = 0; i < series.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1801 if (!seriesFilter(series[i]))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1802 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1803
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1804 var s = series[i],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1805 axisx = s.xaxis,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1806 axisy = s.yaxis,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1807 points = s.datapoints.points,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1808 ps = s.datapoints.pointsize,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1809 mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1810 my = axisy.c2p(mouseY),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1811 maxx = maxDistance / axisx.scale,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1812 maxy = maxDistance / axisy.scale;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1813
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1814 if (s.lines.show || s.points.show) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1815 for (j = 0; j < points.length; j += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1816 var x = points[j], y = points[j + 1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1817 if (x == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1818 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1819
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1820 // For points and lines, the cursor must be within a
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1821 // certain distance to the data point
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1822 if (x - mx > maxx || x - mx < -maxx ||
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1823 y - my > maxy || y - my < -maxy)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1824 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1825
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1826 // We have to calculate distances in pixels, not in
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1827 // data units, because the scales of the axes may be different
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1828 var dx = Math.abs(axisx.p2c(x) - mouseX),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1829 dy = Math.abs(axisy.p2c(y) - mouseY),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1830 dist = dx * dx + dy * dy; // we save the sqrt
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1831
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1832 // use <= to ensure last point takes precedence
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1833 // (last generally means on top of)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1834 if (dist <= smallestDistance) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1835 smallestDistance = dist;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1836 item = [i, j / ps];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1837 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1838 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1839 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1840
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1841 if (s.bars.show && !item) { // no other point can be nearby
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1842 var barLeft = s.bars.align == "left" ? 0 : -s.bars.barWidth/2,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1843 barRight = barLeft + s.bars.barWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1844
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1845 for (j = 0; j < points.length; j += ps) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1846 var x = points[j], y = points[j + 1], b = points[j + 2];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1847 if (x == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1848 continue;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1849
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1850 // for a bar graph, the cursor must be inside the bar
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1851 if (series[i].bars.horizontal ?
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1852 (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1853 my >= y + barLeft && my <= y + barRight) :
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1854 (mx >= x + barLeft && mx <= x + barRight &&
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1855 my >= Math.min(b, y) && my <= Math.max(b, y)))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1856 item = [i, j / ps];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1857 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1858 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1859 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1860
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1861 if (item) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1862 i = item[0];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1863 j = item[1];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1864 ps = series[i].datapoints.pointsize;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1865
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1866 return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1867 dataIndex: j,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1868 series: series[i],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1869 seriesIndex: i };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1870 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1871
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1872 return null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1873 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1874
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1875 function onMouseMove(e) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1876 if (options.grid.hoverable)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1877 triggerClickHoverEvent("plothover", e,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1878 function (s) { return s["hoverable"] != false; });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1879 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1880
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1881 function onClick(e) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1882 triggerClickHoverEvent("plotclick", e,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1883 function (s) { return s["clickable"] != false; });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1884 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1885
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1886 // trigger click or hover event (they send the same parameters
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1887 // so we share their code)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1888 function triggerClickHoverEvent(eventname, event, seriesFilter) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1889 var offset = eventHolder.offset(),
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1890 pos = { pageX: event.pageX, pageY: event.pageY },
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1891 canvasX = event.pageX - offset.left - plotOffset.left,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1892 canvasY = event.pageY - offset.top - plotOffset.top;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1893
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1894 if (axes.xaxis.used)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1895 pos.x = axes.xaxis.c2p(canvasX);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1896 if (axes.yaxis.used)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1897 pos.y = axes.yaxis.c2p(canvasY);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1898 if (axes.x2axis.used)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1899 pos.x2 = axes.x2axis.c2p(canvasX);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1900 if (axes.y2axis.used)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1901 pos.y2 = axes.y2axis.c2p(canvasY);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1902
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1903 var item = findNearbyItem(canvasX, canvasY, seriesFilter);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1904
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1905 if (item) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1906 // fill in mouse pos for any listeners out there
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1907 item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1908 item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1909 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1910
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1911 if (options.grid.autoHighlight) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1912 // clear auto-highlights
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1913 for (var i = 0; i < highlights.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1914 var h = highlights[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1915 if (h.auto == eventname &&
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1916 !(item && h.series == item.series && h.point == item.datapoint))
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1917 unhighlight(h.series, h.point);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1918 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1919
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1920 if (item)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1921 highlight(item.series, item.datapoint, eventname);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1922 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1923
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1924 placeholder.trigger(eventname, [ pos, item ]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1925 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1926
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1927 function triggerRedrawOverlay() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1928 if (!redrawTimeout)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1929 redrawTimeout = setTimeout(drawOverlay, 30);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1930 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1931
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1932 function drawOverlay() {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1933 redrawTimeout = null;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1934
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1935 // draw highlights
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1936 octx.save();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1937 octx.clearRect(0, 0, canvasWidth, canvasHeight);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1938 octx.translate(plotOffset.left, plotOffset.top);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1939
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1940 var i, hi;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1941 for (i = 0; i < highlights.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1942 hi = highlights[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1943
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1944 if (hi.series.bars.show)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1945 drawBarHighlight(hi.series, hi.point);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1946 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1947 drawPointHighlight(hi.series, hi.point);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1948 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1949 octx.restore();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1950
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1951 executeHooks(hooks.drawOverlay, [octx]);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1952 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1953
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1954 function highlight(s, point, auto) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1955 if (typeof s == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1956 s = series[s];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1957
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1958 if (typeof point == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1959 point = s.data[point];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1960
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1961 var i = indexOfHighlight(s, point);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1962 if (i == -1) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1963 highlights.push({ series: s, point: point, auto: auto });
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1964
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1965 triggerRedrawOverlay();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1966 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1967 else if (!auto)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1968 highlights[i].auto = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1969 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1970
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1971 function unhighlight(s, point) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1972 if (s == null && point == null) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1973 highlights = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1974 triggerRedrawOverlay();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1975 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1976
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1977 if (typeof s == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1978 s = series[s];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1979
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1980 if (typeof point == "number")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1981 point = s.data[point];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1982
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1983 var i = indexOfHighlight(s, point);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1984 if (i != -1) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1985 highlights.splice(i, 1);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1986
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1987 triggerRedrawOverlay();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1988 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1989 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1990
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1991 function indexOfHighlight(s, p) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1992 for (var i = 0; i < highlights.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1993 var h = highlights[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1994 if (h.series == s && h.point[0] == p[0]
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1995 && h.point[1] == p[1])
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1996 return i;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1997 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1998 return -1;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1999 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2000
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2001 function drawPointHighlight(series, point) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2002 var x = point[0], y = point[1],
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2003 axisx = series.xaxis, axisy = series.yaxis;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2004
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2005 if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2006 return;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2007
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2008 var pointRadius = series.points.radius + series.points.lineWidth / 2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2009 octx.lineWidth = pointRadius;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2010 octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2011 var radius = 1.5 * pointRadius;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2012 octx.beginPath();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2013 octx.arc(axisx.p2c(x), axisy.p2c(y), radius, 0, 2 * Math.PI, false);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2014 octx.stroke();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2015 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2016
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2017 function drawBarHighlight(series, point) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2018 octx.lineWidth = series.bars.lineWidth;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2019 octx.strokeStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2020 var fillStyle = $.color.parse(series.color).scale('a', 0.5).toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2021 var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2022 drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2023 0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2024 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2025
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2026 function getColorOrGradient(spec, bottom, top, defaultColor) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2027 if (typeof spec == "string")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2028 return spec;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2029 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2030 // assume this is a gradient spec; IE currently only
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2031 // supports a simple vertical gradient properly, so that's
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2032 // what we support too
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2033 var gradient = ctx.createLinearGradient(0, top, 0, bottom);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2034
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2035 for (var i = 0, l = spec.colors.length; i < l; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2036 var c = spec.colors[i];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2037 if (typeof c != "string") {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2038 c = $.color.parse(defaultColor).scale('rgb', c.brightness);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2039 c.a *= c.opacity;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2040 c = c.toString();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2041 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2042 gradient.addColorStop(i / (l - 1), c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2043 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2044
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2045 return gradient;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2046 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2047 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2048 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2049
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2050 $.plot = function(placeholder, data, options) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2051 var plot = new Plot($(placeholder), data, options, $.plot.plugins);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2052 /*var t0 = new Date();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2053 var t1 = new Date();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2054 var tstr = "time used (msecs): " + (t1.getTime() - t0.getTime())
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2055 if (window.console)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2056 console.log(tstr);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2057 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2058 alert(tstr);*/
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2059 return plot;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2060 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2061
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2062 $.plot.plugins = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2063
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2064 // returns a string with the date d formatted according to fmt
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2065 $.plot.formatDate = function(d, fmt, monthNames) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2066 var leftPad = function(n) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2067 n = "" + n;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2068 return n.length == 1 ? "0" + n : n;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2069 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2070
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2071 var r = [];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2072 var escape = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2073 var hours = d.getUTCHours();
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2074 var isAM = hours < 12;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2075 if (monthNames == null)
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2076 monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2077
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2078 if (fmt.search(/%p|%P/) != -1) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2079 if (hours > 12) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2080 hours = hours - 12;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2081 } else if (hours == 0) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2082 hours = 12;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2083 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2084 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2085 for (var i = 0; i < fmt.length; ++i) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2086 var c = fmt.charAt(i);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2087
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2088 if (escape) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2089 switch (c) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2090 case 'h': c = "" + hours; break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2091 case 'H': c = leftPad(hours); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2092 case 'M': c = leftPad(d.getUTCMinutes()); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2093 case 'S': c = leftPad(d.getUTCSeconds()); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2094 case 'd': c = "" + d.getUTCDate(); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2095 case 'm': c = "" + (d.getUTCMonth() + 1); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2096 case 'y': c = "" + d.getUTCFullYear(); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2097 case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2098 case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2099 case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2100 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2101 r.push(c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2102 escape = false;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2103 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2104 else {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2105 if (c == "%")
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2106 escape = true;
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2107 else
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2108 r.push(c);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2109 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2110 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2111 return r.join("");
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2112 };
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2113
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2114 // round to nearby lower multiple of base
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2115 function floorInBase(n, base) {
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2116 return base * Math.floor(n / base);
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2117 }
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2118
2d9ee2b3ae82 Initial commit of iWWS.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2119 })(jQuery);