<commit_before>buckets    = require "../../../../../util/buckets.coffee"
buffer     = require "./buffer.coffee"
fetchValue = require "../../../../../core/fetch/value.coffee"
fontSizes  = require "../../../../../font/sizes.coffee"
textwrap   = require "../../../../../textwrap/textwrap.coffee"

module.exports = (vars, opts) ->

  # Reset margins
  vars.axes.margin = resetMargins vars
  vars.axes.height = vars.height.viz
  vars.axes.width  = vars.width.viz

  # Set ticks, if not previously set
  for axis in ["x","y"]

    if vars[axis].ticks.values is false
      vars[axis].ticks.values = vars[axis].scale.viz.ticks()

    unless vars[axis].ticks.values.length
      values = fetchValue vars, vars.data.viz, vars[axis].value
      values = [values] unless values instanceof Array
      vars[axis].ticks.values = values

    opp = if axis is "x" then "y" else "x"
    if vars[axis].ticks.values.length is 1 or
       (opts.buffer and opts.buffer isnt opp and
       axis is vars.axes.discrete and vars[axis].reset is true)
      buffer vars, axis, opts.buffer
    vars[axis].reset = false

  # Calculate padding for tick labels
  labelPadding vars unless vars.small

  # Create SVG Axes
  for axis in ["x","y"]
    vars[axis].axis.svg = createAxis(vars, axis)

  return

resetMargins = (vars) ->
  if vars.small
    # return
    top:    0
    right:  0
    bottom: 0
    left:   0
  else
    # return
    top:    10
    right:  10
    bottom: 10
    left:   10

labelPadding = (vars) ->

  xDomain = vars.x.scale.viz.domain()
  yDomain = vars.y.scale.viz.domain()

  # Calculate Y axis padding
  yAttrs =
    "font-size":   vars.y.ticks.font.size+"px"
    "font-family": vars.y.ticks.font.family.value
    "font-weight": vars.y.ticks.font.weight
  if vars.y.scale.value is "log"
    yText = vars.y.ticks.values.filter (d) ->
      Math.abs(d).toString().charAt(0) is "1"
    yText = yText.map (d) ->
      n = 10+" "+formatPower Math.round(Math.log(Math.abs(d)) / Math.LN10)
      if d < 0 then "-"+n else n
  else
    yText = vars.y.ticks.values.map (d) ->
      vars.format.value(d, vars.y.value, vars)
  yAxisWidth             = d3.max fontSizes(yText,yAttrs), (d) -> d.width
  yAxisWidth             = Math.round yAxisWidth + vars.labels.padding
  vars.axes.margin.left += yAxisWidth

  yLabel = vars.format.value(vars.y.value, undefined, vars)
  yLabelAttrs =
    "font-family": vars.y.label.font.family.value
    "font-weight": vars.y.label.font.weight
    "font-size":   vars.y.label.font.size+"px"
  vars.y.label.height = fontSizes([yLabel], yLabelAttrs)[0].height
  vars.axes.margin.left += vars.y.label.height
  vars.axes.margin.left += vars.y.label.padding * 2

  vars.axes.width -= (vars.axes.margin.left + vars.axes.margin.right)
  vars.x.scale.viz.range buckets([0,vars.axes.width], xDomain.length)

  # Calculate X axis padding
  xAttrs =
    "font-size":   vars.x.ticks.font.size+"px"
    "font-family": vars.x.ticks.font.family.value
    "font-weight": vars.x.ticks.font.weight
  if vars.x.scale.value is "log"
    xValues = vars.x.ticks.values.filter (d) -> d.toString().charAt(0) is "1"
    xText = xValues.map (d) ->
      10 + " " + formatPower Math.round(Math.log(d) / Math.LN10)
  else
    xValues = vars.x.ticks.values
    if typeof xValues[0] is "string"
      xValues = vars.x.scale.viz.domain().filter (d) ->
        d.indexOf("d3plus_buffer_") != 0
    xText = xValues.map (d) ->
      vars.format.value(d, vars.x.value, vars)

  xSizes      = fontSizes(xText,xAttrs)
  xAxisWidth  = d3.max xSizes, (d) -> d.width
  xAxisHeight = d3.max xSizes, (d) -> d.height
  xMaxWidth   = vars.x.scale.viz(xValues[1]) - vars.x.scale.viz(xValues[0])
  xMaxWidth -= vars.labels.padding * 2
  if xAxisWidth > xMaxWidth and xText.join("").indexOf(" ") > 0
    vars.x.ticks.wrap = true
    xSizes = fontSizes xText, xAttrs,
      mod: (elem) ->
        textwrap().container d3.select(elem)<commit_msg>catch for x axis labels where only 1 is present<commit_after>buckets    = require "../../../../../util/buckets.coffee"
