Browse Source

Merge pull request #2167 from wimrijnders/PR8

Consolidated code for drawing x-axis label
codeClimate
yotamberk 8 years ago
committed by GitHub
parent
commit
db89162cf4
1 changed files with 34 additions and 34 deletions
  1. +34
    -34
      lib/graph3d/Graph3d.js

+ 34
- 34
lib/graph3d/Graph3d.js View File

@ -1228,6 +1228,32 @@ Graph3d.prototype._line = function(ctx, from, to, strokeStyle) {
} }
Graph3d.prototype.drawAxisLabelX = function(ctx, point3d, text, armAngle, yMargin) {
if (yMargin === undefined) {
yMargin = 0;
}
var point2d = this._convert3Dto2D(point3d);
if (Math.cos(armAngle * 2) > 0) {
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
point2d.y += yMargin;
}
else if (Math.sin(armAngle * 2) < 0){
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
}
else {
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
}
ctx.fillStyle = this.axisColor;
ctx.fillText(text, point2d.x, point2d.y);
}
/** /**
* Draw a line between 2d points 'from' and 'to'. * Draw a line between 2d points 'from' and 'to'.
* *
@ -1285,23 +1311,10 @@ Graph3d.prototype._redrawAxis = function() {
this._line3d(ctx, from, to, this.axisColor); this._line3d(ctx, from, to, this.axisColor);
} }
yText = (Math.cos(armAngle) > 0) ? this.yMin : this.yMax;
text = this._convert3Dto2D(new Point3d(x, yText, this.zMin));
if (Math.cos(armAngle * 2) > 0) {
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
text.y += textMargin;
}
else if (Math.sin(armAngle * 2) < 0){
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
}
else {
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
}
ctx.fillStyle = this.axisColor;
ctx.fillText(' ' + this.xValueLabel(step.getCurrent()) + ' ', text.x, text.y);
yText = (Math.cos(armAngle) > 0) ? this.yMin : this.yMax;
var point3d = new Point3d(x, yText, this.zMin);
var msg = ' ' + this.xValueLabel(x) + ' ';
this.drawAxisLabelX(ctx, point3d, msg, armAngle, textMargin);
step.next(); step.next();
} }
@ -1401,23 +1414,10 @@ Graph3d.prototype._redrawAxis = function() {
var xLabel = this.xLabel; var xLabel = this.xLabel;
if (xLabel.length > 0) { if (xLabel.length > 0) {
yOffset = 0.1 / this.scale.y; yOffset = 0.1 / this.scale.y;
xText = (this.xMin + this.xMax) / 2;
yText = (Math.cos(armAngle) > 0) ? this.yMin - yOffset: this.yMax + yOffset;
text = this._convert3Dto2D(new Point3d(xText, yText, this.zMin));
if (Math.cos(armAngle * 2) > 0) {
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
}
else if (Math.sin(armAngle * 2) < 0){
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
}
else {
ctx.textAlign = 'left';
ctx.textBaseline = 'middle';
}
ctx.fillStyle = this.axisColor;
ctx.fillText(xLabel, text.x, text.y);
xText = (this.xMin + this.xMax) / 2;
yText = (Math.cos(armAngle) > 0) ? this.yMin - yOffset: this.yMax + yOffset;
text = new Point3d(xText, yText, this.zMin);
this.drawAxisLabelX(ctx, text, xLabel, armAngle);
} }
// draw y-label // draw y-label

Loading…
Cancel
Save