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 var colorValue = function (value, total, s, b) {
9 return 'hsb(' + [Math.min((1 - value / total) * .4, 1), s || .75, b || .75] + ')';
12 function Dotchart(paper, x, y, width, height, valuesx, valuesy, size, opts) {
16 function drawAxis(ax) {
17 +ax[0] && (ax[0] = chartinst.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t", null, paper));
18 +ax[1] && (ax[1] = chartinst.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t", null, paper));
19 +ax[2] && (ax[2] = chartinst.axis(x + gutter, y + height - gutter + maxR, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t", null, paper));
20 +ax[3] && (ax[3] = chartinst.axis(x + gutter - maxR, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t", null, paper));
24 var xdim = chartinst.snapEnds(Math.min.apply(Math, valuesx), Math.max.apply(Math, valuesx), valuesx.length - 1),
27 gutter = opts.gutter || 10,
28 ydim = chartinst.snapEnds(Math.min.apply(Math, valuesy), Math.max.apply(Math, valuesy), valuesy.length - 1),
31 len = Math.max(valuesx.length, valuesy.length, size.length),
32 symbol = paper[opts.symbol] || "circle",
35 max = opts.max || 100,
36 top = Math.max.apply(Math, size),
38 k = Math.sqrt(top / Math.PI) * 2 / max;
40 for (var i = 0; i < len; i++) {
41 R[i] = Math.min(Math.sqrt(size[i] / Math.PI) * 2 / k, max);
44 gutter = Math.max.apply(Math, R.concat(gutter));
46 var axis = paper.set(),
47 maxR = Math.max.apply(Math, R);
50 var ax = (opts.axis + "").split(/[,\s]+/);
52 drawAxis.call(chartinst, ax);
56 for (var i = 0, ii = ax.length; i < ii; i++) {
57 var bb = ax[i].all ? ax[i].all.getBBox()[["height", "width"][i % 2]] : 0;
63 gutter = Math.max.apply(Math, g.concat(gutter));
65 for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
70 drawAxis.call(chartinst, ax);
72 for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
79 var kx = (width - gutter * 2) / ((maxx - minx) || 1),
80 ky = (height - gutter * 2) / ((maxy - miny) || 1);
82 for (var i = 0, ii = valuesy.length; i < ii; i++) {
83 var sym = paper.raphael.is(symbol, "array") ? symbol[i] : symbol,
84 X = x + gutter + (valuesx[i] - minx) * kx,
85 Y = y + height - gutter - (valuesy[i] - miny) * ky;
87 sym && R[i] && series.push(paper[sym](X, Y, R[i]).attr({ fill: opts.heat ? colorValue(R[i], maxR) : chartinst.colors[0], "fill-opacity": opts.opacity ? R[i] / max : 1, stroke: "none" }));
90 var covers = paper.set();
92 for (var i = 0, ii = valuesy.length; i < ii; i++) {
93 var X = x + gutter + (valuesx[i] - minx) * kx,
94 Y = y + height - gutter - (valuesy[i] - miny) * ky;
96 covers.push(paper.circle(X, Y, maxR).attr(chartinst.shim));
97 opts.href && opts.href[i] && covers[i].attr({href: opts.href[i]});
98 covers[i].r = +R[i].toFixed(3);
99 covers[i].x = +X.toFixed(3);
100 covers[i].y = +Y.toFixed(3);
101 covers[i].X = valuesx[i];
102 covers[i].Y = valuesy[i];
103 covers[i].value = size[i] || 0;
104 covers[i].dot = series[i];
109 res.push(series, axis, covers);
111 res.hover = function (fin, fout) {
112 covers.mouseover(fin).mouseout(fout);
116 res.click = function (f) {
121 res.each = function (f) {
122 if (!paper.raphael.is(f, "function")) {
126 for (var i = covers.length; i--;) {
133 res.href = function (map) {
136 for (var i = covers.length; i--;) {
139 if (cover.X == map.x && cover.Y == map.y && cover.value == map.value) {
140 cover.attr({href: map.href});
148 var F = function() {};
149 F.prototype = Raphael.g
150 Dotchart.prototype = new F;
153 Raphael.fn.dotchart = function(x, y, width, height, valuesx, valuesy, size, opts) {
154 return new Dotchart(this, x, y, width, height, valuesx, valuesy, size, opts);