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.
8 if (typeof(Raphael) == 'undefined') {
9 // support for seed/simple browser usage
10 importz = imports['seed/importz.js'].importz;
12 Raphael = importz('Raphael');
18 function shrink(values, dim) {
19 var k = values.length / dim,
25 while (j < values.length) {
29 sum += values[j] * (1 + l);
31 sum = values[j++] * -l;
34 sum += values[j++] * 1;
40 function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) {
41 var l1 = (p2x - p1x) / 2,
43 a = Math.atan((p2x - p1x) / Math.abs(p2y - p1y)),
44 b = Math.atan((p3x - p2x) / Math.abs(p2y - p3y));
46 a = p1y < p2y ? Math.PI - a : a;
47 b = p3y < p2y ? Math.PI - b : b;
49 var alpha = Math.PI / 2 - ((a + b) % (Math.PI * 2)) / 2,
50 dx1 = l1 * Math.sin(alpha + a),
51 dy1 = l1 * Math.cos(alpha + a),
52 dx2 = l2 * Math.sin(alpha + b),
53 dy2 = l2 * Math.cos(alpha + b);
63 function Linechart(paper, x, y, width, height, valuesx, valuesy, opts) {
69 if (!paper.raphael.is(valuesx[0], "array")) {
73 if (!paper.raphael.is(valuesy[0], "array")) {
78 var gutter = opts.gutter || 10,
79 len = Math.max(valuesx[0].length, valuesy[0].length),
80 symbol = opts.symbol || "",
81 colors = opts.colors || chartinst.colors,
87 for (var i = 0, ii = valuesy.length; i < ii; i++) {
88 len = Math.max(len, valuesy[i].length);
91 var shades = paper.set();
93 for (i = 0, ii = valuesy.length; i < ii; i++) {
95 shades.push(paper.path().attr({ stroke: "none", fill: colors[i], opacity: opts.nostroke ? 1 : .3 }));
98 if (valuesy[i].length > width - 2 * gutter) {
99 valuesy[i] = shrink(valuesy[i], width - 2 * gutter);
100 len = width - 2 * gutter;
103 if (valuesx[i] && valuesx[i].length > width - 2 * gutter) {
104 valuesx[i] = shrink(valuesx[i], width - 2 * gutter);
108 var allx = Array.prototype.concat.apply([], valuesx),
109 ally = Array.prototype.concat.apply([], valuesy),
110 xdim = chartinst.snapEnds(
111 Math.min.apply(Math, allx),
112 Math.max.apply(Math, allx),
113 valuesx[0].length - 1
117 ydim = chartinst.snapEnds(
118 Math.min.apply(Math, ally),
119 Math.max.apply(Math, ally),
120 valuesy[0].length - 1
122 miny = typeof(opts.ymin) == 'undefined' ? ydim.from : opts.ymin,
123 maxy = typeof(opts.maxy) == 'undefined' ? ydim.to : opts.maxy,
124 kx = (width - gutter * 2) / ((maxx - minx) || 1),
125 ky = (height - gutter * 2) / ((maxy - miny) || 1);
127 var axis = paper.set();
129 // Roo.log("Checking for AXIS");
133 // Roo.log(opts.axis);
135 // Value - "top right bottom left". If
136 var ax = (opts.axis + "").split(/[,\s]+/);
146 opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
156 opts.axisystep || Math.floor((height - 2 * gutter) / 20),
161 opts.axisxlabels = opts.axisxlabels || [];
163 opts.axisxstep = opts.axisxstep || (opts.axisxlabels ? opts.axisxlabels.length -1 : false ) || len;
167 y: y + height - gutter,
168 length : width - 2 * gutter, // total width
169 from: minx , // from -- infinity??? if no 'xvalues set'
171 steps : opts.axisxstep , // 9 when we have 8 items?
172 orientation : 0, // orientation
173 labels : opts.axisxlabels || false, // labels
174 type : "-", // type ofbarhgutter line
175 dashsize : 5, // dash size
177 loffset : Math.round(gutter / 100 + (1)),
178 roffset : 100// Math.round(gutter / 100 + (1)),
183 // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
184 chartinst.axis( ax_args)
187 // +ax[2] && axis.push(
189 // // x, y, length, from, to, steps, orientation, labels, type, dashsize, paper
192 // y + height - gutter,
193 // width - 2 * gutter,
196 // opts.axisxstep || Math.floor((width - 2 * gutter) / 20),
198 // opts.axisxlabels || false,
210 opts.axisystep || Math.floor((height - 2 * gutter) / 20),
216 var lines = paper.set(),
217 symbols = paper.set(),
220 for (i = 0, ii = valuesy.length; i < ii; i++) {
221 if (!opts.nostroke) {
222 lines.push(line = paper.path().attr({
224 "stroke-width": opts.width || 2,
225 "stroke-linejoin": "round",
226 "stroke-linecap": "round",
227 "stroke-dasharray": opts.dash || ""
231 var sym = Raphael.is(symbol, "array") ? symbol[i] : symbol,
232 symset = paper.set();
236 for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
237 var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
238 Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
240 (Raphael.is(sym, "array") ? sym[j] : sym) && symset.push(paper[Raphael.is(sym, "array") ? sym[j] : sym](X, Y, (opts.width || 2) * 3).attr({ fill: colors[i], stroke: "none" }));
243 if (j && j != jj - 1) {
244 var X0 = x + gutter + ((valuesx[i] || valuesx[0])[j - 1] - minx) * kx,
245 Y0 = y + height - gutter - (valuesy[i][j - 1] - miny) * ky,
246 X2 = x + gutter + ((valuesx[i] || valuesx[0])[j + 1] - minx) * kx,
247 Y2 = y + height - gutter - (valuesy[i][j + 1] - miny) * ky,
248 a = getAnchors(X0, Y0, X, Y, X2, Y2);
250 path = path.concat([a.x1, a.y1, X, Y, a.x2, a.y2]);
254 path = ["M", X, Y, "C", X, Y];
257 path = path.concat([j ? "L" : "M", X, Y]);
262 path = path.concat([X, Y, X, Y]);
265 symbols.push(symset);
268 shades[i].attr({ path: path.concat(["L", X, y + height - gutter, "L", x + gutter + ((valuesx[i] || valuesx[0])[0] - minx) * kx, y + height - gutter, "z"]).join(",") });
271 !opts.nostroke && line.attr({ path: path.join(",") });
274 function createColumns(f) {
278 for (var i = 0, ii = valuesx.length; i < ii; i++) {
279 Xs = Xs.concat(valuesx[i]);
283 Xs.sort(function(a,b) { return a - b; });
289 for (i = 0, ii = Xs.length; i < ii; i++) {
290 Xs[i] != Xs[i - 1] && Xs2.push(Xs[i]) && xs.push(x + gutter + (Xs[i] - minx) * kx);
296 var cvrs = f || paper.set();
298 for (i = 0; i < ii; i++) {
299 var X = xs[i] - (xs[i] - (xs[i - 1] || x)) / 2,
300 w = ((xs[i + 1] || x + width) - xs[i]) / 2 + (xs[i] - (xs[i - 1] || x)) / 2,
303 f ? (C = {}) : cvrs.push(C = paper.rect(X - 1, y, Math.max(w + 1, 1), height).attr({ stroke: "none", fill: "#000", opacity: 0 }));
305 C.symbols = paper.set();
310 for (var j = 0, jj = valuesy.length; j < jj; j++) {
311 Xs2 = valuesx[j] || valuesx[0];
313 for (var k = 0, kk = Xs2.length; k < kk; k++) {
314 if (Xs2[k] == Xs[i]) {
315 C.values.push(valuesy[j][k]);
316 C.y.push(y + height - gutter - (valuesy[j][k] - miny) * ky);
317 C.symbols.push(chart.symbols[j][k]);
325 !f && (columns = cvrs);
328 function createDots(f) {
329 var cvrs = f || paper.set(),
332 for (var i = 0, ii = valuesy.length; i < ii; i++) {
333 for (var j = 0, jj = valuesy[i].length; j < jj; j++) {
334 var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx,
335 nearX = x + gutter + ((valuesx[i] || valuesx[0])[j ? j - 1 : 1] - minx) * kx,
336 Y = y + height - gutter - (valuesy[i][j] - miny) * ky;
338 f ? (C = {}) : cvrs.push(C = paper.circle(X, Y, Math.abs(nearX - X) / 2).attr({ stroke: "none", fill: "#000", opacity: 0 }));
341 C.value = valuesy[i][j];
342 C.line = chart.lines[i];
343 C.shade = chart.shades[i];
344 C.symbol = chart.symbols[i][j];
345 C.symbols = chart.symbols[i];
346 C.axis = (valuesx[i] || valuesx[0])[j];
354 chart.push(lines, shades, symbols, axis, columns, dots);
356 chart.shades = shades;
357 chart.symbols = symbols;
360 chart.hoverColumn = function (fin, fout) {
361 !columns && createColumns();
362 columns.mouseover(fin).mouseout(fout);
366 chart.clickColumn = function (f) {
367 !columns && createColumns();
372 chart.hrefColumn = function (cols) {
373 var hrefs = paper.raphael.is(arguments[0], "array") ? arguments[0] : arguments;
375 if (!(arguments.length - 1) && typeof cols == "object") {
376 for (var x in cols) {
377 for (var i = 0, ii = columns.length; i < ii; i++) if (columns[i].axis == x) {
378 columns[i].attr("href", cols[x]);
383 !columns && createColumns();
385 for (i = 0, ii = hrefs.length; i < ii; i++) {
386 columns[i] && columns[i].attr("href", hrefs[i]);
392 chart.hover = function (fin, fout) {
393 !dots && createDots();
394 dots.mouseover(fin).mouseout(fout);
398 chart.click = function (f) {
399 !dots && createDots();
404 chart.each = function (f) {
409 chart.eachColumn = function (f) {
418 var F = function() {};
419 F.prototype = Raphael.g;
420 Linechart.prototype = new F;
423 Raphael.fn.linechart = function(x, y, width, height, valuesx, valuesy, opts) {
424 return new Linechart(this, x, y, width, height, valuesx, valuesy, opts);
431 Raphael.fn.drawGrid = function (x, y, w, h, wv, hv, color) {
432 color = color || "#000";
437 Math.round(x + w) + .5,
439 Math.round(x + w) + .5,
440 Math.round(y + h) + .5,
442 Math.round(y + h) + .5,
448 //var columnWidth = w / wv;
449 var columnWidth = w / wv;
451 for (var i = 1; i < hv; i++) {
452 path = path.concat(["M", Math.round(x) + .5, Math.round(y + i * rowHeight) + .5, "H", Math.round(x + w) + .5]);
454 for (i = 1; i < wv; i++) {
455 path = path.concat(["M", Math.round(x + i * columnWidth) + .5, Math.round(y) + .5, "V", Math.round(y + h) + .5]);
457 return this.path(path.join(",")).attr({stroke: color});