<commit_before>
define [
  "underscore",
  "backbone",
  "common/safebind",
  "common/has_parent",
  "common/plot_widget",
  "renderer/properties",
], (_, Backbone, safebind, HasParent, PlotWidget, Properties) ->

  glyph_properties = Properties.glyph_properties
  line_properties  = Properties.line_properties
  text_properties  = Properties.text_properties

  signum = (x) -> x ? x<0 ? -1:1:0

  _angle_lookup = {
    top:
      parallel   : 0
      normal     : -Math.PI/2
      horizontal : 0
      vertical   : -Math.PI/2
    bottom:
      parallel   : 0
      normal     : Math.PI/2
      horizontal : 0
      vertical   : Math.PI/2
    left:
      parallel   : -Math.PI/2
      normal     : 0
      horizontal : 0
      vertical   : -Math.PI/2
    right:
      parallel   : Math.PI/2
      normal     : 0
      horizontal : 0
      vertical   : Math.PI/2
  }

  _baseline_lookup = {
    top:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'alphabetic'
      vertical   : 'middle'
    bottom:
      parallel   : 'hanging'
      normal     : 'middle'
      horizontal : 'hanging'
      vertical   : 'middle'
    left:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'middle'
      vertical   : 'alphabetic'
    right:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'middle'
      vertical   : 'alphabetic'
  }

  _align_lookup = {
    top:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'center'
      vertical   : 'left'
    bottom:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'center'
      vertical   : 'right'
    left:
      parallel   : 'center'
      normal     : 'right'
      horizontal : 'right'
      vertical   : 'center'
    right:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'left'
      vertical   : 'center'
  }

  _align_lookup_negative = {
    top    : 'right'
    bottom : 'left'
    left   : 'right'
    right  : 'left'
  }

  _align_lookup_positive = {
    top    : 'left'
    bottom : 'right'
    left   : 'right'
    right  : 'left'
  }

  # lookup[axis][idir][jdir][loc]
  _normal_lookup = [
    {
      norm: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': -1, 'max': +1}
      }
      flip: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': -1, 'max': +1}
      }
    },
    {
      norm: {
        norm: {'min': -1, 'max': +1}
        flip: {'min': -1, 'max': +1}
      }
      flip: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': +1, 'max': -1}
      }
    }
  ]

  class AxisView extends PlotWidget
    initialize: (options) ->
      super(options)

      @rule_props = new line_properties(@, null, 'axis_')
      @major_tick_props = new line_properties(@, null, 'major_tick_')
      @major_label_props = new text_properties(@, null, 'major_label_')
      @axis_label_props = new text_properties(@, null, 'axis_label_')

      @formatter = options.formatter

    render: () ->
      ctx = @plot_view.ctx

      ctx.save()

      @_draw_rule(ctx)
      @_draw_major_ticks(ctx)
      @_draw_major_labels(ctx)
      @_draw_axis_label(ctx)

      ctx.restore()

    bind_bokeh_events: () ->
      safebind(this, @model, 'change', @request_render)

    padding_request: () ->
      return @_padding_request()

    _draw_rule: (ctx) ->
      if not @rule_props.do_stroke
        return
      [x, y] = coords = @mget('rule_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')

      @rule_props.set(ctx, @)
      ctx.beginPath()
      ctx.moveTo(Math.round(sx[0]), Math.round(sy[0]))
      for i in [1...sx.length]
        ctx.lineTo(Math.round(sx[i]), Math.round(sy[i]))
      ctx.stroke()

    _draw_major_ticks: (ctx) ->
      if not @major_tick_props.do_stroke
        return
      [x, y] = coords = @mget('major_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')

      tin = @mget('major_tick_in')
      tout = @mget('major_tick_out')
      @major_tick_props.set(ctx, @)
      for i in [0...sx.length]
        ctx.beginPath()
        ctx.moveTo(Math.round(sx[i]+nx*tout), Math.round(sy[i]+ny*tout))
        ctx.lineTo(Math.round(sx[i]-nx*tin),  Math.round(sy[i]-ny*tin))
        ctx.stroke()

    _draw_major_labels: (ctx) ->
      [x, y] = coords = @mget('major_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')
      dim = @mget('dimension')
      side = @mget('side')
      orient = @mget('major_label_orientation')

      if _.isString(orient)
        angle = _angle_lookup[side][orient]
      else
        angle = -orient
      standoff = @_tick_extent() + @mget('major_label_standoff')

      labels = @formatter.format(coords[dim])

      # override baseline and alignment with heuristics for tick labels
      @major_label_props.set(ctx, @)
      @_apply_location_heuristics(ctx, side, orient)

      for i in [0...sx.length]
        if angle
          ctx.translate(sx[i]+nx*standoff, sy[i]+ny*standoff)
          ctx.rotate(angle)
          ctx.fillText(labels[i], 0, 0)
          ctx.rotate(-angle)
          ctx.translate(-sx[i]-nx*standoff, -sy[i]-ny*standoff)
        else
          ctx.fillText(labels[i], Math.round(sx[i] + nx*standoff), Math.round(sy[i] + ny*standoff))

    _draw_axis_label: (ctx) ->
      label = @mget('axis_label')

      if not label?
        return

      [x, y] = @mget('rule_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')
      side = @mget('side')
      orient = 'parallel'

      if @normal_offset != 0
        for i in [0...sx.length]
          sx[i] += nx * @normal_offset
          sy[i] += ny * @normal_offset

      angle = _angle_lookup[side][orient]
      standoff = @_tick_extent() + @_tick_label_extent() + @mget('axis_label_standoff')

      sx = (sx[0] + sx[sx.length-1])/2
      sy = (sy[0] + sy[sy.length-1])/2

      # override baseline and alignment with heuristics for axis labels
      @axis_label_props.set(ctx, @)
      @_apply_location_heuristics(ctx, side, orient)

      if angle
        ctx.translate(sx+nx*standoff, sy+ny*standoff)
        ctx.rotate(angle)
        ctx.fillText(label, 0, 0)
        ctx.rotate(-angle)
        ctx.translate(-sx-nx*standoff, -sy-ny*standoff)
      else<commit_msg>remove old code accidentally in frmo categorical experiments<commit_after>
