<commit_before>import _ from 'lodash'
import d3 from 'd3'

import browser from 'components/molecules/charts_d3/utils/browser'
import palettes from 'components/molecules/charts_d3/utils/palettes'

import legend from 'components/molecules/charts_d3/renderer/legend'

var DEFAULTS = {
  aspect: 1,
  domain: _.noop,
  margin: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0
  },
  data_format: 'pct',
  color: palettes.orange,
  onClick: _.noop,
  yFormat: d => d3.format(Math.abs(d) < 1 ? '.4f' : 'n')(d),
  name: _.property('properties.name'),
  maxBubbleValue: 5000,
  maxBubbleRadius: 25,
  bubbleLegendRatio: [0.1, 0.5, 1]
}

function MapLegend () {
}
_.extend(MapLegend.prototype, {
  defaults: DEFAULTS,

  initialize: function (el, data, options) {
    options = this._options = _.defaults({}, options, DEFAULTS)
    var margin = options.margin
    var aspect = _.get(options, 'aspect', 1)
    this._width = _.get(options, 'width', el.clientWidth)
    this._height = _.get(options, 'height', this._width / aspect)

    var svg = this._svg = d3.select(el).append('svg')
      .attr({
        'class': 'reds',
        'viewBox': '-300 80 ' + this._width + ' ' + this._height
      })

    if (browser.isIE() || browser.isWkhtmlToPdf()) {
      svg.attr({
        'width': this._width,
        'height': this._height
      })
    }

    var g = svg.append('g')
      .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')')

    g.append('g').attr('class', 'legend')
    svg.append('g').attr('class', 'bubbles')
    svg.select('.bubbles').append('g').attr('class', 'legend')
    svg.append('g').attr('class', 'stripes')
    svg.select('.stripes').append('g').attr('class', 'legend')

    const lineWidth = 10
    const lineHeight = 10
    const lineInterval = 5

    const defs = svg.append('defs')

    const pattern = defs.append('pattern')
      .attr({
        'id': 'stripe',
        'patternUnits': 'userSpaceOnUse',
        'width': lineWidth,
        'height': lineHeight
      })

    pattern.append('line').attr({'x1': 0, 'y1': 0, 'x2': -lineInterval, 'y2': lineHeight})
    pattern.append('line').attr({'x1': lineInterval, 'y1': 0, 'x2': 0, 'y2': lineHeight})
    pattern.append('line').attr({'x1': 2 * lineInterval, 'y1': 0, 'x2': lineInterval, 'y2': lineHeight})
    pattern.selectAll('line')
      .style('stroke', '#cccccc')
      .attr({'stroke-linecap': 'square', 'stroke-linejoin': 'miter', 'stroke-width': 1})

    this.update(data)
  },
  buildTicksFromBounds: function(options) {
    //green/yellow/red pattern for 0, 1, 2
    //legendText[0] = good bound, [1] = middle, [2] = bad bound
    var legendTicks = []
    if (options.data_format === 'bool') {
      legendTicks[1] = 'No'
      legendTicks[0] = 'Yes'
    } else if (options.data_format === 'pct') {
      options.ticks.bad_bound *= 100;
      options.ticks.good_bound *= 100;
      legendTicks[2] = options.ticks.reversed ? options.ticks.good_bound+"%-100%" : "0%-"+options.ticks.bad_bound+"%"
      legendTicks[1] = options.ticks.bad_bound+"%-"+options.ticks.good_bound+"%"
      legendTicks[0] = options.ticks.reversed ? "0%-"+options.ticks.good_bound+"%" : options.ticks.good_bound+"%-100%"
    } else {
      //double check actual data with this logic
      legendTicks[2] = options.ticks.good_bound+"-100"
      legendTicks[1] = options.ticks.bad_bound+"-"+options.ticks.good_bound
      legendTicks[0] = "0-"+options.ticks.bad_bound
    }
    return legendTicks;
  },
  update: function(data, options) {
    options = _.assign(this._options, options)

    const svg = this._svg

    // MAP COLOR LEGEND
    // ---------------------------------------------------------------------------
    if (options.value) {
      const features = _.reject(data, 'properties.isBorder')
      let domain = options.domain(features)

      if (!_.isArray(domain)) {
        domain = d3.extent(features, options.value)
        domain[0] = Math.min(domain[0], 0)
      }

      const colors = options.color.concat().reverse()<commit_msg>fixed logic bug for legend on non-reversed bounds<commit_after>import _ from 'lodash'
import d3 from 'd3'

import browser from 'components/molecules/charts_d3/utils/browser'
import palettes from 'components/molecules/charts_d3/utils/palettes'

import legend from 'components/molecules/charts_d3/renderer/legend'

var DEFAULTS = {
  aspect: 1,
  domain: _.noop,
  margin: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0
  },
  data_format: 'pct',
  color: palettes.orange,
  onClick: _.noop,
  yFormat: d => d3.format(Math.abs(d) < 1 ? '.4f' : 'n')(d),
  name: _.property('properties.name'),
  maxBubbleValue: 5000,
  maxBubbleRadius: 25,
  bubbleLegendRatio: [0.1, 0.5, 1]
}

function MapLegend () {
}
_.extend(MapLegend.prototype, {
  defaults: DEFAULTS,

  initialize: function (el, data, options) {
    options = this._options = _.defaults({}, options, DEFAULTS)
    var margin = options.margin
    var aspect = _.get(options, 'aspect', 1)
    this._width = _.get(options, 'width', el.clientWidth)
    this._height = _.get(options, 'height', this._width / aspect)

    var svg = this._svg = d3.select(el).append('svg')
      .attr({
        'class': 'reds',
        'viewBox': '-300 80 ' + this._width + ' ' + this._height
      })

    if (browser.isIE() || browser.isWkhtmlToPdf()) {
      svg.attr({
        'width': this._width,
        'height': this._height
      })
    }

    var g = svg.append('g')
      .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')')

    g.append('g').attr('class', 'legend')
    svg.append('g').attr('class', 'bubbles')
    svg.select('.bubbles').append('g').attr('class', 'legend')
    svg.append('g').attr('class', 'stripes')
    svg.select('.stripes').append('g').attr('class', 'legend')

    const lineWidth = 10
    const lineHeight = 10
    const lineInterval = 5

    const defs = svg.append('defs')

    const pattern = defs.append('pattern')
      .attr({
        'id': 'stripe',
        'patternUnits': 'userSpaceOnUse',
        'width': lineWidth,
        'height': lineHeight
      })

    pattern.append('line').attr({'x1': 0, 'y1': 0, 'x2': -lineInterval, 'y2': lineHeight})
    pattern.append('line').attr({'x1': lineInterval, 'y1': 0, 'x2': 0, 'y2': lineHeight})
    pattern.append('line').attr({'x1': 2 * lineInterval, 'y1': 0, 'x2': lineInterval, 'y2': lineHeight})
    pattern.selectAll('line')
      .style('stroke', '#cccccc')
      .attr({'stroke-linecap': 'square', 'stroke-linejoin': 'miter', 'stroke-width': 1})

    this.update(data)
  },
  buildTicksFromBounds: function(options) {
    //green/yellow/red pattern for 0, 1, 2
    //legendText[0] = good bound, [1] = middle, [2] = bad bound
    let legendTicks = []
    if (options.data_format === 'bool') {
      legendTicks[1] = 'No'
      legendTicks[0] = 'Yes'
    } else if (options.data_format === 'pct') {
      options.ticks.bad_bound *= 100;
      options.ticks.good_bound *= 100;
      legendTicks[1] = options.ticks.bad_bound+"%-"+options.ticks.good_bound+"%"
      if (options.ticks.reversed){
        legendTicks[0] = "0%-"+options.ticks.bad_bound+"%"
        legendTicks[2] = options.ticks.good_bound+"%-100%"
      } else {
        legendTicks[2] = "0%-"+options.ticks.bad_bound+"%"
        legendTicks[0] = options.ticks.good_bound+"%-100%"
      }
    } else {
      //double check actual data with this logic
      legendTicks[2] = options.ticks.good_bound+"-100"
      legendTicks[1] = options.ticks.bad_bound+"-"+options.ticks.good_bound
      legendTicks[0] = "0-"+options.ticks.bad_bound
    }
    return legendTicks;
  },
  update: function(data, options) {
    options = _.assign(this._options, options)

    const svg = this._svg

    // MAP COLOR LEGEND
    // ---------------------------------------------------------------------------
    if (options.value) {
      const features = _.reject(data, 'properties.isBorder')
      let domain = options.domain(features)

      if (!_.isArray(domain)) {
        domain = d3.extent(features, options.value)
        domain[0] = Math.min(domain[0], 0)
      }

      const colors = options.color.concat().reverse()
{'fraction_non_alphanumeric': 0.19342573285907028, 'fraction_numerical': 0.02256837973751993, 'mean_word_length': 2.8407913330193124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6723608', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2764, 'n_words': 559}