Class: Spoom::Coverage::D3::CircleMap

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/coverage/d3/circle_map.rb

Direct Known Subclasses

Sigils

Defined Under Namespace

Classes: Sigils

Instance Attribute Summary

Attributes inherited from Base

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#html, #initialize, #tooltip

Constructor Details

This class inherits a constructor from Spoom::Coverage::D3::Base

Class Method Details

.header_scriptObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spoom/coverage/d3/circle_map.rb', line 39

def self.header_script
  <<~JS
    function treeHeight(root, height = 0) {
      height += 1;
      if (root.children && root.children.length > 0)
        return Math.max(...root.children.map(child => treeHeight(child, height)));
      else
        return height;
    }

    function tooltipMap(d) {
      moveTooltip(d)
        .html("<b>" + d.data.name + "</b>")
    }
  JS
end

.header_styleObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spoom/coverage/d3/circle_map.rb', line 13

def self.header_style
  <<~CSS
    .node {
      cursor: pointer;
    }

    .node:hover {
      stroke: #333;
      stroke-width: 1px;
    }

    .label.dir {
      fill: #333;
    }

    .label.file {
      font: 12px Arial, sans-serif;
    }

    .node.root, .node.file {
      pointer-events: none;
    }
  CSS
end

Instance Method Details

#scriptObject



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
# File 'lib/spoom/coverage/d3/circle_map.rb', line 57

def script
  <<~JS
    var root = {children: #{@data.to_json}}
    var dataHeight = treeHeight(root)

    var opacity = d3.scaleLinear()
        .domain([0, dataHeight])
        .range([0, 0.2])

    root = d3.hierarchy(root)
        .sum((d) => d.children ? d.children.length : 1)
        .sort((a, b) => b.value - a.value);

    var dirColor = d3.scaleLinear()
      .domain([1, 0])
      .range([strictnessColor("true"), strictnessColor("false")])
      .interpolate(d3.interpolateRgb);

    function redraw() {
      var diameter = document.getElementById("#{id}").clientWidth - 20;
      d3.select("##{id}").selectAll("*").remove()

      var svg_#{id} = d3.select("##{id}")
        .attr("width", diameter)
        .attr("height", diameter)
        .append("g")
          .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");

      var pack = d3.pack()
          .size([diameter, diameter])
          .padding(2);

      var focus = root,
          nodes = pack(root).descendants(),
          view;

      var circle = svg_#{id}.selectAll("circle")
        .data(nodes)
        .enter().append("circle")
          .attr("class", (d) => d.parent ? d.children ? "node" : "node file" : "node root")
          .attr("fill", (d) => d.children ? dirColor(d.data.score) : strictnessColor(d.data.strictness))
          .attr("fill-opacity", (d) => d.children ? opacity(d.depth) : 1)
          .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); })
          .on("mouseover", (d) => tooltip.style("opacity", 1))
          .on("mousemove", tooltipMap)
          .on("mouseleave", (d) => tooltip.style("opacity", 0));

      var text = svg_#{id}.selectAll("text")
        .data(nodes)
        .enter().append("text")
          .attr("class", (d) => d.children ? "label dir" : "label file")
          .attr("fill-opacity", (d) => d.depth <= 1 ? 1 : 0)
          .attr("display", (d) => d.depth <= 1 ? "inline" : "none")
          .text((d) => d.data.name);

      var node = svg_#{id}.selectAll("circle,text");

      function zoom(d) {
        var focus0 = focus; focus = d;

        var transition = d3.transition()
            .duration(d3.event.altKey ? 7500 : 750)
            .tween("zoom", function(d) {
              var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2]);
              return (t) => zoomTo(i(t));
            });

        transition.selectAll("text")
          .filter(function(d) { return d && d.parent === focus || this.style.display === "inline"; })
            .attr("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
            .on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
            .on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
      }

      function zoomTo(v) {
        var k = diameter / v[2]; view = v;
        node.attr("transform", (d) => "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")");
        circle.attr("r", (d) => d.r * k);
      }

      zoomTo([root.x, root.y, root.r * 2]);
      d3.select("##{id}").on("click", () => zoom(root));
    }

    redraw();
    window.addEventListener("resize", redraw);
  JS
end