<commit_before>/**
 * Graph stuff
 */

import $ from "../../lib/jquery.min";

import {
  MSG_TIME_FATAL, MSG_TIME_DEBUG,
  COLOR_PIE_L1, COLOR_PIE_L2, COLOR_PIE_L3,
  COLOR_PIE_M1, COLOR_PIE_M2, COLOR_PIE_M3,
  COLOR_PIE_S1, COLOR_PIE_S2,
  COLOR_GRAPH_TITLE,
  GRAPH_ZOOM_SPEED, GRAPH_ZOOM_MAX,
  FONT_AXIS_LABEL, FONT_GRAPH_TITLE_LARGE,
  PIE_SMALL_LABEL_OFFSET, PIE_LABEL_INSIDE_RADIUS,
  PIE_LABEL_SWITCH_POINT, PIE_DEPTH,
  PIE_LABEL_RADIUS_START, PIE_LABEL_RADIUS_SCALE,
  PIE_LABEL_SCALE_FACTOR_PRE, PIE_LABEL_SCALE_FACTOR_POST
} from "const";

import { trim } from "misc/misc";
import { formatData } from "misc/format";
import { timeSeriesTicks } from "misc/date";

const pio2 = Math.PI / 2;

export function getTickSize(min, max, numTicks) {
  const minimum = (max - min) / numTicks;
  const magnitude = Math.pow(10, Math.floor(Math.log(minimum) / Math.log(10)));
  const res = minimum / magnitude;
  let tick;

  if (res > 5) {
    tick = 10 * magnitude;
  }
  else if (res > 2) {
    tick = 5 * magnitude;
  }
  else if (res > 1) {
    tick = 2 * magnitude;
  }
  else {
    tick = magnitude;
  }

  return tick;
}

class Graph {
  constructor(options, api, state) {
    this.supported = !!window.CanvasRenderingContext2D;

    this.api = api;
    this.state = state;

    if (!this.supported) {
      this.state.error.newMessage("HTML5 Canvas is not supported! Not drawing graphs", 3, MSG_TIME_FATAL);
    }

    this.width  = options.width;
    this.height = options.height;
    this.$cont  = options.$cont;
    this.title  = options.title;
    this.page   = options.page;

    this.$canvas = $("<canvas></canvas>").attr({
      width:  this.width,
      height: this.height
    });

    this.ctx = this.supported ? this.$canvas[0].getContext("2d") : null;
  }
}

export class LineGraph extends Graph {
  constructor(options, api, state) {
    super(options, api, state);

    this.padX1 = options.pad && options.pad[3] || 0;
    this.padX2 = options.pad && options.pad[1] || 0;
    this.padY1 = options.pad && options.pad[0] || 0;
    this.padY2 = options.pad && options.pad[2] || 0;

    this.originalRange = options.range;
    this.setRange(options.range);

    this.tension = options.tension || 0.5;

    this.fill = options.fill;
    this.stroke = options.stroke || true;

    this.lineWidth = options.lineWidth || 2;

    this.transition = options.transition || [];

    this.$gCont = $("<div></div>")
    .addClass("graph-container")
    .addClass("graph-" + this.title);

    this.$gCont.append(this.$canvas);
    this.$cont.append(this.$gCont);
  }

