Class: Spoom::Coverage::D3::Timeline::RBIs

Inherits:
Stacked show all
Extended by:
T::Sig
Defined in:
lib/spoom/coverage/d3/timeline.rb

Instance Attribute Summary

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Spoom::Coverage::D3::Timeline

#area, header_script, header_style, #points, #x_scale, #x_ticks, #y_scale, #y_ticks

Methods inherited from Base

header_script, header_style, #html

Constructor Details

#initialize(id, snapshots) ⇒ RBIs

Returns a new instance of RBIs.



509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/spoom/coverage/d3/timeline.rb', line 509

def initialize(id, snapshots)
  keys = ["rbis", "files"]
  data = snapshots.map do |snapshot|
    {
      timestamp: snapshot.commit_timestamp,
      commit: snapshot.commit_sha,
      total: snapshot.files,
      values: { files: snapshot.files - snapshot.rbi_files, rbis: snapshot.rbi_files },
    }
  end
  super(id, data, keys)
end

Instance Method Details

#line(y:, color: "strictnessColor(d.key)", curve: "curveCatmullRom.alpha(1)") ⇒ Object



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/spoom/coverage/d3/timeline.rb', line 577

def line(y:, color: "strictnessColor(d.key)", curve: "curveCatmullRom.alpha(1)")
  <<~JS
    var area_#{id} = d3.area()
      .x((d) => xScale_#{id}(parseDate(#{y})))
      .y0((d) => yScale_#{id}(d[0]))
      .y1((d) => yScale_#{id}(d[1]))
      .curve(d3.#{curve});

    var layer = svg_#{id}.selectAll(".layer")
      .data(layers_#{id})
      .enter().append("g")
        .attr("class", "layer")

    layer.append("path")
      .attr("class", "area")
      .attr("d", area_#{id})
      .attr("fill", (d) => #{color})

    layer.append("path")
      .attr("class", "line")
      .attr("d", d3.line()
        .x((d) => xScale_#{id}(parseDate(#{y})))
        .y((d, i) => yScale_#{id}(d[1]))
        .curve(d3.#{curve}))
      .attr("stroke", (d) => #{color})

    svg_#{id}.selectAll("circle")
      .data(points_#{id})
      .enter()
        .append("circle")
        .attr("class", "dot")
        .attr("cx", (d) => xScale_#{id}(parseDate(#{y})))
        .attr("cy", (d, i) => yScale_#{id}(d[1]))
        .on("mouseover", (d) => tooltip.style("opacity", 1))
        .on("mousemove", tooltip_#{id})
        .on("mouseleave", (d) => tooltip.style("opacity", 0));
  JS
end

#plotObject



617
618
619
620
621
622
623
624
625
# File 'lib/spoom/coverage/d3/timeline.rb', line 617

def plot
  <<~JS
    #{x_scale}
    #{y_scale(min: "0", max: "d3.max(data_#{id}, (d) => d.total + 10)", ticks: "tickValues([0, 25, 50, 75, 100])")}
    #{line(y: "d.data.timestamp", color: "d.key == 'rbis' ? '#8673ff' : '#007bff'")}
    #{x_ticks}
    #{y_ticks(ticks: "tickValues([25, 50, 75])", format: "d", padding: 20)}
  JS
end

#scriptObject



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/spoom/coverage/d3/timeline.rb', line 537

def script
  <<~JS
    #{tooltip}

    var data_#{id} = #{@data.to_json};
    var keys_#{id} = #{T.unsafe(@keys).to_json};

    var stack_#{id} = d3.stack()
      .keys(keys_#{id})
      .value((d, key) => d.values[key]);

    var layers_#{id} = stack_#{id}(data_#{id});

    var points_#{id} = []
    layers_#{id}.forEach(function(d) {
      d.forEach(function(p) {
        p.key = d.key
        points_#{id}.push(p);
      });
    })

    function draw_#{id}() {
      var width_#{id} = document.getElementById("#{id}").clientWidth;
      var height_#{id} = 200;

      d3.select("##{id}").selectAll("*").remove()

      var svg_#{id} = d3.select("##{id}")
        .attr("width", width_#{id})
        .attr("height", height_#{id});

      #{plot}
    }

    draw_#{id}();
    window.addEventListener("resize", draw_#{id});
  JS
end

#tooltipObject



523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/spoom/coverage/d3/timeline.rb', line 523

def tooltip
  <<~JS
    function tooltip_#{id}(d) {
      moveTooltip(d)
        .html("commit <b>" + d.data.commit + "</b><br>"
          + d3.timeFormat("%y/%m/%d")(parseDate(d.data.timestamp)) + "<br><br>"
          + "Files: <b>" + d.data.values.files + "</b><br>"
          + "RBIs: <b>" + d.data.values.rbis + "</b><br><br>"
          + "Total: <b>" + d.data.total + "</b>")
    }
  JS
end