All files / publisher/pages/Metrics/metrics/graphs/activeDevicesGraph rendering.ts

54.23% Statements 32/59
59.09% Branches 13/22
50% Functions 7/14
54.23% Lines 32/59

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 151 152 153 154 155 156 157 158 159 160 161 162          4x 4x   4x     4x                 4x 62x 62x 52x   10x 10x   10x   10x 10x 6x   10x             4x 4x   4x     4x     4x     24x 24x 8x 8x       8x   16x           4x   4x 4x     4x             2x                                                       4x                                                                                                        
import { utcFormat } from "d3-time-format";
import { select } from "d3-selection";
 
function renderXAxis(this: any) {
  // Add the x axis
  const freq = 7;
  let xAxis = this.g.selectAll(".axis.axis--x");
 
  Iif (xAxis.size() > 0) {
    xAxis.call(this.xAxis.tickFormat(utcFormat(this.xAxisTickFormat)));
  } else {
    xAxis = this.g
      .append("g")
      .attr("class", "axis axis--x")
      .attr("transform", `translate(0, ${this.height})`)
      .call(this.xAxis.tickFormat(utcFormat(this.xAxisTickFormat)));
  }
 
  let monthCache: string;
 
  xAxis.selectAll(".tick").each((_d: any, i: number, nodes: string | any[]) => {
    const node = select(nodes[i]);
    if (i % freq !== 0 && nodes.length > 7) {
      node.select("text").attr("opacity", "0");
    } else {
      node.classed("active", true);
      node.select("line").attr("transform", "scale(1, 2)");
 
      const text = node.select("text");
 
      const month = text.text().split(/(\s+)/);
      if (month[0] === monthCache) {
        text.text(month[month.length - 1]);
      }
      monthCache = month[0];
    }
  });
}
 
function renderYAxis(this: any) {
  // Add the y axis
  const freq = 5;
  let yAxis = this.g.selectAll(".axis.axis--y");
 
  Iif (yAxis.size() > 0) {
    yAxis.call(this.yAxis);
  } else {
    yAxis = this.g.append("g").attr("class", "axis axis--y").call(this.yAxis);
  }
 
  yAxis
    .selectAll(".tick")
    .each((_d: any, i: number, nodes: { [x: string]: any }) => {
      const node = select(nodes[i]);
      if (i % freq === 0) {
        node.classed("active", true);
        node
          .select("text")
          .attr("opacity", 1)
          .attr("transform", "translate(-13,0)");
        node.select("line").attr("transform", "scale(2.666666, 1)");
      } else {
        node.select("text").attr("opacity", 0);
      }
    });
}
 
function renderArea(this: any) {
  let areaLayer = this.g.selectAll(".layer.data-layer");
 
  Eif (areaLayer.size() === 0) {
    areaLayer = this.g.append("g").attr("class", "layer data-layer");
  }
 
  areaLayer
    .selectAll(".area")
    .data(this.transformedData)
    .enter()
    .append("path")
    .attr("class", "area")
    .attr("pointer-events", "none")
    .attr("fill", (d: { key: any }) => this.colorScale(d.key))
    .attr("d", this.areas);
}
 
function renderLines(this: any) {
  let pathsLayer = this.g.selectAll(".layer.data-layer");
 
  if (pathsLayer.size() === 0) {
    pathsLayer = this.g.append("g").attr("class", "layer data-layer");
  }
 
  const paths = pathsLayer.selectAll(".path").data(this.transformedData);
 
  paths.exit().remove();
 
  paths
    .enter()
    .append("path")
    .attr("class", "path")
    .attr("pointer-events", "none")
    .attr("data-name", (d: { name: any }) => d.name)
    .style("stroke", (d: { name: any }) => this.colorScale(d.name))
    .style("fill", "none")
    .merge(paths)
    .attr("d", (d: any[]) => this.lines(d.values));
}
 
function renderAnnotations(this: any) {
  Iif (this.annotationsData && this.annotationsData.length > 0) {
    this.annotationsData.forEach(
      (annotation: { data: { [x: string]: number }; x: number; y1: any }) => {
        const annotationKey = Object.keys(annotation.data)
          .filter((key) => key !== "date")
          .filter((key) => annotation.data[key] !== 0)[0];
 
        // Add annotations
        const annotationsLayer = this.g
          .append("g")
          .attr("class", "annotationsLayer")
          .attr("id", `category-${annotationKey}`)
          .style("visibility", "hidden");
 
        const lineLayer = annotationsLayer.append("g");
        const textLayer = annotationsLayer.append("g");
 
        lineLayer
          .append("line")
          .attr("class", "annotation-line")
          .attr("transform", `translate(${annotation.x},0)`)
          .attr("y0", 0)
          .attr("y1", annotation.y1)
          .attr("stroke", "#000")
          .attr("style", "pointer-events: none;");
 
        let display_name = annotationKey.split("-").join(" ");
        display_name =
          display_name.substr(0, 1).toUpperCase() + display_name.substring(1);
 
        const text = textLayer
          .append("text")
          .attr("class", "annotation-text")
          .attr("transform", `translate(${annotation.x},10)`)
          .attr("x", 2)
          .style("font-size", "12px")
          .text(`${display_name}`);
 
        const textBox = text._groups[0][0].getBBox();
        const gBox = this.g._groups[0][0].getBBox();
        const textBoxRightEdge = annotation.x + textBox.x + textBox.width;
        if (textBoxRightEdge > gBox.width) {
          text
            .attr("transform", `translate(${annotation.x - textBox.width},10)`)
            .attr("x", 0);
        }
      },
    );
  }
}
 
export { renderXAxis, renderYAxis, renderArea, renderLines, renderAnnotations };