  setRange(range) {
    this.minX = range[0];
    this.maxX = range[1];
    this.minY = range[2];
    this.maxY = range[3];

    this.setLogRange();
  }
  setLogY() {
    if (this.minY * this.maxY <= 0) {
      // can't log a zero value; range contains zero
      this.state.error.newMessage("Attempted to set log range containing zero!", 0, MSG_TIME_DEBUG);
      return;
    }
    this.log = true;
    this.setLogRange();
  }
  setLogRange() {
    this.minY = Math.max(1, this.minY);
    this.lMinY = this.log ? Math.log(this.minY) : this.minY;
    this.lMaxY = this.log ? Math.log(this.maxY) : this.maxY;
  }
  getTimeScale(offset) {<commit_msg>Only set minimum limit for log graphs<commit_after>/**
 * Graph stuff
 */

import $ from "../../lib/jquery.min";

import {
  MSG_TIME_FATAL, MSG_TIME_DEBUG,
  COLOR_PIE_L1, COLOR_PIE_L2, COLOR_PIE_L3,
  COLOR_PIE_M1, COLOR_PIE_M2, COLOR_PIE_M3,
  COLOR_PIE_S1, COLOR_PIE_S2,
  COLOR_GRAPH_TITLE,
  GRAPH_ZOOM_SPEED, GRAPH_ZOOM_MAX,
  FONT_AXIS_LABEL, FONT_GRAPH_TITLE_LARGE,
  PIE_SMALL_LABEL_OFFSET, PIE_LABEL_INSIDE_RADIUS,
  PIE_LABEL_SWITCH_POINT, PIE_DEPTH,
  PIE_LABEL_RADIUS_START, PIE_LABEL_RADIUS_SCALE,
  PIE_LABEL_SCALE_FACTOR_PRE, PIE_LABEL_SCALE_FACTOR_POST
} from "const";

import { trim } from "misc/misc";
import { formatData } from "misc/format";
import { timeSeriesTicks } from "misc/date";

const pio2 = Math.PI / 2;

export function getTickSize(min, max, numTicks) {
  const minimum = (max - min) / numTicks;
  const magnitude = Math.pow(10, Math.floor(Math.log(minimum) / Math.log(10)));
  const res = minimum / magnitude;
  let tick;

  if (res > 5) {
    tick = 10 * magnitude;
  }
  else if (res > 2) {
    tick = 5 * magnitude;
  }
  else if (res > 1) {
    tick = 2 * magnitude;
  }
  else {
    tick = magnitude;
  }

  return tick;
}

class Graph {
  constructor(options, api, state) {
    this.supported = !!window.CanvasRenderingContext2D;

    this.api = api;
    this.state = state;

    if (!this.supported) {
      this.state.error.newMessage("HTML5 Canvas is not supported! Not drawing graphs", 3, MSG_TIME_FATAL);
    }

    this.width  = options.width;
    this.height = options.height;
    this.$cont  = options.$cont;
    this.title  = options.title;
    this.page   = options.page;

    this.$canvas = $("<canvas></canvas>").attr({
      width:  this.width,
      height: this.height
    });

    this.ctx = this.supported ? this.$canvas[0].getContext("2d") : null;
  }
}

export class LineGraph extends Graph {
  constructor(options, api, state) {
    super(options, api, state);

    this.padX1 = options.pad && options.pad[3] || 0;
    this.padX2 = options.pad && options.pad[1] || 0;
    this.padY1 = options.pad && options.pad[0] || 0;
    this.padY2 = options.pad && options.pad[2] || 0;

    this.originalRange = options.range;
    this.setRange(options.range);

    this.tension = options.tension || 0.5;

    this.fill = options.fill;
    this.stroke = options.stroke || true;

    this.lineWidth = options.lineWidth || 2;

    this.transition = options.transition || [];

    this.$gCont = $("<div></div>")
    .addClass("graph-container")
    .addClass("graph-" + this.title);

    this.$gCont.append(this.$canvas);
    this.$cont.append(this.$gCont);
  }

  setRange(range) {
    this.minX = range[0];
    this.maxX = range[1];
    this.minY = range[2];
    this.maxY = range[3];

    this.setLogRange();
  }
  setLogY() {
    if (this.minY * this.maxY <= 0) {
      // can't log a zero value; range contains zero
      this.state.error.newMessage("Attempted to set log range containing zero!", 0, MSG_TIME_DEBUG);
      return;
    }
    this.log = true;
    this.setLogRange();
  }
  setLogRange() {
    if (this.log) {
      this.minY = Math.max(1, this.minY);
    }
    this.lMinY = this.log ? Math.log(this.minY) : this.minY;
    this.lMaxY = this.log ? Math.log(this.maxY) : this.maxY;
  }
  getTimeScale(offset) {
{'fraction_non_alphanumeric': 0.1455637707948244, 'fraction_numerical': 0.014787430683918669, 'mean_word_length': 3.0785175879396984, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5687349', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2442, 'n_words': 480}