Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2162 from wimrijnders/PR5
Browse files Browse the repository at this point in the history
Translation of all data points to single method.
  • Loading branch information
yotamberk authored Oct 17, 2016
2 parents 85222bf + 4b235ab commit fe1dfc8
Showing 1 changed file with 35 additions and 60 deletions.
95 changes: 35 additions & 60 deletions lib/graph3d/Graph3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,37 @@ Graph3d.prototype._convertTranslationToScreen = function(translation) {
this.ycenter - by * this.frame.canvas.clientWidth);
};


/**
* Calculate the translations and screen positions of all points
*/
Graph3d.prototype._calcTranslations = function(points, sort) {
if (sort === undefined) {
sort = true;
}

for (var i = 0; i < points.length; i++) {
var point = points[i];
point.trans = this._convertPointToTranslation(point.point);
point.screen = this._convertTranslationToScreen(point.trans);

// calculate the translation of the point at the bottom (needed for sorting)
var transBottom = this._convertPointToTranslation(point.bottom);
point.dist = this.showPerspective ? transBottom.length() : -transBottom.z;
}

if (!sort) {
return;
}

// sort the points on depth of their (x,y) position (not on z)
var sortDepth = function (a, b) {
return b.dist - a.dist;
};
points.sort(sortDepth);
};


/**
* Set the background styling for the graph
* @param {string | {fill: string, stroke: string, strokeWidth: string}} backgroundColor
Expand Down Expand Up @@ -1467,24 +1498,7 @@ Graph3d.prototype._redrawDataGrid = function() {
if (this.dataPoints === undefined || this.dataPoints.length <= 0)
return; // TODO: throw exception?

// calculate the translations and screen position of all points
for (i = 0; i < this.dataPoints.length; i++) {
var trans = this._convertPointToTranslation(this.dataPoints[i].point);
var screen = this._convertTranslationToScreen(trans);

this.dataPoints[i].trans = trans;
this.dataPoints[i].screen = screen;

// calculate the translation of the point at the bottom (needed for sorting)
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
}

// sort the points on depth of their (x,y) position (not on z)
var sortDepth = function (a, b) {
return b.dist - a.dist;
};
this.dataPoints.sort(sortDepth);
this._calcTranslations(this.dataPoints);

if (this.style === Graph3d.STYLE.SURFACE) {
for (i = 0; i < this.dataPoints.length; i++) {
Expand Down Expand Up @@ -1601,23 +1615,7 @@ Graph3d.prototype._redrawDataDot = function() {
if (this.dataPoints === undefined || this.dataPoints.length <= 0)
return; // TODO: throw exception?

// calculate the translations of all points
for (i = 0; i < this.dataPoints.length; i++) {
var trans = this._convertPointToTranslation(this.dataPoints[i].point);
var screen = this._convertTranslationToScreen(trans);
this.dataPoints[i].trans = trans;
this.dataPoints[i].screen = screen;

// calculate the distance from the point at the bottom to the camera
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
}

// order the translated points by depth
var sortDepth = function (a, b) {
return b.dist - a.dist;
};
this.dataPoints.sort(sortDepth);
this._calcTranslations(this.dataPoints);

// draw the datapoints as colored circles
var dotSize = this.frame.clientWidth * this.dotSizeRatio; // px
Expand Down Expand Up @@ -1692,23 +1690,7 @@ Graph3d.prototype._redrawDataBar = function() {
if (this.dataPoints === undefined || this.dataPoints.length <= 0)
return; // TODO: throw exception?

// calculate the translations of all points
for (i = 0; i < this.dataPoints.length; i++) {
var trans = this._convertPointToTranslation(this.dataPoints[i].point);
var screen = this._convertTranslationToScreen(trans);
this.dataPoints[i].trans = trans;
this.dataPoints[i].screen = screen;

// calculate the distance from the point at the bottom to the camera
var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom);
this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z;
}

// order the translated points by depth
var sortDepth = function (a, b) {
return b.dist - a.dist;
};
this.dataPoints.sort(sortDepth);
this._calcTranslations(this.dataPoints);

ctx.lineJoin = 'round';
ctx.lineCap = 'round';
Expand Down Expand Up @@ -1833,14 +1815,7 @@ Graph3d.prototype._redrawDataLine = function() {
if (this.dataPoints === undefined || this.dataPoints.length <= 0)
return; // TODO: throw exception?

// calculate the translations of all points
for (i = 0; i < this.dataPoints.length; i++) {
var trans = this._convertPointToTranslation(this.dataPoints[i].point);
var screen = this._convertTranslationToScreen(trans);

this.dataPoints[i].trans = trans;
this.dataPoints[i].screen = screen;
}
this._calcTranslations(this.dataPoints, false);

// start the line
if (this.dataPoints.length > 0) {
Expand Down

0 comments on commit fe1dfc8

Please sign in to comment.