All files / platform/ui/src/components/LineChart/d3LineChart events.ts

25% Statements 8/32
0% Branches 0/2
0% Functions 0/9
25% Lines 8/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150          34x                           34x                           34x           34x   34x   34x                                                                                                                                   34x                                                           34x                    
import * as d3Shape from 'd3-shape';
import * as d3Zoom from 'd3-zoom';
 
import chart from './chart';
 
const d3ZoomNativeEvents = [
  'wheel.zoom',
  'mousedown.zoom',
  'dblclick.zoom',
  'touchstart.zoom',
  'touchmove.zoom',
  'touchend.zoom touchcancel.zoom',
];
/**
 * Object to override native zoom events.
 * It defines a specific callback for a event name.
 *
 * @type {Object<string, function>} zoomEvents <zoom d3 event name, callback method>
 */
const zoomEvents = {
  'dblclick.zoom': (root, zoom) => {
    _resetZoom(root, zoom);
  },
};
 
/**
 * It resets given root
 *
 * @param {object} root svg element to be reset
 * @param {object} zoom d3 zoom object
 *
 * @modifies {root}
 */
const _resetZoom = (root, zoom) => {
  Iif (root) {
    root.transition().duration(750).call(zoom.transform, d3Zoom.zoomIdentity);
  }
};
 
const DEFAULT_MAX_ZOOM_SCALE = 20;
 
const _zoom = d3Zoom.zoom().scaleExtent([1, DEFAULT_MAX_ZOOM_SCALE]);
 
const _bindZoom = (
  root,
  gX,
  gY,
  xAxisScale,
  yAxisScale,
  xAxisGenerator,
  yAxisGenerator,
  parseXPoint,
  parseYPoint,
  datasets
) => {
  _zoom.on('zoom', _zoomListener); // .filter(zoomEventsFilter);
 
  _removeListeners();
  _setListeners();
 
  function _removeListeners() {
    for (const eventName of d3ZoomNativeEvents) {
      root.on(eventName, null);
    }
  }
 
  function _setListeners() {
    const keys = Object.keys(zoomEvents);
 
    for (const key of keys) {
      root.call(_zoom).on(key, zoomEvents[key].bind(undefined, root, _zoom));
    }
  }
 
  function _zoomListener(event) {
    const transformedXAxisScale = event.transform.rescaleX(xAxisScale);
    const transformedYAxisScale = event.transform.rescaleY(yAxisScale);
 
    chart.axis.scaleGraphics(
      gX,
      gY,
      xAxisGenerator,
      yAxisGenerator,
      transformedXAxisScale,
      transformedYAxisScale
    );
 
    datasets.forEach((dataset, seriesIndex) => {
      const seriesContainer = root.select(`#series_${seriesIndex}`);
      // create line
      const line = d3Shape
        .line()
        .x(parseXPoint(transformedXAxisScale))
        .y(parseYPoint(transformedYAxisScale));
 
      chart.lines.updateNode(seriesContainer, dataset, line);
 
      chart.points.updateNode(
        seriesContainer,
        dataset,
        parseXPoint(transformedXAxisScale),
        parseYPoint(transformedYAxisScale)
      );
    });
  }
 
  return _zoom;
};
 
const bindMouseEvents = (
  root,
  gX,
  gY,
  xAxisScale,
  yAxisScale,
  xAxisGenerator,
  yAxisGenerator,
  parseXPoint,
  parseYPoint,
  datasets
) => {
  Iif (!root) {
    return;
  }
 
  _bindZoom(
    root,
    gX,
    gY,
    xAxisScale,
    yAxisScale,
    xAxisGenerator,
    yAxisGenerator,
    parseXPoint,
    parseYPoint,
    datasets
  );
};
 
const events = {
  bindMouseEvents,
  external: {
    resetZoom: root => {
      _resetZoom(root, _zoom);
    },
  },
};
 
export default events;