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.
7 imports = typeof(imports) == 'undefined' ? false : imports;
8 Raphael = typeof(Raphael) != 'undefined' ? Raphael : (imports ? imports.seed.Raphael.Raphael : {});
9 Roo = typeof(Roo) != 'undefined' ? Roo: (imports ? imports.seed.Roo.Roo: {});
14 * Create a horizontal chart
15 * support left axis with labels and bottom axis with interval
17 * @param {Raphael} paper to draw on
18 * @param {int} width - width of the chart
19 * @param {int} height - height of the chart
20 * @param {Array} values
21 * @param {Object} opts options
22 * background (string) : background color
23 * colors (array) : colors of the bars
24 * labelfont (string) : font family of bar labels
25 * labelfontsize (number) : font size of bar labels
26 * labelfontweight (string) (number) : font weight of bar labels
27 * labelfontcolors (array) : font colors of bar labels
28 * gutter (string) (number) : height of gutters in terms of percentage of bar height (e.g. '20%' : height raito of a bar to a gutter is 10 to 2)
29 * axis (string) : composed of 4 '0' / '1' separated by space (indicated whether top / right / bottom / left axis should be shown)
30 * (e.g. '0 0 1 1' indicates that the bottom and the left axis should be shown)
31 * leftaxiswidth (number) : width of left axis area
32 * leftaxislabels (array) : labels in left axis area
33 * leftaxisfontsize (number) : font size of labels in left axis
34 * bottomaxisheight (number) : height of bottom axis area
35 * bottomaxisstep (number) : number of steps in bottom axis
36 * bottomaxisfontsize (number) : font size of labels in bottom axis
37 * axisfont (string) : font family of labels in axes
38 * axisfontweight (string) (number) : font weight of labels in axes
39 * axisfontcolor (string) : font color of labels in axes
42 function MHBarchart(paper, width, height, values, opts) {
48 max = values.length == 0 ? 0 : Math.max(...values),
49 background = opts.background || '#FFFFFF',
50 colors = opts.colors || chartinst.colors,
51 labelFont = opts.labelfont || 'Work Sans',
52 labelFontSize = opts.labelfontsize || 12,
53 labelFontWeight = opts.labelfontweight || 'bold',
54 labelFontColors = opts.labelfontcolors || [],
55 gutter = parseFloat(opts.gutter || "40%"),
56 axis = opts.axis || "0 0 0 0",
57 ax = (axis + "").split(/[,\s]+/),
59 leftAxisLabels = opts.leftaxislabels || [];
60 leftAxisFontSize = opts.leftaxisfontsize || 16,
63 bottomAxisStep = opts.bottomaxisstep || 10;
64 bottomAxisFontSize = opts.bottomaxisfontsize || 16,
65 axisFont = opts.axisfont || 'Work Sans',
66 axisFontWeight = opts.axisfontweight || 'normal',
67 axisFontColor = opts.axisfontcolor || '#323232';
71 bottomAxisHeight = opts.bottomaxisheight || 30;
72 max = Math.ceil(max / bottomAxisStep) * bottomAxisStep;
76 leftAxisWidth = opts.leftaxiswidth || 240;
79 if(labelFontColors.length < len) {
80 for(var i = labelFontColors.length; i < len; i++) {
81 labelFontColors.push('#FFFFFF');
85 var step = max / bottomAxisStep;
94 max = Math.ceil(step / factor) * factor * bottomAxisStep;
97 paper.rect(0, 0, width, height).attr({ stroke: "none", fill: background });
99 var barWidth = (width - leftAxisWidth - rightPadding); // maximum width of bar
100 var barHeight = (height - bottomAxisHeight) / (len * (100 + gutter) + gutter) * 100;
101 var barGutter = barHeight * (gutter / 100);
105 chartinst.bottomAxis(
108 height - bottomAxisHeight,
126 height - bottomAxisHeight,
138 var unit = barWidth / max;
140 values.forEach(function(v,k) {
145 paper.rect(leftAxisWidth, barGutter + (k * (barHeight + barGutter)), v * unit, barHeight).attr({ stroke: "none", fill: colors[k] });
148 // 16 pixels away from right of left axis
149 // align bar label in the center
150 paper.text(leftAxisWidth + 16, barGutter + (k * (barHeight + barGutter)) + barHeight / 2, Roo.util.Format.number(v, 0)).attr({
151 "font-size": labelFontSize,
152 "font-family": labelFont,
153 "font-weight": labelFontWeight,
154 "text-anchor": 'start',
155 fill : labelFontColors[k]
165 var F = function() {};
166 F.prototype = Raphael.g;
167 MHBarchart.prototype = new F;
169 Raphael.fn.mhbarchart = function(width, height, values, opts) {
170 return new MHBarchart(this, width, height, values, opts);
173 MHBarchart.prototype.bottomAxis = function (paper, x, y, length, max, steps, axisHeight, axisFont, axisFontSize, axisFontWeight, axisFontColor)
176 var path = ["M", x, y, "l", length, 0],
178 d = Math.ceil(max / steps),
181 for(var i = 0; i <= steps; i++) {
182 paper.text(x + i * dl, y + axisHeight / 2, i * d).attr({
183 "font-size": axisFontSize,
184 "font-family": axisFont,
185 "font-weight": axisFontWeight,
186 "text-anchor": 'middle',
190 // bottom axis interval
191 paper.path(["M", x + i * dl , y, "l", 0, -1 * y]).attr({ stroke: '#9E9E9E', "stroke-width": 1 });
194 var res = paper.path(path).attr({ stroke: '#000', "stroke-width": 0 }); // default no axis
197 res.all = paper.set([res, text]);
198 res.remove = function () {
200 this.constructor.prototype.remove.call(this);
206 MHBarchart.prototype.leftAxis = function (paper, x, y, length, labels, barHeight, barGutter, axisFont, axisFontSize, axisFontWeight, axisFontColor)
208 var path = ["M", x, y, "l", 0, length],
211 labels.forEach(function(v,k) {
212 paper.text(0, barGutter + (k * (barHeight + barGutter)) + barHeight / 2, v).attr({
213 "font-size": axisFontSize,
214 "font-family": axisFont,
215 "font-weight": axisFontWeight,
216 "text-anchor": 'start',
221 var res = paper.path(path).attr({ stroke: '#000', "stroke-width": 0 }); // default no axis
224 res.all = paper.set([res, text]);
225 res.remove = function () {
227 this.constructor.prototype.remove.call(this);