buffer     = require "./buffer.coffee"
fetchValue = require "../../../../../core/fetch/value.coffee"
fontSizes  = require "../../../../../font/sizes.coffee"
textwrap   = require "../../../../../textwrap/textwrap.coffee"

module.exports = (vars, opts) ->

  # Reset margins
  vars.axes.margin = resetMargins vars
  vars.axes.height = vars.height.viz
  vars.axes.width  = vars.width.viz

  # Set ticks, if not previously set
  for axis in ["x","y"]

    if vars[axis].ticks.values is false
      vars[axis].ticks.values = vars[axis].scale.viz.ticks()

    unless vars[axis].ticks.values.length
      values = fetchValue vars, vars.data.viz, vars[axis].value
      values = [values] unless values instanceof Array
      vars[axis].ticks.values = values

    opp = if axis is "x" then "y" else "x"
    if vars[axis].ticks.values.length is 1 or
       (opts.buffer and opts.buffer isnt opp and
       axis is vars.axes.discrete and vars[axis].reset is true)
      buffer vars, axis, opts.buffer
    vars[axis].reset = false

  # Calculate padding for tick labels
  labelPadding vars unless vars.small

  # Create SVG Axes
  for axis in ["x","y"]
    vars[axis].axis.svg = createAxis(vars, axis)

  return

resetMargins = (vars) ->
  if vars.small
    # return
    top:    0
    right:  0
    bottom: 0
    left:   0
  else
    # return
    top:    10
    right:  10
    bottom: 10
    left:   10

labelPadding = (vars) ->

  xDomain = vars.x.scale.viz.domain()
  yDomain = vars.y.scale.viz.domain()

  # Calculate Y axis padding
  yAttrs =
    "font-size":   vars.y.ticks.font.size+"px"
    "font-family": vars.y.ticks.font.family.value
    "font-weight": vars.y.ticks.font.weight
  if vars.y.scale.value is "log"
    yText = vars.y.ticks.values.filter (d) ->
      Math.abs(d).toString().charAt(0) is "1"
    yText = yText.map (d) ->
      n = 10+" "+formatPower Math.round(Math.log(Math.abs(d)) / Math.LN10)
      if d < 0 then "-"+n else n
  else
    yText = vars.y.ticks.values.map (d) ->
      vars.format.value(d, vars.y.value, vars)
  yAxisWidth             = d3.max fontSizes(yText,yAttrs), (d) -> d.width
  yAxisWidth             = Math.round yAxisWidth + vars.labels.padding
  vars.axes.margin.left += yAxisWidth

  yLabel = vars.format.value(vars.y.value, undefined, vars)
  yLabelAttrs =
    "font-family": vars.y.label.font.family.value
    "font-weight": vars.y.label.font.weight
    "font-size":   vars.y.label.font.size+"px"
  vars.y.label.height = fontSizes([yLabel], yLabelAttrs)[0].height
  vars.axes.margin.left += vars.y.label.height
  vars.axes.margin.left += vars.y.label.padding * 2

  vars.axes.width -= (vars.axes.margin.left + vars.axes.margin.right)
  vars.x.scale.viz.range buckets([0,vars.axes.width], xDomain.length)

  # Calculate X axis padding
  xAttrs =
    "font-size":   vars.x.ticks.font.size+"px"
    "font-family": vars.x.ticks.font.family.value
    "font-weight": vars.x.ticks.font.weight
  if vars.x.scale.value is "log"
    xValues = vars.x.ticks.values.filter (d) -> d.toString().charAt(0) is "1"
    xText = xValues.map (d) ->
      10 + " " + formatPower Math.round(Math.log(d) / Math.LN10)
  else
    xValues = vars.x.ticks.values
    if typeof xValues[0] is "string"
      xValues = vars.x.scale.viz.domain().filter (d) ->
        d.indexOf("d3plus_buffer_") != 0
    xText = xValues.map (d) ->
      vars.format.value(d, vars.x.value, vars)

  xSizes      = fontSizes(xText,xAttrs)
  xAxisWidth  = d3.max xSizes, (d) -> d.width
  xAxisHeight = d3.max xSizes, (d) -> d.height

  if xValues.length is 1
    xMaxWidth = vars.axes.width
  else
    xMaxWidth   = vars.x.scale.viz(xValues[1]) - vars.x.scale.viz(xValues[0])
    xMaxWidth -= vars.labels.padding * 2

  if xAxisWidth > xMaxWidth and xText.join("").indexOf(" ") > 0
    vars.x.ticks.wrap = true
    xSizes = fontSizes xText, xAttrs,
      mod: (elem) ->
        textwrap().container d3.select(elem)
{'fraction_non_alphanumeric': 0.16007058230400806, 'fraction_numerical': 0.010335265944038316, 'mean_word_length': 2.58887381275441, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 35, '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': '7551205', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2875, 'n_words': 615}