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 TimeSeriesView provides a visualization class for rendering a single-line time series graph, with axes, 26 and gridlines. 27 28 To use, simply instantiate the view with a content binding either pointing to an array of objects, each 29 containing an x and y attribute. 30 31 For example: 32 33 {{view Ember.HistogramView contentBinding="App.irwinHallController.content"}} 34 35 **/ 36 Ember.TimeSeriesView = Ember.VisualizationView.extend( 37 /** @scope Ember.TimeSeriesView.prototype */ { 38 39 path: function() { 40 var content = this.get('content'), 41 xScale = this.get('xScale'), 42 yScale = this.get('yScale'); 43 44 return d3.svg.line().interpolate("linear").x(function(d) { return xScale(d.x); }).y(function(d, i) { return yScale(d.y); })(content); 45 }.property('content').cacheable(), 46 47 xScale: function() { 48 var content = this.get('content'), 49 width = this.get('width'), 50 xMargin = this.get('xMargin'), 51 first_sample = content[0], 52 last_sample = content[content.length-1]; 53 54 if(first_sample && last_sample) { 55 return d3.time.scale().domain([first_sample.x, last_sample.x]).range([0 + xMargin, width - xMargin]); 56 } 57 }.property('content').cacheable(), 58 59 yScale: function() { 60 var content = this.get('content'), 61 height = this.get('height'), 62 yMax = this.get('yMax'), 63 yMargin = this.get('yMargin'); 64 65 return d3.scale.linear().domain([0, yMax]).range([height - yMargin, 0 + yMargin]); 66 }.property('content').cacheable(), 67 68 yMax: function() { 69 var content = this.get('content'); 70 71 return d3.max(content.map(function(el) { return parseFloat(el.y); })); 72 }.property('samples').cacheable(), 73 74 didInsertElement: function() { 75 var self = this, 76 path = this.get('path'), 77 id = this.$().attr('id'), 78 xScale = this.get('xScale'), 79 yScale = this.get('yScale'), 80 xMargin = this.get('xMargin'), 81 yMargin = this.get('yMargin'), 82 width = this.get('width'), 83 height = this.get('height'), 84 xFormatter = this.get('xFormatter'), 85 yFormatter = this.get('yFormatter'), 86 xAxis, 87 yAxis, 88 svg; 89 90 svg = d3.select("#" + id); 91 92 svg.append("g").append("path").attr("class", "series").attr("d", path); 93 94 this._super(); 95 } 96 97 }); 98 99