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

80.88% Statements 55/68
50% Branches 10/20
69.23% Functions 9/13
80.88% Lines 55/68

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                4x 4x     4x 4x   4x                   4x                   4x 4x   4x 4x   4x 4x 4x 4x   4x 4x 4x   4x 4x 4x   4x   4x         4x   62x 62x 62x   4x 20x   4x   4x 4x       4x   4x             4x       4x   4x             4x             4x       4x       4x 4x         4x   4x   4x                                   4x 4x         4x 4x         4x   4x 4x   4x   4x       4x   4x          
import { select } from "d3-selection";
import { format } from "d3-format";
import { area, curveMonotoneX, line } from "d3-shape";
import debounce from "../../../../../../libs/debounce";
import { arraysMerge } from "../../arrays";
 
import {
  prepareStackedData,
  prepareLineData,
  prepareScales,
  prepareAxis,
} from "./dataProcessing";
import {
  renderXAxis,
  renderYAxis,
  renderArea,
  renderLines,
  renderAnnotations,
} from "./rendering";
import { tooltips } from "./tooltips";
 
class ActiveDevicesGraph {
  [x: string]: any;
  constructor(holderSelector: string, rawData: any, options: any) {
    this.holder = document.querySelector(holderSelector);
    Iif (!this.holder) {
      throw new Error(`${holderSelector} does not exist.`);
    }
    this.rawData = rawData;
    this.options = Object.assign({}, options);
 
    this.margin = Object.assign(
      {
        top: 20,
        right: 0,
        bottom: 30,
        left: 50,
      },
      options.margin || {},
    );
 
    this.padding = Object.assign(
      {
        top: 0,
        right: 0,
        bottom: 16,
        left: 16,
      },
      options.padding || {},
    );
 
    this.width;
    this.height;
 
    this.svg = select(`${holderSelector} svg`);
    this.g = undefined;
 
    this.transformedData = undefined;
    this.data = undefined;
    this.keys = undefined;
    this.maxYValue = undefined;
 
    this.xScale = undefined;
    this.yScale = undefined;
    this.colorScale = undefined;
 
    this.xAxis = undefined;
    this.yAxis = undefined;
    this.xAxisTickFormat = undefined;
 
    this.hasRendered = false;
 
    this.lines = line()
      .curve(curveMonotoneX)
      .x((d: any) => this.xScale(d.date))
      .y((d: any) => this.yScale(d.value));
 
    this.areas = area()
      .curve(curveMonotoneX)
      .x((d: any) => this.xScale(d.data.date))
      .y0((d) => this.yScale(d[0]))
      .y1((d) => this.yScale(d[1]));
 
    this.shortValue = (number: number) =>
      number < 1000 ? number : format(".2s")(number);
 
    this._prepareSVG();
 
    Eif (Object.keys(this.rawData).length > 0) {
      this._prepareData();
    }
 
    // Include the tooltips logic, and set the context to this class
    tooltips.call(this);
 
    const resize = debounce(() => {
      if (this.hasRendered) {
        this._prepareSVG()._prepareData().render().enableTooltip();
      }
    }, 100);
 
    // @ts-ignore
    select(window).on("resize", resize);
  }
 
  _prepareSVG() {
    this.svg.selectAll("*").remove();
 
    this.width =
      this.holder.clientWidth -
      this.margin.left -
      this.margin.right -
      this.padding.left -
      this.padding.right;
 
    this.height =
      this.svg.attr("height") -
      this.margin.top -
      this.margin.bottom -
      this.padding.top -
      this.padding.bottom;
 
    this.g = this.svg
      .append("g")
      .attr("transform", `translate(${this.margin.left},${this.margin.top})`);
 
    return this;
  }
 
  _prepareData() {
    if (this.options.stacked) {
      prepareStackedData.call(this);
    } else E{
      prepareLineData.call(this);
    }
 
    prepareScales.call(this);
 
    prepareAxis.call(this);
 
    return this;
  }
 
  updateData(data: any) {
    if (!this.rawData.series) {
      this.rawData = data;
    } else {
      this.rawData.series = this.rawData.series.concat(data.series);
    }
 
    this.rawData.buckets = arraysMerge(data.buckets, this.rawData.buckets);
 
    this._prepareData();
 
    return this;
  }
 
  render() {
    Eif (!this.tooltip) {
      this.tooltip = select(this.holder)
        .append("div")
        .attr("class", "p-tooltip");
    }
 
    if (this.options.area) {
      renderArea.call(this);
    } else E{
      renderLines.call(this);
    }
 
    renderAnnotations.call(this);
 
    renderXAxis.call(this);
    renderYAxis.call(this);
 
    this.hasRendered = true;
 
    return this;
  }
 
  show() {
    this.holder.style.opacity = 1;
 
    return this;
  }
}
 
export { ActiveDevicesGraph };