<commit_before>import constants from '../misc/constants'

export default class Legend {
  legend = []
  colorScheme = []
  symbolType = ''

  constructor ({ legend, colorScheme, symbolType }) {
    this.legend = legend
    this.colorScheme = colorScheme
    this.symbolType = symbolType
  }

  // see https://css-tricks.com/snippets/javascript/lighten-darken-color/
  darkenColor (color, amount) {
    let usePound = false

    if (color[0] === '#') {
      color = color.slice(1)
      usePound = true
    }

    const num = parseInt(color, 16)

    let r = (num >> 16) + amount

    if (r > 255) r = 255
    else if (r < 0) r = 0

    let b = ((num >> 8) & 0x00FF) + amount

    if (b > 255) b = 255
    else if (b < 0) b = 0

    let g = (num & 0x0000FF) + amount

    if (g > 255) g = 255
    else if (g < 0) g = 0

    return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16)
  }

  mountTo (node) {
    const symbol = constants.symbol[this.symbolType]

    this.legend.forEach((item, index) => {
      node
        .append('span')
        .classed('text-legend', true)
        .style('color', this.darkenColor(this.colorScheme[index], -10))
        .text(`${symbol} ${item}`)
    })
  }
}
<commit_msg>[legend] create d3 selection if necessary<commit_after>import constants from '../misc/constants'
import { select } from 'd3-selection'

export default class Legend {
  legend = []
  colorScheme = []
  symbolType = ''

  constructor ({ legend, colorScheme, symbolType }) {
    this.legend = legend
    this.colorScheme = colorScheme
    this.symbolType = symbolType
  }

  // see https://css-tricks.com/snippets/javascript/lighten-darken-color/
  darkenColor (color, amount) {
    let usePound = false

    if (color[0] === '#') {
      color = color.slice(1)
      usePound = true
    }

    const num = parseInt(color, 16)

    let r = (num >> 16) + amount

    if (r > 255) r = 255
    else if (r < 0) r = 0

    let b = ((num >> 8) & 0x00FF) + amount

    if (b > 255) b = 255
    else if (b < 0) b = 0

    let g = (num & 0x0000FF) + amount

    if (g > 255) g = 255
    else if (g < 0) g = 0

    return (usePound ? '#' : '') + (g | (b << 8) | (r << 16)).toString(16)
  }

  mountTo (node) {
    const symbol = constants.symbol[this.symbolType]

    // create d3 selection if necessary
    if (typeof node === 'string') node = select(node)

    this.legend.forEach((item, index) => {
      node
        .append('span')
        .classed('text-legend', true)
        .style('color', this.darkenColor(this.colorScheme[index], -10))
        .text(`${symbol} ${item}`)
    })
  }
}

{'fraction_non_alphanumeric': 0.1609681137149443, 'fraction_numerical': 0.0364963503649635, 'mean_word_length': 2.3384615384615386, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '2126145', 'n_tokens_mistral': 1002, 'n_tokens_neox': 886, 'n_words': 242}