<commit_before>'use:strict';
var _ = require('underscore')
  , d3 = require('d3')
  , base = require('d3by5-base-chart')
  , horizontal = require('./horizontal-graph')
  , vertical = require('./vertical-graph')
;

module.exports = BarGraph;

/**
 * The entrypoint
 * @return {[type]} [description]
 */
function BarGraph () {

  var chart = {
    // enumish
    ANCHOR_LEFT: 'left',
    ANCHOR_RIGHT: 'right',
    ANCHOR_BOTTOM: 'bottom',
    ANCHOR_TOP: 'top',

    options: {
        padding: 2,
        direction: 'left',
        chartClass: 'chart-bar',
    },

    init: function (selection) {

      if (arguments.length) {
        this.selection = selection;
        this.draw();
      }
      return this;
    },

    draw: function () {
      var that = this
        , graphOptions
        // , data
      ;

      // define the anchoring of the graph
      if (this.options.anchor === this.ANCHOR_LEFT || this.options.anchor === this.ANCHOR_RIGHT) {
        graphOptions = this.horizontalOptions();
      }

      else if (this.options.anchor === this.ANCHOR_BOTTOM || this.options.anchor === this.ANCHOR_TOP) {
        graphOptions = this.verticalOptions();
      }

      // merge graphOptions with the default options
      // fill is not dependednt on direction
      graphOptions.fill = function (d, i) {
                            var _data = that.options.data[i];
                            if (_data && _data.color) {
                              return _data.color;
                            }
                            return that.options.fillColor;
                          };



      this.selection.each(function() {

        var dom = d3.select(this);

        // remove old
        if (dom.select('svg')) {
          dom.select('svg').remove();
        }

        var svg = dom.append('svg')
            .attr('class', 'chart barchart')
            .attr('height', that.options.height)
            .attr('width', that.options.width);

        var bars = svg.selectAll('rect.chart__bar')
            .data(data)
            .enter()
            .append('rect')
            .attr(graphOptions);

      });

      // sets a method to allow redrawing
      this.onAnchorChange = function () {
        this.draw();
      };
    },

    /**
     * Sets the direction of the graph
     * @param  {String} value - the direction used, allowed values = ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_BOTTOM | ANCHOR_TOP
     * @return {Mixed}        - the value or chart
     */
    anchor: function (value) {
      if (!arguments.length) return this.options.anchor;

      value = String(value).toLowerCase();
      // wrong value supplied
      if (value !== this.ANCHOR_LEFT &&
          value !== this.ANCHOR_RIGHT &&
          value !== this.ANCHOR_TOP &&
          value !== this.ANCHOR_BOTTOM) {
        console.error(value, 'is invalid. Only ', this.ANCHOR_LEFT, ', ', this.ANCHOR_RIGHT, ', ', this.ANCHOR_TOP, ' or ', this.ANCHOR_BOTTOM, 'allowed');
        return this;
      }
      this.options.anchor = value;

      // redraw if the graph has been loaded
      if (typeof this.onAnchorChange === 'function') {
        this.draw();
      }

      return this;
    },

  };

  chart = _.extend(chart, vertical);
  chart = _.extend(chart, horizontal);
  chart = _.extend(chart, base);
  return (chart.init());

}<commit_msg>Implement bar options in bar-graph<commit_after>'use:strict';
var _ = require('underscore')
  , d3 = require('d3')
  , base = require('d3by5-base-chart')
  , barOptions = require('./barOptions')
;

module.exports = BarGraph;

/**
 * The entrypoint
 * @return {[type]} [description]
 */
function BarGraph () {

  var chart = {
    // enumish
    ANCHOR_LEFT: 'left',
    ANCHOR_RIGHT: 'right',
    ANCHOR_BOTTOM: 'bottom',
    ANCHOR_TOP: 'top',

    options: {
        padding: 2,
        direction: 'left',
        chartClass: 'chart-bar',
        labelPosition: 'none'
    },

    init: function (selection) {

      if (arguments.length) {
        this.selection = selection;
        this.draw();
      }
      return this;
    },

    draw: function () {
      var that = this
        , barPositions = this.getBarPositions()
        , barDimensions = this.getBarDimensions()
        , barDefaultOptions = this.getBarDefaultOptions()
      ;




      this.selection.each(function() {

        var dom = d3.select(this);

        // remove old
        if (dom.select('svg')) {
          dom.select('svg').remove();
        }

        var svg = dom.append('svg')
            .attr('class', 'chart barchart')
            .attr('height', that.options.height)
            .attr('width', that.options.width);

        var bars = svg.selectAll('rect.chart__bar')
            .data(data)
            .enter()
            .append('rect')
            .attr(_.extend(barPositions, barDimensions, barDefaultOptions));
      });

      // sets a method to allow redrawing
      this.onAnchorChange = function () {
        this.draw();
      };
    },


    /**
     * Sets the direction of the graph
     * @param  {String} value - the direction used, allowed values = ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_BOTTOM | ANCHOR_TOP
     * @return {Mixed}        - the value or chart
     */
    anchor: function (value) {
      if (!arguments.length) return this.options.anchor;

      value = String(value).toLowerCase();
      // wrong value supplied
      if (value !== this.ANCHOR_LEFT &&
          value !== this.ANCHOR_RIGHT &&
          value !== this.ANCHOR_TOP &&
          value !== this.ANCHOR_BOTTOM) {
        console.error(value, 'is invalid. Only ', this.ANCHOR_LEFT, ', ', this.ANCHOR_RIGHT, ', ', this.ANCHOR_TOP, ' or ', this.ANCHOR_BOTTOM, 'allowed');
        return this;
      }
      this.options.anchor = value;

      // redraw if the graph has been loaded
      if (typeof this.onAnchorChange === 'function') {
        this.onAnchorChange();
      }

      return this;
    },

  };

  chart = _.extend(chart, barOptions);
  chart = _.extend(chart, base);
  return (chart.init());

}
{'fraction_non_alphanumeric': 0.1391189135475323, 'fraction_numerical': 0.001987413050679033, 'mean_word_length': 1.9516129032258065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '182908', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1800, 'n_words': 402}