Ejegg has uploaded a new change for review. https://gerrit.wikimedia.org/r/197848
Change subject: Use shared context in amount per second ...................................................................... Use shared context in amount per second Also change y ticks to include cents, for use in smaller campaigns. Bug: T89298 Change-Id: I867b741cddaaaec16bf7d4a25bac6e29a6a9be4b --- M src/components/widgets/amt-per-second-chart/amt-per-second-chart.html M src/components/widgets/amt-per-second-chart/amt-per-second-chart.js 2 files changed, 38 insertions(+), 60 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/dash refs/changes/48/197848/1 diff --git a/src/components/widgets/amt-per-second-chart/amt-per-second-chart.html b/src/components/widgets/amt-per-second-chart/amt-per-second-chart.html index 71185d3..2443393 100644 --- a/src/components/widgets/amt-per-second-chart/amt-per-second-chart.html +++ b/src/components/widgets/amt-per-second-chart/amt-per-second-chart.html @@ -4,6 +4,7 @@ <p>USD/Second Required (Average)</p> </div> <div class="panel-body"> + <p data-bind="visible: !hasData()">No data available. This widget requires a widget like Totals Earned to provide data.</p> <div class="col-md-12"> <div id="avgUSDperSecond"></div> </div> diff --git a/src/components/widgets/amt-per-second-chart/amt-per-second-chart.js b/src/components/widgets/amt-per-second-chart/amt-per-second-chart.js index e48d9db..f009e35 100644 --- a/src/components/widgets/amt-per-second-chart/amt-per-second-chart.js +++ b/src/components/widgets/amt-per-second-chart/amt-per-second-chart.js @@ -3,81 +3,41 @@ 'text!components/widgets/amt-per-second-chart/amt-per-second-chart.html', 'c3', 'numeraljs', - 'momentjs' -], function( ko, template, c3, numeral, moment ){ - + 'WidgetBase' +], function( ko, template, c3, numeral, WidgetBase ){ function AmtPerSecondChartViewModel( params ){ var self = this; - - //TODO: make dayObj (and other params) come from data - self.dayObj = []; - - self.loadData = function ( decemberData, timestamp ) { - var runningTotal = 0, - currentDate = new Date(), - timeFormat = 'dddd, MMMM Do YYYY, h:mm:ss a'; - - currentDate.setTime( timestamp ); - self.displayDate( moment( currentDate ).format( timeFormat ) ); - self.lastDataPoint.day = currentDate.getUTCDate(); - self.lastDataPoint.hour = currentDate.getUTCHours(); - - for (var d = 1; d < 32; d++) { - self.dailyDataArray[d] = 0; - self.dailyCountArray[d] = 0; - if (!self.dayObj[d]) { - self.dayObj[d] = new Array(25); - self.dayObj[d][0] = 'Hourly Totals'; - for (var h = 0; h < 24; h++) { - self.dayObj[d][h + 1] = { total: 0, count: 0 }; - self.secondsByHourDonationData[(d - 1) * 24 + h + 1] = 0; - } - } - } - - var dataCount = decemberData.length; - for (var i = 0; i < dataCount; i++ ) { - - var el = decemberData[i], - day = el.day, - hour = el.hour, - total = el.usd_total; - self.dayObj[day][hour + 1] = { total: total, count: el.donations }; - - self.secondsByHourDonationData[(day - 1) * 24 + hour + 1] = el.usd_per_second; - runningTotal += total; - self.dailyDataArray[day] += total; - self.dailyCountArray[day] += el.donations; - } - - self.raised(runningTotal); - }; + WidgetBase.call( this, params ); + self.hasData = ko.observable( false ); self.makeChart = function() { - if ( self.dayObj.length < 2 ) { + var columns; + + if ( params.sharedContext.dayObj.length < 2 ) { return; } - var numPoints = ( self.lastDataPoint.day - 1 ) * 24 + self.lastDataPoint.hour + 1, + self.hasData( true ); + var numPoints = ( params.sharedContext.lastDataPoint.day - 1 ) * 24 + params.sharedContext.lastDataPoint.hour + 1, xs = new Array( numPoints + 2 ), // label, data to date, final point index = 0, - remainingNeeded = self.goal(); + remainingNeeded = params.sharedContext.goal(); xs[0] = 'x1'; self.needPerSecond = new Array( numPoints + 2 ); self.needPerSecond[0] = 'Needed Per Second'; // secondsByHourDonationData already has a label in [0] - self.gotPerSecond = self.secondsByHourDonationData.slice( 0, numPoints + 1 ); + self.gotPerSecond = params.sharedContext.secondsByHourDonationData.slice( 0, numPoints + 1 ); - for( var d = 1; d < self.dayObj.length; d++ ) { + for( var d = 1; d < params.sharedContext.dayObj.length; d++ ) { for ( var h = 0; h < 24; h++ ) { index = ( d - 1 ) * 24 + h + 1; if ( index > numPoints + 1 ) { break; } - remainingNeeded = remainingNeeded - self.dayObj[d][h + 1].total; + remainingNeeded = remainingNeeded - params.sharedContext.dayObj[d][h + 1].total; if ( remainingNeeded < 0 ) { remainingNeeded = 0; } @@ -94,6 +54,19 @@ self.gotPerSecond[ numPoints + 1 ] = self.gotPerSecond[ numPoints ]; self.needPerSecond[ numPoints + 1 ] = self.needPerSecond[ numPoints ]; } + columns = [ + xs, + self.gotPerSecond, + self.needPerSecond + ]; + + if ( self.avgUSDComboChart ) { + self.avgUSDComboChart.load( { + columns: columns + } ); + return; + } + self.avgUSDComboChart = c3.generate( { bindto: '#avgUSDperSecond', size: { @@ -106,11 +79,7 @@ 'Needed Per Second' : 'x1', 'Donations Per Second' : 'x1' }, - columns: [ - xs, - self.gotPerSecond, - self.needPerSecond - ], + columns: columns, type: 'area', types: { 'USD per second': 'line' @@ -126,7 +95,7 @@ }, y: { tick: { - format: function(x){ return numeral(x).format('$0,0'); } + format: function(x){ return numeral(x).format('$0,0.00'); } } } }, @@ -141,7 +110,15 @@ } } ); }; - self.makeChart(); + self.subscribe = function() { + if ( !params.sharedContext.totalsChanged ) { + window.setTimeout( self.subscribe, 50 ); + return; + } + params.sharedContext.totalsChanged.subscribe( self.makeChart ); + self.makeChart(); + }; + self.subscribe(); } return { viewModel: AmtPerSecondChartViewModel, template: template }; -- To view, visit https://gerrit.wikimedia.org/r/197848 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I867b741cddaaaec16bf7d4a25bac6e29a6a9be4b Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/dash Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits