From 0c1aef2648bb8d261ec51a192b8b34e5231fb24d Mon Sep 17 00:00:00 2001 From: Kyungmin Kim Date: Mon, 9 Jun 2014 16:54:49 -0700 Subject: [PATCH 01/34] Add graph --- Gruntfile.js | 8 +- app/components/chart.js | 115 ++++++++++++++++++++++++ app/models/transaction.js | 23 +++++ app/templates/activity/transactions.hbs | 2 + bower.json | 2 +- static/less/scaffolding.less | 4 + 6 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 app/components/chart.js diff --git a/Gruntfile.js b/Gruntfile.js index b7ca5aea4..6010d6e74 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -103,7 +103,9 @@ module.exports = function(grunt) { 'static/javascripts/jquery-csv/src/jquery.csv.js', 'static/javascripts/moment/moment.js', 'static/lib/daterangepicker.js', - 'static/javascripts/timepicker/jquery.timepicker.js' + 'static/javascripts/timepicker/jquery.timepicker.js', + 'static/javascripts/d3/d3.min.js', + 'static/javascripts/nvd3/nv.d3.min.js' ], dest: 'build/js/lib-dev.js' }, @@ -129,7 +131,9 @@ module.exports = function(grunt) { 'static/javascripts/jquery-csv/src/jquery.csv.js', 'static/javascripts/moment/min/moment.min.js', 'static/lib/daterangepicker.js', - 'static/javascripts/timepicker/jquery.timepicker.min.js' + 'static/javascripts/timepicker/jquery.timepicker.min.js', + 'static/javascripts/d3/d3.min.js', + 'static/javascripts/nvd3/nv.d3.min.js' ], dest: 'build/js/lib-prod.js' }, diff --git a/app/components/chart.js b/app/components/chart.js new file mode 100644 index 000000000..260c5fc92 --- /dev/null +++ b/app/components/chart.js @@ -0,0 +1,115 @@ +Balanced.ChartComponent = Ember.Component.extend({ + tagName: 'svg', + classNames: ['chart-container'], + + chartData: null, + _chart: null, + _chartModel: null, + + x: null, + y: null, + + chartModel: function() { + return nv.models[this.get('_chartModel')](); + }.property("_chartModel"), + + margin: function() { + return { + top: 0, + bottom: 0, + left: 0, + right: 0 + } + }.property(), + + options: function() { + var self = this; + + return { + showXAxis: true, + showYAxis: true, + showControls: true, + showLegend: false + }; + }.property('showControls', 'showLegend'), + + chart: function() { + var self = this; + var chart = self.get('_chart'); + + if (!chart) { + chart = self.get('chartModel'); + } + + chart.useInteractiveGuideline(true); + + chart.xAxis + .axisLabel('Time (ms)') + .tickFormat(d3.format(',r')); + + chart.yAxis + .axisLabel('Voltage (v)') + .tickFormat(d3.format('.02f')); + + chart.margin(self.get('margin')) + .options(self.get('options')); + + return chart; + }.property('chartModel', 'margin', 'options'), + + didInsertElement: function() { + var self = this; + var $el = self.$(); + var el = $el.get(0); + nv.addGraph(function() { + return self.get('chart'); + }); + }, + + updateChart: function() { + var self = this; + var el = self.get('element'); + var chartData = self.get('chartData'); + var chart = self.get('chart'); + + d3.select(el) + .datum(chartData) + .transition() + .duration(250) + .call(chart); + + self.set('_chart', chart); + nv.utils.windowResize(chart.update); + + }.observes('chartData','chart').on('didInsertElement') +}); + + +Balanced.LineChart = Balanced.ChartComponent.extend({ + _chartModel: "lineChart", + + chartData: function() { + var sin = [], + cos = []; + + for (var i = 0; i < 100; i++) { + sin.push({x: i, y: Math.sin(i/10)}); + cos.push({x: i, y: .5 * Math.cos(i/10)}); + } + + return [ + { + values: sin, + key: 'Sine Wave', + color: '#ff7f0e' + }, + { + values: cos, + key: 'Cosine Wave', + color: '#2ca02c' + } + ]; + }.property() +}); + +Ember.Handlebars.helper('line-chart', Balanced.LineChart); diff --git a/app/models/transaction.js b/app/models/transaction.js index fdfc11d2f..444422ba3 100644 --- a/app/models/transaction.js +++ b/app/models/transaction.js @@ -8,6 +8,29 @@ var Computed = { Balanced.Transaction = Balanced.Model.extend( Balanced.MetaArrayMixin, { + // chartData: function() { + // var sin = [], + // cos = []; + + // for (var i = 0; i < 100; i++) { + // sin.push({x: i, y: Math.sin(i/10)}); + // cos.push({x: i, y: .5 * Math.cos(i/10)}); + // } + + // return [ + // { + // values: sin, + // key: 'Sine Wave', + // color: '#ff7f0e' + // }, + // { + // values: cos, + // key: 'Cosine Wave', + // color: '#2ca02c' + // } + // ]; + // }.property(), + customer: Balanced.Model.belongsTo('customer', 'Balanced.Customer'), events: Balanced.Model.hasMany('events', 'Balanced.Event'), diff --git a/app/templates/activity/transactions.hbs b/app/templates/activity/transactions.hbs index eb3dd7163..e81e50712 100644 --- a/app/templates/activity/transactions.hbs +++ b/app/templates/activity/transactions.hbs @@ -1,3 +1,5 @@ +{{line-chart}} +