1 /*globals d3*/ 2 3 /** 4 Copyright 2012 Christopher Meiklejohn and Basho Technologies, Inc. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 18 All of the files in this project are under the project-wide license 19 unless they are otherwise marked. 20 **/ 21 22 /** 23 @class 24 25 HistogramView provides a visualization class for rendering a histogram with axes, margins and gridlines. 26 27 To use, simply instantiate the view with a content binding either pointing to an array of values which 28 the histogram will be dervied from, or an array of objects with an x and y attribute for pre-binned 29 histograms. 30 31 For example: 32 33 {{view Ember.HistogramView contentBinding="App.irwinHallController.content"}} 34 35 **/ 36 Ember.HistogramView = Ember.VisualizationView.extend( 37 /** @scope Ember.HistogramView.prototype */ { 38 39 histogram: function() { 40 var content = this.get('content'), 41 firstElement = content[0]; 42 43 if(firstElement.x && firstElement.y) { 44 return content; 45 } else { 46 return d3.layout.histogram()(this.getPath('content')); 47 } 48 }.property('content').cacheable(), 49 50 xScale: function() { 51 var histogram = this.get('histogram'), 52 width = this.get('width'), 53 xMargin = this.get('xMargin'); 54 55 Ember.assert("You need to provide a width for the histogram view.", width !== undefined); 56 57 return d3.scale.ordinal().domain(histogram.map(function(d) { return d.x; })).rangeRoundBands([0 + xMargin, width - xMargin]); 58 }.property('histogram').cacheable(), 59 60 yScale: function() { 61 var histogram = this.get('histogram'), 62 height = this.get('height'), 63 yMargin = this.get('yMargin'); 64 65 Ember.assert("You need to provide a height for the histogram view.", height !== undefined); 66 67 return d3.scale.linear().domain([0, d3.max(histogram.map(function(d) { return d.y; }))]).range([height - yMargin, 0 + yMargin]); 68 }.property('histogram').cacheable(), 69 70 didInsertElement: function() { 71 var self = this, 72 histogram = this.get('histogram'), 73 id = this.$().attr('id'), 74 xScale = this.get('xScale'), 75 yScale = this.get('yScale'), 76 xMargin = this.get('xMargin'), 77 yMargin = this.get('yMargin'), 78 width = this.get('width'), 79 height = this.get('height'), 80 xFormatter = this.get('xFormatter'), 81 yFormatter = this.get('yFormatter'), 82 xAxis, 83 yAxis, 84 svg; 85 86 svg = d3.select("#" + id); 87 88 svg.selectAll("rect") 89 .data(histogram) 90 .enter().append("rect") 91 .attr("class", "sample") 92 .attr("width", xScale.rangeBand()) 93 .attr("x", function(d) { return xScale(d.x); }) 94 .attr("y", function(d) { return yScale(d.y); }) 95 .attr("height", function(d) { return height - yMargin - yScale(d.y); }); 96 97 this._super(); 98 } 99 100 }); 101