define [
  "underscore",
  "backbone",
  "common/safebind",
  "common/has_parent",
  "common/plot_widget",
  "renderer/properties",
], (_, Backbone, safebind, HasParent, PlotWidget, Properties) ->

  glyph_properties = Properties.glyph_properties
  line_properties  = Properties.line_properties
  text_properties  = Properties.text_properties

  signum = (x) -> x ? x<0 ? -1:1:0

  _angle_lookup = {
    top:
      parallel   : 0
      normal     : -Math.PI/2
      horizontal : 0
      vertical   : -Math.PI/2
    bottom:
      parallel   : 0
      normal     : Math.PI/2
      horizontal : 0
      vertical   : Math.PI/2
    left:
      parallel   : -Math.PI/2
      normal     : 0
      horizontal : 0
      vertical   : -Math.PI/2
    right:
      parallel   : Math.PI/2
      normal     : 0
      horizontal : 0
      vertical   : Math.PI/2
  }

  _baseline_lookup = {
    top:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'alphabetic'
      vertical   : 'middle'
    bottom:
      parallel   : 'hanging'
      normal     : 'middle'
      horizontal : 'hanging'
      vertical   : 'middle'
    left:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'middle'
      vertical   : 'alphabetic'
    right:
      parallel   : 'alphabetic'
      normal     : 'middle'
      horizontal : 'middle'
      vertical   : 'alphabetic'
  }

  _align_lookup = {
    top:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'center'
      vertical   : 'left'
    bottom:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'center'
      vertical   : 'right'
    left:
      parallel   : 'center'
      normal     : 'right'
      horizontal : 'right'
      vertical   : 'center'
    right:
      parallel   : 'center'
      normal     : 'left'
      horizontal : 'left'
      vertical   : 'center'
  }

  _align_lookup_negative = {
    top    : 'right'
    bottom : 'left'
    left   : 'right'
    right  : 'left'
  }

  _align_lookup_positive = {
    top    : 'left'
    bottom : 'right'
    left   : 'right'
    right  : 'left'
  }

  # lookup[axis][idir][jdir][loc]
  _normal_lookup = [
    {
      norm: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': -1, 'max': +1}
      }
      flip: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': -1, 'max': +1}
      }
    },
    {
      norm: {
        norm: {'min': -1, 'max': +1}
        flip: {'min': -1, 'max': +1}
      }
      flip: {
        norm: {'min': +1, 'max': -1}
        flip: {'min': +1, 'max': -1}
      }
    }
  ]

  class AxisView extends PlotWidget
    initialize: (options) ->
      super(options)

      @rule_props = new line_properties(@, null, 'axis_')
      @major_tick_props = new line_properties(@, null, 'major_tick_')
      @major_label_props = new text_properties(@, null, 'major_label_')
      @axis_label_props = new text_properties(@, null, 'axis_label_')

      @formatter = options.formatter

    render: () ->
      ctx = @plot_view.ctx

      ctx.save()

      @_draw_rule(ctx)
      @_draw_major_ticks(ctx)
      @_draw_major_labels(ctx)
      @_draw_axis_label(ctx)

      ctx.restore()

    bind_bokeh_events: () ->
      safebind(this, @model, 'change', @request_render)

    padding_request: () ->
      return @_padding_request()

    _draw_rule: (ctx) ->
      if not @rule_props.do_stroke
        return
      [x, y] = coords = @mget('rule_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')

      @rule_props.set(ctx, @)
      ctx.beginPath()
      ctx.moveTo(Math.round(sx[0]), Math.round(sy[0]))
      for i in [1...sx.length]
        ctx.lineTo(Math.round(sx[i]), Math.round(sy[i]))
      ctx.stroke()

    _draw_major_ticks: (ctx) ->
      if not @major_tick_props.do_stroke
        return
      [x, y] = coords = @mget('major_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')

      tin = @mget('major_tick_in')
      tout = @mget('major_tick_out')
      @major_tick_props.set(ctx, @)
      for i in [0...sx.length]
        ctx.beginPath()
        ctx.moveTo(Math.round(sx[i]+nx*tout), Math.round(sy[i]+ny*tout))
        ctx.lineTo(Math.round(sx[i]-nx*tin),  Math.round(sy[i]-ny*tin))
        ctx.stroke()

    _draw_major_labels: (ctx) ->
      [x, y] = coords = @mget('major_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')
      dim = @mget('dimension')
      side = @mget('side')
      orient = @mget('major_label_orientation')

      if _.isString(orient)
        angle = _angle_lookup[side][orient]
      else
        angle = -orient
      standoff = @_tick_extent() + @mget('major_label_standoff')

      labels = @formatter.format(coords[dim])

      # override baseline and alignment with heuristics for tick labels
      @major_label_props.set(ctx, @)
      @_apply_location_heuristics(ctx, side, orient)

      for i in [0...sx.length]
        if angle
          ctx.translate(sx[i]+nx*standoff, sy[i]+ny*standoff)
          ctx.rotate(angle)
          ctx.fillText(labels[i], 0, 0)
          ctx.rotate(-angle)
          ctx.translate(-sx[i]-nx*standoff, -sy[i]-ny*standoff)
        else
          ctx.fillText(labels[i], Math.round(sx[i] + nx*standoff), Math.round(sy[i] + ny*standoff))

    _draw_axis_label: (ctx) ->
      label = @mget('axis_label')

      if not label?
        return

      [x, y] = @mget('rule_coords')
      [sx, sy] = @plot_view.map_to_screen(x, "data", y, "data")
      [nx, ny] = @mget('normals')
      side = @mget('side')
      orient = 'parallel'

      angle = _angle_lookup[side][orient]
      standoff = @_tick_extent() + @_tick_label_extent() + @mget('axis_label_standoff')

      sx = (sx[0] + sx[sx.length-1])/2
      sy = (sy[0] + sy[sy.length-1])/2

      # override baseline and alignment with heuristics for axis labels
      @axis_label_props.set(ctx, @)
      @_apply_location_heuristics(ctx, side, orient)

      if angle
        ctx.translate(sx+nx*standoff, sy+ny*standoff)
        ctx.rotate(angle)
        ctx.fillText(label, 0, 0)
        ctx.rotate(-angle)
        ctx.translate(-sx-nx*standoff, -sy-ny*standoff)
      else
{'fraction_non_alphanumeric': 0.15019546520719312, 'fraction_numerical': 0.008131352619233777, 'mean_word_length': 2.1388957055214726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4323395', 'n_tokens_mistral': 4672, 'n_tokens_neox': 4503, 'n_words': 893}