2 * g.Raphael 0.51 - Charting library, based on Raphaƫl
4 * Copyright (c) 2009-2012 Dmitry Baranovskiy (http://g.raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 * Tooltips on Element prototype
15 * Puts the context Element in a 'popup' tooltip. Can also be used on sets.
19 - dir (string) location of Element relative to the tail: `'down'`, `'left'`, `'up'` [default], or `'right'`.
20 - size (number) amount of bevel/padding around the Element, as well as half the width and height of the tail [default: `5`]
21 - x (number) x coordinate of the popup's tail [default: Element's `x` or `cx`]
22 - y (number) y coordinate of the popup's tail [default: Element's `y` or `cy`]
24 = (object) path element of the popup
26 Raphael.el.popup = function (dir, size, x, y) {
27 var paper = this.paper || this[0].paper,
28 bb, xy, center, cw, ch;
35 case 'ellipse': center = true; break;
36 default: center = false;
39 dir = dir == null ? 'up' : dir;
43 x = typeof x == 'number' ? x : (center ? bb.x + bb.width / 2 : bb.x);
44 y = typeof y == 'number' ? y : (center ? bb.y + bb.height / 2 : bb.y);
45 cw = Math.max(bb.width / 2 - size, 0);
46 ch = Math.max(bb.height / 2 - size, 0);
48 this.translate(x - bb.x - (center ? bb.width / 2 : 0), y - bb.y - (center ? bb.height / 2 : 0));
54 'l', -size, -size, -cw, 0,
55 'a', size, size, 0, 0, 1, -size, -size,
57 'a', size, size, 0, 0, 1, size, -size,
58 'l', size * 2 + cw * 2, 0,
59 'a', size, size, 0, 0, 1, size, size,
61 'a', size, size, 0, 0, 1, -size, size,
67 'l', size, size, cw, 0,
68 'a', size, size, 0, 0, 1, size, size,
70 'a', size, size, 0, 0, 1, -size, size,
71 'l', -(size * 2 + cw * 2), 0,
72 'a', size, size, 0, 0, 1, -size, -size,
74 'a', size, size, 0, 0, 1, size, -size,
80 'l', -size, size, 0, ch,
81 'a', size, size, 0, 0, 1, -size, size,
83 'a', size, size, 0, 0, 1, -size, -size,
84 'l', 0, -(size * 2 + ch * 2),
85 'a', size, size, 0, 0, 1, size, -size,
87 'a', size, size, 0, 0, 1, size, size,
93 'l', size, -size, 0, -ch,
94 'a', size, size, 0, 0, 1, size, -size,
96 'a', size, size, 0, 0, 1, size, size,
97 'l', 0, size * 2 + ch * 2,
98 'a', size, size, 0, 0, 1, -size, size,
100 'a', size, size, 0, 0, 1, -size, -size,
107 up: { x: -!center * (bb.width / 2), y: -size * 2 - (center ? bb.height / 2 : bb.height) },
108 down: { x: -!center * (bb.width / 2), y: size * 2 + (center ? bb.height / 2 : bb.height) },
109 left: { x: -size * 2 - (center ? bb.width / 2 : bb.width), y: -!center * (bb.height / 2) },
110 right: { x: size * 2 + (center ? bb.width / 2 : bb.width), y: -!center * (bb.height / 2) }
113 this.translate(xy.x, xy.y);
114 return paper.path(paths[dir]).attr({ fill: "#000", stroke: "none" }).insertBefore(this.node ? this : this[0]);
121 * Puts the context Element in a 'tag' tooltip. Can also be used on sets.
125 - angle (number) angle of orientation in degrees [default: `0`]
126 - r (number) radius of the loop [default: `5`]
127 - x (number) x coordinate of the center of the tag loop [default: Element's `x` or `cx`]
128 - y (number) y coordinate of the center of the tag loop [default: Element's `x` or `cx`]
130 = (object) path element of the tag
132 Raphael.el.tag = function (angle, r, x, y) {
134 paper = this.paper || this[0].paper;
138 var p = paper.path().attr({ fill: '#000', stroke: '#000' }),
145 case 'ellipse': center = true; break;
146 default: center = false;
150 x = typeof x == 'number' ? x : (center ? bb.x + bb.width / 2 : bb.x);
151 y = typeof y == 'number' ? y : (center ? bb.y + bb.height / 2 : bb.y);
152 r = r == null ? 5 : r;
155 if (bb.height >= r * 2) {
159 "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2,
161 "a", r + d, r + d, 0, 1, 0, 0, (r + d) * 2,
162 "L", x + r + d, y + bb.height / 2 + d,
163 "l", bb.width + 2 * d, 0, 0, -bb.height - 2 * d, -bb.width - 2 * d, 0,
168 dx = Math.sqrt(Math.pow(r + d, 2) - Math.pow(bb.height / 2 + d, 2));
172 "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,
173 "M", x + dx, y - bb.height / 2 - d,
174 "a", r + d, r + d, 0, 1, 0, 0, bb.height + 2 * d,
175 "l", r + d - dx + bb.width + 2 * d, 0, 0, -bb.height - 2 * d,
176 "L", x + dx, y - bb.height / 2 - d
182 p.rotate(angle, x, y);
186 this.attr(this.attrs.x ? 'x' : 'cx', x + r + d + (!center ? this.type == 'text' ? bb.width : 0 : bb.width / 2)).attr('y', center ? y : y - bb.height / 2);
187 this.rotate(angle, x, y);
188 angle > 90 && angle < 270 && this.attr(this.attrs.x ? 'x' : 'cx', x - r - d - (!center ? bb.width : bb.width / 2)).rotate(180, x, y);
191 if (angle > 90 && angle < 270) {
192 this.translate(x - bb.x - bb.width - r - d, y - bb.y - bb.height / 2);
193 this.rotate(angle - 180, bb.x + bb.width + r + d, bb.y + bb.height / 2);
195 this.translate(x - bb.x + r + d, y - bb.y - bb.height / 2);
196 this.rotate(angle, bb.x - r - d, bb.y + bb.height / 2);
200 return p.insertBefore(this.node ? this : this[0]);
207 * Puts the context Element in a 'drop' tooltip. Can also be used on sets.
211 - angle (number) angle of orientation in degrees [default: `0`]
212 - x (number) x coordinate of the drop's point [default: Element's `x` or `cx`]
213 - y (number) y coordinate of the drop's point [default: Element's `x` or `cx`]
215 = (object) path element of the drop
217 Raphael.el.drop = function (angle, x, y) {
218 var bb = this.getBBox(),
219 paper = this.paper || this[0].paper,
220 center, size, p, dx, dy;
227 case 'ellipse': center = true; break;
228 default: center = false;
233 x = typeof x == 'number' ? x : (center ? bb.x + bb.width / 2 : bb.x);
234 y = typeof y == 'number' ? y : (center ? bb.y + bb.height / 2 : bb.y);
235 size = Math.max(bb.width, bb.height) + Math.min(bb.width, bb.height);
239 "A", size * .4, size * .4, 0, 1, 0, x + size * .7, y - size * .7,
241 ]).attr({fill: "#000", stroke: "none"}).rotate(22.5 - angle, x, y);
243 angle = (angle + 90) * Math.PI / 180;
244 dx = (x + size * Math.sin(angle)) - (center ? 0 : bb.width / 2);
245 dy = (y + size * Math.cos(angle)) - (center ? 0 : bb.height / 2);
248 this.attr(this.attrs.x ? 'x' : 'cx', dx).attr(this.attrs.y ? 'y' : 'cy', dy) :
249 this.translate(dx - bb.x, dy - bb.y);
251 return p.insertBefore(this.node ? this : this[0]);
258 * Puts the context Element in a 'flag' tooltip. Can also be used on sets.
262 - angle (number) angle of orientation in degrees [default: `0`]
263 - x (number) x coordinate of the flag's point [default: Element's `x` or `cx`]
264 - y (number) y coordinate of the flag's point [default: Element's `x` or `cx`]
266 = (object) path element of the flag
268 Raphael.el.flag = function (angle, x, y) {
270 paper = this.paper || this[0].paper;
274 var p = paper.path().attr({ fill: '#000', stroke: '#000' }),
282 case 'ellipse': center = true; break;
283 default: center = false;
287 x = typeof x == 'number' ? x : (center ? bb.x + bb.width / 2 : bb.x);
288 y = typeof y == 'number' ? y : (center ? bb.y + bb.height / 2: bb.y);
293 "l", h + d, -h - d, bb.width + 2 * d, 0, 0, bb.height + 2 * d, -bb.width - 2 * d, 0,
299 p.rotate(angle, x, y);
303 this.attr(this.attrs.x ? 'x' : 'cx', x + h + d + (!center ? this.type == 'text' ? bb.width : 0 : bb.width / 2)).attr('y', center ? y : y - bb.height / 2);
304 this.rotate(angle, x, y);
305 angle > 90 && angle < 270 && this.attr(this.attrs.x ? 'x' : 'cx', x - h - d - (!center ? bb.width : bb.width / 2)).rotate(180, x, y);
308 if (angle > 90 && angle < 270) {
309 this.translate(x - bb.x - bb.width - h - d, y - bb.y - bb.height / 2);
310 this.rotate(angle - 180, bb.x + bb.width + h + d, bb.y + bb.height / 2);
312 this.translate(x - bb.x + h + d, y - bb.y - bb.height / 2);
313 this.rotate(angle, bb.x - h - d, bb.y + bb.height / 2);
317 return p.insertBefore(this.node ? this : this[0]);
324 * Puts the context Element in a 'label' tooltip. Can also be used on sets.
326 = (object) path element of the label.
328 Raphael.el.label = function () {
329 var bb = this.getBBox(),
330 paper = this.paper || this[0].paper,
331 r = Math.min(20, bb.width + 10, bb.height + 10) / 2;
335 return paper.rect(bb.x - r / 2, bb.y - r / 2, bb.width + r, bb.height + r, r).attr({ stroke: 'none', fill: '#000' }).insertBefore(this.node ? this : this[0]);
342 * Puts the context Element in a 'blob' tooltip. Can also be used on sets.
346 - angle (number) angle of orientation in degrees [default: `0`]
347 - x (number) x coordinate of the blob's tail [default: Element's `x` or `cx`]
348 - y (number) y coordinate of the blob's tail [default: Element's `x` or `cx`]
350 = (object) path element of the blob
352 Raphael.el.blob = function (angle, x, y) {
353 var bb = this.getBBox(),
355 paper = this.paper || this[0].paper,
363 case 'ellipse': center = true; break;
364 default: center = false;
367 p = paper.path().attr({ fill: "#000", stroke: "none" });
368 angle = (+angle + 1 ? angle : 45) + 90;
369 size = Math.min(bb.height, bb.width);
370 x = typeof x == 'number' ? x : (center ? bb.x + bb.width / 2 : bb.x);
371 y = typeof y == 'number' ? y : (center ? bb.y + bb.height / 2 : bb.y);
373 var w = Math.max(bb.width + size, size * 25 / 12),
374 h = Math.max(bb.height + size, size * 25 / 12),
375 x2 = x + size * Math.sin((angle - 22.5) * rad),
376 y2 = y + size * Math.cos((angle - 22.5) * rad),
377 x1 = x + size * Math.sin((angle + 22.5) * rad),
378 y1 = y + size * Math.cos((angle + 22.5) * rad),
383 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)),
384 cx = k * rx * dy / ry + (x1 + x2) / 2,
385 cy = k * -ry * dx / rx + (y1 + y2) / 2;
393 "A", rx, ry, 0, 1, 1, x2, y2,
398 this.translate(cx - bb.x - bb.width / 2, cy - bb.y - bb.height / 2);
400 return p.insertBefore(this.node ? this : this[0]);
404 * Tooltips on Paper prototype
410 * Puts the given `text` into a 'label' tooltip. The text is given a default style according to @g.txtattr. See @Element.label
414 - x (number) x coordinate of the center of the label
415 - y (number) y coordinate of the center of the label
416 - text (string) text to place inside the label
418 = (object) set containing the label path and the text element
420 | paper.label(50, 50, "$9.99");
422 Raphael.fn.label = function (x, y, text) {
423 var set = this.set();
425 text = this.text(x, y, text).attr(Raphael.g.txtattr);
426 return set.push(text.label(), text);
433 * Puts the given `text` into a 'popup' tooltip. The text is given a default style according to @g.txtattr. See @Element.popup
435 * Note: The `dir` parameter has changed from g.Raphael 0.4.1 to 0.5. The options `0`, `1`, `2`, and `3` has been changed to `'down'`, `'left'`, `'up'`, and `'right'` respectively.
439 - x (number) x coordinate of the popup's tail
440 - y (number) y coordinate of the popup's tail
441 - text (string) text to place inside the popup
442 - dir (string) location of the text relative to the tail: `'down'`, `'left'`, `'up'` [default], or `'right'`.
443 - size (number) amount of padding around the Element [default: `5`]
445 = (object) set containing the popup path and the text element
447 | paper.popup(50, 50, "$9.99", 'down');
449 Raphael.fn.popup = function (x, y, text, dir, size) {
450 var set = this.set();
452 text = this.text(x, y, text).attr(Raphael.g.txtattr);
453 return set.push(text.popup(dir, size), text);
460 * Puts the given text into a 'tag' tooltip. The text is given a default style according to @g.txtattr. See @Element.tag
464 - x (number) x coordinate of the center of the tag loop
465 - y (number) y coordinate of the center of the tag loop
466 - text (string) text to place inside the tag
467 - angle (number) angle of orientation in degrees [default: `0`]
468 - r (number) radius of the loop [default: `5`]
470 = (object) set containing the tag path and the text element
472 | paper.tag(50, 50, "$9.99", 60);
474 Raphael.fn.tag = function (x, y, text, angle, r) {
475 var set = this.set();
477 text = this.text(x, y, text).attr(Raphael.g.txtattr);
478 return set.push(text.tag(angle, r), text);
485 * Puts the given `text` into a 'flag' tooltip. The text is given a default style according to @g.txtattr. See @Element.flag
489 - x (number) x coordinate of the flag's point
490 - y (number) y coordinate of the flag's point
491 - text (string) text to place inside the flag
492 - angle (number) angle of orientation in degrees [default: `0`]
494 = (object) set containing the flag path and the text element
496 | paper.flag(50, 50, "$9.99", 60);
498 Raphael.fn.flag = function (x, y, text, angle) {
499 var set = this.set();
501 text = this.text(x, y, text).attr(Raphael.g.txtattr);
502 return set.push(text.flag(angle), text);
509 * Puts the given text into a 'drop' tooltip. The text is given a default style according to @g.txtattr. See @Element.drop
513 - x (number) x coordinate of the drop's point
514 - y (number) y coordinate of the drop's point
515 - text (string) text to place inside the drop
516 - angle (number) angle of orientation in degrees [default: `0`]
518 = (object) set containing the drop path and the text element
520 | paper.drop(50, 50, "$9.99", 60);
522 Raphael.fn.drop = function (x, y, text, angle) {
523 var set = this.set();
525 text = this.text(x, y, text).attr(Raphael.g.txtattr);
526 return set.push(text.drop(angle), text);
533 * Puts the given text into a 'blob' tooltip. The text is given a default style according to @g.txtattr. See @Element.blob
537 - x (number) x coordinate of the blob's tail
538 - y (number) y coordinate of the blob's tail
539 - text (string) text to place inside the blob
540 - angle (number) angle of orientation in degrees [default: `0`]
542 = (object) set containing the blob path and the text element
544 | paper.blob(50, 50, "$9.99", 60);
546 Raphael.fn.blob = function (x, y, text, angle) {
547 var set = this.set();
549 text = this.text(x, y, text).attr(Raphael.g.txtattr);
550 return set.push(text.blob(angle), text);
554 * Brightness functions on the Element prototype
560 * Makes the context element lighter by increasing the brightness and reducing the saturation by a given factor. Can be called on Sets.
564 - times (number) adjustment factor [default: `2`]
568 | paper.circle(50, 50, 20).attr({
574 Raphael.el.lighter = function (times) {
577 var fs = [this.attrs.fill, this.attrs.stroke];
579 this.fs = this.fs || [fs[0], fs[1]];
581 fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
582 fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
583 fs[0].b = Math.min(fs[0].b * times, 1);
584 fs[0].s = fs[0].s / times;
585 fs[1].b = Math.min(fs[1].b * times, 1);
586 fs[1].s = fs[1].s / times;
588 this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
596 * Makes the context element darker by decreasing the brightness and increasing the saturation by a given factor. Can be called on Sets.
600 - times (number) adjustment factor [default: `2`]
604 | paper.circle(50, 50, 20).attr({
610 Raphael.el.darker = function (times) {
613 var fs = [this.attrs.fill, this.attrs.stroke];
615 this.fs = this.fs || [fs[0], fs[1]];
617 fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex);
618 fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex);
619 fs[0].s = Math.min(fs[0].s * times, 1);
620 fs[0].b = fs[0].b / times;
621 fs[1].s = Math.min(fs[1].s * times, 1);
622 fs[1].b = fs[1].b / times;
624 this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"});
629 * Element.resetBrightness
632 * Resets brightness and saturation levels to their original values. See @Element.lighter and @Element.darker. Can be called on Sets.
636 | paper.circle(50, 50, 20).attr({
640 | }).lighter(6).resetBrightness();
642 Raphael.el.resetBrightness = function () {
644 this.attr({ fill: this.fs[0], stroke: this.fs[1] });
650 //alias to set prototype
652 var brightness = ['lighter', 'darker', 'resetBrightness'],
653 tooltips = ['popup', 'tag', 'flag', 'label', 'drop', 'blob'];
655 for (var f in tooltips) (function (name) {
656 Raphael.st[name] = function () {
657 return Raphael.el[name].apply(this, arguments);
661 for (var f in brightness) (function (name) {
662 Raphael.st[name] = function () {
663 for (var i = 0; i < this.length; i++) {
664 this[i][name].apply(this[i], arguments);
672 //chart prototype for storing common functions
678 * An attribute object that charts will set on all generated shims (shims being the invisible objects that mouse events are bound to)
681 | { stroke: 'none', fill: '#000', 'fill-opacity': 0 }
683 shim: { stroke: 'none', fill: '#000', 'fill-opacity': 0 },
689 * An attribute object that charts and tooltips will set on any generated text
692 | { font: '12px Arial, sans-serif', fill: '#fff' }
694 txtattr: { font: '12px Arial, sans-serif', fill: '#fff' },
700 * An array of color values that charts will iterate through when drawing chart data values.
703 colors: (function () {
704 var hues = [.6, .2, .05, .1333, .75, 0],
707 for (var i = 0; i < 10; i++) {
708 if (i < hues.length) {
709 colors.push('hsb(' + hues[i] + ',.75, .75)');
711 colors.push('hsb(' + hues[i - hues.length] + ', 1, .5)');
718 snapEnds: function(from, to, steps) {
723 return {from: f, to: t, power: 0};
727 return Math.abs(a - .5) < .25 ? ~~(a) + .5 : Math.round(a);
730 var d = (t - f) / steps,
738 R = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
743 if(d == 0 || !isFinite(d)) {
748 r = ~~(d * Math.pow(10, i)) / Math.pow(10, i);
756 t = round(to * Math.pow(10, i)) / Math.pow(10, i);
759 t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i);
762 f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i);
763 return { from: f, to: t, power: i };
766 * @param x = starting postion.
767 * @param orentation //0=top 1=right 2=bottom 3=left
770 axis: function (x, y, length, from, to, steps, orientation, labels, type, dashsize, paper)
775 if (Raphael.is(x,"object")) {
779 length = opts.length;
783 orientation = opts.orientation;
784 labels = opts.labels,
786 dashsize = opts.dashsize;
789 paper = arguments[arguments.length-1] //paper is always last argument
791 opts.loffset = (opts.loffset || 0);
792 opts.roffset = (opts.roffset || 0);
794 dashsize = dashsize == null ? 2 : dashsize;
799 var path = type == "|" || type == " " ? ["M", x + .5, y, "l", 0, .001] : // this is the main bar line.
800 orientation == 1 || orientation == 3 ?
801 ["M", x + .5, y, "l", 0, -length] :
802 ["M", x, y + .5, "l", length, 0],
803 ends = this.snapEnds(from, to, steps),
808 txtattr = { font: "11px 'Fontin Sans', Fontin-Sans, sans-serif" },
820 if (+orientation == 1 || +orientation == 3) {
822 addon = (orientation - 1 ? 1 : -1) * (dashsize + 3 + !!(orientation - 1));
824 while (Y >= y - length) {
825 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), Y + .5, "l", dashsize * 2 + 1, 0]));
826 text.push(paper.text(x + addon, Y, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(txtattr).attr({ "text-anchor": orientation - 1 ? "start" : "end" }));
831 if (Math.round(Y + dx - (y - length))) {
832 type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), y - length + .5, "l", dashsize * 2 + 1, 0]));
833 text.push(paper.text(x + addon, y - length, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(txtattr).attr({ "text-anchor": orientation - 1 ? "start" : "end" }));
841 addon = (orientation ? -1 : 1) * (dashsize + 9 + !orientation);
843 var X = x + opts.loffset,
844 dx = (length - (opts.loffset + opts.roffset)) / steps,
852 while (X <= x + length) {
853 Roo.log('label ' + j + ":" + labels[j]);
855 if (typeof(labels[j]) == 'undefined') {
861 type != "-" && type != " " && (path = path.concat(["M", X + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
863 text.push(txt = paper.text(X, y + addon, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(txtattr));
865 var bb = txt.getBBox();
867 if (prev >= bb.x - 5) {
868 text.pop(text.length - 1).remove();
870 prev = bb.x + bb.width;
877 if (Math.round(X - dx - x - length) && typeof(labels[j]) != 'undefined') {
879 type != "-" && type != " " && (path = path.concat(["M", x + length + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1]));
880 text.push(paper.text(x + length, y + addon, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(txtattr));
884 var res = paper.path(path);
887 res.all = paper.set([res, text]);
888 res.remove = function () {
890 this.constructor.prototype.remove.call(this);
896 labelise: function(label, val, total) {
898 return (label + "").replace(/(##+(?:\.#+)?)|(%%+(?:\.%+)?)/g, function (all, value, percent) {
900 return (+val).toFixed(value.replace(/^#+\.?/g, "").length);
903 return (val * 100 / total).toFixed(percent.replace(/^%+\.?/g, "").length) + "%";
907 return (+val).toFixed(0);