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;

@@ -1605,36 +1605,212 @@
   my.when(%5B%22
-dataset%22, %22x
+xScale%22, %22xColumn%22%5D, function (xScale, xColumn)%7B%0A    //my.xAccessor = function (d)%7B return d%5BxColumn%5D; %7D);%0A    my.xScaled = function (d)%7B return xScale(d%5BxColumn%5D); %7D;%0A  %7D);%0A%0A  my.when(%5B%22y
 Scale%22, %22xCo
@@ -1810,36 +1810,214 @@
 %22, %22
-x
+y
 Column%22
+%5D
 , 
-%22
+function (
 yScale
-%22
 , 
-%22
 yColumn
+)%7B%0A    //my.yAccessor = function (d)%7B return d%5ByColumn%5D; %7D);%0A    my.yScaled = function (d)%7B return yScale(d%5ByColumn%5D); %7D;%0A  %7D);%0A%0A  my.when(%5B%22dataset%22, %22xScaled%22, %22yScaled
 %22, %22
@@ -2073,34 +2073,18 @@
 cale
-, xColumn, yScale, yColumn
+d, yScaled
 , he
@@ -2220,50 +2220,15 @@
 x%22, 
-function (d)%7B return xScale(d%5BxColumn%5D); %7D
+xScaled
 )%0A  
@@ -2277,50 +2277,15 @@
 y%22, 
-function (d)%7B return yScale(d%5ByColumn%5D); %7D
+yScaled
 )%0A  
@@ -2336,35 +2336,27 @@
 ght - yScale
+d
 (d
-%5ByColumn%5D
 ); %7D);%0A    b

Refactor bar chart to expose xScaled, yScaled