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

Commit ab9d5f3

Browse files
authored
Merge pull request #2210 from wimrijnders/PR21
Added point draw methods for graph styles 'bar-color' and 'bar-size'
2 parents cf75437 + fcc817b commit ab9d5f3

File tree

1 file changed

+51
-137
lines changed

1 file changed

+51
-137
lines changed

lib/graph3d/Graph3d.js

Lines changed: 51 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,12 @@ Graph3d.prototype.redraw = function() {
836836
case Graph3d.STYLE.BAR:
837837
pointDrawingMethod = Graph3d.prototype._redrawBarGraphPoint;
838838
break;
839+
case Graph3d.STYLE.BARCOLOR:
840+
pointDrawingMethod = Graph3d.prototype._redrawBarColorGraphPoint;
841+
break;
842+
case Graph3d.STYLE.BARSIZE:
843+
pointDrawingMethod = Graph3d.prototype._redrawBarSizeGraphPoint;
844+
break;
839845
}
840846

841847
if (pointDrawingMethod !== undefined) {
@@ -851,9 +857,6 @@ Graph3d.prototype.redraw = function() {
851857
}
852858
else if (this.style === Graph3d.STYLE.LINE) {
853859
this._redrawDataLine();
854-
} else if (this.style === Graph3d.STYLE.BARCOLOR ||
855-
this.style === Graph3d.STYLE.BARSIZE) {
856-
this._redrawDataBar();
857860
}
858861
else {
859862
// style is DOT, DOTLINE, DOTCOLOR, DOTSIZE
@@ -1577,12 +1580,19 @@ Graph3d.prototype._redrawDataDot = function() {
15771580
};
15781581

15791582

1583+
// -----------------------------------------------------------------------------
1584+
// Methods for drawing points per graph style.
1585+
// -----------------------------------------------------------------------------
1586+
15801587
/**
15811588
* Draw a bar element in the view with the given properties.
15821589
*/
15831590
Graph3d.prototype._redrawBar = function(ctx, point, xWidth, yWidth, color, borderColor) {
15841591
var i, j, surface, corners;
15851592

1593+
ctx.lineJoin = 'round';
1594+
ctx.lineCap = 'round';
1595+
15861596
// calculate all corner points
15871597
var me = this;
15881598
var point3d = point.point;
@@ -1661,24 +1671,50 @@ Graph3d.prototype._redrawBar = function(ctx, point, xWidth, yWidth, color, borde
16611671

16621672

16631673
/**
1664-
* Draw single datapoint for graph style 'Bar'.
1674+
* Draw single datapoint for graph style 'bar'.
16651675
*/
16661676
Graph3d.prototype._redrawBarGraphPoint = function(ctx, point) {
1667-
var i, j, surface, corners;
1677+
var xWidth = this.xBarWidth / 2;
1678+
var yWidth = this.yBarWidth / 2;
1679+
1680+
// calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
1681+
var hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240;
1682+
var color = this._hsv2rgb(hue, 1, 1);
1683+
var borderColor = this._hsv2rgb(hue, 1, 0.8);
1684+
1685+
this._redrawBar(ctx, point, xWidth, yWidth, color, borderColor);
1686+
};
16681687

1669-
ctx.lineJoin = 'round';
1670-
ctx.lineCap = 'round';
16711688

1689+
/**
1690+
* Draw single datapoint for graph style 'bar-color'.
1691+
*/
1692+
Graph3d.prototype._redrawBarColorGraphPoint = function(ctx, point) {
16721693
var xWidth = this.xBarWidth / 2;
16731694
var yWidth = this.yBarWidth / 2;
16741695

1696+
// calculate the color based on the value
1697+
var hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240;
1698+
var color = this._hsv2rgb(hue, 1, 1);
1699+
var borderColor = this._hsv2rgb(hue, 1, 0.8);
1700+
1701+
this._redrawBar(ctx, point, xWidth, yWidth, color, borderColor);
1702+
};
1703+
1704+
1705+
/**
1706+
* Draw single datapoint for graph style 'bar-size'.
1707+
*/
1708+
Graph3d.prototype._redrawBarSizeGraphPoint = function(ctx, point) {
1709+
// calculate size for the bar
1710+
var fraction = (point.point.value - this.valueMin) / (this.valueMax - this.valueMin);
1711+
var xWidth = (this.xBarWidth / 2) * (fraction * 0.8 + 0.2);
1712+
var yWidth = (this.yBarWidth / 2) * (fraction * 0.8 + 0.2);
1713+
16751714

16761715
// determine color
1677-
var hue, color, borderColor;
1678-
// calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
1679-
hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240;
1680-
color = this._hsv2rgb(hue, 1, 1);
1681-
borderColor = this._hsv2rgb(hue, 1, 0.8);
1716+
var color = this.dataColor.fill;
1717+
var borderColor = this.dataColor.stroke;
16821718

16831719
this._redrawBar(ctx, point, xWidth, yWidth, color, borderColor);
16841720
};
@@ -1707,131 +1743,9 @@ Graph3d.prototype._redrawDataGraph = function(pointDrawMethod) {
17071743
};
17081744

17091745

1710-
/**
1711-
* Draw all datapoints as bars.
1712-
* This function can be used when the style is 'bar', 'bar-color', or 'bar-size'
1713-
*/
1714-
Graph3d.prototype._redrawDataBar = function() {
1715-
var ctx = this._getContext();
1716-
var i, j, surface, corners;
1717-
1718-
if (this.dataPoints === undefined || this.dataPoints.length <= 0)
1719-
return; // TODO: throw exception?
1720-
1721-
this._calcTranslations(this.dataPoints);
1722-
1723-
ctx.lineJoin = 'round';
1724-
ctx.lineCap = 'round';
1725-
1726-
// draw the datapoints as bars
1727-
var xWidth = this.xBarWidth / 2;
1728-
var yWidth = this.yBarWidth / 2;
1729-
for (i = 0; i < this.dataPoints.length; i++) {
1730-
var point = this.dataPoints[i];
1731-
1732-
// TODO: Remove code for style `Bar` here - it has been refactored to separate routine
1733-
1734-
// determine color
1735-
var hue, color, borderColor;
1736-
if (this.style === Graph3d.STYLE.BARCOLOR ) {
1737-
// calculate the color based on the value
1738-
hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240;
1739-
color = this._hsv2rgb(hue, 1, 1);
1740-
borderColor = this._hsv2rgb(hue, 1, 0.8);
1741-
}
1742-
else if (this.style === Graph3d.STYLE.BARSIZE) {
1743-
color = this.dataColor.fill;
1744-
borderColor = this.dataColor.stroke;
1745-
}
1746-
else {
1747-
// calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0
1748-
hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240;
1749-
color = this._hsv2rgb(hue, 1, 1);
1750-
borderColor = this._hsv2rgb(hue, 1, 0.8);
1751-
}
1752-
1753-
// calculate size for the bar
1754-
if (this.style === Graph3d.STYLE.BARSIZE) {
1755-
xWidth = (this.xBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2);
1756-
yWidth = (this.yBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2);
1757-
}
1758-
1759-
// calculate all corner points
1760-
var me = this;
1761-
var point3d = point.point;
1762-
var top = [
1763-
{point: new Point3d(point3d.x - xWidth, point3d.y - yWidth, point3d.z)},
1764-
{point: new Point3d(point3d.x + xWidth, point3d.y - yWidth, point3d.z)},
1765-
{point: new Point3d(point3d.x + xWidth, point3d.y + yWidth, point3d.z)},
1766-
{point: new Point3d(point3d.x - xWidth, point3d.y + yWidth, point3d.z)}
1767-
];
1768-
var bottom = [
1769-
{point: new Point3d(point3d.x - xWidth, point3d.y - yWidth, this.zMin)},
1770-
{point: new Point3d(point3d.x + xWidth, point3d.y - yWidth, this.zMin)},
1771-
{point: new Point3d(point3d.x + xWidth, point3d.y + yWidth, this.zMin)},
1772-
{point: new Point3d(point3d.x - xWidth, point3d.y + yWidth, this.zMin)}
1773-
];
1774-
1775-
// calculate screen location of the points
1776-
top.forEach(function (obj) {
1777-
obj.screen = me._convert3Dto2D(obj.point);
1778-
});
1779-
bottom.forEach(function (obj) {
1780-
obj.screen = me._convert3Dto2D(obj.point);
1781-
});
1782-
1783-
// create five sides, calculate both corner points and center points
1784-
var surfaces = [
1785-
{corners: top, center: Point3d.avg(bottom[0].point, bottom[2].point)},
1786-
{corners: [top[0], top[1], bottom[1], bottom[0]], center: Point3d.avg(bottom[1].point, bottom[0].point)},
1787-
{corners: [top[1], top[2], bottom[2], bottom[1]], center: Point3d.avg(bottom[2].point, bottom[1].point)},
1788-
{corners: [top[2], top[3], bottom[3], bottom[2]], center: Point3d.avg(bottom[3].point, bottom[2].point)},
1789-
{corners: [top[3], top[0], bottom[0], bottom[3]], center: Point3d.avg(bottom[0].point, bottom[3].point)}
1790-
];
1791-
point.surfaces = surfaces;
1792-
1793-
// calculate the distance of each of the surface centers to the camera
1794-
for (j = 0; j < surfaces.length; j++) {
1795-
surface = surfaces[j];
1796-
var transCenter = this._convertPointToTranslation(surface.center);
1797-
surface.dist = this.showPerspective ? transCenter.length() : -transCenter.z;
1798-
// TODO: this dept calculation doesn't work 100% of the cases due to perspective,
1799-
// but the current solution is fast/simple and works in 99.9% of all cases
1800-
// the issue is visible in example 14, with graph.setCameraPosition({horizontal: 2.97, vertical: 0.5, distance: 0.9})
1801-
}
1802-
1803-
// order the surfaces by their (translated) depth
1804-
surfaces.sort(function (a, b) {
1805-
var diff = b.dist - a.dist;
1806-
if (diff) return diff;
1807-
1808-
// if equal depth, sort the top surface last
1809-
if (a.corners === top) return 1;
1810-
if (b.corners === top) return -1;
1811-
1812-
// both are equal
1813-
return 0;
1814-
});
1815-
1816-
// draw the ordered surfaces
1817-
ctx.lineWidth = this._getStrokeWidth(point);
1818-
ctx.strokeStyle = borderColor;
1819-
ctx.fillStyle = color;
1820-
// NOTE: we start at j=2 instead of j=0 as we don't need to draw the two surfaces at the backside
1821-
for (j = 2; j < surfaces.length; j++) {
1822-
surface = surfaces[j];
1823-
corners = surface.corners;
1824-
ctx.beginPath();
1825-
ctx.moveTo(corners[3].screen.x, corners[3].screen.y);
1826-
ctx.lineTo(corners[0].screen.x, corners[0].screen.y);
1827-
ctx.lineTo(corners[1].screen.x, corners[1].screen.y);
1828-
ctx.lineTo(corners[2].screen.x, corners[2].screen.y);
1829-
ctx.lineTo(corners[3].screen.x, corners[3].screen.y);
1830-
ctx.fill();
1831-
ctx.stroke();
1832-
}
1833-
}
1834-
};
1746+
// -----------------------------------------------------------------------------
1747+
// End methods for drawing points per graph style.
1748+
// -----------------------------------------------------------------------------
18351749

18361750

18371751
/**

0 commit comments

Comments
 (0)