diff --git a/lib/torque/provider/internal.js b/lib/torque/provider/internal.js index 467efd0..df8401b 100644 --- a/lib/torque/provider/internal.js +++ b/lib/torque/provider/internal.js @@ -22,7 +22,6 @@ var internal = function (options) { [Number.MAX_VALUE, Number.MAX_VALUE], [Number.MIN_VALUE, Number.MIN_VALUE] ]; - }; @@ -34,46 +33,111 @@ internal.prototype = { this.timestampType = "number"; } } else { + time = new Date(Date.parse(time)); if (this.timestampType === undefined) { this.timestampType = "date"; } } this.points.push({lat: parseFloat(lat), lon: parseFloat(lon), time: time, value: parseFloat(value) || value}); }, - getBounds: function() { + getBounds: function () { return this.options.bounds; }, + getCategories: function () { + if (!this.options.countby) { + return []; + } + + var self = this; + + var checkCategoryExists = function (category) { + return category.name == self.points[i].value; + }; + + var categories = []; + for (var i = 0; i < this.points.length; i++) { + if (!categories.some(checkCategoryExists)) { + var randomColor = 0x1000000 + Math.floor(Math.random() * 0xffffff); + randomColor = "#" + randomColor.toString(16).slice(1, 7); + categories.push({ + value: this.points[i].value, + name: this.points[i].value, + color: randomColor + }); + } + } + + return categories; + }, + setOptions: function(opt) { + var refresh = false; + + if (opt.resolution !== undefined && opt.resolution !== this.options.resolution) { + this.options.resolution = opt.resolution; + refresh = true; + } + + if (opt.steps !== undefined && opt.steps !== this.options.steps) { + this.setSteps(opt.steps, { silent: true }); + refresh = true; + } + + if (opt.column !== undefined && opt.column !== this.options.column) { + this.options.column = opt.column; + refresh = true; + } + + if (opt.countby !== undefined && opt.countby !== this.options.countby) { + this.options.countby = opt.countby; + refresh = true; + } + + if (opt.data_aggregation !== undefined) { + var c = opt.data_aggregation === 'cumulative'; + if (this.options.cumulative !== c) { + this.options.cumulative = c; + refresh = true; + } + } + + if (refresh) { + this.reload(); + } + + return refresh; + }, setReady: function (ready) { if (ready == false) { this._ready = false; } else { var self = this; + this.options.data_steps = this.points.length; + this.points.sort(function (point1, point2) { - if (self.timestampType == "number") { - return point1.time - point2.time; - } else { - if (typeof(point1.time) == "string") { - return new Date(point1.time) - new Date(point2.time); - } else { - return point1.time - point2.time; - } - } + return point1.time - point2.time; }); - if (this.timestampType == "date") { - this.timestamps = {}; // {date: timestamp_id} - var timestampIdx = 0; - for (var i = 0; i < this.points.length; i++) { - var point = this.points[i]; - this.timestamps[point.time] || (this.timestamps[point.time] = timestampIdx++); + if (this.timestampType == "number") { + this.options.start = this.points[0].time; + this.options.end = this.points[this.points.length - 1].time; + } else { + this.options.start = this.points[0].time.getTime(); + this.options.end = this.points[this.points.length - 1].time.getTime(); + } + + this.timestamps = {}; // {date: timestamp_id} + for (var i = 0; i < this.points.length; i++) { + var point = this.points[i]; + if ((this.options.end - this.options.start) != 0) { + this.timestamps[point.time] || (this.timestamps[point.time] = parseInt(this.getSteps() * (point.time - this.options.start) / (this.options.end - this.options.start))); + } else { + this.timestamps[point.time] || (this.timestamps[point.time] = 0); } } - if (this.timestampType == "number") { - this.maxTimestamp = this.points[this.points.length - 1].time; - } else { - this.maxTimestamp = this.timestamps[this.points[this.points.length - 1].time]; - } + + this.maxTimestamp = this.timestamps[this.points[this.points.length - 1].time]; + this._ready = true; this._processQueue(); this.options.ready && this.options.ready(); @@ -166,14 +230,9 @@ internal.prototype = { x[pointIdx] = xInTile; y[pointIdx] = yInTile; - var pointTimestamp; - if (this.timestampType == 'date') { - pointTimestamp = this.timestamps[point.time]; - } else { - pointTimestamp = point.time; - } + var pointTimestamp = this.timestamps[point.time]; - if (this.options.cumulative) { + if (this.options.cumulative && this.maxTimestamp > 0) { if (accumulatedValues[xInTile] === undefined) { accumulatedValues[xInTile] = []; } @@ -229,16 +288,25 @@ internal.prototype = { }; }, getKeySpan: function () { - return { - start: this.options.start * 1000, - end: this.options.end * 1000, - step: this.options.step, - steps: this.options.steps, - columnType: this.timestampType - }; + return { + start: this.options.start, + end: this.options.end, + step: this.options.step || ((this.options.end - this.options.start) / this.getSteps()), + steps: this.getSteps(), + columnType: this.timestampType + }; }, getSteps: function () { return this.options.steps; + }, + setSteps: function(steps, opt) { + opt = opt || {}; + if (this.options.steps !== steps) { + this.options.steps = steps; + this.options.step = (this.options.end - this.options.start) / this.getSteps(); + this.options.step = this.options.step || 1; + if (!opt.silent) this.reload(); + } } };