All files / src/objects/interfaces/graph LineGraph.tsx

0% Statements 0/12
0% Branches 0/4
0% Functions 0/2
0% Lines 0/12

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                                                                                                                 
import { Point } from "./Point";
import { GraphAxis } from "./GraphAxis";
import { getRangeTotal } from "../number/Range";
import { Size } from "../number/Size";
 
export interface Line {
  label: string;
  data: Point[];
  lineColor?: string;
  pointColor?: string;
}
 
export interface LineGraph {
  lines: Line[];
  x_axis: GraphAxis;
  y_axis: GraphAxis;
  title: string;
}
 
export const scalePointOnAxis = (
  val: number,
  axis: GraphAxis,
  max_width: number
) => {
  return ((val - axis.range.min) / getRangeTotal(axis.range)) * max_width;
};
 
export const scaleGraphPoints = (
  graph: LineGraph,
  size: Size,
  offset?: Point
) => {
  const offset_x = offset !== undefined ? offset.x : 0;
  const offset_y = offset !== undefined ? offset.y : 0;
  // console.log(graph.lines);
  var new_lines: Line[] = [];
  for (let line of graph.lines) {
    var points: Point[] = [];
    for (let point of line.data) {
      points.push({
        x:
          scalePointOnAxis(point.x, graph.x_axis, size.width - offset_x * 2) +
          offset_x,
        y:
          size.height -
          (scalePointOnAxis(point.y, graph.y_axis, size.height - offset_y * 2) +
            offset_y),
      });
    }
    new_lines.push({
      ...line,
      data: points,
    });
  }
  return new_lines;
};