g.dot.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. (function () {
  8. var colorValue = function (value, total, s, b) {
  9. return 'hsb(' + [Math.min((1 - value / total) * .4, 1), s || .75, b || .75] + ')';
  10. };
  11. function Dotchart(paper, x, y, width, height, valuesx, valuesy, size, opts) {
  12. var chartinst = this;
  13. function drawAxis(ax) {
  14. +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));
  15. +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));
  16. +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));
  17. +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));
  18. }
  19. opts = opts || {};
  20. var xdim = chartinst.snapEnds(Math.min.apply(Math, valuesx), Math.max.apply(Math, valuesx), valuesx.length - 1),
  21. minx = xdim.from,
  22. maxx = xdim.to,
  23. gutter = opts.gutter || 10,
  24. ydim = chartinst.snapEnds(Math.min.apply(Math, valuesy), Math.max.apply(Math, valuesy), valuesy.length - 1),
  25. miny = ydim.from,
  26. maxy = ydim.to,
  27. len = Math.max(valuesx.length, valuesy.length, size.length),
  28. symbol = paper[opts.symbol] || "circle",
  29. res = paper.set(),
  30. series = paper.set(),
  31. max = opts.max || 100,
  32. top = Math.max.apply(Math, size),
  33. R = [],
  34. k = Math.sqrt(top / Math.PI) * 2 / max;
  35. for (var i = 0; i < len; i++) {
  36. R[i] = Math.min(Math.sqrt(size[i] / Math.PI) * 2 / k, max);
  37. }
  38. gutter = Math.max.apply(Math, R.concat(gutter));
  39. var axis = paper.set(),
  40. maxR = Math.max.apply(Math, R);
  41. if (opts.axis) {
  42. var ax = (opts.axis + "").split(/[,\s]+/);
  43. drawAxis.call(chartinst, ax);
  44. var g = [], b = [];
  45. for (var i = 0, ii = ax.length; i < ii; i++) {
  46. var bb = ax[i].all ? ax[i].all.getBBox()[["height", "width"][i % 2]] : 0;
  47. g[i] = bb + gutter;
  48. b[i] = bb;
  49. }
  50. gutter = Math.max.apply(Math, g.concat(gutter));
  51. for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
  52. ax[i].remove();
  53. ax[i] = 1;
  54. }
  55. drawAxis.call(chartinst, ax);
  56. for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) {
  57. axis.push(ax[i].all);
  58. }
  59. res.axis = axis;
  60. }
  61. var kx = (width - gutter * 2) / ((maxx - minx) || 1),
  62. ky = (height - gutter * 2) / ((maxy - miny) || 1);
  63. for (var i = 0, ii = valuesy.length; i < ii; i++) {
  64. var sym = paper.raphael.is(symbol, "array") ? symbol[i] : symbol,
  65. X = x + gutter + (valuesx[i] - minx) * kx,
  66. Y = y + height - gutter - (valuesy[i] - miny) * ky;
  67. 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" }));
  68. }
  69. var covers = paper.set();
  70. for (var i = 0, ii = valuesy.length; i < ii; i++) {
  71. var X = x + gutter + (valuesx[i] - minx) * kx,
  72. Y = y + height - gutter - (valuesy[i] - miny) * ky;
  73. covers.push(paper.circle(X, Y, maxR).attr(chartinst.shim));
  74. opts.href && opts.href[i] && covers[i].attr({href: opts.href[i]});
  75. covers[i].r = +R[i].toFixed(3);
  76. covers[i].x = +X.toFixed(3);
  77. covers[i].y = +Y.toFixed(3);
  78. covers[i].X = valuesx[i];
  79. covers[i].Y = valuesy[i];
  80. covers[i].value = size[i] || 0;
  81. covers[i].dot = series[i];
  82. }
  83. res.covers = covers;
  84. res.series = series;
  85. res.push(series, axis, covers);
  86. res.hover = function (fin, fout) {
  87. covers.mouseover(fin).mouseout(fout);
  88. return this;
  89. };
  90. res.click = function (f) {
  91. covers.click(f);
  92. return this;
  93. };
  94. res.each = function (f) {
  95. if (!paper.raphael.is(f, "function")) {
  96. return this;
  97. }
  98. for (var i = covers.length; i--;) {
  99. f.call(covers[i]);
  100. }
  101. return this;
  102. };
  103. res.href = function (map) {
  104. var cover;
  105. for (var i = covers.length; i--;) {
  106. cover = covers[i];
  107. if (cover.X == map.x && cover.Y == map.y && cover.value == map.value) {
  108. cover.attr({href: map.href});
  109. }
  110. }
  111. };
  112. return res;
  113. };
  114. //inheritance
  115. var F = function() {};
  116. F.prototype = Raphael.g
  117. Dotchart.prototype = new F;
  118. //public
  119. Raphael.fn.dotchart = function(x, y, width, height, valuesx, valuesy, size, opts) {
  120. return new Dotchart(this, x, y, width, height, valuesx, valuesy, size, opts);
  121. }
  122. })();