From 46aa9fa2f2c749fed0e0ce3cd059e2c77c328be7 Mon Sep 17 00:00:00 2001 From: javi Date: Fri, 22 Nov 2013 15:40:36 +0100 Subject: [PATCH] fixed cumulative --- lib/torque/provider.json.js | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/torque/provider.json.js b/lib/torque/provider.json.js index 0505975..31cc1b1 100644 --- a/lib/torque/provider.json.js +++ b/lib/torque/provider.json.js @@ -77,10 +77,12 @@ dates = (1 + maxDateSlots) * rows.length; } + var type = this.options.cumulative ? Uint32Array: Uint8Array; + // reserve memory for all the dates var timeIndex = new Int32Array(maxDateSlots + 1); //index-size var timeCount = new Int32Array(maxDateSlots + 1); - var renderData = new (this.options.valueDataType || Uint8Array)(dates); + var renderData = new (this.options.valueDataType || type)(dates); var renderDataPos = new Uint32Array(dates); prof_mem.inc( @@ -108,23 +110,38 @@ var dates = row.dates__uint16; var vals = row.vals__uint8; - var prev_val = 0; - for (var j = 0, len = dates.length; j < len; ++j) { - var rr = rowsPerSlot[dates[j]] || (rowsPerSlot[dates[j]] = []); - if(this.options.cumulative) { - vals[j] += prev_val; - } - prev_val = vals[j]; - rr.push([r, vals[j]]); - } + if (!this.options.cumulative) { + for (var j = 0, len = dates.length; j < len; ++j) { + var rr = rowsPerSlot[dates[j]] || (rowsPerSlot[dates[j]] = []); + if(this.options.cumulative) { + vals[j] += prev_val; + } + prev_val = vals[j]; + rr.push([r, vals[j]]); + } + } else { + var valByDate = {} + for (var j = 0, len = dates.length; j < len; ++j) { + valByDate[dates[j]] = vals[j]; + } + var accum = 0; - // extend the latest to the end - if(this.options.cumulative) { - var lastDateSlot = dates[dates.length - 1]; + // extend the latest to the end + for (var j = dates[0]; j <= maxDateSlots; ++j) { + var rr = rowsPerSlot[j] || (rowsPerSlot[j] = []); + var v = valByDate[j]; + if (v) { + accum += v; + } + rr.push([r, accum]); + } + + /*var lastDateSlot = dates[dates.length - 1]; for (var j = lastDateSlot + 1; j <= maxDateSlots; ++j) { var rr = rowsPerSlot[j] || (rowsPerSlot[j] = []); rr.push([r, prev_val]); } + */ } }