--- a/src/main/resources/scripts/graph.js Sun Aug 28 09:55:52 2011 -0400
+++ b/src/main/resources/scripts/graph.js Tue Aug 30 10:53:44 2011 -0400
@@ -9,7 +9,8 @@
'left':20
};
this.axisTextMergin = 10;
- this.normarizeSec = 30;
+ this.normarizeSec = 10;
+ this.numberOfMovingAverage = 20;
this.init = function(){
this.SN = "http://www.w3.org/2000/svg";
@@ -63,31 +64,90 @@
//Calculate speed
var speedData = [];
var topSpeed = 0;
- var counter = 0;
var duration = (jsonData.events[jsonData.events.length-1].t - jsonData.events[0].t)/1000;
- for ( var i = 0; i < jsonData.events.length; i++) {
- if ((jsonData.events[i].t - jsonData.events[counter].t) < 1000 * this.normarizeSec) continue;
- var distance = Util.geo.calculateDistance(jsonData.events[counter].c[0], jsonData.events[counter].c[1], jsonData.events[i].c[0], jsonData.events[i].c[1]);
- var timeSec = (jsonData.events[i].t - jsonData.events[counter].t)/1000;
+
+ var s = [];
+
+ for ( var i = 1; i < jsonData.events.length; i++) {
+ var distance = Util.geo.calculateDistance(jsonData.events[i-1].c[0], jsonData.events[i-1].c[1], jsonData.events[i].c[0], jsonData.events[i].c[1]);
+ var timeSec = (jsonData.events[i].t - jsonData.events[i-1].t)/1000;
var elapsedTime = (jsonData.events[i].t - jsonData.events[0].t)/1000;
- console.log(distance);
var speed = distance / timeSec * 3600;
- if (topSpeed < speed) {
- topSpeed = speed;
- };
- speedData.push({
+// if (topSpeed < speed) {
+// topSpeed = speed;
+// };
+ s.push({
'speed':speed,
'elapsedTime':elapsedTime,
});
- counter = i;
};
- console.log(speedData);
+ var sma = 0;
+ for ( var k = this.numberOfMovingAverage -1 ; k < s.length; k++) {
+ for ( var l = 1; l <= this.numberOfMovingAverage; l++) {
+ sma = sma + s[k-this.numberOfMovingAverage + l].speed;
+ }
+ sma = sma / this.numberOfMovingAverage;
+ if (topSpeed < sma) {
+ topSpeed = sma;
+ };
+ speedData.push({
+ 'speed':sma,
+ 'elapsedTime':s[k].elapsedTime,
+ });
+ }
+
+ topSpeed = topSpeed +5;
+
+ //Draw a measure line
+ for ( var i = 0; i < topSpeed; i++) {
+ if (i == 0) continue;
+ if (i % 5 ==0){
+ var y = Math.round(i / topSpeed * (this.graphHeight - this.mergin.top - this.mergin.bottom));
+ this.drawLine(this.svg, -4, y, 90, 8);
+ }else{
+ var y = Math.round(i / topSpeed * (this.graphHeight - this.mergin.top - this.mergin.bottom));
+ this.drawLine(this.svg, -2, y, 90, 4);
+ }
+ }
+
+ //nomirized
+
+// for ( var i = 0; i < jsonData.events.length; i++) {
+// if ((jsonData.events[i].t - jsonData.events[counter].t) < 1000 * this.normarizeSec) continue;
+// var distance = Util.geo.calculateDistance(jsonData.events[counter].c[0], jsonData.events[counter].c[1], jsonData.events[i].c[0], jsonData.events[i].c[1]);
+// var timeSec = (jsonData.events[i].t - jsonData.events[counter].t)/1000;
+// var elapsedTime = (jsonData.events[i].t - jsonData.events[0].t)/1000;
+// console.log(distance);
+// var speed = distance / timeSec * 3600;
+// if (topSpeed < speed) {
+// topSpeed = speed;
+// };
+// speedData.push({
+// 'speed':speed,
+// 'elapsedTime':elapsedTime,
+// });
+// counter = i;
+// };
+
//Plot data
+// for ( var j = 0; j < speedData.length; j++) {
+// var x = speedData[j].elapsedTime / duration * (this.graphWidth-this.mergin.left - this.mergin.right);
+// var y = speedData[j].speed / topSpeed * (this.graphHeight - this.mergin.top - this.mergin.bottom);
+// this.drawPoint(this.svg, x, y, 1);
+// };
+
+ var path = [];
for ( var j = 0; j < speedData.length; j++) {
- var x = speedData[j].elapsedTime / duration * (this.graphWidth-this.mergin.left - this.mergin.right);
- var y = speedData[j].speed / topSpeed * (this.graphHeight - this.mergin.top - this.mergin.bottom);
- this.drawPoint(this.svg, x, y, 1);
+ var x = Math.round(speedData[j].elapsedTime / duration * (this.graphWidth-this.mergin.left - this.mergin.right));
+ var y = Math.round(speedData[j].speed / topSpeed * (this.graphHeight - this.mergin.top - this.mergin.bottom));
+ path.push(this.getAbsoluteCoord(x, y));
}
+
+ console.log(duration);
+ console.log(topSpeed);
+
+ this.drawPath(this.svg, path);
+
};
this.drawPoint = function(graph,x,y,size){
@@ -99,6 +159,62 @@
graph.appendChild(point);
};
+ this.drawLine = function(graph,x,y,angle,length){
+ var line = document.createElementNS(this.SN,"line");
+ var relative_end = {
+ x: x + length * Math.sin(angle * (Math.PI/ 180)),
+ y: y + length * Math.cos(angle * (Math.PI/ 180))
+ };
+ var endPoint = this.getAbsoluteCoord(relative_end.x, relative_end.y);
+ var startPoint = this.getAbsoluteCoord(x, y);
+ line.setAttribute('x1',startPoint.x);
+ line.setAttribute('y1',startPoint.y);
+ line.setAttribute('x2',endPoint.x);
+ line.setAttribute('y2',endPoint.y);
+ line.setAttribute('stroke','black');
+ line.setAttribute('stroke-width','1');
+ graph.appendChild(line);
+ };
+
+ this.drawPath = function(graph,paths){
+
+ var path_dom = document.createElementNS(this.SN,'path');
+ var path = [];
+ console.log(paths);
+ for ( var i = 3; i < paths.length-3; i= i+3) {
+// var path_dom = document.createElementNS(this.SN,'path');
+ var tp = " M" + paths[i-3].x + "," + paths[i-3].y +
+ " Q" + paths[i-2].x + "," + paths[i-2].y +
+ " " + paths[i-1].x + "," + paths[i-1].y +
+ " T" + paths[i].x + "," + paths[i].y;
+// path.push(" M");
+// path.push(paths[i-3].x);
+// path.push(",");
+// path.push(paths[i-3].y);
+// path.push(" Q");
+// path.push(paths[i-2].x);
+// path.push(",");
+// path.push(paths[i-2].y);
+// path.push(" ");
+// path.push(paths[i-1].x);
+// path.push(",");
+// path.push(paths[i-1].y);
+// path.push(" T");
+// path.push(paths[i].x);
+// path.push(",");
+// path.push(paths[i].y);
+ path.push(tp);
+
+ };
+ console.log(path);
+ tp = path.join("");
+ path_dom.setAttribute('d',tp);
+ path_dom.setAttribute('fill','none');
+ path_dom.setAttribute('stroke','black');
+ graph.appendChild(path_dom);
+
+ };
+
/**
* Returns absolute Coord data from x,y based on graph
* @returns: int x,int y