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 VisualizationView provides a base visualization class for rendering graph axes, 26 performing axis and label formatting, and displaying gridlines. 27 28 In any of your views, simply override xFormatter, and yFormatter to provide custom axis formatting. 29 30 VisualizationView is also responsible for redrawing the element on the page when the content changes. 31 32 **/ 33 Ember.VisualizationView = Ember.View.extend( 34 /** @scope Ember.VisualizationView.prototype */ { 35 36 tagName: 'svg', 37 38 attributeBindings: ['width', 'height', 'preserveAspectRatio', 'viewBox'], 39 40 xMargin: 0, 41 42 yMargin: 0, 43 44 xScale: function() { 45 return d3.scale.linear(); 46 }.property('content').cacheable(), 47 48 yScale: function() { 49 return d3.scale.linear(); 50 }.property('content').cacheable(), 51 52 xFormatter: function(x) { 53 return x; 54 }, 55 56 yFormatter: function(y) { 57 return y; 58 }, 59 60 redraw: function() { 61 this.rerender(); 62 }.observes('content'), 63 64 didInsertElement: function() { 65 var self = this, 66 id = this.$().attr('id'), 67 xScale = this.get('xScale'), 68 yScale = this.get('yScale'), 69 xMargin = this.get('xMargin'), 70 yMargin = this.get('yMargin'), 71 width = this.get('width'), 72 height = this.get('height'), 73 xFormatter = this.get('xFormatter'), 74 yFormatter = this.get('yFormatter'), 75 xAxis, 76 yAxis, 77 svg; 78 79 svg = d3.select("#" + id); 80 81 xAxis = d3.svg.axis().scale(xScale).orient("bottom").tickFormat(xFormatter); 82 83 yAxis = d3.svg.axis().scale(yScale).orient("left").tickFormat(yFormatter); 84 85 svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + (height - yMargin) + ")").call(xAxis); 86 87 svg.append("g").attr("class", "y axis").attr("transform", "translate(" + xMargin + ", 0)").call(yAxis); 88 89 svg.insert("g", ":first-child").attr("class", "x grid").attr("transform", "translate(0," + (height - yMargin) + ")"). 90 call(xAxis.tickSize(-height + (yMargin * 2), 0, 0).tickFormat("")); 91 92 svg.insert("g", ":first-child").attr("class", "y grid").attr("transform", "translate(" + xMargin + ", 0)"). 93 call(yAxis.tickSize(-width + (xMargin * 2), 0, 0).tickFormat("")); 94 } 95 96 }); 97