1 | /** |
---|
2 | * jqPlot |
---|
3 | * Pure JavaScript plotting plugin using jQuery |
---|
4 | * |
---|
5 | * Version: @VERSION |
---|
6 | * |
---|
7 | * Copyright (c) 2009-2011 Chris Leonello |
---|
8 | * jqPlot is currently available for use in all personal or commercial projects |
---|
9 | * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL |
---|
10 | * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can |
---|
11 | * choose the license that best suits your project and use it accordingly. |
---|
12 | * |
---|
13 | * Although not required, the author would appreciate an email letting him |
---|
14 | * know of any substantial use of jqPlot. You can reach the author at: |
---|
15 | * chris at jqplot dot com or see http://www.jqplot.com/info.php . |
---|
16 | * |
---|
17 | * If you are feeling kind and generous, consider supporting the project by |
---|
18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
19 | * |
---|
20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
21 | * |
---|
22 | * version 2007.04.27 |
---|
23 | * author Ash Searle |
---|
24 | * http://hexmen.com/blog/2007/03/printf-sprintf/ |
---|
25 | * http://hexmen.com/js/sprintf.js |
---|
26 | * The author (Ash Searle) has placed this code in the public domain: |
---|
27 | * "This code is unrestricted: you are free to use it however you like." |
---|
28 | * |
---|
29 | */ |
---|
30 | (function($) { |
---|
31 | // class: $.jqplot.shapeRenderer |
---|
32 | // The default jqPlot shape renderer. Given a set of points will |
---|
33 | // plot them and either stroke a line (fill = false) or fill them (fill = true). |
---|
34 | // If a filled shape is desired, closePath = true must also be set to close |
---|
35 | // the shape. |
---|
36 | $.jqplot.ShapeRenderer = function(options){ |
---|
37 | |
---|
38 | this.lineWidth = 1.5; |
---|
39 | // prop: lineJoin |
---|
40 | // How line segments of the shadow are joined. |
---|
41 | this.lineJoin = 'miter'; |
---|
42 | // prop: lineCap |
---|
43 | // how ends of the shadow line are rendered. |
---|
44 | this.lineCap = 'round'; |
---|
45 | // prop; closePath |
---|
46 | // whether line path segment is closed upon itself. |
---|
47 | this.closePath = false; |
---|
48 | // prop: fill |
---|
49 | // whether to fill the shape. |
---|
50 | this.fill = false; |
---|
51 | // prop: isarc |
---|
52 | // wether the shadow is an arc or not. |
---|
53 | this.isarc = false; |
---|
54 | // prop: fillRect |
---|
55 | // true to draw shape as a filled rectangle. |
---|
56 | this.fillRect = false; |
---|
57 | // prop: strokeRect |
---|
58 | // true to draw shape as a stroked rectangle. |
---|
59 | this.strokeRect = false; |
---|
60 | // prop: clearRect |
---|
61 | // true to cear a rectangle. |
---|
62 | this.clearRect = false; |
---|
63 | // prop: strokeStyle |
---|
64 | // css color spec for the stoke style |
---|
65 | this.strokeStyle = '#999999'; |
---|
66 | // prop: fillStyle |
---|
67 | // css color spec for the fill style. |
---|
68 | this.fillStyle = '#999999'; |
---|
69 | |
---|
70 | $.extend(true, this, options); |
---|
71 | }; |
---|
72 | |
---|
73 | $.jqplot.ShapeRenderer.prototype.init = function(options) { |
---|
74 | $.extend(true, this, options); |
---|
75 | }; |
---|
76 | |
---|
77 | // function: draw |
---|
78 | // draws the shape. |
---|
79 | // |
---|
80 | // ctx - canvas drawing context |
---|
81 | // points - array of points for shapes or |
---|
82 | // [x, y, width, height] for rectangles or |
---|
83 | // [x, y, radius, start angle (rad), end angle (rad)] for circles and arcs. |
---|
84 | $.jqplot.ShapeRenderer.prototype.draw = function(ctx, points, options) { |
---|
85 | ctx.save(); |
---|
86 | var opts = (options != null) ? options : {}; |
---|
87 | var fill = (opts.fill != null) ? opts.fill : this.fill; |
---|
88 | var closePath = (opts.closePath != null) ? opts.closePath : this.closePath; |
---|
89 | var fillRect = (opts.fillRect != null) ? opts.fillRect : this.fillRect; |
---|
90 | var strokeRect = (opts.strokeRect != null) ? opts.strokeRect : this.strokeRect; |
---|
91 | var clearRect = (opts.clearRect != null) ? opts.clearRect : this.clearRect; |
---|
92 | var isarc = (opts.isarc != null) ? opts.isarc : this.isarc; |
---|
93 | ctx.lineWidth = opts.lineWidth || this.lineWidth; |
---|
94 | ctx.lineJoin = opts.lineJoin || this.lineJoin; |
---|
95 | ctx.lineCap = opts.lineCap || this.lineCap; |
---|
96 | ctx.strokeStyle = (opts.strokeStyle || opts.color) || this.strokeStyle; |
---|
97 | ctx.fillStyle = opts.fillStyle || this.fillStyle; |
---|
98 | ctx.beginPath(); |
---|
99 | if (isarc) { |
---|
100 | ctx.arc(points[0], points[1], points[2], points[3], points[4], true); |
---|
101 | if (closePath) { |
---|
102 | ctx.closePath(); |
---|
103 | } |
---|
104 | if (fill) { |
---|
105 | ctx.fill(); |
---|
106 | } |
---|
107 | else { |
---|
108 | ctx.stroke(); |
---|
109 | } |
---|
110 | ctx.restore(); |
---|
111 | return; |
---|
112 | } |
---|
113 | else if (clearRect) { |
---|
114 | ctx.clearRect(points[0], points[1], points[2], points[3]); |
---|
115 | ctx.restore(); |
---|
116 | return; |
---|
117 | } |
---|
118 | else if (fillRect || strokeRect) { |
---|
119 | if (fillRect) { |
---|
120 | ctx.fillRect(points[0], points[1], points[2], points[3]); |
---|
121 | } |
---|
122 | if (strokeRect) { |
---|
123 | ctx.strokeRect(points[0], points[1], points[2], points[3]); |
---|
124 | ctx.restore(); |
---|
125 | return; |
---|
126 | } |
---|
127 | } |
---|
128 | else if (points && points.length){ |
---|
129 | var move = true; |
---|
130 | for (var i=0; i<points.length; i++) { |
---|
131 | // skip to the first non-null point and move to it. |
---|
132 | if (points[i][0] != null && points[i][1] != null) { |
---|
133 | if (move) { |
---|
134 | ctx.moveTo(points[i][0], points[i][1]); |
---|
135 | move = false; |
---|
136 | } |
---|
137 | else { |
---|
138 | ctx.lineTo(points[i][0], points[i][1]); |
---|
139 | } |
---|
140 | } |
---|
141 | else { |
---|
142 | move = true; |
---|
143 | } |
---|
144 | } |
---|
145 | if (closePath) { |
---|
146 | ctx.closePath(); |
---|
147 | } |
---|
148 | if (fill) { |
---|
149 | ctx.fill(); |
---|
150 | } |
---|
151 | else { |
---|
152 | ctx.stroke(); |
---|
153 | } |
---|
154 | } |
---|
155 | ctx.restore(); |
---|
156 | }; |
---|
157 | })(jQuery); |
---|