d3-axis.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // https://d3js.org/d3-axis/ v3.0.0 Copyright 2010-2021 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7. function identity(x) {
  8. return x;
  9. }
  10. var top = 1,
  11. right = 2,
  12. bottom = 3,
  13. left = 4,
  14. epsilon = 1e-6;
  15. function translateX(x) {
  16. return "translate(" + x + ",0)";
  17. }
  18. function translateY(y) {
  19. return "translate(0," + y + ")";
  20. }
  21. function number(scale) {
  22. return d => +scale(d);
  23. }
  24. function center(scale, offset) {
  25. offset = Math.max(0, scale.bandwidth() - offset * 2) / 2;
  26. if (scale.round()) offset = Math.round(offset);
  27. return d => +scale(d) + offset;
  28. }
  29. function entering() {
  30. return !this.__axis;
  31. }
  32. function axis(orient, scale) {
  33. var tickArguments = [],
  34. tickValues = null,
  35. tickFormat = null,
  36. tickSizeInner = 6,
  37. tickSizeOuter = 6,
  38. tickPadding = 3,
  39. offset = typeof window !== "undefined" && window.devicePixelRatio > 1 ? 0 : 0.5,
  40. k = orient === top || orient === left ? -1 : 1,
  41. x = orient === left || orient === right ? "x" : "y",
  42. transform = orient === top || orient === bottom ? translateX : translateY;
  43. function axis(context) {
  44. var values = tickValues == null ? (scale.ticks ? scale.ticks.apply(scale, tickArguments) : scale.domain()) : tickValues,
  45. format = tickFormat == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments) : identity) : tickFormat,
  46. spacing = Math.max(tickSizeInner, 0) + tickPadding,
  47. range = scale.range(),
  48. range0 = +range[0] + offset,
  49. range1 = +range[range.length - 1] + offset,
  50. position = (scale.bandwidth ? center : number)(scale.copy(), offset),
  51. selection = context.selection ? context.selection() : context,
  52. path = selection.selectAll(".domain").data([null]),
  53. tick = selection.selectAll(".tick").data(values, scale).order(),
  54. tickExit = tick.exit(),
  55. tickEnter = tick.enter().append("g").attr("class", "tick"),
  56. line = tick.select("line"),
  57. text = tick.select("text");
  58. path = path.merge(path.enter().insert("path", ".tick")
  59. .attr("class", "domain")
  60. .attr("stroke", "currentColor"));
  61. tick = tick.merge(tickEnter);
  62. line = line.merge(tickEnter.append("line")
  63. .attr("stroke", "currentColor")
  64. .attr(x + "2", k * tickSizeInner));
  65. text = text.merge(tickEnter.append("text")
  66. .attr("fill", "currentColor")
  67. .attr(x, k * spacing)
  68. .attr("dy", orient === top ? "0em" : orient === bottom ? "0.71em" : "0.32em"));
  69. if (context !== selection) {
  70. path = path.transition(context);
  71. tick = tick.transition(context);
  72. line = line.transition(context);
  73. text = text.transition(context);
  74. tickExit = tickExit.transition(context)
  75. .attr("opacity", epsilon)
  76. .attr("transform", function(d) { return isFinite(d = position(d)) ? transform(d + offset) : this.getAttribute("transform"); });
  77. tickEnter
  78. .attr("opacity", epsilon)
  79. .attr("transform", function(d) { var p = this.parentNode.__axis; return transform((p && isFinite(p = p(d)) ? p : position(d)) + offset); });
  80. }
  81. tickExit.remove();
  82. path
  83. .attr("d", orient === left || orient === right
  84. ? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H" + offset + "V" + range1 + "H" + k * tickSizeOuter : "M" + offset + "," + range0 + "V" + range1)
  85. : (tickSizeOuter ? "M" + range0 + "," + k * tickSizeOuter + "V" + offset + "H" + range1 + "V" + k * tickSizeOuter : "M" + range0 + "," + offset + "H" + range1));
  86. tick
  87. .attr("opacity", 1)
  88. .attr("transform", function(d) { return transform(position(d) + offset); });
  89. line
  90. .attr(x + "2", k * tickSizeInner);
  91. text
  92. .attr(x, k * spacing)
  93. .text(format);
  94. selection.filter(entering)
  95. .attr("fill", "none")
  96. .attr("font-size", 10)
  97. .attr("font-family", "sans-serif")
  98. .attr("text-anchor", orient === right ? "start" : orient === left ? "end" : "middle");
  99. selection
  100. .each(function() { this.__axis = position; });
  101. }
  102. axis.scale = function(_) {
  103. return arguments.length ? (scale = _, axis) : scale;
  104. };
  105. axis.ticks = function() {
  106. return tickArguments = Array.from(arguments), axis;
  107. };
  108. axis.tickArguments = function(_) {
  109. return arguments.length ? (tickArguments = _ == null ? [] : Array.from(_), axis) : tickArguments.slice();
  110. };
  111. axis.tickValues = function(_) {
  112. return arguments.length ? (tickValues = _ == null ? null : Array.from(_), axis) : tickValues && tickValues.slice();
  113. };
  114. axis.tickFormat = function(_) {
  115. return arguments.length ? (tickFormat = _, axis) : tickFormat;
  116. };
  117. axis.tickSize = function(_) {
  118. return arguments.length ? (tickSizeInner = tickSizeOuter = +_, axis) : tickSizeInner;
  119. };
  120. axis.tickSizeInner = function(_) {
  121. return arguments.length ? (tickSizeInner = +_, axis) : tickSizeInner;
  122. };
  123. axis.tickSizeOuter = function(_) {
  124. return arguments.length ? (tickSizeOuter = +_, axis) : tickSizeOuter;
  125. };
  126. axis.tickPadding = function(_) {
  127. return arguments.length ? (tickPadding = +_, axis) : tickPadding;
  128. };
  129. axis.offset = function(_) {
  130. return arguments.length ? (offset = +_, axis) : offset;
  131. };
  132. return axis;
  133. }
  134. function axisTop(scale) {
  135. return axis(top, scale);
  136. }
  137. function axisRight(scale) {
  138. return axis(right, scale);
  139. }
  140. function axisBottom(scale) {
  141. return axis(bottom, scale);
  142. }
  143. function axisLeft(scale) {
  144. return axis(left, scale);
  145. }
  146. exports.axisBottom = axisBottom;
  147. exports.axisLeft = axisLeft;
  148. exports.axisRight = axisRight;
  149. exports.axisTop = axisTop;
  150. Object.defineProperty(exports, '__esModule', { value: true });
  151. })));