g.bar.js
[g.raphael] / g.bar.js
1 /*!
2  * g.Raphael 0.5 - Charting library, based on RaphaĆ«l
3  *
4  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
7
8 Raphael = typeof(Raphael) != 'undefined' ? Raphael :  (imports ? imports.seed.Raphael.Raphael : {});
9 Roo = typeof(Roo) != 'undefined' ? Roo:  (imports ? imports.seed.Roo.Roo: {});
10
11
12 (function () {
13     var mmin = Math.min,
14         mmax = Math.max;
15
16     function finger(x, y, width, height, dir, ending, isPath, paper) {
17         var path,
18             ends = { round: 'round', sharp: 'sharp', soft: 'soft', square: 'square' };
19
20         // dir 0 for horizontal and 1 for vertical
21         if ((dir && !height) || (!dir && !width)) {
22             return isPath ? "" : paper.path();
23         }
24
25         ending = ends[ending] || "square";
26         height = Math.round(height);
27         width = Math.round(width);
28         x = Math.round(x);
29         y = Math.round(y);
30
31         switch (ending) {
32             case "round":
33                 if (!dir) {
34                     var r = ~~(height / 2);
35
36                     if (width < r) {
37                         r = width;
38                         path = [
39                             "M", x + .5, y + .5 - ~~(height / 2),
40                             "l", 0, 0,
41                             "a", r, ~~(height / 2), 0, 0, 1, 0, height,
42                             "l", 0, 0,
43                             "z"
44                         ];
45                     } else {
46                         path = [
47                             "M", x + .5, y + .5 - r,
48                             "l", width - r, 0,
49                             "a", r, r, 0, 1, 1, 0, height,
50                             "l", r - width, 0,
51                             "z"
52                         ];
53                     }
54                 } else {
55                     r = ~~(width / 2);
56
57                     if (height < r) {
58                         r = height;
59                         path = [
60                             "M", x - ~~(width / 2), y,
61                             "l", 0, 0,
62                             "a", ~~(width / 2), r, 0, 0, 1, width, 0,
63                             "l", 0, 0,
64                             "z"
65                         ];
66                     } else {
67                         path = [
68                             "M", x - r, y,
69                             "l", 0, r - height,
70                             "a", r, r, 0, 1, 1, width, 0,
71                             "l", 0, height - r,
72                             "z"
73                         ];
74                     }
75                 }
76                 break;
77             case "sharp":
78                 if (!dir) {
79                     var half = ~~(height / 2);
80
81                     path = [
82                         "M", x, y + half,
83                         "l", 0, -height, mmax(width - half, 0), 0, mmin(half, width), half, -mmin(half, width), half + (half * 2 < height),
84                         "z"
85                     ];
86                 } else {
87                     half = ~~(width / 2);
88                     path = [
89                         "M", x + half, y,
90                         "l", -width, 0, 0, -mmax(height - half, 0), half, -mmin(half, height), half, mmin(half, height), half,
91                         "z"
92                     ];
93                 }
94                 break;
95             case "square":
96                 if (!dir) {
97                     path = [
98                         "M", x, y + ~~(height / 2),
99                         "l", 0, -height, width, 0, 0, height,
100                         "z"
101                     ];
102                 } else {
103                     path = [
104                         "M", x + ~~(width / 2), y,
105                         "l", 1 - width, 0, 0, -height, width - 1, 0,
106                         "z"
107                     ];
108                 }
109                 break;
110             case "soft":
111                 if (!dir) {
112                     r = mmin(width, Math.round(height / 5));
113                     path = [
114                         "M", x + .5, y + .5 - ~~(height / 2),
115                         "l", width - r, 0,
116                         "a", r, r, 0, 0, 1, r, r,
117                         "l", 0, height - r * 2,
118                         "a", r, r, 0, 0, 1, -r, r,
119                         "l", r - width, 0,
120                         "z"
121                     ];
122                 } else {
123                     r = mmin(Math.round(width / 5), height);
124                     path = [
125                         "M", x - ~~(width / 2), y,
126                         "l", 0, r - height,
127                         "a", r, r, 0, 0, 1, r, -r,
128                         "l", width - 2 * r, 0,
129                         "a", r, r, 0, 0, 1, r, r,
130                         "l", 0, height - r,
131                         "z"
132                     ];
133                 }
134         }
135
136         if (isPath) {
137             return path.join(",");
138         } else {
139             return paper.path(path);
140         }
141     }
142
143     /*
144      * Vertical Barchart
145      * @param {Raphael} paper  The paper to render the graph on
146      * @param {int} x    left coord
147      * @param {int} y   right coord
148      * @param {int} width  width of graph
149      * @param {int} height heigt of graph
150      * @param {Array} values - single = [1,2,3] or multiple [ [1,2,3] , [1,2,3]]
151      * @param {Object} opts  - options
152      *    type :  {String} square | ???
153      *    gutter: {Number} percentage width default 20%
154      *    vgutter : {Number}  vertical gutter?
155      *    colors : {Array} list of colours - default chartinst.colors
156      *    xvalues : {Array} not sure - this looks like can be single or multi dimensional array 
157      *    yvalues : {Array} not sure - this looks like can be single or multi dimensional array
158      *    ymin :
159      *    axis: {String} eg. "top right bottom left" - which sides to show an axis eg. "0 0 1 1"
160      *    axisxstep
161      *    axisystep
162      *    axisxlabels: {Array} - note - first element appears left of first bar..
163      *    stacked
164      *    to
165      *    stretch
166      *    labels : {Array} labels to go above the chart..
167      *    
168      */
169     function VBarchart(paper, x, y, width, height, values, opts) {
170         opts = opts || {};
171          
172         var chartinst = this,
173             type = opts.type || "square",
174             gutter = parseFloat(opts.gutter || "20%"),
175             chart = paper.set(),
176             bars = paper.set(),
177             covers = paper.set(),
178             covers2 = paper.set(),
179             total = Math.max.apply(Math, values),
180             stacktotal = [],
181             multi = 0,
182             colors = opts.colors || chartinst.colors,
183             len = values.length + 2,
184             sum = 0;
185             
186         opts.xvalues = opts.xvalues|| [];
187         if (!paper.raphael.is(opts.xvalues[0], "array")) {
188             opts.xvalues = [opts.xvalues];
189         }
190         opts.yvalues = opts.yvalues|| [];
191         if (!paper.raphael.is(opts.yvalues[0], "array")) {
192             opts.yvalues = [opts.yvalues];
193         }
194         
195         
196         var allx = Array.prototype.concat.apply([], opts.xvalues),
197             ally = Array.prototype.concat.apply([], opts.yvalues),
198             
199             xdim = chartinst.snapEnds(
200                 Math.min.apply(Math, allx),
201                 Math.max.apply(Math, allx),
202                 opts.xvalues[0].length - 1
203             ),
204             minx = xdim.from,
205             maxx = xdim.to,
206             ydim = chartinst.snapEnds(
207                     Math.min.apply(Math, ally),
208                     Math.max.apply(Math, ally),
209                     opts.yvalues[0].length - 1
210             ),
211             miny = typeof(opts.ymin) == 'undefined' ? ydim.from : opts.ymin,
212             maxy = ydim.to,
213             kx = (width - gutter * 2) / ((maxx - minx) || 1),
214             ky = (height - gutter * 2) / ((maxy - miny) || 1);
215             
216        //Roo.log({ally : ally});
217         
218         if (Raphael.is(values[0], "array")) // && values.length > 1) {
219             total = [];
220             multi = len;
221             len = 0;
222
223             for (var i = values.length; i--;) {
224                 bars.push(paper.set());
225                 total.push(Math.max.apply(Math, values[i]));
226                 len = Math.max(len, values[i].length);
227             }
228  
229             if (opts.stacked) {
230                 for (var i = len; i--;) {
231                     var tot = 0;
232  
233                     for (var j = values.length; j--;) {
234                         tot +=+ values[j][i] || 0;
235                     }
236  
237                     stacktotal.push(tot);
238                 }
239             }
240  
241
242             for (var i = values.length; i--;) {
243                 if (values[i].length < len) {
244                     for (var j = len; j--;) {
245                         values[i].push(0);
246                     }
247                 }
248             }
249
250             total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
251         }
252         
253         for (var i = 0; i < len; i++) {
254             sum += values[i];
255             values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
256         }
257         
258         total = (opts.to) || total;
259         Roo.log([width, len, gutter]);
260         var barwidth = width / (len * (90 + gutter) + gutter) * 100,
261             barhgutter = barwidth * (gutter) / 100,
262             barvgutter = opts.vgutter == null ? 20 : opts.vgutter,
263             stack = [],
264             X = x + barhgutter,
265             Y = (height - 2 * barvgutter) / total;
266         Roo.log([barwidth, X, barhgutter]);
267         if (!opts.stretch) {
268             barhgutter = Math.round(barhgutter);
269             barwidth = Math.floor(barwidth);
270         }
271         
272         
273          var axis = paper.set();
274         
275         if (opts.axis) {
276             
277             // Roo.log(opts.axis);
278             
279             // Value - "top right bottom left". If
280             var ax = (opts.axis + "").split(/[,\s]+/);
281             
282             // top axis
283             +ax[0] && axis.push(
284                     chartinst.axis(
285                         x + gutter,
286                         y + gutter,
287                         width - 2 * gutter,
288                         minx,
289                         maxx,
290                         opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
291                         2,
292                         paper));
293             // right axis
294             +ax[1] && axis.push(
295                     chartinst.axis(
296                         x + width - gutter,
297                         y + height - gutter,
298                         height - 2 * gutter,
299                         miny, maxy,
300                         opts.axisystep || Math.floor((height - 2 * gutter) / 20),
301                         3,
302                         paper
303                     ));
304             // bottom axis
305             opts.axisxlabels = opts.axisxlabels || [];
306             // add elements at beginning and end of array...
307             opts.axisxlabels.unshift(' ');
308             opts.axisxlabels.push(' ');
309             opts.axisxstep  = opts.axisxstep  || (opts.axisxlabels ? opts.axisxlabels.length  -1    : false ) || len;
310             
311             
312             +ax[2] && axis.push(
313                 // bottom
314                 // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
315                 chartinst.axis(
316                     x + gutter ,
317                     y + height - gutter,
318                     width - (gutter + barhgutter), // total width
319                     minx ,  // from
320                     maxx, //to
321                     opts.axisxstep ,
322                     0, // orientation
323                     opts.axisxlabels || false, // labels
324                     "-", // type
325                     5, // dash size
326                     paper
327                 ));
328             // left axis
329             
330              // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
331             
332             // vertical steps (eg. number of Lables) - should be related to the range.
333             // eg. around 10? 
334             opts.axisystep = opts.axisystep  || Math.floor((height - 2 * gutter) / 20);
335             var yrangedivs = Math.ceil((maxy - miny) / opts.axisystep) 
336             var yrange = yrangedivs * opts.axisystep;
337             if (yrange < (maxy - miny) ) {
338                 opts.axisystep++;
339                 maxy = miny + (yrangedivs * opts.axisystep);
340             }
341             Roo.log(opts.axisystep);
342             
343             
344             
345             +ax[3] && axis.push(
346                     chartinst.axis(
347                         x + gutter,
348                         y + height - gutter,
349                         height - 2 * gutter,
350                         miny,
351                         maxy ,
352                         opts.axisystep, 
353                         
354                         1,
355                         paper
356             ));
357         }
358         
359         
360         !opts.stacked && (barwidth /= multi || 1);
361         
362         for (var i = 0; i < len; i++) {
363             stack = [];
364             if(i == 0){
365                 X += barwidth;
366             }
367             
368             Roo.log( {X : X});
369             for (var j = 0; j < (multi || 1); j++) {
370                 var h = Math.round((multi ? values[j][i] : values[i]) * Y),
371                     top = y + height - barvgutter - h,
372                     bar = finger(Math.round(X + barwidth / 2), top + h, barwidth, h, true, type, null, paper).attr({
373                             stroke: "none",
374                             fill: colors[multi ? j : i]
375                     });
376                     
377                     
378                 
379                 
380                 if (multi) { // bars[j] did not appear to exist?
381                     bars[j].push(bar);
382                 } else {
383                     bars.push(bar);
384                 }
385                 
386                 bar.y = top;
387                 bar.x = Math.round(X + barwidth / 2);
388                 bar.w = barwidth;
389                 bar.h = h;
390                 bar.value = multi ? values[j][i] : values[i];
391                 
392                 if(!isNaN(bar.y) && opts.labels && opts.labels[i]){
393                     bar.labels = paper.set();
394                     var qtyLbl = paper.text(
395                             bar.x - 10,
396                             (bar.y - 10),
397                             opts.labels[i]).attr({
398                                 fill : '#000',
399                                 'text-anchor' : 'start',
400                                 font : '10px Arial'
401                             });
402
403                     bar.labels.push(qtyLbl);
404                 }
405                 
406                 if (!opts.stacked) {
407                     X += barwidth;
408                 } else {
409                     stack.push(bar);
410                 }
411             }
412
413             //X += barhgutter + 25; -- why add 25??
414             X += barhgutter;
415             if (opts.stacked) {
416                var cvr;
417
418                covers2.push(cvr = paper.rect(stack[0].x - stack[0].w / 2, y, barwidth, height).attr(chartinst.shim));
419                cvr.bars = paper.set();
420
421                var size = 0;
422
423                for (var s = stack.length; s--;) {
424                    stack[s].toFront();
425                }
426
427                for (var s = 0, ss = stack.length; s < ss; s++) {
428                    var bar = stack[s],
429                        cover,
430                        h = (size + bar.value) * Y,
431                        path = finger(bar.x, y + height - barvgutter - !!size * .5, barwidth, h, true, type, 1, paper);
432
433                    cvr.bars.push(bar);
434                    size && bar.attr({path: path});
435                    bar.h = h;
436                    bar.y = y + height - barvgutter - !!size * .5 - h;
437                    covers.push(cover = paper.rect(bar.x - bar.w / 2, bar.y, barwidth, bar.value * Y).attr(chartinst.shim));
438                    cover.bar = bar;
439                    cover.value = bar.value;
440                    size += bar.value;
441                }
442
443                X += barwidth;
444            }
445
446
447         }
448         X = x + barhgutter;
449         
450         
451         /*
452          * create the legend for Vbarchart
453          * 
454          */
455         
456 //        var legend = function (labels, otherslabel, mark, dir) {
457 //            var h = Y + 10;
458 //            
459 //            labels = labels || [];
460 ////            dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "south"; 
461 //            dir = "east"; // default set to east, do not support other position first...
462 //            mark = paper[mark && mark.toLowerCase()] || "circle";
463 //            chart.labels = paper.set();
464 //            
465 //            for (var i = 0; i < len; i++) {
466 //                var txt;
467 //                
468 //                values[i].others && (labels[i] = otherslabel || "Others");
469 //                
470 //                labels[i] = chartinst.labelise(labels[i], values[i], sum);
471 //                chart.labels.push(paper.set());
472 //                chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ stroke: "none", fill: colors[i] }));
473 //                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"}));
474 //                covers[i].label = chart.labels[i];
475 //                h += txt.getBBox().height * 1.2;
476 //            }
477 //            
478 //            var tr = {
479 //                    east: [width + 15, - Y +30]
480 ////                    west: [-bb.width - 2 * r - 20, -bb.height / 2],
481 ////                    north: [-r - bb.width / 2, -r - bb.height - 10],
482 ////                    south: [ x - 5, 10]
483 //                }[dir];
484 //                
485 //            chart.labels.translate.apply(chart.labels, tr);
486 //            chart.push(chart.labels);
487 //        };
488 //
489 //        if (opts.legend) {
490 //            legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
491 //        }
492         
493         
494         
495 //        chart.hover = function (fin, fout) {
496 //            covers2.hide();
497 //            covers.show();
498 //            covers.mouseover(fin).mouseout(fout);
499 //            return this;
500 //        };
501 //
502 //        chart.hoverColumn = function (fin, fout) {
503 //            covers.hide();
504 //            covers2.show();
505 //            fout = fout || function () {};
506 //            covers2.mouseover(fin).mouseout(fout);
507 //            return this;
508 //        };
509 //
510 //        chart.click = function (f) {
511 //            covers2.hide();
512 //            covers.show();
513 //            covers.click(f);
514 //            return this;
515 //        };
516
517 //        chart.each = function (f) {
518 //            if (!Raphael.is(f, "function")) {
519 //                return this;
520 //            }
521 //            for (var i = covers.length; i--;) {
522 //                f.call(covers[i]);
523 //            }
524 //            return this;
525 //        };
526 //
527 //        chart.eachColumn = function (f) {
528 //            if (!Raphael.is(f, "function")) {
529 //                return this;
530 //            }
531 //            for (var i = covers2.length; i--;) {
532 //                f.call(covers2[i]);
533 //            }
534 //            return this;
535 //        };
536 //
537 //        chart.clickColumn = function (f) {
538 //            covers.hide();
539 //            covers2.show();
540 //            covers2.click(f);
541 //            return this;
542 //        };
543         
544 //        chart.push(bars, covers, covers2);
545         chart.bars = bars;
546 //        chart.covers = covers;
547         return chart;
548     };
549
550     /**
551      * Horizontal Barchart
552      */
553     function HBarchart(paper, x, y, width, height, values, opts) {
554         opts = opts || {};
555         
556         var chartinst = this,
557             type = opts.type || "square",
558             gutter = parseFloat(opts.gutter || "20%"),
559             chart = paper.set(),
560             bars = paper.set(),
561             covers = paper.set(),
562             covers2 = paper.set(),
563             total = Math.max.apply(Math, values),
564             stacktotal = [],
565             multi = 0,
566             colors = opts.colors || chartinst.colors,
567             len = values.length,
568             others = 0,
569             sum = 0;
570             
571         if (Raphael.is(values[0], "array")) {
572             total = [];
573             multi = len;
574             len = 0;
575
576             for (var i = values.length; i--;) {
577                 bars.push(paper.set());
578                 total.push(Math.max.apply(Math, values[i]));
579                 len = Math.max(len, values[i].length);
580             }
581
582             if (opts.stacked) {
583                 for (var i = len; i--;) {
584                     var tot = 0;
585                     for (var j = values.length; j--;) {
586                         tot +=+ values[j][i] || 0;
587                     }
588                     stacktotal.push(tot);
589                 }
590             }
591
592             for (var i = values.length; i--;) {
593                 if (values[i].length < len) {
594                     for (var j = len; j--;) {
595                         values[i].push(0);
596                     }
597                 }
598             }
599
600             total = Math.max.apply(Math, opts.stacked ? stacktotal : total);
601         }
602         
603         for (var i = 0; i < len; i++) {
604             sum += values[i];
605             values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
606         }
607         
608         if(opts.cut){
609            for (i = 0; i < len; i++) {
610                 if (i > opts.cut) {
611                     values[opts.cut].value += values[i];
612                     values[opts.cut].others = true;
613                     others = values[opts.cut].value;
614                 }
615             }
616             
617             len = Math.min(opts.cut + 1, values.length);
618             
619             others && values.splice(len) && (values[opts.cut].others = true);
620         }
621         
622         total = (opts.to) || Math.max.apply(Math, values);
623
624         var barheight = Math.floor(height / (len * (100 + gutter) + gutter) * 100),
625             bargutter = Math.floor(barheight * gutter / 100),
626             stack = [],
627             Y = y + bargutter,
628             X = (width - 1) / total;
629
630         !opts.stacked && (barheight /= multi || 1);
631     
632         for (var i = 0; i < len; i++) {
633             stack = [];
634
635             for (var j = 0; j < (multi || 1); j++) {
636                 var val = multi ? values[j][i] : values[i],
637                     bar = finger(x, Y + barheight / 2, Math.round(val * X), barheight - 1, false, type, null, paper).attr({stroke: "none", fill: colors[multi ? j : i]});
638
639                 if (multi) {
640                     bars[j].push(bar);
641                 } else {
642                     bars.push(bar);
643                 }
644
645                 bar.x = x + Math.round(val * X);
646                 bar.y = Y + barheight / 2;
647                 bar.w = Math.round(val * X);
648                 bar.h = barheight;
649                 bar.value = +val;
650
651                 if (!opts.stacked) {
652                     Y += barheight;
653                 } else {
654                     stack.push(bar);
655                 }
656             }
657             
658             if (opts.stacked) {
659                 var cvr = paper.rect(x, stack[0].y - stack[0].h / 2, width, barheight).attr(chartinst.shim);
660
661                 covers2.push(cvr);
662                 cvr.bars = paper.set();
663
664                 var size = 0;
665
666                 for (var s = stack.length; s--;) {
667                     stack[s].toFront();
668                 }
669
670                 for (var s = 0, ss = stack.length; s < ss; s++) {
671                     var bar = stack[s],
672                         cover,
673                         val = Math.round((size + bar.value) * X),
674                         path = finger(x, bar.y, val, barheight - 1, false, type, 1, paper);
675
676                     cvr.bars.push(bar);
677                     size && bar.attr({ path: path });
678                     bar.w = val;
679                     bar.x = x + val;
680                     covers.push(cover = paper.rect(x + size * X, bar.y - bar.h / 2, bar.value * X, barheight).attr(chartinst.shim));
681                     cover.bar = bar;
682                     size += bar.value;
683                 }
684
685                 Y += barheight;
686             }
687
688             Y += bargutter;
689         }
690
691         covers2.toFront();
692         Y = y + bargutter;
693
694         if (!opts.stacked) {
695             for (var i = 0; i < len; i++) {
696                 for (var j = 0; j < (multi || 1); j++) {
697                     var cover = paper.rect(x, Y, width, barheight).attr(chartinst.shim);
698
699                     covers.push(cover);
700                     cover.bar = multi ? bars[j][i] : bars[i];
701                     cover.value = cover.bar.value;
702                     Y += barheight;
703                 }
704
705                 Y += bargutter;
706             }
707         }
708
709         chart.label = function (labels, isRight) {
710             labels = labels || [];
711             this.labels = paper.set();
712             var txtattr =  { };
713             for (var i = 0; i < len; i++) {
714                 for (var j = 0; j < (multi || 1); j++) {
715                     //var  label = paper.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total),
716                     var  label = Raphael.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total),
717                         X = isRight ?
718                                 bars[i * (multi || 1) + j].x + 5 : // - barheight / 2 + 3 :
719                                 x + 5,
720                         A = !isRight ? "end" : "start",
721                         L;
722
723                     this.labels.push(L = paper.text(X, bars[i * (multi || 1) + j].y, label).attr(txtattr).attr({ "text-anchor": A }).insertBefore(covers[0]));
724
725                     if (L.getBBox().x < x + 5) {
726                         L.attr({x: x + 5, "text-anchor": "start"});
727                     } else {
728                         bars[i * (multi || 1) + j].label = L;
729                     }
730                 }
731             }
732
733             return this;
734         };
735         
736         var axis = paper.set();
737         
738         if (opts.axis) {    
739             // Roo.log(opts.axis);
740             
741             // Value - "top right bottom left". If
742             var ax = (opts.axis + "").split(/[,\s]+/);
743             
744             // top axis
745             +ax[0] && axis.push(// not in used at the moment
746                     chartinst.axis(
747                         x + gutter,
748                         y + gutter,
749                         width - 2 * gutter,
750                         0,
751                         0,
752                         opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
753                         2,
754                         paper));
755             // right axis
756             +ax[1] && axis.push(// not in used at the moment
757                     chartinst.axis(
758                         x + width - gutter,
759                         y + height - gutter,
760                         height - 2 * gutter,
761                         0,
762                         0,
763                         opts.axisystep || Math.floor((height - 2 * gutter) / 20),
764                         3,
765                         paper
766                     ));
767             // bottom axis
768             +ax[2] && axis.push(// not in used at the moment
769                 // bottom
770                 // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
771                 chartinst.axis(
772                     x + gutter,
773                     y + height - gutter,
774 //                    width - 2 * gutter,
775 //                    20,
776 //                    50,
777                     width - 20, // total width
778                     0,
779                     0,
780 //                    20,
781 //                    50,
782                     opts.axisxstep || Math.floor((width - 2 * gutter) / 20), // data length
783                     0,
784                     opts.axisxlabels || false,
785                     "-",
786                     5,
787                     paper
788                 ));
789             // left axis
790             +ax[3] && axis.push(// just hardcode this
791                 chartinst.axis(
792                     19, // x pos 
793                     240, // y pos
794                     220,// length
795                     0, // from
796                     0, // to
797                     0,// steps
798                     1,// orientation
799                     false, // labels
800                     "-",// type
801 //                        opts.axisystep || Math.floor((height - 2 * gutter) / 20),
802                     100,// dashsize
803                     paper
804             ));
805         }
806         
807         
808
809         chart.hover = function (fin, fout) {
810             covers2.hide();
811             covers.show();
812             fout = fout || function () {};
813             covers.mouseover(fin).mouseout(fout);
814             return this;
815         };
816
817         chart.hoverColumn = function (fin, fout) {
818             covers.hide();
819             covers2.show();
820             fout = fout || function () {};
821             covers2.mouseover(fin).mouseout(fout);
822             return this;
823         };
824
825         chart.each = function (f) {
826             if (!Raphael.is(f, "function")) {
827                 return this;
828             }
829             for (var i = covers.length; i--;) {
830                 f.call(covers[i]);
831             }
832             return this;
833         };
834
835         chart.eachColumn = function (f) {
836             if (!Raphael.is(f, "function")) {
837                 return this;
838             }
839             for (var i = covers2.length; i--;) {
840                 f.call(covers2[i]);
841             }
842             return this;
843         };
844
845         chart.click = function (f) {
846             covers2.hide();
847             covers.show();
848             covers.click(f);
849             return this;
850         };
851
852         chart.clickColumn = function (f) {
853             covers.hide();
854             covers2.show();
855             covers2.click(f);
856             return this;
857         };
858         
859         /*
860          * create the legend for Hbarchart
861          * 
862          */
863         
864         var legend = function (labels, otherslabel, mark, dir) {
865             var h = Y + 10;
866             
867             labels = labels || [];
868 //            dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "south"; 
869             dir = "east"; // default set to east, do not support other position first...
870             mark = paper[mark && mark.toLowerCase()] || "circle";
871             chart.labels = paper.set();
872             
873             for (var i = 0; i < len; i++) {
874                 var txt;
875                 
876                 values[i].others && (labels[i] = otherslabel || "Others");
877 //                Roo.log(values[i]);
878                 labels[i] = chartinst.labelise(labels[i], values[i], sum);
879                 chart.labels.push(paper.set());
880                 chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ stroke: "none", fill: colors[i] }));
881                 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"}));
882                 covers[i].label = chart.labels[i];
883                 h += txt.getBBox().height * 1.2;
884             }
885 //            Roo.log(labels)
886             var tr = {
887                     east: [width + 25, - Y +30]
888 //                    west: [-bb.width - 2 * r - 20, -bb.height / 2],
889 //                    north: [-r - bb.width / 2, -r - bb.height - 10],
890 //                    south: [ x - 5, 10]
891                 }[dir];
892                 
893             chart.labels.translate.apply(chart.labels, tr);
894             chart.push(chart.labels);
895         };
896
897         if (opts.legend) {
898             legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
899         }
900         
901         chart.push(bars, covers, covers2);
902         chart.bars = bars;
903         chart.covers = covers;
904         return chart;
905     };
906     
907     //inheritance
908     var F = function() {};
909     F.prototype = Raphael.g;
910     HBarchart.prototype = VBarchart.prototype = new F;
911     
912     Raphael.fn.hbarchart = function(x, y, width, height, values, opts) {
913         return new HBarchart(this, x, y, width, height, values, opts);
914     };
915     
916     Raphael.fn.barchart = function(x, y, width, height, values, opts) {
917         return new VBarchart(this, x, y, width, height, values, opts);
918     };
919 })();