d3-axis.js 5.7 KB

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