/* * g.Raphael 0.4 - Charting library, based on Raphaƫl * * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. */ (function () { Raphael.fn.g = Raphael.fn.g || {}; Raphael.fn.g.markers = { disc: "disc", o: "disc", flower: "flower", f: "flower", diamond: "diamond", d: "diamond", square: "square", s: "square", triangle: "triangle", t: "triangle", star: "star", "*": "star", cross: "cross", x: "cross", plus: "plus", "+": "plus", arrow: "arrow", "->": "arrow" }; Raphael.fn.g.shim = {stroke: "none", fill: "#000", "fill-opacity": 0}; Raphael.fn.g.txtattr = {font: "12px Arial, sans-serif"}; Raphael.fn.g.colors = []; var hues = [.6, .2, .05, .1333, .75, 0]; for (var i = 0; i < 10; i++) { if (i < hues.length) { Raphael.fn.g.colors.push("hsb(" + hues[i] + ", .75, .75)"); } else { Raphael.fn.g.colors.push("hsb(" + hues[i - hues.length] + ", 1, .5)"); } } Raphael.fn.g.text = function (x, y, text) { return this.text(x, y, text).attr(this.g.txtattr); }; Raphael.fn.g.labelise = function (label, val, total) { if (label) { return (label + "").replace(/(##+(?:\.#+)?)|(%%+(?:\.%+)?)/g, function (all, value, percent) { if (value) { return (+val).toFixed(value.replace(/^#+\.?/g, "").length); } if (percent) { return (val * 100 / total).toFixed(percent.replace(/^%+\.?/g, "").length) + "%"; } }); } else { return (+val).toFixed(0); } }; Raphael.fn.g.finger = function (x, y, width, height, dir, ending, isPath) { // dir 0 for horisontal and 1 for vertical if ((dir && !height) || (!dir && !width)) { return isPath ? "" : this.path(); } ending = {square: "square", sharp: "sharp", soft: "soft"}[ending] || "round"; var path; height = Math.round(height); width = Math.round(width); x = Math.round(x); y = Math.round(y); switch (ending) { case "round": if (!dir) { var r = Math.floor(height / 2); if (width < r) { r = width; path = ["M", x + .5, y + .5 - Math.floor(height / 2), "l", 0, 0, "a", r, Math.floor(height / 2), 0, 0, 1, 0, height, "l", 0, 0, "z"]; } else { path = ["M", x + .5, y + .5 - r, "l", width - r, 0, "a", r, r, 0, 1, 1, 0, height, "l", r - width, 0, "z"]; } } else { var r = Math.floor(width / 2); if (height < r) { r = height; path = ["M", x - Math.floor(width / 2), y, "l", 0, 0, "a", Math.floor(width / 2), r, 0, 0, 1, width, 0, "l", 0, 0, "z"]; } else { path = ["M", x - r, y, "l", 0, r - height, "a", r, r, 0, 1, 1, width, 0, "l", 0, height - r, "z"]; } } break; case "sharp": if (!dir) { var half = Math.floor(height / 2); path = ["M", x, y + half, "l", 0, -height, Math.max(width - half, 0), 0, Math.min(half, width), half, -Math.min(half, width), half + (half * 2 < height), "z"]; } else { var half = Math.floor(width / 2); path = ["M", x + half, y, "l", -width, 0, 0, -Math.max(height - half, 0), half, -Math.min(half, height), half, Math.min(half, height), half, "z"]; } break; case "square": if (!dir) { path = ["M", x, y + Math.floor(height / 2), "l", 0, -height, width, 0, 0, height, "z"]; } else { path = ["M", x + Math.floor(width / 2), y, "l", 1 - width, 0, 0, -height, width - 1, 0, "z"]; } break; case "soft": var r; if (!dir) { r = Math.min(width, Math.round(height / 5)); path = ["M", x + .5, y + .5 - Math.floor(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"]; } else { r = Math.min(Math.round(width / 5), height); path = ["M", x - Math.floor(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"]; } } if (isPath) { return path.join(","); } else { return this.path(path); } }; // Symbols Raphael.fn.g.disc = function (cx, cy, r) { return this.circle(cx, cy, r); }; Raphael.fn.g.line = function (cx, cy, r) { return this.rect(cx - r, cy - r / 5, 2 * r, 2 * r / 5); }; Raphael.fn.g.square = function (cx, cy, r) { r = r * .7; return this.rect(cx - r, cy - r, 2 * r, 2 * r); }; Raphael.fn.g.triangle = function (cx, cy, r) { r *= 1.75; return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z")); }; Raphael.fn.g.diamond = function (cx, cy, r) { return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]); }; Raphael.fn.g.flower = function (cx, cy, r, n) { r = r * 1.25; var rout = r, rin = rout * .5; n = +n < 3 || !n ? 5 : n; var points = ["M", cx, cy + rin, "Q"], R; for (var i = 1; i < n * 2 + 1; i++) { R = i % 2 ? rout : rin; points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]); } points.push("z"); return this.path(points.join(",")); }; Raphael.fn.g.star = function (cx, cy, r, r2) { r2 = r2 || r * .5; var points = ["M", cx, cy + r2, "L"], R; for (var i = 1; i < 10; i++) { R = i % 2 ? r : r2; points = points.concat([(cx + R * Math.sin(i * Math.PI * .2)).toFixed(3), (cy + R * Math.cos(i * Math.PI * .2)).toFixed(3)]); } points.push("z"); return this.path(points.join(",")); }; Raphael.fn.g.cross = function (cx, cy, r) { r = r / 2.5; 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"])); }; Raphael.fn.g.plus = function (cx, cy, r) { r = r / 2; 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"])); }; Raphael.fn.g.arrow = function (cx, cy, r) { 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")); }; // Tooltips Raphael.fn.g.tag = function (x, y, text, angle, r) { angle = angle || 0; r = r == null ? 5 : r; text = text == null ? "$9.99" : text; var R = .5522 * r, res = this.set(), d = 3; res.push(this.path().attr({fill: "#000", stroke: "none"})); res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); res.update = function () { this.rotate(0, x, y); var bb = this[1].getBBox(); if (bb.height >= r * 2) { 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(",")}); } else { var dx = Math.sqrt(Math.pow(r + d, 2) - Math.pow(bb.height / 2 + d, 2)); // ["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] // "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2, 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(",")}); } this[1].attr({x: x + r + d + bb.width / 2, y: y}); angle = (360 - angle) % 360; this.rotate(angle, x, y); angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]}); return this; }; res.update(); return res; }; Raphael.fn.g.popupit = function (x, y, set, dir, size) { dir = dir == null ? 2 : dir; size = size || 5; x = Math.round(x) + .5; y = Math.round(y) + .5; var bb = set.getBBox(), w = Math.round(bb.width / 2), h = Math.round(bb.height / 2), dx = [0, w + size * 2, 0, -w - size * 2], dy = [-h * 2 - size * 3, -h - size, 0, -h - size], p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size, "l", 0, -Math.max(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -Math.max(h - size, 0), "a", size, size, 0, 0, 1, size, -size, "l", Math.max(w - size, 0), 0, size, !dir * -size, size, !dir * size, Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size, "l", 0, Math.max(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, Math.max(h - size, 0), "a", size, size, 0, 0, 1, -size, size, "l", -Math.max(w - size, 0), 0, "z"].join(","), 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]; set.translate(xy.x - w - bb.x, xy.y - h - bb.y); return this.path(p).attr({fill: "#000", stroke: "none"}).insertBefore(set.node ? set : set[0]); }; Raphael.fn.g.popup = function (x, y, text, dir, size) { dir = dir == null ? 2 : dir; size = size || 5; text = text || "$9.99"; var res = this.set(), d = 3; res.push(this.path().attr({fill: "#000", stroke: "none"})); res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); res.update = function (X, Y, withAnimation) { X = X || x; Y = Y || y; var bb = this[1].getBBox(), w = bb.width / 2, h = bb.height / 2, dx = [0, w + size * 2, 0, -w - size * 2], dy = [-h * 2 - size * 3, -h - size, 0, -h - size], p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size, "l", 0, -Math.max(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -Math.max(h - size, 0), "a", size, size, 0, 0, 1, size, -size, "l", Math.max(w - size, 0), 0, size, !dir * -size, size, !dir * size, Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size, "l", 0, Math.max(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, Math.max(h - size, 0), "a", size, size, 0, 0, 1, -size, size, "l", -Math.max(w - size, 0), 0, "z"].join(","), 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]; if (withAnimation) { this[0].animate({path: p}, 500, ">"); this[1].animate(xy, 500, ">"); } else { this[0].attr({path: p}); this[1].attr(xy); } return this; }; return res.update(x, y); }; Raphael.fn.g.flag = function (x, y, text, angle) { angle = angle || 0; text = text || "$9.99"; var res = this.set(), d = 3; res.push(this.path().attr({fill: "#000", stroke: "none"})); res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); res.update = function (x, y) { this.rotate(0, x, y); var bb = this[1].getBBox(), h = bb.height / 2; 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(",")}); this[1].attr({x: x + h + d + bb.width / 2, y: y}); angle = 360 - angle; this.rotate(angle, x, y); angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]}); return this; }; return res.update(x, y); }; Raphael.fn.g.label = function (x, y, text) { var res = this.set(); res.push(this.rect(x, y, 10, 10).attr({stroke: "none", fill: "#000"})); res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); res.update = function () { var bb = this[1].getBBox(), r = Math.min(bb.width + 10, bb.height + 10) / 2; this[0].attr({x: bb.x - r / 2, y: bb.y - r / 2, width: bb.width + r, height: bb.height + r, r: r}); }; res.update(); return res; }; Raphael.fn.g.labelit = function (set) { var bb = set.getBBox(), r = Math.min(20, bb.width + 10, bb.height + 10) / 2; 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[0]); }; Raphael.fn.g.drop = function (x, y, text, size, angle) { size = size || 30; angle = angle || 0; var res = this.set(); 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]})); angle = (angle + 90) * Math.PI / 180; 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"})); res.drop = res[0]; res.text = res[1]; return res; }; Raphael.fn.g.blob = function (x, y, text, angle, size) { angle = (+angle + 1 ? angle : 45) + 90; size = size || 12; var rad = Math.PI / 180, fontSize = size * 12 / 12; var res = this.set(); res.push(this.path().attr({fill: "#000", stroke: "none"})); 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"})); res.update = function (X, Y, withAnimation) { X = X || x; Y = Y || y; var bb = this[1].getBBox(), w = Math.max(bb.width + fontSize, size * 25 / 12), h = Math.max(bb.height + fontSize, size * 25 / 12), x2 = X + size * Math.sin((angle - 22.5) * rad), y2 = Y + size * Math.cos((angle - 22.5) * rad), x1 = X + size * Math.sin((angle + 22.5) * rad), y1 = Y + size * Math.cos((angle + 22.5) * rad), dx = (x1 - x2) / 2, dy = (y1 - y2) / 2, rx = w / 2, ry = h / 2, 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)), cx = k * rx * dy / ry + (x1 + x2) / 2, cy = k * -ry * dx / rx + (y1 + y2) / 2; if (withAnimation) { this.animate({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}, 500, ">"); } else { this.attr({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}); } return this; }; res.update(x, y); return res; }; Raphael.fn.g.colorValue = function (value, total, s, b) { return "hsb(" + [Math.min((1 - value / total) * .4, 1), s || .75, b || .75] + ")"; }; Raphael.fn.g.snapEnds = function (from, to, steps) { var f = from, t = to; if (f == t) { return {from: f, to: t, power: 0}; } function round(a) { return Math.abs(a - .5) < .25 ? Math.floor(a) + .5 : Math.round(a); } var d = (t - f) / steps, r = Math.floor(d), R = r, i = 0; if (r) { while (R) { i--; R = Math.floor(d * Math.pow(10, i)) / Math.pow(10, i); } i ++; } else { while (!r) { i = i || 1; r = Math.floor(d * Math.pow(10, i)) / Math.pow(10, i); i++; } i && i--; } var t = round(to * Math.pow(10, i)) / Math.pow(10, i); if (t < to) { t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i); } var f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i); return {from: f, to: t, power: i}; }; Raphael.fn.g.axis = function (x, y, length, from, to, steps, orientation, labels, type, dashsize) { dashsize = dashsize == null ? 2 : dashsize; type = type || "t"; steps = steps || 10; 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], ends = this.g.snapEnds(from, to, steps), f = ends.from, t = ends.to, i = ends.power, j = 0, text = this.set(); d = (t - f) / steps; var label = f, rnd = i > 0 ? i : 0; dx = length / steps; if (+orientation == 1 || +orientation == 3) { var Y = y, addon = (orientation - 1 ? 1 : -1) * (dashsize + 3 + !!(orientation - 1)); while (Y >= y - length) { type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), Y + .5, "l", dashsize * 2 + 1, 0])); 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"})); label += d; Y -= dx; } if (Math.round(Y + dx - (y - length))) { type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), y - length + .5, "l", dashsize * 2 + 1, 0])); 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"})); } } else { var X = x, label = f, rnd = i > 0 ? i : 0, addon = (orientation ? -1 : 1) * (dashsize + 9 + !orientation), dx = length / steps, txt = 0, prev = 0; while (X <= x + length) { type != "-" && type != " " && (path = path.concat(["M", X + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1])); text.push(txt = this.text(X, y + addon, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr)); var bb = txt.getBBox(); if (prev >= bb.x - 5) { text.pop(text.length - 1).remove(); } else { prev = bb.x + bb.width; } label += d; X += dx; } if (Math.round(X - dx - x - length)) { type != "-" && type != " " && (path = path.concat(["M", x + length + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1])); text.push(this.text(x + length, y + addon, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr)); } } var res = this.path(path); res.text = text; res.all = this.set([res, text]); res.remove = function () { this.text.remove(); this.constructor.prototype.remove.call(this); }; return res; }; Raphael.el.lighter = function (times) { times = times || 2; var fs = [this.attrs.fill, this.attrs.stroke]; this.fs = this.fs || [fs[0], fs[1]]; fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex); fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex); fs[0].b = Math.min(fs[0].b * times, 1); fs[0].s = fs[0].s / times; fs[1].b = Math.min(fs[1].b * times, 1); fs[1].s = fs[1].s / times; this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"}); }; Raphael.el.darker = function (times) { times = times || 2; var fs = [this.attrs.fill, this.attrs.stroke]; this.fs = this.fs || [fs[0], fs[1]]; fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex); fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex); fs[0].s = Math.min(fs[0].s * times, 1); fs[0].b = fs[0].b / times; fs[1].s = Math.min(fs[1].s * times, 1); fs[1].b = fs[1].b / times; this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"}); }; Raphael.el.original = function () { if (this.fs) { this.attr({fill: this.fs[0], stroke: this.fs[1]}); delete this.fs; } }; })();