<commit_before>var Model = require("model-js");
var ChiasmComponent = require("chiasm-component");
var mixins = require("../mixins");
var ChiasmDataset = require("chiasm-dataset");
var getColumnMetadata = ChiasmDataset.getColumnMetadata;

function BarChart(){

  var my = new ChiasmComponent({
    xColumn: Model.None,
    yColumn: Model.None
  });

  var svg = d3.select(my.initSVG());
  var g = mixins.marginConvention(my, svg);

  var xAxisG = mixins.xAxis(my, g);
  mixins.scale(my, "x", "ordinal");
  mixins.xAxisLabel(my, xAxisG);

  var yAxisG = mixins.yAxis(my, g);
  mixins.scale(my, "y", "linear");
  mixins.yAxisLabel(my, yAxisG);

  my.when(["dataset", "xColumn"], function (dataset, xColumn){
    if(xColumn !== Model.None){
      var interval = getColumnMetadata(dataset, xColumn).interval;
      if(interval){
        my.xScaleType = "linear";
        my.xScaleDomain = d3.extent(dataset.data, function (d) { return d[xColumn]; });
        my.xScaleDomain[1] += interval;
      } else {
        my.xScaleType = "ordinal";
        my.xScaleDomain = dataset.data.map( function (d) { return d[xColumn]; });
      }
    }
  });
  
  my.when(["dataset", "yColumn"], function (dataset, yColumn){
    if(yColumn !== Model.None){
      my.yScaleDomain = [0, d3.max(dataset.data, function (d) { return d[yColumn]; })];
    }
  });

  my.when(["dataset", "xScale", "xColumn", "yScale", "yColumn", "height"],
      function (dataset, xScale, xColumn, yScale, yColumn, height) {

    var barWidth;
    var interval = getColumnMetadata(dataset, xColumn).interval;
    if(interval){
      barWidth = xScale(interval) - xScale(0);
    } else {
      barWidth = xScale.rangeBand();
    }

    var bars = g.selectAll("rect").data(dataset.data);
      bars.enter().append("rect");
      bars.exit().remove();
      bars
        .attr("x", function (d){ return xScale(d[xColumn]); })
        .attr("width", barWidth)
        .attr("y", function (d){ return yScale(d[yColumn]); })
        .attr("height", function (d){ return height - yScale(d[yColumn]); });

  });

  return my;
}

module.exports = BarChart;<commit_msg>Refactor bar chart vs. histogram logic<commit_after>var Model = require("model-js");
var ChiasmComponent = require("chiasm-component");
var mixins = require("../mixins");
var ChiasmDataset = require("chiasm-dataset");
var getColumnMetadata = ChiasmDataset.getColumnMetadata;

function BarChart(){

  var my = new ChiasmComponent({
    xColumn: Model.None,
    yColumn: Model.None
  });

  var svg = d3.select(my.initSVG());
  var g = mixins.marginConvention(my, svg);

  var xAxisG = mixins.xAxis(my, g);
  mixins.scale(my, "x", "ordinal");
  mixins.xAxisLabel(my, xAxisG);

  var yAxisG = mixins.yAxis(my, g);
  mixins.scale(my, "y", "linear");
  mixins.yAxisLabel(my, yAxisG);

  my.when(["dataset", "xColumn"], function (dataset, xColumn){
    if(xColumn !== Model.None){
      var interval = getColumnMetadata(dataset, xColumn).interval;
      if(interval){
        my.xScaleType = "linear";
        my.xScaleDomain = d3.extent(dataset.data, function (d) { return d[xColumn]; });
        my.xScaleDomain[1] += interval;
      } else {
        my.xScaleType = "ordinal";
        my.xScaleDomain = dataset.data.map( function (d) { return d[xColumn]; });
      }
    }
  });
  
  my.when(["dataset", "yColumn"], function (dataset, yColumn){
    if(yColumn !== Model.None){
      my.yScaleDomain = [0, d3.max(dataset.data, function (d) { return d[yColumn]; })];
    }
  });

  my.when(["dataset", "xScale", "xColumn"], function (dataset, xScale, xColumn) {
    var interval = getColumnMetadata(dataset, xColumn).interval;
    if(interval){
      my.barWidth = xScale(interval) - xScale(0);
    } else {
      my.barWidth = xScale.rangeBand();
    }
  });

  my.when(["dataset", "xScale", "xColumn", "yScale", "yColumn", "height", "barWidth"],
      function (dataset, xScale, xColumn, yScale, yColumn, height, barWidth) {

    var bars = g.selectAll("rect").data(dataset.data);
    bars.enter().append("rect");
    bars
      .attr("x", function (d){ return xScale(d[xColumn]); })
      .attr("width", barWidth)
      .attr("y", function (d){ return yScale(d[yColumn]); })
      .attr("height", function (d){ return height - yScale(d[yColumn]); });
    bars.exit().remove();

  });

  return my;
}

module.exports = BarChart;
{'fraction_non_alphanumeric': 0.17853029255931813, 'fraction_numerical': 0.0027643400138217, 'mean_word_length': 3.462487153134635, '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': '2982020', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1464, 'n_words': 309}