d3-linear.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.D3Linear = void 0;
  4. function d3Linear(cfg) {
  5. var min = cfg.min, max = cfg.max, nice = cfg.nice, tickCount = cfg.tickCount;
  6. var linear = new D3Linear();
  7. linear.domain([min, max]);
  8. if (nice) {
  9. linear.nice(tickCount);
  10. }
  11. return linear.ticks(tickCount);
  12. }
  13. exports.default = d3Linear;
  14. var DEFAULT_COUNT = 5;
  15. var e10 = Math.sqrt(50);
  16. var e5 = Math.sqrt(10);
  17. var e2 = Math.sqrt(2);
  18. // https://github.com/d3/d3-scale
  19. var D3Linear = /** @class */ (function () {
  20. function D3Linear() {
  21. this._domain = [0, 1];
  22. }
  23. D3Linear.prototype.domain = function (domain) {
  24. if (domain) {
  25. this._domain = Array.from(domain, Number);
  26. return this;
  27. }
  28. return this._domain.slice();
  29. };
  30. D3Linear.prototype.nice = function (count) {
  31. var _a, _b;
  32. if (count === void 0) { count = DEFAULT_COUNT; }
  33. var d = this._domain.slice();
  34. var i0 = 0;
  35. var i1 = this._domain.length - 1;
  36. var start = this._domain[i0];
  37. var stop = this._domain[i1];
  38. var step;
  39. if (stop < start) {
  40. _a = [stop, start], start = _a[0], stop = _a[1];
  41. _b = [i1, i0], i0 = _b[0], i1 = _b[1];
  42. }
  43. step = tickIncrement(start, stop, count);
  44. if (step > 0) {
  45. start = Math.floor(start / step) * step;
  46. stop = Math.ceil(stop / step) * step;
  47. step = tickIncrement(start, stop, count);
  48. }
  49. else if (step < 0) {
  50. start = Math.ceil(start * step) / step;
  51. stop = Math.floor(stop * step) / step;
  52. step = tickIncrement(start, stop, count);
  53. }
  54. if (step > 0) {
  55. d[i0] = Math.floor(start / step) * step;
  56. d[i1] = Math.ceil(stop / step) * step;
  57. this.domain(d);
  58. }
  59. else if (step < 0) {
  60. d[i0] = Math.ceil(start * step) / step;
  61. d[i1] = Math.floor(stop * step) / step;
  62. this.domain(d);
  63. }
  64. return this;
  65. };
  66. D3Linear.prototype.ticks = function (count) {
  67. if (count === void 0) { count = DEFAULT_COUNT; }
  68. return d3ArrayTicks(this._domain[0], this._domain[this._domain.length - 1], count || DEFAULT_COUNT);
  69. };
  70. return D3Linear;
  71. }());
  72. exports.D3Linear = D3Linear;
  73. function d3ArrayTicks(start, stop, count) {
  74. var reverse;
  75. var i = -1;
  76. var n;
  77. var ticks;
  78. var step;
  79. (stop = +stop), (start = +start), (count = +count);
  80. if (start === stop && count > 0) {
  81. return [start];
  82. }
  83. // tslint:disable-next-line
  84. if ((reverse = stop < start)) {
  85. (n = start), (start = stop), (stop = n);
  86. }
  87. // tslint:disable-next-line
  88. if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) {
  89. return [];
  90. }
  91. if (step > 0) {
  92. start = Math.ceil(start / step);
  93. stop = Math.floor(stop / step);
  94. ticks = new Array((n = Math.ceil(stop - start + 1)));
  95. while (++i < n) {
  96. ticks[i] = (start + i) * step;
  97. }
  98. }
  99. else {
  100. start = Math.floor(start * step);
  101. stop = Math.ceil(stop * step);
  102. ticks = new Array((n = Math.ceil(start - stop + 1)));
  103. while (++i < n) {
  104. ticks[i] = (start - i) / step;
  105. }
  106. }
  107. if (reverse) {
  108. ticks.reverse();
  109. }
  110. return ticks;
  111. }
  112. function tickIncrement(start, stop, count) {
  113. var step = (stop - start) / Math.max(0, count);
  114. var power = Math.floor(Math.log(step) / Math.LN10);
  115. var error = step / Math.pow(10, power);
  116. return power >= 0
  117. ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
  118. : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
  119. }
  120. //# sourceMappingURL=d3-linear.js.map