ticks.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { __assign, __read, __spreadArray } from "tslib";
  2. import { isFunction, memoize } from '@antv/util';
  3. import { fadeOut, onAnimateFinished, transition, } from '../../../animation';
  4. import { getCallbackValue, select, splitStyle, subStyleProps } from '../../../util';
  5. import { CLASS_NAMES } from '../constant';
  6. import { getValuePos } from './line';
  7. import { filterExec, getCallbackStyle, getDirectionVector } from './utils';
  8. export function getTickVector(value, attr) {
  9. return getDirectionVector(value, attr.tickDirection, attr);
  10. }
  11. export var getTickPoints = memoize(function (unitVector, tickLength) {
  12. var _a = __read(unitVector, 2), dx = _a[0], dy = _a[1];
  13. return [
  14. [0, 0],
  15. [dx * tickLength, dy * tickLength],
  16. ];
  17. }, function (unitVector, tickLength) { return __spreadArray(__spreadArray([], __read(unitVector), false), [tickLength], false).join(); });
  18. function getTickLineLayout(datum, index, data, tickVector, attr) {
  19. var tickLength = attr.tickLength;
  20. var _a = __read(getTickPoints(tickVector, getCallbackValue(tickLength, [datum, index, data])), 2), _b = __read(_a[0], 2), x1 = _b[0], y1 = _b[1], _c = __read(_a[1], 2), x2 = _c[0], y2 = _c[1];
  21. return { x1: x1, x2: x2, y1: y1, y2: y2 };
  22. }
  23. function createTickEl(container, datum, index, data, attr) {
  24. var formatter = attr.tickFormatter;
  25. var tickVector = getTickVector(datum.value, attr);
  26. var el = 'line';
  27. if (isFunction(formatter))
  28. el = function () { return getCallbackValue(formatter, [datum, index, data, tickVector]); };
  29. return container.append(el).attr('className', CLASS_NAMES.tickItem.name);
  30. }
  31. function applyTickStyle(datum, index, data, tick, group, attr, style) {
  32. var tickVector = getTickVector(datum.value, attr);
  33. var _a = getTickLineLayout(datum, index, data, tickVector, attr), x1 = _a.x1, x2 = _a.x2, y1 = _a.y1, y2 = _a.y2;
  34. var _b = __read(splitStyle(getCallbackStyle(style, [datum, index, data, tickVector])), 2), tickStyle = _b[0], groupStyle = _b[1];
  35. tick.node().nodeName === 'line' && tick.styles(__assign({ x1: x1, x2: x2, y1: y1, y2: y2 }, tickStyle));
  36. group.attr(groupStyle);
  37. tick.styles(tickStyle);
  38. }
  39. function createTick(datum, index, data, attr, tickAttr, animate) {
  40. var tick = createTickEl(select(this), datum, index, data, attr);
  41. applyTickStyle(datum, index, data, tick, this, attr, tickAttr);
  42. var _a = __read(getValuePos(datum.value, attr), 2), x = _a[0], y = _a[1];
  43. return transition(this, { x: x, y: y }, animate);
  44. }
  45. export function renderTicks(container, axisData, attr, animate) {
  46. var finalData = filterExec(axisData, attr.tickFilter);
  47. var tickAttr = subStyleProps(attr, 'tick');
  48. return container
  49. .selectAll(CLASS_NAMES.tick.class)
  50. .data(finalData, function (d) { return d.id || d.label; })
  51. .join(function (enter) {
  52. return enter
  53. .append('g')
  54. .attr('className', CLASS_NAMES.tick.name)
  55. .transition(function (datum, index) {
  56. return createTick.call(this, datum, index, finalData, attr, tickAttr, false);
  57. });
  58. }, function (update) {
  59. return update.transition(function (datum, index) {
  60. this.removeChildren();
  61. return createTick.call(this, datum, index, finalData, attr, tickAttr, animate.update);
  62. });
  63. }, function (exit) {
  64. return exit.transition(function () {
  65. var _this = this;
  66. var animation = fadeOut(this, animate.exit);
  67. onAnimateFinished(animation, function () { return _this.remove(); });
  68. return animation;
  69. });
  70. })
  71. .transitions();
  72. }
  73. //# sourceMappingURL=ticks.js.map