brushable-genome-axis.ts 1.93 KB
Newer Older
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
import { BrushableAxisData, GenericAxisData } from "../../types";
import { select, Selection } from "d3-selection";
import { scaleLinear } from "d3-scale";
import GenomeAxis from "./genome-axis";
import { brushX, BrushBehavior } from "d3-brush";


export default function () {
  const genomeAxisComponent = GenomeAxis();
  function globalGenomeAxis(
    _selection: Selection<SVGElement, BrushableAxisData, SVGElement, any>,
    width: number,
    yPosition: number
  ) {
    _selection.each(function (_data) {
      const xScale = scaleLinear()
        .domain(_data.interval)
        .range([0, width]);

      const brush: BrushBehavior<any> = brushX()
        .extent([[0, 0], [width, 50]])
        .on("brush", () => {
          if (_data.eventHandler) {
            _data.eventHandler.brushed(genomeAxisComponent.scale())
          }
        });

      const container = select(this)
        .attr("transform", "translate(0," + yPosition + ")");
      const globalAxis = container
        .selectAll<SVGElement, BrushableAxisData>("g.global-axis-container")
        .data([_data]);

      const globalAxisEnter = globalAxis
        .enter()
        .append<SVGElement>("g")
        .classed("global-axis-container", true);

      globalAxisEnter.append("g")
        .classed("generic-axis", true);


      globalAxisEnter.append("g")
        .attr("transform", "translate(0,25)")
        .classed("brush-selection", true);

      globalAxis.exit().remove();

      const globalAxisUpdate = globalAxis.merge(globalAxisEnter);


      globalAxisUpdate.select<any>("g.brush-selection")
        .call(brush)
        .call(brush.move, [xScale(_data.window[0]), xScale(_data.window[1])]);

      globalAxisUpdate
        .select<SVGElement>("g.generic-axis")
        .datum((d: BrushableAxisData): GenericAxisData => ({ title: d.title, interval: [d.interval[0], d.interval[1]] }))
        .call(genomeAxisComponent, width, yPosition);

    });
  }
  return globalGenomeAxis;
}