2 * g.Raphael 0.5 - Charting library, based on Raphaƫl
4 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
11 function finger(x, y, width, height, dir, ending, isPath, paper) {
13 ends = { round: 'round', sharp: 'sharp', soft: 'soft', square: 'square' };
15 // dir 0 for horizontal and 1 for vertical
16 if ((dir && !height) || (!dir && !width)) {
17 return isPath ? "" : paper.path();
20 ending = ends[ending] || "square";
21 height = Math.round(height);
22 width = Math.round(width);
29 var r = ~~(height / 2);
34 "M", x + .5, y + .5 - ~~(height / 2),
36 "a", r, ~~(height / 2), 0, 0, 1, 0, height,
42 "M", x + .5, y + .5 - r,
44 "a", r, r, 0, 1, 1, 0, height,
55 "M", x - ~~(width / 2), y,
57 "a", ~~(width / 2), r, 0, 0, 1, width, 0,
65 "a", r, r, 0, 1, 1, width, 0,
74 var half = ~~(height / 2);
78 "l", 0, -height, mmax(width - half, 0), 0, mmin(half, width), half, -mmin(half, width), half + (half * 2 < height),
85 "l", -width, 0, 0, -mmax(height - half, 0), half, -mmin(half, height), half, mmin(half, height), half,
93 "M", x, y + ~~(height / 2),
94 "l", 0, -height, width, 0, 0, height,
99 "M", x + ~~(width / 2), y,
100 "l", 1 - width, 0, 0, -height, width - 1, 0,
107 r = mmin(width, Math.round(height / 5));
109 "M", x + .5, y + .5 - ~~(height / 2),
111 "a", r, r, 0, 0, 1, r, r,
112 "l", 0, height - r * 2,
113 "a", r, r, 0, 0, 1, -r, r,
118 r = mmin(Math.round(width / 5), height);
120 "M", x - ~~(width / 2), y,
122 "a", r, r, 0, 0, 1, r, -r,
123 "l", width - 2 * r, 0,
124 "a", r, r, 0, 0, 1, r, r,
132 return path.join(",");
134 return paper.path(path);
140 * @param {Raphael} paper The paper to render the graph on
141 * @param {int} x left coord
142 * @param {int} y right coord
143 * @param {int} width width of graph
144 * @param {int} height heigt of graph
145 * @param {Array} values - single = [1,2,3] or multiple [ [1,2,3] , [1,2,3]]
146 * @param {Object} opts - options
147 * type : {String} square | ???
148 * gutter: {Number} percentage width default 20%
149 * vgutter : {Number} vertical gutter?
150 * colors : {Array} list of colours - default chartinst.colors
151 * xvalues : {Array} not sure - this looks like can be single or multi dimensional array
152 * yvalues : {Array} not sure - this looks like can be single or multi dimensional array
154 * axis: {String} eg. "top right bottom left" - which sides to show an axis eg. "0 0 1 1"
157 * axisxlabels: {Array} - note - first element appears left of first bar..
161 * labels : {Array} labels to go above the chart..
164 function VBarchart(paper, x, y, width, height, values, opts) {
168 var chartinst = this,
169 type = opts.type || "square",
170 gutter = parseFloat(opts.gutter || "20%"),
173 // covers = paper.set(),
174 // covers2 = paper.set(),
175 total = Math.max.apply(Math, values),
178 colors = opts.colors || chartinst.colors,
179 len = values.length + 2,
182 opts.xvalues = opts.xvalues|| [];
183 if (!paper.raphael.is(opts.xvalues[0], "array")) {
184 opts.xvalues = [opts.xvalues];
186 opts.yvalues = opts.yvalues|| [];
187 if (!paper.raphael.is(opts.yvalues[0], "array")) {
188 opts.yvalues = [opts.yvalues];
192 var allx = Array.prototype.concat.apply([], opts.xvalues),
193 ally = Array.prototype.concat.apply([], opts.yvalues),
194 xdim = chartinst.snapEnds(
195 Math.min.apply(Math, allx),
196 Math.max.apply(Math, allx),
197 opts.xvalues[0].length - 1
201 ydim = chartinst.snapEnds(
202 Math.min.apply(Math, ally),
203 Math.max.apply(Math, ally),
204 opts.yvalues[0].length - 1
206 miny = typeof(opts.ymin) == 'undefined' ? ydim.from : opts.ymin,
208 kx = (width - gutter * 2) / ((maxx - minx) || 1),
209 ky = (height - gutter * 2) / ((maxy - miny) || 1);
213 if (Raphael.is(values[0], "array") && values.length > 1) {
218 for (var i = values.length; i--;) {
219 bars.push(paper.set());
220 total.push(Math.max.apply(Math, values[i]));
221 len = Math.max(len, values[i].length);
224 for (var i = values.length; i--;) {
225 if (values[i].length < len) {
226 for (var j = len; j--;) {
232 total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
235 for (var i = 0; i < len; i++) {
237 values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
240 total = (opts.to) || total;
241 Roo.log([width, len, gutter]);
242 var barwidth = width / (len * (90 + gutter) + gutter) * 100,
243 barhgutter = barwidth * (gutter) / 100,
244 barvgutter = opts.vgutter == null ? 20 : opts.vgutter,
247 Y = (height - 2 * barvgutter) / total;
248 Roo.log([barwidth, X, barhgutter]);
250 barhgutter = Math.round(barhgutter);
251 barwidth = Math.floor(barwidth);
255 var axis = paper.set();
259 // Roo.log(opts.axis);
261 // Value - "top right bottom left". If
262 var ax = (opts.axis + "").split(/[,\s]+/);
272 opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
282 opts.axisystep || Math.floor((height - 2 * gutter) / 20),
287 opts.axisxlabels = opts.axisxlabels || [];
288 // add elements at beginning and end of array...
289 opts.axisxlabels.unshift('');
290 opts.axisxlabels.push('');
291 opts.axisxstep = opts.axisxstep || (opts.axisxlabels ? opts.axisxlabels.length -1 : false ) || len;
292 Roo.log(opts.axisxlabels);
296 // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
300 width - gutter, // total width
305 opts.axisxlabels || false, // labels
318 // opts.axisystep || Math.floor((height - 2 * gutter) / 20),
326 !opts.stacked && (barwidth /= multi || 1);
328 for (var i = 0; i < len; i++) {
334 for (var j = 0; j < (multi || 1); j++) {
335 var h = Math.round((multi ? values[j][i] : values[i]) * Y),
336 top = y + height - barvgutter - h,
337 bar = finger(Math.round(X + barwidth / 2), top + h, barwidth, h, true, type, null, paper).attr({ stroke: "none", fill: colors[multi ? j : i] });
339 // bar = finger(x, Y + barwidth / 2, Math.round(8 * X), barwidth - 1, false, type, null, paper).attr({stroke: "none", fill: colors[multi ? j : i]});
342 if (multi && bars[j]) { // bars[j] did not appear to exist?
349 bar.x = Math.round(X + barwidth / 2);
352 bar.value = multi ? values[j][i] : values[i];
354 if(!isNaN(bar.y) && opts.labels && opts.labels[i]){
355 bar.labels = paper.set();
356 var qtyLbl = paper.text(
362 'text-anchor' : 'start',
366 bar.labels.push(qtyLbl);
376 //X += barhgutter + 25; -- why add 25??
383 * create the legend for Vbarchart
387 // var legend = function (labels, otherslabel, mark, dir) {
390 // labels = labels || [];
391 //// dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "south";
392 // dir = "east"; // default set to east, do not support other position first...
393 // mark = paper[mark && mark.toLowerCase()] || "circle";
394 // chart.labels = paper.set();
396 // for (var i = 0; i < len; i++) {
399 // values[i].others && (labels[i] = otherslabel || "Others");
401 // labels[i] = chartinst.labelise(labels[i], values[i], sum);
402 // chart.labels.push(paper.set());
403 // chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ stroke: "none", fill: colors[i] }));
404 // chart.labels[i].push(txt = paper.text(x + 20, h, labels[i] || values[i]).attr(chartinst.txtattr).attr({ fill: opts.legendcolor || "#000", "text-anchor": "start"}));
405 // covers[i].label = chart.labels[i];
406 // h += txt.getBBox().height * 1.2;
410 // east: [width + 15, - Y +30]
411 //// west: [-bb.width - 2 * r - 20, -bb.height / 2],
412 //// north: [-r - bb.width / 2, -r - bb.height - 10],
413 //// south: [ x - 5, 10]
416 // chart.labels.translate.apply(chart.labels, tr);
417 // chart.push(chart.labels);
420 // if (opts.legend) {
421 // legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
426 // chart.hover = function (fin, fout) {
429 // covers.mouseover(fin).mouseout(fout);
433 // chart.hoverColumn = function (fin, fout) {
436 // fout = fout || function () {};
437 // covers2.mouseover(fin).mouseout(fout);
441 // chart.click = function (f) {
448 // chart.each = function (f) {
449 // if (!Raphael.is(f, "function")) {
452 // for (var i = covers.length; i--;) {
453 // f.call(covers[i]);
458 // chart.eachColumn = function (f) {
459 // if (!Raphael.is(f, "function")) {
462 // for (var i = covers2.length; i--;) {
463 // f.call(covers2[i]);
468 // chart.clickColumn = function (f) {
475 // chart.push(bars, covers, covers2);
477 // chart.covers = covers;
482 * Horizontal Barchart
484 function HBarchart(paper, x, y, width, height, values, opts) {
487 var chartinst = this,
488 type = opts.type || "square",
489 gutter = parseFloat(opts.gutter || "20%"),
492 covers = paper.set(),
493 covers2 = paper.set(),
494 total = Math.max.apply(Math, values),
497 colors = opts.colors || chartinst.colors,
502 if (Raphael.is(values[0], "array")) {
507 for (var i = values.length; i--;) {
508 bars.push(paper.set());
509 total.push(Math.max.apply(Math, values[i]));
510 len = Math.max(len, values[i].length);
514 for (var i = len; i--;) {
516 for (var j = values.length; j--;) {
517 tot +=+ values[j][i] || 0;
519 stacktotal.push(tot);
523 for (var i = values.length; i--;) {
524 if (values[i].length < len) {
525 for (var j = len; j--;) {
531 total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
534 for (var i = 0; i < len; i++) {
536 values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
540 for (i = 0; i < len; i++) {
542 values[opts.cut].value += values[i];
543 values[opts.cut].others = true;
544 others = values[opts.cut].value;
548 len = Math.min(opts.cut + 1, values.length);
550 others && values.splice(len) && (values[opts.cut].others = true);
553 total = (opts.to) || Math.max.apply(Math, values);
555 var barheight = Math.floor(height / (len * (100 + gutter) + gutter) * 100),
556 bargutter = Math.floor(barheight * gutter / 100),
559 X = (width - 1) / total;
561 !opts.stacked && (barheight /= multi || 1);
563 for (var i = 0; i < len; i++) {
566 for (var j = 0; j < (multi || 1); j++) {
567 var val = multi ? values[j][i] : values[i],
568 bar = finger(x, Y + barheight / 2, Math.round(val * X), barheight - 1, false, type, null, paper).attr({stroke: "none", fill: colors[multi ? j : i]});
576 bar.x = x + Math.round(val * X);
577 bar.y = Y + barheight / 2;
578 bar.w = Math.round(val * X);
590 var cvr = paper.rect(x, stack[0].y - stack[0].h / 2, width, barheight).attr(chartinst.shim);
593 cvr.bars = paper.set();
597 for (var s = stack.length; s--;) {
601 for (var s = 0, ss = stack.length; s < ss; s++) {
604 val = Math.round((size + bar.value) * X),
605 path = finger(x, bar.y, val, barheight - 1, false, type, 1, paper);
608 size && bar.attr({ path: path });
611 covers.push(cover = paper.rect(x + size * X, bar.y - bar.h / 2, bar.value * X, barheight).attr(chartinst.shim));
626 for (var i = 0; i < len; i++) {
627 for (var j = 0; j < (multi || 1); j++) {
628 var cover = paper.rect(x, Y, width, barheight).attr(chartinst.shim);
631 cover.bar = multi ? bars[j][i] : bars[i];
632 cover.value = cover.bar.value;
640 chart.label = function (labels, isRight) {
641 labels = labels || [];
642 this.labels = paper.set();
644 for (var i = 0; i < len; i++) {
645 for (var j = 0; j < (multi || 1); j++) {
646 //var label = paper.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total),
647 var label = Raphael.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total),
649 bars[i * (multi || 1) + j].x + 5 : // - barheight / 2 + 3 :
651 A = !isRight ? "end" : "start",
654 this.labels.push(L = paper.text(X, bars[i * (multi || 1) + j].y, label).attr(txtattr).attr({ "text-anchor": A }).insertBefore(covers[0]));
656 if (L.getBBox().x < x + 5) {
657 L.attr({x: x + 5, "text-anchor": "start"});
659 bars[i * (multi || 1) + j].label = L;
667 var axis = paper.set();
670 // Roo.log(opts.axis);
672 // Value - "top right bottom left". If
673 var ax = (opts.axis + "").split(/[,\s]+/);
676 +ax[0] && axis.push(// not in used at the moment
683 opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
687 +ax[1] && axis.push(// not in used at the moment
694 opts.axisystep || Math.floor((height - 2 * gutter) / 20),
699 +ax[2] && axis.push(// not in used at the moment
701 // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
705 // width - 2 * gutter,
708 width - 20, // total width
713 opts.axisxstep || Math.floor((width - 2 * gutter) / 20), // data length
715 opts.axisxlabels || false,
721 +ax[3] && axis.push(// just hardcode this
732 // opts.axisystep || Math.floor((height - 2 * gutter) / 20),
740 chart.hover = function (fin, fout) {
743 fout = fout || function () {};
744 covers.mouseover(fin).mouseout(fout);
748 chart.hoverColumn = function (fin, fout) {
751 fout = fout || function () {};
752 covers2.mouseover(fin).mouseout(fout);
756 chart.each = function (f) {
757 if (!Raphael.is(f, "function")) {
760 for (var i = covers.length; i--;) {
766 chart.eachColumn = function (f) {
767 if (!Raphael.is(f, "function")) {
770 for (var i = covers2.length; i--;) {
776 chart.click = function (f) {
783 chart.clickColumn = function (f) {
791 * create the legend for Hbarchart
795 var legend = function (labels, otherslabel, mark, dir) {
798 labels = labels || [];
799 // dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "south";
800 dir = "east"; // default set to east, do not support other position first...
801 mark = paper[mark && mark.toLowerCase()] || "circle";
802 chart.labels = paper.set();
804 for (var i = 0; i < len; i++) {
807 values[i].others && (labels[i] = otherslabel || "Others");
808 // Roo.log(values[i]);
809 labels[i] = chartinst.labelise(labels[i], values[i], sum);
810 chart.labels.push(paper.set());
811 chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ stroke: "none", fill: colors[i] }));
812 chart.labels[i].push(txt = paper.text(x + 20, h, labels[i] || values[i]).attr(chartinst.txtattr).attr({ fill: opts.legendcolor || "#000", "text-anchor": "start"}));
813 covers[i].label = chart.labels[i];
814 h += txt.getBBox().height * 1.2;
818 east: [width + 25, - Y +30]
819 // west: [-bb.width - 2 * r - 20, -bb.height / 2],
820 // north: [-r - bb.width / 2, -r - bb.height - 10],
821 // south: [ x - 5, 10]
824 chart.labels.translate.apply(chart.labels, tr);
825 chart.push(chart.labels);
829 legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
832 chart.push(bars, covers, covers2);
834 chart.covers = covers;
839 var F = function() {};
840 F.prototype = Raphael.g;
841 HBarchart.prototype = VBarchart.prototype = new F;
843 Raphael.fn.hbarchart = function(x, y, width, height, values, opts) {
844 return new HBarchart(this, x, y, width, height, values, opts);
847 Raphael.fn.barchart = function(x, y, width, height, values, opts) {
848 return new VBarchart(this, x, y, width, height, values, opts);