time scale is linear now

getCategories is not random anymore :-/
This commit is contained in:
Dani Carrion 2015-09-04 11:51:17 +02:00
parent e91a29aec2
commit dacef123f8

View File

@ -22,7 +22,6 @@ var internal = function (options) {
[Number.MAX_VALUE, Number.MAX_VALUE], [Number.MAX_VALUE, Number.MAX_VALUE],
[Number.MIN_VALUE, Number.MIN_VALUE] [Number.MIN_VALUE, Number.MIN_VALUE]
]; ];
}; };
@ -34,6 +33,7 @@ internal.prototype = {
this.timestampType = "number"; this.timestampType = "number";
} }
} else { } else {
time = new Date(Date.parse(time));
if (this.timestampType === undefined) { if (this.timestampType === undefined) {
this.timestampType = "date"; this.timestampType = "date";
} }
@ -43,37 +43,101 @@ internal.prototype = {
getBounds: function () { getBounds: function () {
return this.options.bounds; 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) { setReady: function (ready) {
if (ready == false) { if (ready == false) {
this._ready = false; this._ready = false;
} else { } else {
var self = this; var self = this;
this.options.data_steps = this.points.length;
this.points.sort(function (point1, point2) { this.points.sort(function (point1, point2) {
if (self.timestampType == "number") {
return point1.time - point2.time; 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;
}
}
}); });
if (this.timestampType == "date") { 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} this.timestamps = {}; // {date: timestamp_id}
var timestampIdx = 0;
for (var i = 0; i < this.points.length; i++) { for (var i = 0; i < this.points.length; i++) {
var point = this.points[i]; var point = this.points[i];
this.timestamps[point.time] || (this.timestamps[point.time] = timestampIdx++); 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)));
}
if (this.timestampType == "number") {
this.maxTimestamp = this.points[this.points.length - 1].time;
} else { } else {
this.maxTimestamp = this.timestamps[this.points[this.points.length - 1].time]; this.timestamps[point.time] || (this.timestamps[point.time] = 0);
} }
}
this.maxTimestamp = this.timestamps[this.points[this.points.length - 1].time];
this._ready = true; this._ready = true;
this._processQueue(); this._processQueue();
this.options.ready && this.options.ready(); this.options.ready && this.options.ready();
@ -166,14 +230,9 @@ internal.prototype = {
x[pointIdx] = xInTile; x[pointIdx] = xInTile;
y[pointIdx] = yInTile; y[pointIdx] = yInTile;
var pointTimestamp; var pointTimestamp = this.timestamps[point.time];
if (this.timestampType == 'date') {
pointTimestamp = this.timestamps[point.time];
} else {
pointTimestamp = point.time;
}
if (this.options.cumulative) { if (this.options.cumulative && this.maxTimestamp > 0) {
if (accumulatedValues[xInTile] === undefined) { if (accumulatedValues[xInTile] === undefined) {
accumulatedValues[xInTile] = []; accumulatedValues[xInTile] = [];
} }
@ -230,15 +289,24 @@ internal.prototype = {
}, },
getKeySpan: function () { getKeySpan: function () {
return { return {
start: this.options.start * 1000, start: this.options.start,
end: this.options.end * 1000, end: this.options.end,
step: this.options.step, step: this.options.step || ((this.options.end - this.options.start) / this.getSteps()),
steps: this.options.steps, steps: this.getSteps(),
columnType: this.timestampType columnType: this.timestampType
}; };
}, },
getSteps: function () { getSteps: function () {
return this.options.steps; 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();
}
} }
}; };