d3-hexbin.v0.2.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // https://github.com/d3/d3-hexbin Version 0.2.2. Copyright 2017 Mike Bostock.
  2. //license:BSD 3-clause "New" or "Revised"
  3. (function (global, factory) {
  4. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  5. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  6. (factory((global.d3 = global.d3 || {})));
  7. }(this, (function (exports) { 'use strict';
  8. var thirdPi = Math.PI / 3;
  9. var angles = [0, thirdPi, 2 * thirdPi, 3 * thirdPi, 4 * thirdPi, 5 * thirdPi];
  10. function pointX(d) {
  11. return d[0];
  12. }
  13. function pointY(d) {
  14. return d[1];
  15. }
  16. var hexbin = function() {
  17. var x0 = 0,
  18. y0 = 0,
  19. x1 = 1,
  20. y1 = 1,
  21. x = pointX,
  22. y = pointY,
  23. r,
  24. dx,
  25. dy;
  26. function hexbin(points) {
  27. var binsById = {}, bins = [], i, n = points.length;
  28. for (i = 0; i < n; ++i) {
  29. if (isNaN(px = +x.call(null, point = points[i], i, points))
  30. || isNaN(py = +y.call(null, point, i, points))) continue;
  31. var point,
  32. px,
  33. py,
  34. pj = Math.round(py = py / dy),
  35. pi = Math.round(px = px / dx - (pj & 1) / 2),
  36. py1 = py - pj;
  37. if (Math.abs(py1) * 3 > 1) {
  38. var px1 = px - pi,
  39. pi2 = pi + (px < pi ? -1 : 1) / 2,
  40. pj2 = pj + (py < pj ? -1 : 1),
  41. px2 = px - pi2,
  42. py2 = py - pj2;
  43. if (px1 * px1 + py1 * py1 > px2 * px2 + py2 * py2) pi = pi2 + (pj & 1 ? 1 : -1) / 2, pj = pj2;
  44. }
  45. var id = pi + "-" + pj, bin = binsById[id];
  46. if (bin) bin.push(point);
  47. else {
  48. bins.push(bin = binsById[id] = [point]);
  49. bin.x = (pi + (pj & 1) / 2) * dx;
  50. bin.y = pj * dy;
  51. }
  52. }
  53. return bins;
  54. }
  55. function hexagon(radius) {
  56. var x0 = 0, y0 = 0;
  57. return angles.map(function(angle) {
  58. var x1 = Math.sin(angle) * radius,
  59. y1 = -Math.cos(angle) * radius,
  60. dx = x1 - x0,
  61. dy = y1 - y0;
  62. x0 = x1, y0 = y1;
  63. return [dx, dy];
  64. });
  65. }
  66. hexbin.hexagon = function(radius) {
  67. return "m" + hexagon(radius == null ? r : +radius).join("l") + "z";
  68. };
  69. hexbin.centers = function() {
  70. var centers = [],
  71. j = Math.round(y0 / dy),
  72. i = Math.round(x0 / dx);
  73. for (var y = j * dy; y < y1 + r; y += dy, ++j) {
  74. for (var x = i * dx + (j & 1) * dx / 2; x < x1 + dx / 2; x += dx) {
  75. centers.push([x, y]);
  76. }
  77. }
  78. return centers;
  79. };
  80. hexbin.mesh = function() {
  81. var fragment = hexagon(r).slice(0, 4).join("l");
  82. return hexbin.centers().map(function(p) { return "M" + p + "m" + fragment; }).join("");
  83. };
  84. hexbin.x = function(_) {
  85. return arguments.length ? (x = _, hexbin) : x;
  86. };
  87. hexbin.y = function(_) {
  88. return arguments.length ? (y = _, hexbin) : y;
  89. };
  90. hexbin.radius = function(_) {
  91. return arguments.length ? (r = +_, dx = r * 2 * Math.sin(thirdPi), dy = r * 1.5, hexbin) : r;
  92. };
  93. hexbin.size = function(_) {
  94. return arguments.length ? (x0 = y0 = 0, x1 = +_[0], y1 = +_[1], hexbin) : [x1 - x0, y1 - y0];
  95. };
  96. hexbin.extent = function(_) {
  97. return arguments.length ? (x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1], hexbin) : [[x0, y0], [x1, y1]];
  98. };
  99. return hexbin.radius(1);
  100. };
  101. exports.hexbin = hexbin;
  102. Object.defineProperty(exports, '__esModule', { value: true });
  103. })));