2 * Copyright 2010-2013 Ben Birch
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this software except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * Raphael 1.5.2 - JavaScript Vector Library
19 * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
20 * Licensed under the MIT (http://raphaeljs.com/license.html) license.
21 * from fork at git@github.com:mobz/g.raphael.git
25 if (R.is(arguments[0], array)) {
27 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
29 for (var i = 0, ii = a[length]; i < ii; i++) {
31 elements[has](j.type) && res[push](cnv[j.type]().attr(j));
35 return create[apply](R, arguments);
38 var separator = /[, ]+/,
39 elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
40 formatrg = /\{(\d+)\}/g,
42 has = "hasOwnProperty",
46 was: Object[proto][has].call(win, "Raphael"),
50 this.customAttributes = {};
53 appendChild = "appendChild",
56 supportsTouch = "createTouch" in doc,
61 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
63 mousedown: "touchstart",
64 mousemove: "touchmove",
69 lowerCase = Str[proto].toLowerCase,
79 toString = "toString",
81 objectToString = Object[proto][toString],
84 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
85 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
86 isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
87 bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
89 setAttribute = "setAttribute",
92 ms = " progid:DXImageTransform.Microsoft",
93 upperCase = Str[proto].toUpperCase,
94 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
95 availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
97 animKeyFrames= /^(from|to|\d+%?)$/,
98 commaSpaces = /\s*,\s*/,
99 hsrg = {hs: 1, rg: 1},
100 p2s = /,?([achlmqrstvxz]),?/gi,
101 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
102 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
103 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
104 sortByKey = function (a, b) {
105 return a.key - b.key;
108 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
109 if (R.type == "VML") {
110 var d = doc.createElement("div"),
112 d.innerHTML = '<v:shape adj="1"/>';
114 b.style.behavior = "url(#default#VML)";
115 if (!(b && typeof b.adj == "object")) {
116 return R.type = null;
120 R.svg = !(R.vml = R.type == "VML");
121 Paper[proto] = R[proto];
122 paperproto = Paper[proto];
126 R.is = function (o, type) {
127 type = lowerCase.call(type);
128 if (type == "finite") {
129 return !isnan[has](+o);
131 return (type == "null" && o === null) ||
132 (type == typeof o) ||
133 (type == "object" && o === Object(o)) ||
134 (type == "array" && Array.isArray && Array.isArray(o)) ||
135 objectToString.call(o).slice(8, -1).toLowerCase() == type;
137 R.angle = function (x1, y1, x2, y2, x3, y3) {
144 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / PI + 360) % 360;
146 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
149 R.rad = function (deg) {
150 return deg % 360 * PI / 180;
152 R.deg = function (rad) {
153 return rad * 180 / PI % 360;
155 R.snapTo = function (values, value, tolerance) {
156 tolerance = R.is(tolerance, "finite") ? tolerance : 10;
157 if (R.is(values, array)) {
158 var i = values.length;
159 while (i--) if (abs(values[i] - value) <= tolerance) {
164 var rem = value % values;
165 if (rem < tolerance) {
168 if (rem > values - tolerance) {
169 return value - rem + values;
174 function createUUID() {
175 // http://www.ietf.org/rfc/rfc4122.txt
178 for (; i < 32; i++) {
179 s[i] = (~~(math.random() * 16))[toString](16);
181 s[12] = 4; // bits 12-15 of the time_hi_and_version field to 0010
182 s[16] = ((s[16] & 3) | 8)[toString](16); // bits 6-7 of the clock_seq_hi_and_reserved to 01
183 return "r-" + s[join]("");
186 R.setWindow = function (newwin) {
191 var toHex = function (color) {
193 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
194 var trim = /^\s+|\s+$/g;
197 var docum = new ActiveXObject("htmlfile");
198 docum.write("<body>");
202 bod = createPopup().document.body;
204 var range = bod.createTextRange();
205 toHex = cacher(function (color) {
207 bod.style.color = Str(color)[rp](trim, E);
208 var value = range.queryCommandValue("ForeColor");
209 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
210 return "#" + ("000000" + value[toString](16)).slice(-6);
216 var i = doc.createElement("i");
217 i.title = "Rapha\xebl Colour Picker";
218 i.style.display = "none";
219 doc.body[appendChild](i);
220 toHex = cacher(function (color) {
221 i.style.color = color;
222 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
227 hsbtoString = function () {
228 return "hsb(" + [this.h, this.s, this.b] + ")";
230 hsltoString = function () {
231 return "hsl(" + [this.h, this.s, this.l] + ")";
233 rgbtoString = function () {
236 R.hsb2rgb = function (h, s, b, o) {
237 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
243 return R.hsl2rgb(h, s, b / 2, o);
245 R.hsl2rgb = function (h, s, l, o) {
246 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
251 if (h > 1 || s > 1 || l > 1) {
257 channels = ["r", "g", "b"],
272 for (var i = 0; i < 3; i++) {
273 t3 = h + 1 / 3 * -(i - 1);
277 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
278 } else if (t3 * 2 < 1) {
279 rgb[channels[i]] = t2;
280 } else if (t3 * 3 < 2) {
281 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
283 rgb[channels[i]] = t1;
290 rgb.hex = "#" + (16777216 | rgb.b | (rgb.g << 8) | (rgb.r << 16)).toString(16).slice(1);
291 R.is(o, "finite") && (rgb.opacity = o);
292 rgb.toString = rgbtoString;
295 R.rgb2hsb = function (red, green, blue) {
296 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
301 if (green == null && R.is(red, string)) {
302 var clr = R.getRGB(red);
307 if (red > 1 || green > 1 || blue > 1) {
312 var max = mmax(red, green, blue),
313 min = mmin(red, green, blue),
318 return {h: 0, s: 0, b: max, toString: hsbtoString};
320 var delta = (max - min);
321 saturation = delta / max;
323 hue = (green - blue) / delta;
324 } else if (green == max) {
325 hue = 2 + ((blue - red) / delta);
327 hue = 4 + ((red - green) / delta);
333 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
335 R.rgb2hsl = function (red, green, blue) {
336 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
341 if (green == null && R.is(red, string)) {
342 var clr = R.getRGB(red);
347 if (red > 1 || green > 1 || blue > 1) {
352 var max = mmax(red, green, blue),
353 min = mmin(red, green, blue),
359 hsl = {h: 0, s: 0, l: l};
361 var delta = max - min;
362 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
364 h = (green - blue) / delta;
365 } else if (green == max) {
366 h = 2 + (blue - red) / delta;
368 h = 4 + (red - green) / delta;
373 hsl = {h: h, s: s, l: l};
375 hsl.toString = hsltoString;
378 R._path2string = function () {
379 return this.join(",")[rp](p2s, "$1");
381 function cacher(f, scope, postprocessor) {
383 var arg = Array[proto].slice.call(arguments, 0),
384 args = arg[join]("\u25ba"),
385 cache = newf.cache = newf.cache || {},
386 count = newf.count = newf.count || [];
387 if (cache[has](args)) {
388 return postprocessor ? postprocessor(cache[args]) : cache[args];
390 count[length] >= 1e3 && delete cache[count.shift()];
392 cache[args] = f[apply](scope, arg);
393 return postprocessor ? postprocessor(cache[args]) : cache[args];
398 R.getRGB = cacher(function (colour) {
399 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
400 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
402 if (colour == "none") {
403 return {r: -1, g: -1, b: -1, hex: "none"};
405 !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
413 rgb = colour.match(colourRegExp);
416 blue = toInt(rgb[2].substring(5), 16);
417 green = toInt(rgb[2].substring(3, 5), 16);
418 red = toInt(rgb[2].substring(1, 3), 16);
421 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
422 green = toInt((t = rgb[3].charAt(2)) + t, 16);
423 red = toInt((t = rgb[3].charAt(1)) + t, 16);
426 values = rgb[4][split](commaSpaces);
427 red = toFloat(values[0]);
428 values[0].slice(-1) == "%" && (red *= 2.55);
429 green = toFloat(values[1]);
430 values[1].slice(-1) == "%" && (green *= 2.55);
431 blue = toFloat(values[2]);
432 values[2].slice(-1) == "%" && (blue *= 2.55);
433 rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
434 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
437 values = rgb[5][split](commaSpaces);
438 red = toFloat(values[0]);
439 values[0].slice(-1) == "%" && (red *= 2.55);
440 green = toFloat(values[1]);
441 values[1].slice(-1) == "%" && (green *= 2.55);
442 blue = toFloat(values[2]);
443 values[2].slice(-1) == "%" && (blue *= 2.55);
444 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
445 rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
446 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
447 return R.hsb2rgb(red, green, blue, opacity);
450 values = rgb[6][split](commaSpaces);
451 red = toFloat(values[0]);
452 values[0].slice(-1) == "%" && (red *= 2.55);
453 green = toFloat(values[1]);
454 values[1].slice(-1) == "%" && (green *= 2.55);
455 blue = toFloat(values[2]);
456 values[2].slice(-1) == "%" && (blue *= 2.55);
457 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
458 rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
459 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
460 return R.hsl2rgb(red, green, blue, opacity);
462 rgb = {r: red, g: green, b: blue};
463 rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
464 R.is(opacity, "finite") && (rgb.opacity = opacity);
467 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
469 R.getColor = function (value) {
470 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
471 rgb = this.hsb2rgb(start.h, start.s, start.b);
476 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
480 R.getColor.reset = function () {
484 R.parsePathString = cacher(function (pathString) {
488 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
490 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
491 data = pathClone(pathString);
494 Str(pathString)[rp](pathCommand, function (a, b, c) {
496 name = lowerCase.call(b);
497 c[rp](pathValues, function (a, b) {
498 b && params[push](+b);
500 if (name == "m" && params[length] > 2) {
501 data[push]([b][concat](params.splice(0, 2)));
503 b = b == "m" ? "l" : "L";
505 while (params[length] >= paramCounts[name]) {
506 data[push]([b][concat](params.splice(0, paramCounts[name])));
507 if (!paramCounts[name]) {
513 data[toString] = R._path2string;
516 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
518 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
519 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
520 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
521 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
522 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
523 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
524 ax = (1 - t) * p1x + t * c1x,
525 ay = (1 - t) * p1y + t * c1y,
526 cx = (1 - t) * c2x + t * p2x,
527 cy = (1 - t) * c2y + t * p2y,
528 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / PI);
529 (mx > nx || my < ny) && (alpha += 180);
530 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
532 var pathDimensions = cacher(function (path) {
534 return {x: 0, y: 0, width: 0, height: 0};
536 path = path2curve(path);
542 for (var i = 0, ii = path[length]; i < ii; i++) {
550 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
551 X = X[concat](dim.min.x, dim.max.x);
552 Y = Y[concat](dim.min.y, dim.max.y);
557 var xmin = mmin[apply](0, X),
558 ymin = mmin[apply](0, Y);
562 width: mmax[apply](0, X) - xmin,
563 height: mmax[apply](0, Y) - ymin
566 pathClone = function (pathArray) {
568 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
569 pathArray = R.parsePathString(pathArray);
571 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
573 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
574 res[i][j] = pathArray[i][j];
577 res[toString] = R._path2string;
580 pathToRelative = cacher(function (pathArray) {
581 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
582 pathArray = R.parsePathString(pathArray);
590 if (pathArray[0][0] == "M") {
596 res[push](["M", x, y]);
598 for (var i = start, ii = pathArray[length]; i < ii; i++) {
601 if (pa[0] != lowerCase.call(pa[0])) {
602 r[0] = lowerCase.call(pa[0]);
610 r[6] = +(pa[6] - x).toFixed(3);
611 r[7] = +(pa[7] - y).toFixed(3);
614 r[1] = +(pa[1] - y).toFixed(3);
620 for (var j = 1, jj = pa[length]; j < jj; j++) {
621 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
630 for (var k = 0, kk = pa[length]; k < kk; k++) {
634 var len = res[i][length];
641 x += +res[i][len - 1];
644 y += +res[i][len - 1];
647 x += +res[i][len - 2];
648 y += +res[i][len - 1];
651 res[toString] = R._path2string;
654 pathToAbsolute = cacher(function (pathArray) {
655 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
656 pathArray = R.parsePathString(pathArray);
664 if (pathArray[0][0] == "M") {
665 x = +pathArray[0][1];
666 y = +pathArray[0][2];
670 res[0] = ["M", x, y];
672 for (var i = start, ii = pathArray[length]; i < ii; i++) {
675 if (pa[0] != upperCase.call(pa[0])) {
676 r[0] = upperCase.call(pa[0]);
697 for (var j = 1, jj = pa[length]; j < jj; j++) {
698 r[j] = +pa[j] + ((j % 2) ? x : y);
702 for (var k = 0, kk = pa[length]; k < kk; k++) {
718 mx = res[i][res[i][length] - 2];
719 my = res[i][res[i][length] - 1];
721 x = res[i][res[i][length] - 2];
722 y = res[i][res[i][length] - 1];
725 res[toString] = R._path2string;
728 l2c = function (x1, y1, x2, y2) {
729 return [x1, y1, x2, y2, x2, y2];
731 q2c = function (x1, y1, ax, ay, x2, y2) {
743 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
744 // for more information of where this math came from visit:
745 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
746 var _120 = PI * 120 / 180,
747 rad = PI / 180 * (+angle || 0),
750 rotate = cacher(function (x, y, rad) {
751 var X = x * math.cos(rad) - y * math.sin(rad),
752 Y = x * math.sin(rad) + y * math.cos(rad);
756 xy = rotate(x1, y1, -rad);
759 xy = rotate(x2, y2, -rad);
762 var cos = math.cos(PI / 180 * angle),
763 sin = math.sin(PI / 180 * angle),
766 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
774 k = (large_arc_flag == sweep_flag ? -1 : 1) *
775 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
776 cx = k * rx * y / ry + (x1 + x2) / 2,
777 cy = k * -ry * x / rx + (y1 + y2) / 2,
778 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
779 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
781 f1 = x1 < cx ? PI - f1 : f1;
782 f2 = x2 < cx ? PI - f2 : f2;
783 f1 < 0 && (f1 = PI * 2 + f1);
784 f2 < 0 && (f2 = PI * 2 + f2);
785 if (sweep_flag && f1 > f2) {
788 if (!sweep_flag && f2 > f1) {
798 if (abs(df) > _120) {
802 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
803 x2 = cx + rx * math.cos(f2);
804 y2 = cy + ry * math.sin(f2);
805 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
808 var c1 = math.cos(f1),
812 t = math.tan(df / 4),
816 m2 = [x1 + hx * s1, y1 - hy * c1],
817 m3 = [x2 + hx * s2, y2 - hy * c2],
819 m2[0] = 2 * m1[0] - m2[0];
820 m2[1] = 2 * m1[1] - m2[1];
822 return [m2, m3, m4][concat](res);
824 res = [m2, m3, m4][concat](res)[join]()[split](",");
826 for (var i = 0, ii = res[length]; i < ii; i++) {
827 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
832 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
835 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
836 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
839 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
840 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
841 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
843 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
844 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
848 abs(t1) > "1e12" && (t1 = .5);
849 abs(t2) > "1e12" && (t2 = .5);
850 if (t1 > 0 && t1 < 1) {
851 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
855 if (t2 > 0 && t2 < 1) {
856 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
860 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
861 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
863 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
864 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
865 abs(t1) > "1e12" && (t1 = .5);
866 abs(t2) > "1e12" && (t2 = .5);
867 if (t1 > 0 && t1 < 1) {
868 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
872 if (t2 > 0 && t2 < 1) {
873 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
878 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
879 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
882 path2curve = cacher(function (path, path2) {
883 var p = pathToAbsolute(path),
884 p2 = path2 && pathToAbsolute(path2),
885 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
886 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
887 processPath = function (path, d) {
890 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
892 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
899 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
902 nx = d.x + (d.x - (d.bx || d.x));
903 ny = d.y + (d.y - (d.by || d.y));
904 path = ["C", nx, ny][concat](path.slice(1));
907 d.qx = d.x + (d.x - (d.qx || d.x));
908 d.qy = d.y + (d.y - (d.qy || d.y));
909 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
914 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
917 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
920 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
923 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
926 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
931 fixArc = function (pp, i) {
932 if (pp[i][length] > 7) {
936 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
939 ii = mmax(p[length], p2 && p2[length] || 0);
942 fixM = function (path1, path2, a1, a2, i) {
943 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
944 path2.splice(i, 0, ["M", a2.x, a2.y]);
949 ii = mmax(p[length], p2 && p2[length] || 0);
952 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
953 p[i] = processPath(p[i], attrs);
955 p2 && (p2[i] = processPath(p2[i], attrs2));
957 fixM(p, p2, attrs, attrs2, i);
958 fixM(p2, p, attrs2, attrs, i);
961 seglen = seg[length],
962 seg2len = p2 && seg2[length];
963 attrs.x = seg[seglen - 2];
964 attrs.y = seg[seglen - 1];
965 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
966 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
967 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
968 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
969 attrs2.x = p2 && seg2[seg2len - 2];
970 attrs2.y = p2 && seg2[seg2len - 1];
972 return p2 ? [p, p2] : p;
974 parseDots = cacher(function (gradient) {
976 for (var i = 0, ii = gradient[length]; i < ii; i++) {
978 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
979 dot.color = R.getRGB(par[1]);
980 if (dot.color.error) {
983 dot.color = dot.color.hex;
984 par[2] && (dot.offset = par[2] + "%");
987 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
988 if (!dots[i].offset) {
989 var start = toFloat(dots[i - 1].offset || 0),
991 for (var j = i + 1; j < ii; j++) {
992 if (dots[j].offset) {
993 end = dots[j].offset;
1002 var d = (end - start) / (j - i + 1);
1003 for (; i < j; i++) {
1005 dots[i].offset = start + "%";
1011 getContainer = function (x, y, w, h) {
1013 if (R.is(x, string) || R.is(x, "object")) {
1014 container = R.is(x, string) ? doc.getElementById(x) : x;
1015 if (container.tagName) {
1018 container: container,
1019 width: container.style.pixelWidth || container.offsetWidth,
1020 height: container.style.pixelHeight || container.offsetHeight
1023 return {container: container, width: y, height: w};
1027 return {container: 1, x: x, y: y, width: w, height: h};
1030 plugins = function (con, add) {
1032 for (var prop in add) {
1033 if (add[has](prop) && !(prop in con)) {
1034 switch (typeof add[prop]) {
1037 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1041 con[prop] = con[prop] || {};
1042 plugins.call(this, con[prop], add[prop]);
1045 con[prop] = add[prop];
1051 tear = function (el, paper) {
1052 el == paper.top && (paper.top = el.prev);
1053 el == paper.bottom && (paper.bottom = el.next);
1054 el.next && (el.next.prev = el.prev);
1055 el.prev && (el.prev.next = el.next);
1057 tofront = function (el, paper) {
1058 if (paper.top === el) {
1063 el.prev = paper.top;
1064 paper.top.next = el;
1067 toback = function (el, paper) {
1068 if (paper.bottom === el) {
1072 el.next = paper.bottom;
1074 paper.bottom.prev = el;
1077 insertafter = function (el, el2, paper) {
1079 el2 == paper.top && (paper.top = el);
1080 el2.next && (el2.next.prev = el);
1085 insertbefore = function (el, el2, paper) {
1087 el2 == paper.bottom && (paper.bottom = el);
1088 el2.prev && (el2.prev.next = el);
1093 removed = function (methodname) {
1094 return function () {
1095 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1098 R.pathToRelative = pathToRelative;
1101 paperproto.svgns = "http://www.w3.org/2000/svg";
1102 paperproto.xlink = "http://www.w3.org/1999/xlink";
1103 round = function (num) {
1104 return +num + (~~num === num) * .5;
1106 var $ = function (el, attr) {
1108 for (var key in attr) {
1109 if (attr[has](key)) {
1110 el[setAttribute](key, Str(attr[key]));
1114 el = doc.createElementNS(paperproto.svgns, el);
1115 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1119 R[toString] = function () {
1120 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1122 var thePath = function (pathString, SVG) {
1124 SVG.canvas && SVG.canvas[appendChild](el);
1125 var p = new Element(el, SVG);
1127 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1130 var addGradientFill = function (o, gradient, SVG) {
1131 var type = "linear",
1134 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1139 var dir = ((fy > .5) * 2 - 1);
1140 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1141 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1143 (fy = fy.toFixed(5) - 1e-5 * dir);
1147 gradient = gradient[split](/\s*\-\s*/);
1148 if (type == "linear") {
1149 var angle = gradient.shift();
1150 angle = -toFloat(angle);
1154 var vector = [0, 0, math.cos(angle * PI / 180), math.sin(angle * PI / 180)],
1155 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1158 if (vector[2] < 0) {
1159 vector[0] = -vector[2];
1162 if (vector[3] < 0) {
1163 vector[1] = -vector[3];
1167 var dots = parseDots(gradient);
1171 var id = o.getAttribute(fillString);
1172 id = id.match(/^url\(#(.*)\)$/);
1173 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1175 var el = $(type + "Gradient");
1176 el.id = createUUID();
1177 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1178 SVG.defs[appendChild](el);
1179 for (var i = 0, ii = dots[length]; i < ii; i++) {
1180 var stop = $("stop");
1182 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1183 "stop-color": dots[i].color || "#fff"
1185 el[appendChild](stop);
1188 fill: "url(#" + el.id + ")",
1197 var updatePosition = function (o) {
1198 var bbox = o.getBBox();
1199 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1201 var setFillAndStroke = function (o, params) {
1208 "-..": [3, 1, 1, 1, 1, 1],
1212 "- .": [4, 3, 1, 3],
1213 "--.": [8, 3, 1, 3],
1214 "--..": [8, 3, 1, 3, 1, 3]
1219 addDashes = function (o, value) {
1220 value = dasharray[lowerCase.call(value)];
1222 var width = o.attrs["stroke-width"] || "1",
1223 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1225 var i = value[length];
1227 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1229 $(node, {"stroke-dasharray": dashes[join](",")});
1232 params[has]("rotation") && (rot = params.rotation);
1233 var rotxy = Str(rot)[split](separator);
1234 if (!(rotxy.length - 1)) {
1237 rotxy[1] = +rotxy[1];
1238 rotxy[2] = +rotxy[2];
1240 toFloat(rot) && o.rotate(0, true);
1241 for (var att in params) {
1242 if (params[has](att)) {
1243 if (!availableAttrs[has](att)) {
1246 var value = params[att];
1253 o.rotate(value, true);
1258 var pn = node.parentNode;
1259 if (lowerCase.call(pn.tagName) != "a") {
1261 pn.insertBefore(hl, node);
1262 hl[appendChild](node);
1265 if (att == "target" && value == "blank") {
1266 pn.setAttributeNS(o.paper.xlink, "show", "new");
1268 pn.setAttributeNS(o.paper.xlink, att, value);
1272 node.style.cursor = value;
1275 var rect = Str(value)[split](separator);
1276 if (rect[length] == 4) {
1277 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1278 var el = $("clipPath"),
1280 el.id = createUUID();
1287 el[appendChild](rc);
1288 o.paper.defs[appendChild](el);
1289 $(node, {"clip-path": "url(#" + el.id + ")"});
1293 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1294 clip && clip.parentNode.removeChild(clip);
1295 $(node, {"clip-path": E});
1300 if (o.type == "path") {
1301 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1305 node[setAttribute](att, value);
1314 value = -attrs.x - (attrs.width || 0);
1317 if (att == "rx" && o.type == "rect") {
1321 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1322 node[setAttribute](att, value);
1323 o.pattern && updatePosition(o);
1326 node[setAttribute](att, value);
1335 value = -attrs.y - (attrs.height || 0);
1338 if (att == "ry" && o.type == "rect") {
1342 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1343 node[setAttribute](att, value);
1344 o.pattern && updatePosition(o);
1347 if (o.type == "rect") {
1348 $(node, {rx: value, ry: value});
1350 node[setAttribute](att, value);
1354 if (o.type == "image") {
1355 node.setAttributeNS(o.paper.xlink, "href", value);
1358 case "stroke-width":
1359 node.style.strokeWidth = value;
1360 // Need following line for Firefox
1361 node[setAttribute](att, value);
1362 if (attrs["stroke-dasharray"]) {
1363 addDashes(o, attrs["stroke-dasharray"]);
1366 case "stroke-dasharray":
1367 addDashes(o, value);
1370 var xy = Str(value)[split](separator);
1371 xy[0] = +xy[0] || 0;
1372 xy[1] = +xy[1] || 0;
1377 translate.call(o, xy[0], xy[1]);
1380 xy = Str(value)[split](separator);
1381 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1384 var isURL = Str(value).match(ISURL);
1387 var ig = $("image");
1388 el.id = createUUID();
1389 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1390 $(ig, {x: 0, y: 0});
1391 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1392 el[appendChild](ig);
1394 var img = doc.createElement("img");
1395 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1396 img.onload = function () {
1397 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1398 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1399 doc.body.removeChild(this);
1402 doc.body[appendChild](img);
1404 o.paper.defs[appendChild](el);
1405 node.style.fill = "url(#" + el.id + ")";
1406 $(node, {fill: "url(#" + el.id + ")"});
1408 o.pattern && updatePosition(o);
1411 var clr = R.getRGB(value);
1413 delete params.gradient;
1414 delete attrs.gradient;
1415 !R.is(attrs.opacity, "undefined") &&
1416 R.is(params.opacity, "undefined") &&
1417 $(node, {opacity: attrs.opacity});
1418 !R.is(attrs["fill-opacity"], "undefined") &&
1419 R.is(params["fill-opacity"], "undefined") &&
1420 $(node, {"fill-opacity": attrs["fill-opacity"]});
1421 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1422 attrs.gradient = value;
1423 attrs.fill = "none";
1426 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1428 clr = R.getRGB(value);
1429 node[setAttribute](att, clr.hex);
1430 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1433 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1436 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
1437 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
1440 case "fill-opacity":
1441 if (attrs.gradient) {
1442 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1444 var stops = gradient.getElementsByTagName("stop");
1445 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1450 att == "font-size" && (value = toInt(value, 10) + "px");
1451 var cssrule = att[rp](/(\-.)/g, function (w) {
1452 return upperCase.call(w.substring(1));
1454 node.style[cssrule] = value;
1455 // Need following line for Firefox
1456 node[setAttribute](att, value);
1462 tuneText(o, params);
1464 o.rotate(rotxy.join(S));
1466 toFloat(rot) && o.rotate(rot, true);
1470 tuneText = function (el, params) {
1471 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1476 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1478 if (params[has]("text")) {
1479 a.text = params.text;
1480 while (node.firstChild) {
1481 node.removeChild(node.firstChild);
1483 var texts = Str(params.text)[split]("\n");
1484 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1485 var tspan = $("tspan");
1486 i && $(tspan, {dy: fontSize * leading, x: a.x});
1487 tspan[appendChild](doc.createTextNode(texts[i]));
1488 node[appendChild](tspan);
1491 texts = node.getElementsByTagName("tspan");
1492 for (i = 0, ii = texts[length]; i < ii; i++) {
1493 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1497 var bb = el.getBBox(),
1498 dif = a.y - (bb.y + bb.height / 2);
1499 dif && R.is(dif, "finite") && $(node, {y: a.y + dif});
1501 Element = function (node, svg) {
1507 node.raphael = this;
1509 this.attrs = this.attrs || {};
1510 this.transformations = []; // rotate, translate, scale
1514 rt: {deg: 0, cx: 0, cy: 0},
1518 !svg.bottom && (svg.bottom = this);
1519 this.prev = svg.top;
1520 svg.top && (svg.top.next = this);
1524 var elproto = Element[proto];
1525 Element[proto].rotate = function (deg, cx, cy) {
1531 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1533 return this._.rt.deg;
1535 var bbox = this.getBBox();
1536 deg = Str(deg)[split](separator);
1537 if (deg[length] - 1) {
1538 cx = toFloat(deg[1]);
1539 cy = toFloat(deg[2]);
1541 deg = toFloat(deg[0]);
1542 if (cx != null && cx !== false) {
1543 this._.rt.deg = deg;
1545 this._.rt.deg += deg;
1547 (cy == null) && (cx = null);
1550 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1551 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1552 if (this._.rt.deg) {
1553 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1554 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1556 this.transformations[0] = E;
1557 this.clip && $(this.clip, {transform: E});
1559 $(this.node, {transform: this.transformations[join](S)});
1562 Element[proto].hide = function () {
1563 !this.removed && (this.node.style.display = "none");
1566 Element[proto].show = function () {
1567 !this.removed && (this.node.style.display = "");
1570 Element[proto].remove = function () {
1574 tear(this, this.paper);
1575 this.node.parentNode.removeChild(this.node);
1576 for (var i in this) {
1579 this.removed = true;
1581 Element[proto].getBBox = function () {
1585 if (this.type == "path") {
1586 return pathDimensions(this.attrs.path);
1588 if (this.node.style.display == "none") {
1594 bbox = this.node.getBBox();
1596 // Firefox 3.0.x plays badly here
1600 if (this.type == "text") {
1601 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1602 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1603 var bb = this.node.getExtentOfChar(i);
1604 (bb.y < bbox.y) && (bbox.y = bb.y);
1605 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1606 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1609 hide && this.hide();
1612 Element[proto].attr = function (name, value) {
1618 for (var i in this.attrs) if (this.attrs[has](i)) {
1619 res[i] = this.attrs[i];
1621 this._.rt.deg && (res.rotation = this.rotate());
1622 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1623 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1626 if (value == null && R.is(name, string)) {
1627 if (name == "translation") {
1628 return translate.call(this);
1630 if (name == "rotation") {
1631 return this.rotate();
1633 if (name == "scale") {
1634 return this.scale();
1636 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1637 return this.attrs.gradient;
1639 return this.attrs[name];
1641 if (value == null && R.is(name, array)) {
1643 for (var j = 0, jj = name.length; j < jj; j++) {
1644 values[name[j]] = this.attr(name[j]);
1648 if (value != null) {
1650 params[name] = value;
1651 } else if (name != null && R.is(name, "object")) {
1654 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1655 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1656 this.attrs[key] = params[key];
1657 for (var subkey in par) if (par[has](subkey)) {
1658 params[subkey] = par[subkey];
1661 setFillAndStroke(this, params);
1664 Element[proto].toFront = function () {
1668 this.node.parentNode[appendChild](this.node);
1669 var svg = this.paper;
1670 svg.top != this && tofront(this, svg);
1673 Element[proto].toBack = function () {
1677 if (this.node.parentNode.firstChild != this.node) {
1678 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1679 toback(this, this.paper);
1680 var svg = this.paper;
1684 Element[proto].insertAfter = function (element) {
1688 var node = element.node || element[element.length - 1].node;
1689 if (node.nextSibling) {
1690 node.parentNode.insertBefore(this.node, node.nextSibling);
1692 node.parentNode[appendChild](this.node);
1694 insertafter(this, element, this.paper);
1697 Element[proto].insertBefore = function (element) {
1701 var node = element.node || element[0].node;
1702 node.parentNode.insertBefore(this.node, node);
1703 insertbefore(this, element, this.paper);
1706 Element[proto].blur = function (size) {
1707 // Experimental. No Safari support. Use it on your own risk.
1710 var fltr = $("filter"),
1711 blur = $("feGaussianBlur");
1712 t.attrs.blur = size;
1713 fltr.id = createUUID();
1714 $(blur, {stdDeviation: +size || 1.5});
1715 fltr.appendChild(blur);
1716 t.paper.defs.appendChild(fltr);
1718 $(t.node, {filter: "url(#" + fltr.id + ")"});
1721 t._blur.parentNode.removeChild(t._blur);
1723 delete t.attrs.blur;
1725 t.node.removeAttribute("filter");
1728 var theCircle = function (svg, x, y, r) {
1729 var el = $("circle");
1730 svg.canvas && svg.canvas[appendChild](el);
1731 var res = new Element(el, svg);
1732 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1733 res.type = "circle";
1737 theRect = function (svg, x, y, w, h, r) {
1739 svg.canvas && svg.canvas[appendChild](el);
1740 var res = new Element(el, svg);
1741 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1746 theEllipse = function (svg, x, y, rx, ry) {
1747 var el = $("ellipse");
1748 svg.canvas && svg.canvas[appendChild](el);
1749 var res = new Element(el, svg);
1750 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1751 res.type = "ellipse";
1755 theImage = function (svg, src, x, y, w, h) {
1756 var el = $("image");
1757 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1758 el.setAttributeNS(svg.xlink, "href", src);
1759 svg.canvas && svg.canvas[appendChild](el);
1760 var res = new Element(el, svg);
1761 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1765 theText = function (svg, x, y, text) {
1767 $(el, {x: x, y: y, "text-anchor": "middle"});
1768 svg.canvas && svg.canvas[appendChild](el);
1769 var res = new Element(el, svg);
1770 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1772 setFillAndStroke(res, res.attrs);
1775 setSize = function (width, height) {
1776 this.width = width || this.width;
1777 this.height = height || this.height;
1778 this.canvas[setAttribute]("width", this.width);
1779 this.canvas[setAttribute]("height", this.height);
1782 create = function () {
1783 var con = getContainer[apply](0, arguments),
1784 container = con && con.container,
1788 height = con.height;
1790 throw new Error("SVG container not found.");
1792 var cnvs = $("svg");
1795 width = width || 512;
1796 height = height || 342;
1798 xmlns: "http://www.w3.org/2000/svg",
1803 if (container == 1) {
1804 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1805 doc.body[appendChild](cnvs);
1807 if (container.firstChild) {
1808 container.insertBefore(cnvs, container.firstChild);
1810 container[appendChild](cnvs);
1813 container = new Paper;
1814 container.width = width;
1815 container.height = height;
1816 container.canvas = cnvs;
1817 plugins.call(container, container, R.fn);
1821 paperproto.clear = function () {
1822 var c = this.canvas;
1823 while (c.firstChild) {
1824 c.removeChild(c.firstChild);
1826 this.bottom = this.top = null;
1827 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1828 c[appendChild](this.desc);
1829 c[appendChild](this.defs = $("defs"));
1831 paperproto.remove = function () {
1832 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1833 for (var i in this) {
1834 this[i] = removed(i);
1841 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1842 bites = /([clmz]),?([^clmz]*)/gi,
1843 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1844 val = /-?[^,\s-]+/g,
1845 coordsize = 1e3 + S + 1e3,
1847 pathlike = {path: 1, rect: 1},
1848 path2vml = function (path) {
1849 var total = /[ahqstv]/ig,
1850 command = pathToAbsolute;
1851 Str(path).match(total) && (command = path2curve);
1853 if (command == pathToAbsolute && !Str(path).match(total)) {
1854 var res = Str(path)[rp](bites, function (all, command, args) {
1856 isMove = lowerCase.call(command) == "m",
1858 args[rp](val, function (value) {
1859 if (isMove && vals[length] == 2) {
1860 res += vals + map[command == "m" ? "l" : "L"];
1863 vals[push](round(value * zoom));
1869 var pa = command(path), p, r;
1871 for (var i = 0, ii = pa[length]; i < ii; i++) {
1873 r = lowerCase.call(pa[i][0]);
1874 r == "z" && (r = "x");
1875 for (var j = 1, jj = p[length]; j < jj; j++) {
1876 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1880 return res[join](S);
1883 R[toString] = function () {
1884 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1886 thePath = function (pathString, vml) {
1887 var g = createNode("group");
1888 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1889 g.coordsize = vml.coordsize;
1890 g.coordorigin = vml.coordorigin;
1891 var el = createNode("shape"), ol = el.style;
1892 ol.width = vml.width + "px";
1893 ol.height = vml.height + "px";
1894 el.coordsize = coordsize;
1895 el.coordorigin = vml.coordorigin;
1897 var p = new Element(el, g, vml),
1898 attr = {fill: "none", stroke: "#000"};
1899 pathString && (attr.path = pathString);
1903 setFillAndStroke(p, attr);
1904 vml.canvas[appendChild](g);
1907 setFillAndStroke = function (o, params) {
1908 o.attrs = o.attrs || {};
1913 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1916 for (var par in params) if (params[has](par)) {
1917 a[par] = params[par];
1920 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1926 params.href && (node.href = params.href);
1927 params.title && (node.title = params.title);
1928 params.target && (node.target = params.target);
1929 params.cursor && (s.cursor = params.cursor);
1930 "blur" in params && o.blur(params.blur);
1931 if (params.path && o.type == "path" || newpath) {
1932 node.path = path2vml(a.path);
1934 if (params.rotation != null) {
1935 o.rotate(params.rotation, true);
1937 if (params.translation) {
1938 xy = Str(params.translation)[split](separator);
1939 translate.call(o, xy[0], xy[1]);
1940 if (o._.rt.cx != null) {
1941 o._.rt.cx +=+ xy[0];
1942 o._.rt.cy +=+ xy[1];
1943 o.setBox(o.attrs, xy[0], xy[1]);
1947 xy = Str(params.scale)[split](separator);
1948 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1950 if ("clip-rect" in params) {
1951 var rect = Str(params["clip-rect"])[split](separator);
1952 if (rect[length] == 4) {
1953 rect[2] = +rect[2] + (+rect[0]);
1954 rect[3] = +rect[3] + (+rect[1]);
1955 var div = node.clipRect || doc.createElement("div"),
1957 group = node.parentNode;
1958 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1959 if (!node.clipRect) {
1960 dstyle.position = "absolute";
1963 dstyle.width = o.paper.width + "px";
1964 dstyle.height = o.paper.height + "px";
1965 group.parentNode.insertBefore(div, group);
1966 div[appendChild](group);
1967 node.clipRect = div;
1970 if (!params["clip-rect"]) {
1971 node.clipRect && (node.clipRect.style.clip = E);
1974 if (o.type == "image" && params.src) {
1975 node.src = params.src;
1977 if (o.type == "image" && params.opacity) {
1978 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1979 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1981 params.font && (s.font = params.font);
1982 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1983 params["font-size"] && (s.fontSize = params["font-size"]);
1984 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1985 params["font-style"] && (s.fontStyle = params["font-style"]);
1986 if (params.opacity != null ||
1987 params["stroke-width"] != null ||
1988 params.fill != null ||
1989 params.stroke != null ||
1990 params["stroke-width"] != null ||
1991 params["stroke-opacity"] != null ||
1992 params["fill-opacity"] != null ||
1993 params["stroke-dasharray"] != null ||
1994 params["stroke-miterlimit"] != null ||
1995 params["stroke-linejoin"] != null ||
1996 params["stroke-linecap"] != null) {
1997 node = o.shape || node;
1998 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
2000 !fill && (newfill = fill = createNode(fillString));
2001 if ("fill-opacity" in params || "opacity" in params) {
2002 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
2003 opacity = mmin(mmax(opacity, 0), 1);
2004 fill.opacity = opacity;
2006 params.fill && (fill.on = true);
2007 if (fill.on == null || params.fill == "none") {
2010 if (fill.on && params.fill) {
2011 var isURL = params.fill.match(ISURL);
2013 fill.src = isURL[1];
2016 fill.color = R.getRGB(params.fill).hex;
2018 fill.type = "solid";
2019 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
2021 a.gradient = params.fill;
2025 newfill && node[appendChild](fill);
2026 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
2028 !stroke && (newstroke = stroke = createNode("stroke"));
2029 if ((params.stroke && params.stroke != "none") ||
2030 params["stroke-width"] ||
2031 params["stroke-opacity"] != null ||
2032 params["stroke-dasharray"] ||
2033 params["stroke-miterlimit"] ||
2034 params["stroke-linejoin"] ||
2035 params["stroke-linecap"]) {
2038 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2039 var strokeColor = R.getRGB(params.stroke);
2040 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2041 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2042 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2043 opacity = mmin(mmax(opacity, 0), 1);
2044 params["stroke-width"] == null && (width = a["stroke-width"]);
2045 params["stroke-width"] && (stroke.weight = width);
2046 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2047 stroke.opacity = opacity;
2049 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2050 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2051 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2052 if (params["stroke-dasharray"]) {
2056 "-.": "shortdashdot",
2057 "-..": "shortdashdotdot",
2062 "--.": "longdashdot",
2063 "--..": "longdashdotdot"
2065 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2067 newstroke && node[appendChild](stroke);
2069 if (res.type == "text") {
2070 s = res.paper.span.style;
2071 a.font && (s.font = a.font);
2072 a["font-family"] && (s.fontFamily = a["font-family"]);
2073 a["font-size"] && (s.fontSize = a["font-size"]);
2074 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2075 a["font-style"] && (s.fontStyle = a["font-style"]);
2076 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
2077 res.W = a.w = res.paper.span.offsetWidth;
2078 res.H = a.h = res.paper.span.offsetHeight;
2080 res.Y = a.y + round(res.H / 2);
2082 // text-anchor emulationm
2083 switch (a["text-anchor"]) {
2085 res.node.style["v-text-align"] = "left";
2086 res.bbx = round(res.W / 2);
2089 res.node.style["v-text-align"] = "right";
2090 res.bbx = -round(res.W / 2);
2093 res.node.style["v-text-align"] = "center";
2098 addGradientFill = function (o, gradient) {
2099 o.attrs = o.attrs || {};
2100 var attrs = o.attrs,
2104 o.attrs.gradient = gradient;
2105 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2110 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2115 gradient = gradient[split](/\s*\-\s*/);
2116 if (type == "linear") {
2117 var angle = gradient.shift();
2118 angle = -toFloat(angle);
2123 var dots = parseDots(gradient);
2127 o = o.shape || o.node;
2128 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2129 !fill.parentNode && o.appendChild(fill);
2132 fill.method = "none";
2133 fill.color = dots[0].color;
2134 fill.color2 = dots[dots[length] - 1].color;
2136 for (var i = 0, ii = dots[length]; i < ii; i++) {
2137 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2139 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2140 if (type == "radial") {
2141 fill.type = "gradientradial";
2142 fill.focus = "100%";
2143 fill.focussize = fxfy;
2144 fill.focusposition = fxfy;
2146 fill.type = "gradient";
2147 fill.angle = (270 - angle) % 360;
2152 Element = function (node, group, vml) {
2160 node.raphael = this;
2173 !vml.bottom && (vml.bottom = this);
2174 this.prev = vml.top;
2175 vml.top && (vml.top.next = this);
2179 elproto = Element[proto];
2180 elproto.rotate = function (deg, cx, cy) {
2186 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2188 return this._.rt.deg;
2190 deg = Str(deg)[split](separator);
2191 if (deg[length] - 1) {
2192 cx = toFloat(deg[1]);
2193 cy = toFloat(deg[2]);
2195 deg = toFloat(deg[0]);
2197 this._.rt.deg = deg;
2199 this._.rt.deg += deg;
2201 cy == null && (cx = null);
2204 this.setBox(this.attrs, cx, cy);
2205 this.Group.style.rotation = this._.rt.deg;
2206 // gradient fix for rotation. TODO
2207 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2208 // fill = fill[0] || {};
2209 // var b = ((360 - this._.rt.deg) - 270) % 360;
2210 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2213 elproto.setBox = function (params, cx, cy) {
2217 var gs = this.Group.style,
2218 os = (this.shape && this.shape.style) || this.node.style;
2219 params = params || {};
2220 for (var i in params) if (params[has](i)) {
2221 this.attrs[i] = params[i];
2223 cx = cx || this._.rt.cx;
2224 cy = cy || this._.rt.cy;
2225 var attr = this.attrs,
2230 switch (this.type) {
2232 x = attr.cx - attr.r;
2233 y = attr.cy - attr.r;
2237 x = attr.cx - attr.rx;
2238 y = attr.cy - attr.ry;
2245 w = attr.width || 0;
2246 h = attr.height || 0;
2249 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2250 x = attr.x - round(this.W / 2);
2251 y = attr.y - this.H / 2;
2257 if (!this.attrs.path) {
2260 w = this.paper.width;
2261 h = this.paper.height;
2263 var dim = pathDimensions(this.attrs.path);
2273 w = this.paper.width;
2274 h = this.paper.height;
2277 cx = (cx == null) ? x + w / 2 : cx;
2278 cy = (cy == null) ? y + h / 2 : cy;
2279 var left = cx - this.paper.width / 2,
2280 top = cy - this.paper.height / 2, t;
2281 gs.left != (t = left + "px") && (gs.left = t);
2282 gs.top != (t = top + "px") && (gs.top = t);
2283 this.X = pathlike[has](this.type) ? -left : x;
2284 this.Y = pathlike[has](this.type) ? -top : y;
2287 if (pathlike[has](this.type)) {
2288 os.left != (t = -left * zoom + "px") && (os.left = t);
2289 os.top != (t = -top * zoom + "px") && (os.top = t);
2290 } else if (this.type == "text") {
2291 os.left != (t = -left + "px") && (os.left = t);
2292 os.top != (t = -top + "px") && (os.top = t);
2294 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2295 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2296 os.left != (t = x - left + "px") && (os.left = t);
2297 os.top != (t = y - top + "px") && (os.top = t);
2298 os.width != (t = w + "px") && (os.width = t);
2299 os.height != (t = h + "px") && (os.height = t);
2302 elproto.hide = function () {
2303 !this.removed && (this.Group.style.display = "none");
2306 elproto.show = function () {
2307 !this.removed && (this.Group.style.display = "block");
2310 elproto.getBBox = function () {
2314 if (pathlike[has](this.type)) {
2315 return pathDimensions(this.attrs.path);
2318 x: this.X + (this.bbx || 0),
2324 elproto.remove = function () {
2328 tear(this, this.paper);
2329 this.node.parentNode.removeChild(this.node);
2330 this.Group.parentNode.removeChild(this.Group);
2331 this.shape && this.shape.parentNode.removeChild(this.shape);
2332 for (var i in this) {
2335 this.removed = true;
2337 elproto.attr = function (name, value) {
2343 for (var i in this.attrs) if (this.attrs[has](i)) {
2344 res[i] = this.attrs[i];
2346 this._.rt.deg && (res.rotation = this.rotate());
2347 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2348 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2351 if (value == null && R.is(name, "string")) {
2352 if (name == "translation") {
2353 return translate.call(this);
2355 if (name == "rotation") {
2356 return this.rotate();
2358 if (name == "scale") {
2359 return this.scale();
2361 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2362 return this.attrs.gradient;
2364 return this.attrs[name];
2366 if (this.attrs && value == null && R.is(name, array)) {
2367 var ii, values = {};
2368 for (i = 0, ii = name[length]; i < ii; i++) {
2369 values[name[i]] = this.attr(name[i]);
2374 if (value != null) {
2376 params[name] = value;
2378 value == null && R.is(name, "object") && (params = name);
2380 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2381 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2382 this.attrs[key] = params[key];
2383 for (var subkey in par) if (par[has](subkey)) {
2384 params[subkey] = par[subkey];
2387 if (params.text && this.type == "text") {
2388 this.node.string = params.text;
2390 setFillAndStroke(this, params);
2391 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2392 addGradientFill(this, params.gradient);
2394 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2398 elproto.toFront = function () {
2399 !this.removed && this.Group.parentNode[appendChild](this.Group);
2400 this.paper.top != this && tofront(this, this.paper);
2403 elproto.toBack = function () {
2407 if (this.Group.parentNode.firstChild != this.Group) {
2408 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2409 toback(this, this.paper);
2413 elproto.insertAfter = function (element) {
2417 if (element.constructor == Set) {
2418 element = element[element.length - 1];
2420 if (element.Group.nextSibling) {
2421 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2423 element.Group.parentNode[appendChild](this.Group);
2425 insertafter(this, element, this.paper);
2428 elproto.insertBefore = function (element) {
2432 if (element.constructor == Set) {
2433 element = element[0];
2435 element.Group.parentNode.insertBefore(this.Group, element.Group);
2436 insertbefore(this, element, this.paper);
2439 elproto.blur = function (size) {
2440 var s = this.node.runtimeStyle,
2442 f = f.replace(blurregexp, E);
2444 this.attrs.blur = size;
2445 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2446 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2450 delete this.attrs.blur;
2454 theCircle = function (vml, x, y, r) {
2455 var g = createNode("group"),
2456 o = createNode("oval"),
2458 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2459 g.coordsize = coordsize;
2460 g.coordorigin = vml.coordorigin;
2462 var res = new Element(o, g, vml);
2463 res.type = "circle";
2464 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2468 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2469 vml.canvas[appendChild](g);
2472 function rectPath(x, y, w, h, r) {
2474 return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
2476 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2479 theRect = function (vml, x, y, w, h, r) {
2480 var path = rectPath(x, y, w, h, r),
2481 res = vml.path(path),
2485 res.W = a.width = w;
2486 res.H = a.height = h;
2492 theEllipse = function (vml, x, y, rx, ry) {
2493 var g = createNode("group"),
2494 o = createNode("oval"),
2496 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2497 g.coordsize = coordsize;
2498 g.coordorigin = vml.coordorigin;
2500 var res = new Element(o, g, vml);
2501 res.type = "ellipse";
2502 setFillAndStroke(res, {stroke: "#000"});
2507 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2508 vml.canvas[appendChild](g);
2511 theImage = function (vml, src, x, y, w, h) {
2512 var g = createNode("group"),
2513 o = createNode("image");
2514 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2515 g.coordsize = coordsize;
2516 g.coordorigin = vml.coordorigin;
2519 var res = new Element(o, g, vml);
2521 res.attrs.src = src;
2526 res.setBox({x: x, y: y, width: w, height: h});
2527 vml.canvas[appendChild](g);
2530 theText = function (vml, x, y, text) {
2531 var g = createNode("group"),
2532 el = createNode("shape"),
2534 path = createNode("path"),
2536 o = createNode("textpath");
2537 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2538 g.coordsize = coordsize;
2539 g.coordorigin = vml.coordorigin;
2540 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2541 path.textpathok = true;
2542 ol.width = vml.width;
2543 ol.height = vml.height;
2544 o.string = Str(text);
2547 el[appendChild](path);
2549 var res = new Element(o, g, vml);
2551 res.textpath = path;
2553 res.attrs.text = text;
2558 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2560 vml.canvas[appendChild](g);
2563 setSize = function (width, height) {
2564 var cs = this.canvas.style;
2565 width == +width && (width += "px");
2566 height == +height && (height += "px");
2569 cs.clip = "rect(0 " + width + " " + height + " 0)";
2573 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2575 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2576 createNode = function (tagName) {
2577 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2580 createNode = function (tagName) {
2581 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2584 create = function () {
2585 var con = getContainer[apply](0, arguments),
2586 container = con.container,
2587 height = con.height,
2593 throw new Error("VML container not found.");
2595 var res = new Paper,
2596 c = res.canvas = doc.createElement("div"),
2600 width = width || 512;
2601 height = height || 342;
2602 width == +width && (width += "px");
2603 height == +height && (height += "px");
2606 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2607 res.coordorigin = "0 0";
2608 res.span = doc.createElement("span");
2609 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2610 c[appendChild](res.span);
2611 cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2612 if (container == 1) {
2613 doc.body[appendChild](c);
2616 cs.position = "absolute";
2618 if (container.firstChild) {
2619 container.insertBefore(c, container.firstChild);
2621 container[appendChild](c);
2624 plugins.call(res, res, R.fn);
2627 paperproto.clear = function () {
2628 this.canvas.innerHTML = E;
2629 this.span = doc.createElement("span");
2630 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2631 this.canvas[appendChild](this.span);
2632 this.bottom = this.top = null;
2634 paperproto.remove = function () {
2635 this.canvas.parentNode.removeChild(this.canvas);
2636 for (var i in this) {
2637 this[i] = removed(i);
2644 // WebKit rendering bug workaround method
2645 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2646 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2647 paperproto.safari = function () {
2648 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2649 win.setTimeout(function () {rect.remove();});
2652 paperproto.safari = function () {};
2656 var preventDefault = function () {
2657 this.returnValue = false;
2659 preventTouch = function () {
2660 return this.originalEvent.preventDefault();
2662 stopPropagation = function () {
2663 this.cancelBubble = true;
2665 stopTouch = function () {
2666 return this.originalEvent.stopPropagation();
2668 addEvent = (function () {
2669 if (doc.addEventListener) {
2670 return function (obj, type, fn, element) {
2671 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2672 var f = function (e) {
2673 if (supportsTouch && touchMap[has](type)) {
2674 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2675 if (e.targetTouches[i].target == obj) {
2677 e = e.targetTouches[i];
2678 e.originalEvent = olde;
2679 e.preventDefault = preventTouch;
2680 e.stopPropagation = stopTouch;
2685 return fn.call(element, e);
2687 obj.addEventListener(realName, f, false);
2688 return function () {
2689 obj.removeEventListener(realName, f, false);
2693 } else if (doc.attachEvent) {
2694 return function (obj, type, fn, element) {
2695 var f = function (e) {
2697 e.preventDefault = e.preventDefault || preventDefault;
2698 e.stopPropagation = e.stopPropagation || stopPropagation;
2699 return fn.call(element, e);
2701 obj.attachEvent("on" + type, f);
2702 var detacher = function () {
2703 obj.detachEvent("on" + type, f);
2711 dragMove = function (e) {
2714 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2715 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2720 if (supportsTouch) {
2721 var i = e.touches.length,
2724 touch = e.touches[i];
2725 if (touch.identifier == dragi.el._drag.id) {
2728 (e.originalEvent ? e.originalEvent : e).preventDefault();
2737 dragi.move && dragi.move.call(dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
2740 dragUp = function (e) {
2741 R.unmousemove(dragMove).unmouseup(dragUp);
2742 var i = drag.length,
2746 dragi.el._drag = {};
2747 dragi.end && dragi.end.call(dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
2751 for (var i = events[length]; i--;) {
2752 (function (eventName) {
2753 R[eventName] = Element[proto][eventName] = function (fn, scope) {
2754 if (R.is(fn, "function")) {
2755 this.events = this.events || [];
2756 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, scope || this)});
2760 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2761 var events = this.events,
2763 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2765 events.splice(l, 1);
2766 !events.length && delete this.events;
2773 elproto.hover = function (f_in, f_out, scope_in, scope_out) {
2774 return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
2776 elproto.unhover = function (f_in, f_out) {
2777 return this.unmouseover(f_in).unmouseout(f_out);
2779 elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
2781 this.mousedown(function (e) {
2782 (e.originalEvent || e).preventDefault();
2783 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2784 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2785 this._drag.x = e.clientX + scrollX;
2786 this._drag.y = e.clientY + scrollY;
2787 this._drag.id = e.identifier;
2788 onstart && onstart.call(start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
2789 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2790 drag.push({el: this, move: onmove, end: onend, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
2794 elproto.undrag = function (onmove, onstart, onend) {
2795 var i = drag.length;
2797 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i++, 1);
2799 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2801 paperproto.circle = function (x, y, r) {
2802 return theCircle(this, x || 0, y || 0, r || 0);
2804 paperproto.rect = function (x, y, w, h, r) {
2805 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2807 paperproto.ellipse = function (x, y, rx, ry) {
2808 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2810 paperproto.path = function (pathString) {
2811 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2812 return thePath(R.format[apply](R, arguments), this);
2814 paperproto.image = function (src, x, y, w, h) {
2815 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2817 paperproto.text = function (x, y, text) {
2818 return theText(this, x || 0, y || 0, Str(text));
2820 paperproto.set = function (itemsArray) {
2821 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2822 return new Set(itemsArray);
2824 paperproto.setSize = setSize;
2825 paperproto.top = paperproto.bottom = null;
2826 paperproto.raphael = R;
2828 return this.x + S + this.y;
2830 elproto.resetScale = function () {
2836 this.attrs.scale = "1 1";
2838 elproto.scale = function (x, y, cx, cy) {
2842 if (x == null && y == null) {
2857 var bb = this.getBBox(),
2858 rcx = bb.x + bb.width / 2,
2859 rcy = bb.y + bb.height / 2,
2860 kx = abs(x / this._.sx),
2861 ky = abs(y / this._.sy);
2862 cx = (+cx || cx == 0) ? cx : rcx;
2863 cy = (+cy || cy == 0) ? cy : rcy;
2864 var posx = this._.sx > 0,
2865 posy = this._.sy > 0,
2866 dirx = ~~(x / abs(x)),
2867 diry = ~~(y / abs(y)),
2870 s = this.node.style,
2871 ncx = cx + abs(rcx - cx) * dkx * (rcx > cx == posx ? 1 : -1),
2872 ncy = cy + abs(rcy - cy) * dky * (rcy > cy == posy ? 1 : -1),
2873 fr = (x * dirx > y * diry ? ky : kx);
2874 switch (this.type) {
2877 var neww = a.width * kx,
2878 newh = a.height * ky;
2904 var path = pathToRelative(a.path),
2906 fx = posx ? dkx : kx,
2907 fy = posy ? dky : ky;
2908 for (var i = 0, ii = path[length]; i < ii; i++) {
2910 P0 = upperCase.call(p[0]);
2911 if (P0 == "M" && skip) {
2917 p[path[i][length] - 2] *= fx;
2918 p[path[i][length] - 1] *= fy;
2921 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2922 } else if (P0 == "H") {
2923 for (var j = 1, jj = p[length]; j < jj; j++) {
2926 } else if (P0 == "V") {
2927 for (j = 1, jj = p[length]; j < jj; j++) {
2931 for (j = 1, jj = p[length]; j < jj; j++) {
2932 p[j] *= (j % 2) ? fx : fy;
2936 var dim2 = pathDimensions(path);
2937 dx = ncx - dim2.x - dim2.width / 2;
2938 dy = ncy - dim2.y - dim2.height / 2;
2941 this.attr({path: path});
2944 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2945 if (this.transformations) {
2946 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2947 this.node[setAttribute]("transform", this.transformations[join](S));
2948 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2949 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2950 this.attr({x: dx, y: dy});
2954 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2955 ", M12=0, M21=0, M22=", diry,
2956 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2957 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2960 if (this.transformations) {
2961 this.transformations[2] = E;
2962 this.node[setAttribute]("transform", this.transformations[join](S));
2966 this.node.filterMatrix = E;
2967 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2970 a.scale = [x, y, cx, cy][join](S);
2976 elproto.clone = function () {
2980 var attr = this.attr();
2982 delete attr.translation;
2983 return this.paper[this.type]().attr(attr);
2985 var curveslengths = {},
2986 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2989 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2990 cache = curveslengths[name],
2992 !cache && (curveslengths[name] = cache = {data: []});
2993 cache.timer && clearTimeout(cache.timer);
2994 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2995 if (length != null) {
2996 var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
2997 precision = ~~total * 10;
2999 for (var i = 0; i < precision + 1; i++) {
3000 if (cache.data[length] > i) {
3001 dot = cache.data[i * precision];
3003 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
3004 cache.data[i] = dot;
3006 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
3007 if (length != null && len >= length) {
3012 if (length == null) {
3016 getLengthFactory = function (istotal, subpath) {
3017 return function (path, length, onlystart) {
3018 path = path2curve(path);
3019 var x, y, p, l, sp = "", subpaths = {}, point,
3021 for (var i = 0, ii = path.length; i < ii; i++) {
3027 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
3028 if (len + l > length) {
3029 if (subpath && !subpaths.start) {
3030 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3031 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
3032 if (onlystart) {return sp;}
3033 subpaths.start = sp;
3034 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
3040 if (!istotal && !subpath) {
3041 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3042 return {x: point.x, y: point.y, alpha: point.alpha};
3052 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3053 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3057 var getTotalLength = getLengthFactory(1),
3058 getPointAtLength = getLengthFactory(),
3059 getSubpathsAtLength = getLengthFactory(0, 1);
3060 elproto.getTotalLength = function () {
3061 if (this.type != "path") {return;}
3062 if (this.node.getTotalLength) {
3063 return this.node.getTotalLength();
3065 return getTotalLength(this.attrs.path);
3067 elproto.getPointAtLength = function (length) {
3068 if (this.type != "path") {return;}
3069 return getPointAtLength(this.attrs.path, length);
3071 elproto.getSubpath = function (from, to) {
3072 if (this.type != "path") {return;}
3073 if (abs(this.getTotalLength() - to) < "1e-6") {
3074 return getSubpathsAtLength(this.attrs.path, from).end;
3076 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3077 return from ? getSubpathsAtLength(a, from).end : a;
3080 // animation easing formulas
3081 R.easing_formulas = {
3082 linear: function (n) {
3089 return pow(n - 1, 3) + 1;
3091 "<>": function (n) {
3094 return pow(n, 3) / 2;
3097 return (pow(n, 3) + 2) / 2;
3099 backIn: function (n) {
3101 return n * n * ((s + 1) * n - s);
3103 backOut: function (n) {
3106 return n * n * ((s + 1) * n + s) + 1;
3108 elastic: function (n) {
3109 if (n == 0 || n == 1) {
3114 return pow(2, -10 * n) * math.sin((n - s) * (2 * PI) / p) + 1;
3116 bounce: function (n) {
3125 l = s * n * n + .75;
3127 if (n < (2.5 / p)) {
3129 l = s * n * n + .9375;
3132 l = s * n * n + .984375;
3140 var animationElements = [],
3141 animation = function () {
3142 var Now = +new Date;
3143 for (var l = 0; l < animationElements[length]; l++) {
3144 var e = animationElements[l];
3145 if (e.stop || e.el.removed) {
3148 var time = Now - e.start,
3159 var pos = easing(time / ms);
3160 for (var attr in from) if (from[has](attr)) {
3161 switch (availableAnimAttrs[attr]) {
3163 now = pos * ms * diff[attr];
3164 to.back && (now = to.len - now);
3165 var point = getPointAtLength(to[attr], now);
3166 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3169 that.translate(point.x - diff.sx, point.y - diff.sy);
3170 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3173 now = +from[attr] + pos * ms * diff[attr];
3177 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3178 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3179 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3184 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3185 now[i] = [from[attr][i][0]];
3186 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3187 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3189 now[i] = now[i][join](S);
3196 var x = pos * ms * diff[attr][0] - t.x,
3197 y = pos * ms * diff[attr][1] - t.y;
3203 now = +from[attr][0] + pos * ms * diff[attr][0];
3204 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3207 now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S);
3213 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3219 var from2 = [].concat(from[attr]);
3221 i = that.paper.customAttributes[attr].length;
3223 now[i] = +from2[i] + pos * ms * diff[attr][i];
3230 that._run && that._run.call(that);
3233 point = getPointAtLength(to.along, to.len * !to.back);
3234 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3235 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3237 (t.x || t.y) && that.translate(-t.x, -t.y);
3238 to.scale && (to.scale += E);
3240 animationElements.splice(l--, 1);
3243 R.svg && that && that.paper && that.paper.safari();
3244 animationElements[length] && setTimeout(animation);
3246 keyframesRun = function (attr, element, time, prev, prevcallback) {
3247 var dif = time - prev;
3248 element.timeouts.push(setTimeout(function () {
3249 R.is(prevcallback, "function") && prevcallback.call(element);
3250 element.animate(attr, dif, attr.easing);
3253 upto255 = function (color) {
3254 return mmax(mmin(color, 255), 0);
3256 translate = function (x, y) {
3258 return {x: this._.tx, y: this._.ty, toString: x_y};
3262 switch (this.type) {
3265 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3270 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3273 var path = pathToRelative(this.attrs.path);
3276 this.attr({path: path});
3281 elproto.animateWith = function (element, params, ms, easing, callback) {
3282 for (var i = 0, ii = animationElements.length; i < ii; i++) {
3283 if (animationElements[i].el.id == element.id) {
3284 params.start = animationElements[i].start;
3287 return this.animate(params, ms, easing, callback);
3289 elproto.animateAlong = along();
3290 elproto.animateAlongBack = along(1);
3291 function along(isBack) {
3292 return function (path, ms, rotate, callback) {
3293 var params = {back: isBack};
3294 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3295 path && path.constructor == Element && (path = path.attrs.path);
3296 path && (params.along = path);
3297 return this.animate(params, ms, callback);
3300 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3302 bx = 3 * (p2x - p1x) - cx,
3305 by = 3 * (p2y - p1y) - cy,
3307 function sampleCurveX(t) {
3308 return ((ax * t + bx) * t + cx) * t;
3310 function solve(x, epsilon) {
3311 var t = solveCurveX(x, epsilon);
3312 return ((ay * t + by) * t + cy) * t;
3314 function solveCurveX(x, epsilon) {
3315 var t0, t1, t2, x2, d2, i;
3316 for(t2 = x, i = 0; i < 8; i++) {
3317 x2 = sampleCurveX(t2) - x;
3318 if (abs(x2) < epsilon) {
3321 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3322 if (abs(d2) < 1e-6) {
3337 x2 = sampleCurveX(t2);
3338 if (abs(x2 - x) < epsilon) {
3346 t2 = (t1 - t0) / 2 + t0;
3350 return solve(t, 1 / (200 * duration));
3352 elproto.onAnimation = function (f) {
3356 elproto.animate = function (params, ms, easing, callback) {
3358 element.timeouts = element.timeouts || [];
3359 if (R.is(easing, "function") || !easing) {
3360 callback = easing || null;
3362 if (element.removed) {
3363 callback && callback.call(element);
3368 animateable = false,
3370 for (var attr in params) if (params[has](attr)) {
3371 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3373 from[attr] = element.attr(attr);
3374 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3375 to[attr] = params[attr];
3376 switch (availableAnimAttrs[attr]) {
3378 var len = getTotalLength(params[attr]);
3379 var point = getPointAtLength(params[attr], len * !!params.back);
3380 var bb = element.getBBox();
3381 diff[attr] = len / ms;
3386 to.rot = params.rot;
3387 to.back = params.back;
3389 params.rot && (diff.r = toFloat(element.rotate()) || 0);
3392 diff[attr] = (to[attr] - from[attr]) / ms;
3395 from[attr] = R.getRGB(from[attr]);
3396 var toColour = R.getRGB(to[attr]);
3398 r: (toColour.r - from[attr].r) / ms,
3399 g: (toColour.g - from[attr].g) / ms,
3400 b: (toColour.b - from[attr].b) / ms
3404 var pathes = path2curve(from[attr], to[attr]);
3405 from[attr] = pathes[0];
3406 var toPath = pathes[1];
3408 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3409 diff[attr][i] = [0];
3410 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3411 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3416 var values = Str(params[attr])[split](separator),
3417 from2 = Str(from[attr])[split](separator);
3420 from[attr] = [0, 0];
3421 diff[attr] = [values[0] / ms, values[1] / ms];
3424 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3425 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3428 params[attr] = values;
3429 from[attr] = Str(from[attr])[split](separator);
3430 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3433 from[attr] = Str(from[attr])[split](separator);
3437 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3444 values = [].concat(params[attr]);
3445 from2 = [].concat(from[attr]);
3447 i = element.paper.customAttributes[attr][length];
3449 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3458 for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3459 attr = {value: params[key]};
3460 key == "from" && (key = 0);
3461 key == "to" && (key = 100);
3462 attr.key = toInt(key, 10);
3465 attrs.sort(sortByKey);
3467 attrs.unshift({key: 0, value: element.attrs});
3469 for (i = 0, ii = attrs[length]; i < ii; i++) {
3470 keyframesRun(attrs[i].value, element, ms / 100 * attrs[i].key, ms / 100 * (attrs[i - 1] && attrs[i - 1].key || 0), attrs[i - 1] && attrs[i - 1].value.callback);
3472 lastcall = attrs[attrs[length] - 1].value.callback;
3474 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3477 var easyeasy = R.easing_formulas[easing];
3479 easyeasy = Str(easing).match(bezierrg);
3480 if (easyeasy && easyeasy[length] == 5) {
3481 var curve = easyeasy;
3482 easyeasy = function (t) {
3483 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3486 easyeasy = function (t) {
3491 animationElements.push({
3492 start: params.start || +new Date,
3501 R.is(callback, "function") && (element._ac = setTimeout(function () {
3502 callback.call(element);
3504 animationElements[length] == 1 && setTimeout(animation);
3508 elproto.stop = function () {
3509 for (var i = 0; i < animationElements.length; i++) {
3510 animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3512 for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3513 clearTimeout(this.timeouts[i]);
3516 clearTimeout(this._ac);
3520 elproto.translate = function (x, y) {
3521 return this.attr({translation: x + " " + y});
3523 elproto[toString] = function () {
3524 return "Rapha\xebl\u2019s object";
3526 R.ae = animationElements;
3529 var Set = function (items) {
3534 for (var i = 0, ii = items[length]; i < ii; i++) {
3535 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3536 this[this.items[length]] = this.items[this.items[length]] = items[i];
3542 Set[proto][push] = function () {
3545 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3546 item = arguments[i];
3547 if (item && (item.constructor == Element || item.constructor == Set)) {
3548 len = this.items[length];
3549 this[len] = this.items[len] = item;
3555 Set[proto].pop = function () {
3556 delete this[this[length]--];
3557 return this.items.pop();
3559 for (var method in elproto) if (elproto[has](method)) {
3560 Set[proto][method] = (function (methodname) {
3561 return function () {
3562 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3563 this.items[i][methodname][apply](this.items[i], arguments);
3569 Set[proto].attr = function (name, value) {
3570 if (name && R.is(name, array) && R.is(name[0], "object")) {
3571 for (var j = 0, jj = name[length]; j < jj; j++) {
3572 this.items[j].attr(name[j]);
3575 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3576 this.items[i].attr(name, value);
3581 Set[proto].animate = function (params, ms, easing, callback) {
3582 (R.is(easing, "function") || !easing) && (callback = easing || null);
3583 var len = this.items[length],
3588 callback && (collector = function () {
3589 !--len && callback.call(set);
3591 easing = R.is(easing, string) ? easing : collector;
3592 item = this.items[--i].animate(params, ms, easing, collector);
3594 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3598 Set[proto].insertAfter = function (el) {
3599 var i = this.items[length];
3601 this.items[i].insertAfter(el);
3605 Set[proto].getBBox = function () {
3610 for (var i = this.items[length]; i--;) {
3611 var box = this.items[i].getBBox();
3614 w[push](box.x + box.width);
3615 h[push](box.y + box.height);
3617 x = mmin[apply](0, x);
3618 y = mmin[apply](0, y);
3622 width: mmax[apply](0, w) - x,
3623 height: mmax[apply](0, h) - y
3626 Set[proto].clone = function (s) {
3628 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3629 s[push](this.items[i].clone());
3634 R.registerFont = function (font) {
3638 this.fonts = this.fonts || {};
3644 family = font.face["font-family"];
3645 for (var prop in font.face) if (font.face[has](prop)) {
3646 fontcopy.face[prop] = font.face[prop];
3648 if (this.fonts[family]) {
3649 this.fonts[family][push](fontcopy);
3651 this.fonts[family] = [fontcopy];
3654 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3655 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3656 var path = font.glyphs[glyph];
3657 fontcopy.glyphs[glyph] = {
3660 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3661 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3665 for (var k in path.k) if (path[has](k)) {
3666 fontcopy.glyphs[glyph].k[k] = path.k[k];
3673 paperproto.getFont = function (family, weight, style, stretch) {
3674 stretch = stretch || "normal";
3675 style = style || "normal";
3676 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3680 var font = R.fonts[family];
3682 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3683 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3684 if (name.test(fontName)) {
3685 font = R.fonts[fontName];
3692 for (var i = 0, ii = font[length]; i < ii; i++) {
3694 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3701 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3702 origin = origin || "middle"; // baseline|middle
3703 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3704 var out = this.set(),
3705 letters = Str(string)[split](E),
3709 R.is(font, string) && (font = this.getFont(font));
3711 scale = (size || 16) / font.face["units-per-em"];
3712 var bb = font.face.bbox.split(separator),
3714 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3715 for (var i = 0, ii = letters[length]; i < ii; i++) {
3716 var prev = i && font.glyphs[letters[i - 1]] || {},
3717 curr = font.glyphs[letters[i]];
3718 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3719 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3721 out.scale(scale, scale, top, height).translate(x - top, y - height);
3726 R.format = function (token, params) {
3727 var args = R.is(params, array) ? [0][concat](params) : arguments;
3728 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3729 return args[++i] == null ? E : args[i];
3733 R.ninja = function () {
3734 oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3740 oldRaphael.was ? (win.Raphael = R) : (Raphael = R);
3742 * g.Raphael 0.4.1 - Charting library, based on Raphaƫl
3744 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
3745 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
3750 var mmax = Math.max,
3752 Raphael.fn.g = Raphael.fn.g || {};
3753 Raphael.fn.g.markers = {
3762 triangle: "triangle",
3773 Raphael.fn.g.shim = {stroke: "none", fill: "#000", "fill-opacity": 0};
3774 Raphael.fn.g.txtattr = {font: "12px Arial, sans-serif"};
3775 Raphael.fn.g.colors = [];
3776 var hues = [.6, .2, .05, .1333, .75, 0];
3777 for (var i = 0; i < 10; i++) {
3778 if (i < hues.length) {
3779 Raphael.fn.g.colors.push("hsb(" + hues[i] + ", .75, .75)");
3781 Raphael.fn.g.colors.push("hsb(" + hues[i - hues.length] + ", 1, .5)");
3784 Raphael.fn.g.text = function (x, y, text) {
3785 return this.text(x, y, text).attr(this.g.txtattr);
3787 Raphael.fn.g.labelise = function (label, val, total) {
3789 return (label + "").replace(/(##+(?:\.#+)?)|(%%+(?:\.%+)?)/g, function (all, value, percent) {
3791 return (+val).toFixed(value.replace(/^#+\.?/g, "").length);
3794 return (val * 100 / total).toFixed(percent.replace(/^%+\.?/g, "").length) + "%";
3798 return (+val).toFixed(0);
3802 Raphael.fn.g.finger = function (x, y, width, height, dir, ending, isPath) {
3803 // dir 0 for horisontal and 1 for vertical
3804 if ((dir && !height) || (!dir && !width)) {
3805 return isPath ? "" : this.path();
3807 ending = {square: "square", sharp: "sharp", soft: "soft"}[ending] || "round";
3809 height = Math.round(height);
3810 width = Math.round(width);
3816 var r = ~~(height / 2);
3819 path = ["M", x + .5, y + .5 - ~~(height / 2), "l", 0, 0, "a", r, ~~(height / 2), 0, 0, 1, 0, height, "l", 0, 0, "z"];
3821 path = ["M", x + .5, y + .5 - r, "l", width - r, 0, "a", r, r, 0, 1, 1, 0, height, "l", r - width, 0, "z"];
3827 path = ["M", x - ~~(width / 2), y, "l", 0, 0, "a", ~~(width / 2), r, 0, 0, 1, width, 0, "l", 0, 0, "z"];
3829 path = ["M", x - r, y, "l", 0, r - height, "a", r, r, 0, 1, 1, width, 0, "l", 0, height - r, "z"];
3835 var half = ~~(height / 2);
3836 path = ["M", x, y + half, "l", 0, -height, mmax(width - half, 0), 0, mmin(half, width), half, -mmin(half, width), half + (half * 2 < height), "z"];
3838 half = ~~(width / 2);
3839 path = ["M", x + half, y, "l", -width, 0, 0, -mmax(height - half, 0), half, -mmin(half, height), half, mmin(half, height), half, "z"];
3844 path = ["M", x, y + ~~(height / 2), "l", 0, -height, width, 0, 0, height, "z"];
3846 path = ["M", x + ~~(width / 2), y, "l", 1 - width, 0, 0, -height, width - 1, 0, "z"];
3851 r = mmin(width, Math.round(height / 5));
3852 path = ["M", x + .5, y + .5 - ~~(height / 2), "l", width - r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r * 2, "a", r, r, 0, 0, 1, -r, r, "l", r - width, 0, "z"];
3854 r = mmin(Math.round(width / 5), height);
3855 path = ["M", x - ~~(width / 2), y, "l", 0, r - height, "a", r, r, 0, 0, 1, r, -r, "l", width - 2 * r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r, "z"];
3859 return path.join(",");
3861 return this.path(path);
3866 Raphael.fn.g.disc = function (cx, cy, r) {
3867 return this.circle(cx, cy, r);
3869 Raphael.fn.g.line = function (cx, cy, r) {
3870 return this.rect(cx - r, cy - r / 5, 2 * r, 2 * r / 5);
3872 Raphael.fn.g.square = function (cx, cy, r) {
3874 return this.rect(cx - r, cy - r, 2 * r, 2 * r);
3876 Raphael.fn.g.triangle = function (cx, cy, r) {
3878 return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z"));
3880 Raphael.fn.g.diamond = function (cx, cy, r) {
3881 return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]);
3883 Raphael.fn.g.flower = function (cx, cy, r, n) {
3887 n = +n < 3 || !n ? 5 : n;
3888 var points = ["M", cx, cy + rin, "Q"],
3890 for (var i = 1; i < n * 2 + 1; i++) {
3891 R = i % 2 ? rout : rin;
3892 points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]);
3895 return this.path(points.join(","));
3897 Raphael.fn.g.star = function (cx, cy, r, r2, rays) {
3898 r2 = r2 || r * .382;
3900 var points = ["M", cx, cy + r2, "L"],
3902 for (var i = 1; i < rays * 2; i++) {
3904 points = points.concat([(cx + R * Math.sin(i * Math.PI / rays)), (cy + R * Math.cos(i * Math.PI / rays))]);
3907 return this.path(points.join(","));
3909 Raphael.fn.g.cross = function (cx, cy, r) {
3911 return this.path("M".concat(cx - r, ",", cy, "l", [-r, -r, r, -r, r, r, r, -r, r, r, -r, r, r, r, -r, r, -r, -r, -r, r, -r, -r, "z"]));
3913 Raphael.fn.g.plus = function (cx, cy, r) {
3915 return this.path("M".concat(cx - r / 2, ",", cy - r / 2, "l", [0, -r, r, 0, 0, r, r, 0, 0, r, -r, 0, 0, r, -r, 0, 0, -r, -r, 0, 0, -r, "z"]));
3917 Raphael.fn.g.arrow = function (cx, cy, r) {
3918 return this.path("M".concat(cx - r * .7, ",", cy - r * .4, "l", [r * .6, 0, 0, -r * .4, r, r * .8, -r, r * .8, 0, -r * .4, -r * .6, 0], "z"));
3922 Raphael.fn.g.tag = function (x, y, text, angle, r) {
3924 r = r == null ? 5 : r;
3925 text = text == null ? "$9.99" : text;
3929 res.push(this.path().attr({fill: "#000", stroke: "#000"}));
3930 res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
3931 res.update = function () {
3932 this.rotate(0, x, y);
3933 var bb = this[1].getBBox();
3934 if (bb.height >= r * 2) {
3935 this[0].attr({path: ["M", x, y + r, "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2, "m", 0, -r * 2 -d, "a", r + d, r + d, 0, 1, 0, 0, (r + d) * 2, "L", x + r + d, y + bb.height / 2 + d, "l", bb.width + 2 * d, 0, 0, -bb.height - 2 * d, -bb.width - 2 * d, 0, "L", x, y - r - d].join(",")});
3937 var dx = Math.sqrt(Math.pow(r + d, 2) - Math.pow(bb.height / 2 + d, 2));
3938 this[0].attr({path: ["M", x, y + r, "c", -R, 0, -r, R - r, -r, -r, 0, -R, r - R, -r, r, -r, R, 0, r, r - R, r, r, 0, R, R - r, r, -r, r, "M", x + dx, y - bb.height / 2 - d, "a", r + d, r + d, 0, 1, 0, 0, bb.height + 2 * d, "l", r + d - dx + bb.width + 2 * d, 0, 0, -bb.height - 2 * d, "L", x + dx, y - bb.height / 2 - d].join(",")});
3940 this[1].attr({x: x + r + d + bb.width / 2, y: y});
3941 angle = (360 - angle) % 360;
3942 this.rotate(angle, x, y);
3943 angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]});
3949 Raphael.fn.g.popupit = function (x, y, set, dir, size) {
3950 dir = dir == null ? 2 : dir;
3954 var bb = set.getBBox(),
3955 w = Math.round(bb.width / 2),
3956 h = Math.round(bb.height / 2),
3957 dx = [0, w + size * 2, 0, -w - size * 2],
3958 dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
3959 p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
3960 "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
3961 "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
3962 "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
3963 "l", -mmax(w - size, 0), 0, "z"].join(","),
3964 xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];
3965 set.translate(xy.x - w - bb.x, xy.y - h - bb.y);
3966 return this.path(p).attr({fill: "#000", stroke: "none"}).insertBefore(set.node ? set : set[0]);
3968 Raphael.fn.g.popup = function (x, y, text, dir, size) {
3969 dir = dir == null ? 2 : dir > 3 ? 3 : dir;
3971 text = text || "$9.99";
3972 var res = this.set(),
3974 res.push(this.path().attr({fill: "#000", stroke: "#000"}));
3975 res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
3976 res.update = function (X, Y, withAnimation) {
3979 var bb = this[1].getBBox(),
3982 dx = [0, w + size * 2, 0, -w - size * 2],
3983 dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
3984 p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
3985 "l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
3986 "l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
3987 "l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
3988 "l", -mmax(w - size, 0), 0, "z"].join(","),
3989 xy = [{x: X, y: Y + size * 2 + h}, {x: X - size * 2 - w, y: Y}, {x: X, y: Y - size * 2 - h}, {x: X + size * 2 + w, y: Y}][dir];
3991 if (withAnimation) {
3992 this.animate(xy, 500, ">");
3998 return res.update(x, y);
4000 Raphael.fn.g.flag = function (x, y, text, angle) {
4002 text = text || "$9.99";
4003 var res = this.set(),
4005 res.push(this.path().attr({fill: "#000", stroke: "#000"}));
4006 res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
4007 res.update = function (x, y) {
4008 this.rotate(0, x, y);
4009 var bb = this[1].getBBox(),
4011 this[0].attr({path: ["M", x, y, "l", h + d, -h - d, bb.width + 2 * d, 0, 0, bb.height + 2 * d, -bb.width - 2 * d, 0, "z"].join(",")});
4012 this[1].attr({x: x + h + d + bb.width / 2, y: y});
4013 angle = 360 - angle;
4014 this.rotate(angle, x, y);
4015 angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]});
4018 return res.update(x, y);
4020 Raphael.fn.g.label = function (x, y, text) {
4021 var res = this.set();
4022 res.push(this.rect(x, y, 10, 10).attr({stroke: "none", fill: "#000"}));
4023 res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"}));
4024 res.update = function () {
4025 var bb = this[1].getBBox(),
4026 r = mmin(bb.width + 10, bb.height + 10) / 2;
4027 this[0].attr({x: bb.x - r / 2, y: bb.y - r / 2, width: bb.width + r, height: bb.height + r, r: r});
4032 Raphael.fn.g.labelit = function (set) {
4033 var bb = set.getBBox(),
4034 r = mmin(20, bb.width + 10, bb.height + 10) / 2;
4035 return this.rect(bb.x - r / 2, bb.y - r / 2, bb.width + r, bb.height + r, r).attr({stroke: "none", fill: "#000"}).insertBefore(set.node ? set : set[0]);
4037 Raphael.fn.g.drop = function (x, y, text, size, angle) {
4040 var res = this.set();
4041 res.push(this.path(["M", x, y, "l", size, 0, "A", size * .4, size * .4, 0, 1, 0, x + size * .7, y - size * .7, "z"]).attr({fill: "#000", stroke: "none", rotation: [22.5 - angle, x, y]}));
4042 angle = (angle + 90) * Math.PI / 180;
4043 res.push(this.text(x + size * Math.sin(angle), y + size * Math.cos(angle), text).attr(this.g.txtattr).attr({"font-size": size * 12 / 30, fill: "#fff"}));
4048 Raphael.fn.g.blob = function (x, y, text, angle, size) {
4049 angle = (+angle + 1 ? angle : 45) + 90;
4051 var rad = Math.PI / 180,
4052 fontSize = size * 12 / 12;
4053 var res = this.set();
4054 res.push(this.path().attr({fill: "#000", stroke: "none"}));
4055 res.push(this.text(x + size * Math.sin((angle) * rad), y + size * Math.cos((angle) * rad) - fontSize / 2, text).attr(this.g.txtattr).attr({"font-size": fontSize, fill: "#fff"}));
4056 res.update = function (X, Y, withAnimation) {
4059 var bb = this[1].getBBox(),
4060 w = mmax(bb.width + fontSize, size * 25 / 12),
4061 h = mmax(bb.height + fontSize, size * 25 / 12),
4062 x2 = X + size * Math.sin((angle - 22.5) * rad),
4063 y2 = Y + size * Math.cos((angle - 22.5) * rad),
4064 x1 = X + size * Math.sin((angle + 22.5) * rad),
4065 y1 = Y + size * Math.cos((angle + 22.5) * rad),
4070 k = -Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * dy * dy - ry * ry * dx * dx) / (rx * rx * dy * dy + ry * ry * dx * dx)),
4071 cx = k * rx * dy / ry + (x1 + x2) / 2,
4072 cy = k * -ry * dx / rx + (y1 + y2) / 2;
4073 if (withAnimation) {
4074 this.animate({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}, 500, ">");
4076 this.attr({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")});
4084 Raphael.fn.g.colorValue = function (value, total, s, b) {
4085 return "hsb(" + [mmin((1 - value / total) * .4, 1), s || .75, b || .75] + ")";
4088 Raphael.fn.g.snapEnds = function (from, to, steps) {
4092 return {from: f, to: t, power: 0};
4095 return Math.abs(a - .5) < .25 ? ~~(a) + .5 : Math.round(a);
4097 var d = (t - f) / steps,
4104 R = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
4110 r = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
4115 t = round(to * Math.pow(10, i)) / Math.pow(10, i);
4117 t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i);
4119 f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i);
4120 return {from: f, to: t, power: i};
4122 Raphael.fn.g.axis = function (x, y, length, from, to, steps, orientation, labels, type, dashsize) {
4123 dashsize = dashsize == null ? 2 : dashsize;
4125 steps = steps || 10;
4126 var path = type == "|" || type == " " ? ["M", x + .5, y, "l", 0, .001] : orientation == 1 || orientation == 3 ? ["M", x + .5, y, "l", 0, -length] : ["M", x, y + .5, "l", length, 0],
4127 ends = this.g.snapEnds(from, to, steps),
4133 d = (t - f) / steps;
4135 rnd = i > 0 ? i : 0;
4136 dx = length / steps;
4137 if (+orientation == 1 || +orientation == 3) {
4139 addon = (orientation - 1 ? 1 : -1) * (dashsize + 3 + !!(orientation - 1));
4140 while (Y >= y - length) {
4141 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), Y + .5, "l", dashsize * 2 + 1, 0]));
4142 text.push(this.text(x + addon, Y, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"}));
4146 if (Math.round(Y + dx - (y - length))) {
4147 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), y - length + .5, "l", dashsize * 2 + 1, 0]));
4148 text.push(this.text(x + addon, y - length, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"}));
4153 addon = (orientation ? -1 : 1) * (dashsize + 9 + !orientation);
4155 dx = length / steps,
4158 while (X <= x + length) {
4159 type != "-" && type != " " && (path = path.concat(["M", X + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
4160 text.push(txt = this.text(X, y + addon, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr));
4161 var bb = txt.getBBox();
4162 if (prev >= bb.x - 5) {
4163 text.pop(text.length - 1).remove();
4165 prev = bb.x + bb.width;
4170 if (Math.round(X - dx - x - length)) {
4171 type != "-" && type != " " && (path = path.concat(["M", x + length + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
4172 text.push(this.text(x + length, y + addon, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr));
4175 var res = this.path(path);
4177 res.all = this.set([res, text]);
4178 res.remove = function () {
4180 this.constructor.prototype.remove.call(this);
4185 Raphael.el.lighter = function (times) {
4187 var fs = [this.attrs.fill, this.attrs.stroke];
4188 this.fs = this.fs || [fs[0], fs[1]];
4189 fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
4190 fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
4191 fs[0].b = mmin(fs[0].b * times, 1);
4192 fs[0].s = fs[0].s / times;
4193 fs[1].b = mmin(fs[1].b * times, 1);
4194 fs[1].s = fs[1].s / times;
4195 this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
4197 Raphael.el.darker = function (times) {
4199 var fs = [this.attrs.fill, this.attrs.stroke];
4200 this.fs = this.fs || [fs[0], fs[1]];
4201 fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
4202 fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
4203 fs[0].s = mmin(fs[0].s * times, 1);
4204 fs[0].b = fs[0].b / times;
4205 fs[1].s = mmin(fs[1].s * times, 1);
4206 fs[1].b = fs[1].b / times;
4207 this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
4209 Raphael.el.original = function () {
4211 this.attr({fill: this.fs[0], stroke: this.fs[1]});
4216 * g.Raphael 0.4.1 - Charting library, based on Raphaƫl
4218 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4219 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4221 Raphael.fn.g.barchart = function (x, y, width, height, values, opts) {
4223 var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square",
4224 gutter = parseFloat(opts.gutter || "20%"),
4227 covers = this.set(),
4228 covers2 = this.set(),
4229 total = Math.max.apply(Math, values),
4233 colors = opts.colors || this.g.colors,
4234 len = values.length;
4235 if (this.raphael.is(values[0], "array")) {
4239 for (var i = values.length; i--;) {
4240 bars.push(this.set());
4241 total.push(Math.max.apply(Math, values[i]));
4242 len = Math.max(len, values[i].length);
4245 for (var i = len; i--;) {
4247 for (var j = values.length; j--;) {
4248 tot +=+ values[j][i] || 0;
4250 stacktotal.push(tot);
4253 for (var i = values.length; i--;) {
4254 if (values[i].length < len) {
4255 for (var j = len; j--;) {
4260 total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
4263 total = (opts.to) || total;
4264 var barwidth = width / (len * (100 + gutter) + gutter) * 100,
4265 barhgutter = barwidth * gutter / 100,
4266 barvgutter = opts.vgutter == null ? 20 : opts.vgutter,
4269 Y = (height - 2 * barvgutter) / total;
4270 if (!opts.stretch) {
4271 barhgutter = Math.round(barhgutter);
4272 barwidth = Math.floor(barwidth);
4274 !opts.stacked && (barwidth /= multi || 1);
4275 for (var i = 0; i < len; i++) {
4277 for (var j = 0; j < (multi || 1); j++) {
4278 var h = Math.round((multi ? values[j][i] : values[i]) * Y),
4279 top = y + height - barvgutter - h,
4280 bar = this.g.finger(Math.round(X + barwidth / 2), top + h, barwidth, h, true, type).attr({stroke: "none", fill: colors[multi ? j : i]});
4287 bar.x = Math.round(X + barwidth / 2);
4291 bar.value = multi ? values[j][i] : values[i];
4292 if (!opts.stacked) {
4300 covers2.push(cvr = this.rect(stack[0].x - stack[0].w / 2, y, barwidth, height).attr(this.g.shim));
4301 cvr.bars = this.set();
4303 for (var s = stack.length; s--;) {
4306 for (var s = 0, ss = stack.length; s < ss; s++) {
4309 h = (size + bar.value) * Y,
4310 path = this.g.finger(bar.x, y + height - barvgutter - !!size * .5, barwidth, h, true, type, 1);
4312 size && bar.attr({path: path});
4314 bar.y = y + height - barvgutter - !!size * .5 - h;
4315 covers.push(cover = this.rect(bar.x - bar.w / 2, bar.y, barwidth, bar.value * Y).attr(this.g.shim));
4317 cover.value = bar.value;
4326 if (!opts.stacked) {
4327 for (var i = 0; i < len; i++) {
4328 for (var j = 0; j < (multi || 1); j++) {
4330 covers.push(cover = this.rect(Math.round(X), y + barvgutter, barwidth, height - barvgutter).attr(this.g.shim));
4331 cover.bar = multi ? bars[j][i] : bars[i];
4332 cover.value = cover.bar.value;
4338 chart.label = function (labels, isBottom, rotate) {
4339 labels = labels || [];
4340 isBottom = isBottom == undefined ? true : isBottom;
4341 rotate = rotate == undefined ? false : rotate;
4342 this.labels = paper.set();
4343 var L, l = -Infinity;
4345 for (var i = 0; i < len; i++) {
4347 for (var j = 0; j < (multi || 1); j++) {
4348 tot += multi ? values[j][i] : values[i];
4350 var label = paper.g.labelise(labels[j][i], tot, total);
4351 L = paper.g.text(bars[j][i].x, isBottom ? y + height - barvgutter / 2 : bars[j][i].y - 10, label);
4355 var bb = L.getBBox();
4359 this.labels.push(L);
4360 l = bb.x + (rotate ? bb.height : bb.width);
4366 for (var i = 0; i < len; i++) {
4367 for (var j = 0; j < (multi || 1); j++) {
4368 // did not remove the loop because don't yet know whether to accept multi array input for arrays
4369 var label = paper.g.labelise(multi ? labels[0] && labels[0][i] : labels[i], multi ? values[0][i] : values[i], total);
4370 L = paper.g.text(bars[0][i].x, isBottom ? y + 5 + height - barvgutter / 2 : bars[0][i].y - 10, label);
4373 // If we rotated it, we need to move it as well. Still have to use the width
4374 // to get the "length" of the label, divided it in two and shift down.
4375 L.translate(0, (L.getBBox().width / 2));
4377 var bb = L.getBBox();
4378 // if (bb.x - 7 < l) {
4379 if (bb.x - (this.getBBox().width) < l) {
4382 this.labels.push(L);
4383 l = bb.x + (rotate ? bb.height : bb.width);
4390 chart.hover = function (fin, fout) {
4393 covers.mouseover(fin).mouseout(fout);
4396 chart.hoverColumn = function (fin, fout) {
4399 fout = fout || function () {};
4400 covers2.mouseover(fin).mouseout(fout);
4403 chart.click = function (f) {
4409 chart.each = function (f) {
4410 if (!Raphael.is(f, "function")) {
4413 for (var i = covers.length; i--;) {
4418 chart.eachColumn = function (f) {
4419 if (!Raphael.is(f, "function")) {
4422 for (var i = covers2.length; i--;) {
4427 chart.clickColumn = function (f) {
4433 chart.push(bars, covers, covers2);
4435 chart.covers = covers;
4438 Raphael.fn.g.hbarchart = function (x, y, width, height, values, opts) {
4440 var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square",
4441 gutter = parseFloat(opts.gutter || "20%"),
4444 covers = this.set(),
4445 covers2 = this.set(),
4446 total = Math.max.apply(Math, values),
4450 colors = opts.colors || this.g.colors,
4451 len = values.length;
4452 if (this.raphael.is(values[0], "array")) {
4456 for (var i = values.length; i--;) {
4457 bars.push(this.set());
4458 total.push(Math.max.apply(Math, values[i]));
4459 len = Math.max(len, values[i].length);
4462 for (var i = len; i--;) {
4464 for (var j = values.length; j--;) {
4465 tot +=+ values[j][i] || 0;
4467 stacktotal.push(tot);
4470 for (var i = values.length; i--;) {
4471 if (values[i].length < len) {
4472 for (var j = len; j--;) {
4477 total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
4480 total = (opts.to) || total;
4481 var barheight = Math.floor(height / (len * (100 + gutter) + gutter) * 100),
4482 bargutter = Math.floor(barheight * gutter / 100),
4485 X = (width - 1) / total;
4486 !opts.stacked && (barheight /= multi || 1);
4487 for (var i = 0; i < len; i++) {
4489 for (var j = 0; j < (multi || 1); j++) {
4490 var val = multi ? values[j][i] : values[i],
4491 bar = this.g.finger(x, Y + barheight / 2, Math.round(val * X), barheight - 1, false, type).attr({stroke: "none", fill: colors[multi ? j : i]});
4497 bar.x = x + Math.round(val * X);
4498 bar.y = Y + barheight / 2;
4499 bar.w = Math.round(val * X);
4502 if (!opts.stacked) {
4509 var cvr = this.rect(x, stack[0].y - stack[0].h / 2, width, barheight).attr(this.g.shim);
4511 cvr.bars = this.set();
4513 for (var s = stack.length; s--;) {
4516 for (var s = 0, ss = stack.length; s < ss; s++) {
4519 val = Math.round((size + bar.value) * X),
4520 path = this.g.finger(x, bar.y, val, barheight - 1, false, type, 1);
4522 size && bar.attr({path: path});
4525 covers.push(cover = this.rect(x + size * X, bar.y - bar.h / 2, bar.value * X, barheight).attr(this.g.shim));
4535 if (!opts.stacked) {
4536 for (var i = 0; i < len; i++) {
4537 for (var j = 0; j < (multi || 1); j++) {
4538 var cover = this.rect(x, Y, width, barheight).attr(this.g.shim);
4540 cover.bar = multi ? bars[j][i] : bars[i];
4541 cover.value = cover.bar.value;
4547 chart.label = function (labels, isRight) {
4548 labels = labels || [];
4549 this.labels = paper.set();
4550 for (var i = 0; i < len; i++) {
4551 for (var j = 0; j < multi; j++) {
4552 var label = paper.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total);
4553 var X = isRight ? bars[i * (multi || 1) + j].x - barheight / 2 + 3 : x + 5,
4554 A = isRight ? "end" : "start",
4556 this.labels.push(L = paper.g.text(X, bars[i * (multi || 1) + j].y, label).attr({"text-anchor": A}).insertBefore(covers[0]));
4557 if (L.getBBox().x < x + 5) {
4558 L.attr({x: x + 5, "text-anchor": "start"});
4560 bars[i * (multi || 1) + j].label = L;
4566 chart.hover = function (fin, fout) {
4569 fout = fout || function () {};
4570 covers.mouseover(fin).mouseout(fout);
4573 chart.hoverColumn = function (fin, fout) {
4576 fout = fout || function () {};
4577 covers2.mouseover(fin).mouseout(fout);
4580 chart.each = function (f) {
4581 if (!Raphael.is(f, "function")) {
4584 for (var i = covers.length; i--;) {
4589 chart.eachColumn = function (f) {
4590 if (!Raphael.is(f, "function")) {
4593 for (var i = covers2.length; i--;) {
4598 chart.click = function (f) {
4604 chart.clickColumn = function (f) {
4610 chart.push(bars, covers, covers2);
4612 chart.covers = covers;
4616 * g.Raphael 0.4.1 - Charting library, based on Raphaƫl
4618 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4619 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4621 Raphael.fn.g.dotchart = function (x, y, width, height, valuesx, valuesy, size, opts) {
4622 function drawAxis(ax) {
4623 +ax[0] && (ax[0] = paper.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t"));
4624 +ax[1] && (ax[1] = paper.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t"));
4625 +ax[2] && (ax[2] = paper.g.axis(x + gutter, y + height - gutter + maxR, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t"));
4626 +ax[3] && (ax[3] = paper.g.axis(x + gutter - maxR, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t"));
4629 var xdim = this.g.snapEnds(Math.min.apply(Math, valuesx), Math.max.apply(Math, valuesx), valuesx.length - 1),
4632 gutter = opts.gutter || 10,
4633 ydim = this.g.snapEnds(Math.min.apply(Math, valuesy), Math.max.apply(Math, valuesy), valuesy.length - 1),
4636 len = Math.max(valuesx.length, valuesy.length, size.length),
4637 symbol = this.g.markers[opts.symbol] || "disc",
4639 series = this.set(),
4640 max = opts.max || 100,
4641 top = Math.max.apply(Math, size),
4644 k = Math.sqrt(top / Math.PI) * 2 / max;
4646 for (var i = 0; i < len; i++) {
4647 R[i] = Math.min(Math.sqrt(size[i] / Math.PI) * 2 / k, max);
4649 gutter = Math.max.apply(Math, R.concat(gutter));
4650 var axis = this.set(),
4651 maxR = Math.max.apply(Math, R);
4653 var ax = (opts.axis + "").split(/[,\s]+/);
4656 for (var i = 0, ii = ax.length; i < ii; i++) {
4657 var bb = ax[i].all ? ax[i].all.getBBox()[["height", "width"][i % 2]] : 0;
4661 gutter = Math.max.apply(Math, g.concat(gutter));
4662 for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
4667 for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
4668 axis.push(ax[i].all);
4672 var kx = (width - gutter * 2) / ((maxx - minx) || 1),
4673 ky = (height - gutter * 2) / ((maxy - miny) || 1);
4674 for (var i = 0, ii = valuesy.length; i < ii; i++) {
4675 var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol,
4676 X = x + gutter + (valuesx[i] - minx) * kx,
4677 Y = y + height - gutter - (valuesy[i] - miny) * ky;
4678 sym && R[i] && series.push(this.g[sym](X, Y, R[i]).attr({fill: opts.heat ? this.g.colorValue(R[i], maxR) : Raphael.fn.g.colors[0], "fill-opacity": opts.opacity ? R[i] / max : 1, stroke: "none"}));
4680 var covers = this.set();
4681 for (var i = 0, ii = valuesy.length; i < ii; i++) {
4682 var X = x + gutter + (valuesx[i] - minx) * kx,
4683 Y = y + height - gutter - (valuesy[i] - miny) * ky;
4684 covers.push(this.circle(X, Y, maxR).attr(this.g.shim));
4685 opts.href && opts.href[i] && covers[i].attr({href: opts.href[i]});
4686 covers[i].r = +R[i].toFixed(3);
4687 covers[i].x = +X.toFixed(3);
4688 covers[i].y = +Y.toFixed(3);
4689 covers[i].X = valuesx[i];
4690 covers[i].Y = valuesy[i];
4691 covers[i].value = size[i] || 0;
4692 covers[i].dot = series[i];
4694 res.covers = covers;
4695 res.series = series;
4696 res.push(series, axis, covers);
4697 res.hover = function (fin, fout) {
4698 covers.mouseover(fin).mouseout(fout);
4701 res.click = function (f) {
4705 res.each = function (f) {
4706 if (!Raphael.is(f, "function")) {
4709 for (var i = covers.length; i--;) {
4714 res.href = function (map) {
4716 for (var i = covers.length; i--;) {
4718 if (cover.X == map.x && cover.Y == map.y && cover.value == map.value) {
4719 cover.attr({href: map.href});
4726 * g.Raphael 0.4.2 - Charting library, based on Raphaƫl
4728 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4729 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4731 Raphael.fn.g.linechart = function (x, y, width, height, valuesx, valuesy, opts) {
4732 function shrink(values, dim) {
4733 var k = values.length / dim,
4738 while (j < values.length) {
4741 sum += values[j] * (1 + l);
4743 sum = values[j++] * -l;
4751 function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) {
4752 var l1 = (p2x - p1x) / 2,
4753 l2 = (p3x - p2x) / 2,
4754 a = Math.atan((p2x - p1x) / Math.abs(p2y - p1y)),
4755 b = Math.atan((p3x - p2x) / Math.abs(p2y - p3y));
4756 a = p1y < p2y ? Math.PI - a : a;
4757 b = p3y < p2y ? Math.PI - b : b;
4758 var alpha = Math.PI / 2 - ((a + b) % (Math.PI * 2)) / 2,
4759 dx1 = l1 * Math.sin(alpha + a),
4760 dy1 = l1 * Math.cos(alpha + a),
4761 dx2 = l2 * Math.sin(alpha + b),
4762 dy2 = l2 * Math.cos(alpha + b);
4771 if (!this.raphael.is(valuesx[0], "array")) {
4772 valuesx = [valuesx];
4774 if (!this.raphael.is(valuesy[0], "array")) {
4775 valuesy = [valuesy];
4777 var gutter = opts.gutter || 10,
4778 len = Math.max(valuesx[0].length, valuesy[0].length),
4779 symbol = opts.symbol || "",
4780 colors = opts.colors || Raphael.fn.g.colors,
4787 for (var i = 0, ii = valuesy.length; i < ii; i++) {
4788 len = Math.max(len, valuesy[i].length);
4790 var shades = this.set();
4791 for (i = 0, ii = valuesy.length; i < ii; i++) {
4793 shades.push(this.path().attr({stroke: "none", fill: colors[i], opacity: opts.nostroke ? 1 : .3}));
4795 if (valuesy[i].length > width - 2 * gutter) {
4796 valuesy[i] = shrink(valuesy[i], width - 2 * gutter);
4797 len = width - 2 * gutter;
4799 if (valuesx[i] && valuesx[i].length > width - 2 * gutter) {
4800 valuesx[i] = shrink(valuesx[i], width - 2 * gutter);
4803 var allx = Array.prototype.concat.apply([], valuesx),
4804 ally = Array.prototype.concat.apply([], valuesy),
4805 xdim = this.g.snapEnds(Math.min.apply(Math, allx), Math.max.apply(Math, allx), valuesx[0].length - 1);
4807 var minx = opts.minx || xdim.from,
4808 maxx = opts.maxx || xdim.to,
4809 ydim = this.g.snapEnds(Math.min.apply(Math, ally), Math.max.apply(Math, ally), valuesy[0].length - 1),
4810 miny = opts.miny || ydim.from,
4811 maxy = opts.maxy || ydim.to;
4813 var minx = opts.minx && Math.min(opts.minx, xdim.from) || xdim.from,
4814 maxx = opts.maxx && Math.max(opts.maxx, xdimt.to) || xdim.to,
4815 ydim = this.g.snapEnds(Math.min.apply(Math, ally), Math.max.apply(Math, ally), valuesy[0].length - 1),
4816 miny = opts.miny && Math.min(opts.miny, ydim.from) || ydim.from,
4817 maxy = opts.maxy && Math.max(opts.maxy, ydim.to) || ydim.to;
4819 kx = (width - gutter * 2) / ((maxx - minx) || 1),
4820 ky = (height - gutter * 2) / ((maxy - miny) || 1);
4822 var lines = this.set(),
4823 symbols = this.set(),
4825 for (i = 0, ii = valuesy.length; i < ii; i++) {
4826 if (!opts.nostroke) {
4827 lines.push(line = this.path().attr({
4829 "stroke-width": opts.width || 2,
4830 "stroke-linejoin": "round",
4831 "stroke-linecap": "round",
4832 "stroke-dasharray": opts.dash || ""
4835 var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol,
4836 symset = this.set();
4838 for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
4839 var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
4840 Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
4841 (Raphael.is(sym, "array") ? sym[j] : sym) && symset.push(this.g[Raphael.fn.g.markers[this.raphael.is(sym, "array") ? sym[j] : sym]](X, Y, (opts.width || 2) * 3).attr({fill: colors[i], stroke: "none"}));
4843 if (j && j != jj - 1) {
4844 var X0 = x + gutter + ((valuesx[i] || valuesx[0])[j - 1] - minx) * kx,
4845 Y0 = y + height - gutter - (valuesy[i][j - 1] - miny) * ky,
4846 X2 = x + gutter + ((valuesx[i] || valuesx[0])[j + 1] - minx) * kx,
4847 Y2 = y + height - gutter - (valuesy[i][j + 1] - miny) * ky;
4848 var a = getAnchors(X0, Y0, X, Y, X2, Y2);
4849 path = path.concat([a.x1, a.y1, X, Y, a.x2, a.y2]);
4852 path = ["M", X, Y, "C", X, Y];
4855 path = path.concat([j ? "L" : "M", X, Y]);
4859 path = path.concat([X, Y, X, Y]);
4861 symbols.push(symset);
4863 shades[i].attr({path: path.concat(["L", X, y + height - gutter, "L", x + gutter + ((valuesx[i] || valuesx[0])[0] - minx) * kx, y + height - gutter, "z"]).join(",")});
4865 !opts.nostroke && line.attr({path: path.join(","), 'clip-rect': [x + gutter, y + gutter, width - 2 * gutter, height - 2 * gutter].join(",")});
4868 function createColumns(f) {
4869 // unite Xs together
4871 for (var i = 0, ii = valuesx.length; i < ii; i++) {
4872 Xs = Xs.concat(valuesx[i]);
4874 Xs.sort(function(a,b) { return a - b; });
4875 // remove duplicates
4878 for (i = 0, ii = Xs.length; i < ii; i++) {
4879 Xs[i] != Xs[i - 1] && Xs2.push(Xs[i]) && xs.push(x + gutter + (Xs[i] - minx) * kx);
4883 var cvrs = f || that.set();
4884 for (i = 0; i < ii; i++) {
4885 var X = xs[i] - (xs[i] - (xs[i - 1] || x)) / 2,
4886 w = ((xs[i + 1] || x + width) - xs[i]) / 2 + (xs[i] - (xs[i - 1] || x)) / 2,
4888 f ? (C = {}) : cvrs.push(C = that.rect(X - 1, y, Math.max(w + 1, 1), height).attr({stroke: "none", fill: "#000", opacity: 0}));
4890 C.symbols = that.set();
4894 for (var j = 0, jj = valuesy.length; j < jj; j++) {
4895 Xs2 = valuesx[j] || valuesx[0];
4896 for (var k = 0, kk = Xs2.length; k < kk; k++) {
4897 if (Xs2[k] == Xs[i]) {
4898 C.values.push(valuesy[j][k]);
4899 C.y.push(y + height - gutter - (valuesy[j][k] - miny) * ky);
4900 C.symbols.push(chart.symbols[j][k]);
4906 !f && (columns = cvrs);
4908 function createDots(f) {
4909 var cvrs = f || that.set(),
4911 for (var i = 0, ii = valuesy.length; i < ii; i++) {
4912 for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
4913 var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
4914 nearX = x + gutter + ((valuesx[i] || valuesx[0])[j ? j - 1 : 1] - minx) * kx,
4915 Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
4916 f ? (C = {}) : cvrs.push(C = that.circle(X, Y, Math.abs(nearX - X) / 2).attr({stroke: "none", fill: "#000", opacity: 0}));
4919 C.value = valuesy[i][j];
4920 C.line = chart.lines[i];
4921 C.shade = chart.shades[i];
4922 C.symbol = chart.symbols[i][j];
4923 C.symbols = chart.symbols[i];
4924 C.axis = (valuesx[i] || valuesx[0])[j];
4928 !f && (dots = cvrs);
4931 var axis = this.set();
4933 var ax = (opts.axis + "").split(/[,\s]+/);
4934 +ax[0] && axis.push(this.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t"));
4935 +ax[1] && axis.push(this.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t"));
4936 +ax[2] && axis.push(this.g.axis(x + gutter, y + height - gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t"));
4937 +ax[3] && axis.push(this.g.axis(x + gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t"));
4940 chart.push(lines, shades, symbols, axis, columns, dots);
4941 chart.lines = lines;
4942 chart.shades = shades;
4943 chart.symbols = symbols;
4945 chart.hoverColumn = function (fin, fout) {
4946 !columns && createColumns();
4947 columns.mouseover(fin).mouseout(fout);
4950 chart.clickColumn = function (f) {
4951 !columns && createColumns();
4955 chart.hrefColumn = function (cols) {
4956 var hrefs = that.raphael.is(arguments[0], "array") ? arguments[0] : arguments;
4957 if (!(arguments.length - 1) && typeof cols == "object") {
4958 for (var x in cols) {
4959 for (var i = 0, ii = columns.length; i < ii; i++) if (columns[i].axis == x) {
4960 columns[i].attr("href", cols[x]);
4964 !columns && createColumns();
4965 for (i = 0, ii = hrefs.length; i < ii; i++) {
4966 columns[i] && columns[i].attr("href", hrefs[i]);
4970 chart.hover = function (fin, fout) {
4971 !dots && createDots();
4972 dots.mouseover(fin).mouseout(fout);
4975 chart.click = function (f) {
4976 !dots && createDots();
4980 chart.each = function (f) {
4984 chart.eachColumn = function (f) {
4991 * g.Raphael 0.4.1 - Charting library, based on Raphaƫl
4993 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
4994 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
4996 Raphael.fn.g.piechart = function (cx, cy, r, values, opts) {
5000 covers = this.set(),
5002 series = this.set(),
5004 len = values.length,
5012 for (var i = 0; i < len; i++)
5015 var single_index = -1;
5016 for (var i = 0; i < len; i++)
5017 if (sum == values[i]) {
5022 if (len == 1 || single == true) {
5023 for(var i = 0; i < len; i++) {
5025 if (i == single_index) {
5028 series.push(this.circle(cx, cy, radius).attr({fill: opts.colors && opts.colors[i] || this.g.colors[i], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth}));
5029 covers.push(this.circle(cx, cy, radius).attr({href: opts.href ? opts.href[i] : null}).attr(this.g.shim));
5030 values[i] = {value: values[i], order: i, valueOf: function () { return this.value; }};
5031 series[i].middle = {x: cx, y: cy};
5032 series[i].mangle = 180;
5034 total = values[single_index];
5036 function sector(cx, cy, r, startAngle, endAngle, fill) {
5037 var rad = Math.PI / 180,
5038 x1 = cx + r * Math.cos(-startAngle * rad),
5039 x2 = cx + r * Math.cos(-endAngle * rad),
5040 xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
5041 y1 = cy + r * Math.sin(-startAngle * rad),
5042 y2 = cy + r * Math.sin(-endAngle * rad),
5043 ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
5044 res = ["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2, "z"];
5045 res.middle = {x: xm, y: ym};
5048 for (var i = 0; i < len; i++) {
5050 values[i] = {value: values[i], order: i, valueOf: function () { return this.value; }};
5052 values.sort(function (a, b) {
5053 return b.value - a.value;
5055 for (i = 0; i < len; i++) {
5056 if (defcut && values[i] * 360 / total <= 1.5) {
5062 values[cut].value += values[i];
5063 values[cut].others = true;
5064 others = values[cut].value;
5067 len = Math.min(cut + 1, values.length);
5068 others && values.splice(len) && (values[cut].others = true);
5069 for (i = 0; i < len; i++) {
5070 var valueOrder = values[i].order;
5071 var mangle = angle - 360 * values[i] / total / 2;
5073 angle = 90 - mangle;
5074 mangle = angle - 360 * values[i] / total / 2;
5077 var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
5079 var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
5080 var p = this.path(opts.init ? ipath : path).attr({fill: opts.colors && opts.colors[valueOrder] || this.g.colors[valueOrder] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round"});
5081 p.value = values[i];
5082 p.middle = path.middle;
5086 opts.init && p.animate({path: path.join(",")}, (+opts.init - 1) || 1000, ">");
5088 for (i = 0; i < len; i++) {
5089 p = paper.path(sectors[i].attr("path")).attr(this.g.shim);
5090 var valueOrder = values[i].order;
5091 opts.href && opts.href[valueOrder] && p.attr({href: opts.href[valueOrder]});
5092 //p.attr = function () {}; // this breaks translate!
5097 chart.hover = function (fin, fout) {
5098 fout = fout || function () {};
5100 for (var i = 0; i < len; i++) {
5101 (function (sector, cover, j) {
5107 mx: sector.middle.x,
5108 my: sector.middle.y,
5109 mangle: sector.mangle,
5113 label: that.labels && that.labels[j]
5115 cover.mouseover(function () {
5117 }).mouseout(function () {
5120 })(series[i], covers[i], i);
5124 // x: where label could be put
5125 // y: where label could be put
5126 // value: value to show
5127 // total: total number to count %
5128 chart.each = function (f) {
5130 for (var i = 0; i < len; i++) {
5131 (function (sector, cover, j) {
5139 mangle: sector.mangle,
5143 label: that.labels && that.labels[j]
5146 })(series[i], covers[i], i);
5150 chart.click = function (f) {
5152 for (var i = 0; i < len; i++) {
5153 (function (sector, cover, j) {
5159 mx: sector.middle.x,
5160 my: sector.middle.y,
5161 mangle: sector.mangle,
5165 label: that.labels && that.labels[j]
5167 cover.click(function () { f.call(o); });
5168 })(series[i], covers[i], i);
5172 chart.inject = function (element) {
5173 element.insertBefore(covers[0]);
5175 var legend = function (labels, otherslabel, mark, dir) {
5176 var x = cx + r + r / 5,
5179 labels = labels || [];
5180 dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
5181 mark = paper.g.markers[mark && mark.toLowerCase()] || "disc";
5182 chart.labels = paper.set();
5183 for (var i = 0; i < len; i++) {
5184 var clr = series[i].attr("fill"),
5185 j = values[i].order,
5187 values[i].others && (labels[j] = otherslabel || "Others");
5188 labels[j] = paper.g.labelise(labels[j], values[i], total);
5189 chart.labels.push(paper.set());
5190 chart.labels[i].push(paper.g[mark](x + 5, h, 5).attr({fill: clr, stroke: "none"}));
5191 chart.labels[i].push(txt = paper.text(x + 20, h, labels[j] || values[j]).attr(paper.g.txtattr).attr({fill: opts.legendcolor || "#000", "text-anchor": "start"}));
5192 covers[i].label = chart.labels[i];
5193 h += txt.getBBox().height * 1.2;
5195 var bb = chart.labels.getBBox(),
5197 east: [0, -bb.height / 2],
5198 west: [-bb.width - 2 * r - 20, -bb.height / 2],
5199 north: [-r - bb.width / 2, -r - bb.height - 10],
5200 south: [-r - bb.width / 2, r + 10]
5202 chart.labels.translate.apply(chart.labels, tr);
5203 chart.push(chart.labels);
5206 legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
5208 chart.push(series, covers);
5209 chart.series = series;
5210 chart.covers = covers;
5212 var w = paper.width,
5214 bb = chart.getBBox(),
5215 tr = [(w - bb.width)/2 - bb.x, (h - bb.height)/2 - bb.y];
5218 chart.translate.apply(chart, tr);