// Generated by CoffeeScript 1.10.0 (function() { window.Chart = Backbone.D3View.extend({ initialize: function(conf) { this.width = this.el.getBoundingClientRect().width; this.height = this.el.getBoundingClientRect().height; return this.d3el.classed('chart', true); } }); window.BarChart = Chart.extend({ initialize: function(conf) { Chart.prototype.initialize.call(this, conf); this.d3el.classed('bar_chart', true); this.orientation = conf.orientation != null ? conf.orientation : 'horizontal'; this.key = conf.key != null ? conf.key : function(d, i) { return i; }; this.value = conf.value != null ? conf.value : function(d) { return d; }; this.scales_config = { size: { type: d3.scale.linear, range: function(data) { return [0, this.orientation === 'horizontal' ? this.width : this.height]; }, domain: function(data) { return [0, d3.max(data, this.value)]; } }, position: { type: d3.scale.ordinal, range: function(data) { return [0, this.orientation === 'horizontal' ? this.height : this.width]; }, domain: function(data) { return data.map(this.key); } }, color: { type: d3.scale.category10, range: function(data) { return this.scales.color.range(); }, domain: function(data) { return data.map(this.key); } } }; if (conf.scales != null) { Object.keys(conf.scales).forEach((function(_this) { return function(s) { return Object.keys(conf.scales[s]).forEach(function(p) { return _this.scales_config[s][p] = conf.scales[s][p]; }); }; })(this)); } Object.keys(this.scales_config).forEach((function(_this) { return function(s) { return Object.keys(_this.scales_config[s]).forEach(function(p) { return _this.scales_config[s][p] = d3.functor(_this.scales_config[s][p]); }); }; })(this)); this.scales = { size: this.scales_config.size.type(), position: this.scales_config.position.type(), color: this.scales_config.color.type() }; if (conf.tooltip != null) { this.tooltip = conf.tooltip; } this.listenTo(this.model, 'change:data', this.render); this.listenTo(this.model, 'change:focus', this.update_focus); this.render(); return this.update_focus(); }, render: function() { var bars, data, enter_bars, size_axis; data = this.model.attributes.data; this.update_scales(data); size_axis = d3.svg.axis().scale(this.scales.size).tickSize(this.orientation === 'horizontal' ? this.height : this.width).orient(this.orientation === 'horizontal' ? 'bottom' : 'right'); this.d3el.append('g').attr({ "class": 'axis' }).call(size_axis); bars = this.d3el.selectAll('.bar').data(data, this.key); enter_bars = bars.enter().append('rect').attr({ "class": 'bar', x: 0 }).on('mouseover', (function(_this) { return function(d, i) { return _this.model.set({ focus: _this.key(d, i) }); }; })(this)).on('mouseout', (function(_this) { return function() { return _this.model.set({ focus: null }); }; })(this)); if (this.orientation === 'horizontal') { bars.attr({ y: (function(_this) { return function(d, i) { return _this.scales.position(_this.key(d, i)); }; })(this), width: (function(_this) { return function(d, i) { return _this.scales.size(_this.value(d, i)); }; })(this), height: this.scales.position.rangeBand() }); } else { bars.attr({ x: (function(_this) { return function(d, i) { return _this.scales.position(_this.key(d, i)); }; })(this), height: (function(_this) { return function(d, i) { return _this.scales.size(_this.value(d, i)); }; })(this), y: (function(_this) { return function(d, i) { return _this.height - _this.scales.size(_this.value(d, i)); }; })(this), width: this.scales.position.rangeBand() }); } bars.attr({ fill: (function(_this) { return function(d, i) { return _this.scales.color(_this.key(d, i)); }; })(this) }); bars.exit().remove(); if (this.tooltip != null) { enter_bars.append('title'); return bars.select('title').text(this.tooltip); } }, update_focus: function() { return this.d3el.selectAll('.bar').classed('focus', (function(_this) { return function(d, i) { return _this.key(d, i) === _this.model.attributes.focus; }; })(this)); }, update_scales: function(data) { this.scales.size.domain(this.scales_config.size.domain.call(this, data)).range(this.scales_config.size.range.call(this, data)); this.scales.position.domain(this.scales_config.position.domain.call(this, data)).rangeRoundBands(this.scales_config.position.range.call(this, data), 0.05); return this.scales.color.domain(this.scales_config.color.domain.call(this, data)).range(this.scales_config.color.range.call(this, data)); } }); }).call(this);