Skip to content

Add barpolar traces #2954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
347979c
expose setGroupPositions from bar/set_positions.js
etpinard Aug 2, 2018
8ca4101
don't set undefined class names via Lib.ensureSingle
etpinard Aug 28, 2018
52ed444
:hocho: now useless _module.style loop
etpinard Aug 15, 2018
d35d13b
DRY up polar mock cartesian axis 'range' logic
etpinard Aug 15, 2018
eb9307e
rename Polar.prototype.isPtWithinSector -> isPtInside
etpinard Aug 24, 2018
4d20388
move a few things to lib/angles.js + add isPtInsideSector
etpinard Aug 24, 2018
95119c4
move polar polygon logic to polar/helper.js
etpinard Aug 24, 2018
facb2f8
mv arc/sector/annular path fn to angles.js
etpinard Aug 24, 2018
5ea3d52
update polar baselines post pathSector/pathArc cleanup
etpinard Aug 27, 2018
a328b1a
add barpolar :tada:
etpinard Jul 24, 2018
dcdd6b5
fix 'date' radial polar axes
etpinard Aug 28, 2018
6edd45e
:lock: barpolar on funky subplots
etpinard Aug 28, 2018
7ae3b7c
bullet-proof barpolar hover
etpinard Aug 28, 2018
0f62c0f
add barpolar mock to svg mock list
etpinard Aug 28, 2018
235b425
add barpolar hover tests
etpinard Aug 28, 2018
6b5895f
add barpolar select test
etpinard Aug 23, 2018
296f088
fixup jsDoc header
etpinard Aug 27, 2018
a32484a
do not .classed() layers in ensureSingle if className not given
etpinard Aug 31, 2018
e938974
standardize API of polar internal routine
etpinard Aug 31, 2018
cd05d3d
confirm use of tip-of-bar/mid-angle pt for selection and hover labels
etpinard Aug 31, 2018
971c6f6
make reversed-radial-axis hover logic more readable
etpinard Aug 31, 2018
e6c4dad
align hover label to the left for negative radial coords
etpinard Aug 31, 2018
01e15b4
improve barpolar attribute descriptions
etpinard Aug 31, 2018
160f742
:hocho: comment of rounding up bar borders
etpinard Aug 31, 2018
6494302
change polar bargap 0.2 -> 0.2
etpinard Sep 4, 2018
183b734
set miter-limit to 2 + :lock: in polar_bar-overlay mock
etpinard Sep 4, 2018
452e792
compute bar corners p0/p1/s0/s1 in crossTraceCalc
etpinard Sep 5, 2018
e792bc8
add 1e-15 tolerance in isFullCircle
etpinard Sep 5, 2018
8575b27
replace wrap360 with Lib.mod & wrap180 with new Lib.modHalf
etpinard Sep 5, 2018
939bdad
use mod to compute angle delta
etpinard Sep 5, 2018
d2484f3
make modHalf restrict output to "half" of Math.abs(input)
etpinard Sep 5, 2018
235fe5b
fixup doc for Lib.modHalf
etpinard Sep 5, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 103 additions & 9 deletions src/lib/angles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,119 @@

var PI = Math.PI;

exports.deg2rad = function(deg) {
function deg2rad(deg) {
return deg / 180 * PI;
};
}

exports.rad2deg = function(rad) {
function rad2deg(rad) {
return rad / PI * 180;
};
}

exports.wrap360 = function(deg) {
function wrap360(deg) {
var out = deg % 360;
return out < 0 ? out + 360 : out;
};
}

exports.wrap180 = function(deg) {
function wrap180(deg) {
if(Math.abs(deg) > 180) deg -= Math.round(deg / 360) * 360;
return deg;
};
}

exports.isFullCircle = function(sector) {
/* is sector a full circle?
* ... this comes up a lot in SVG path-drawing routines
*
* @param {2-item array} sector sector angles in *degrees*
* @return {boolean}
*/
function isFullCircle(sector) {
var arc = Math.abs(sector[1] - sector[0]);
return arc === 360;
}

/* angular delta between angle 'a' and 'b'
*
* solution taken from: https://stackoverflow.com/a/2007279
*
* @param {number} a : first angle in *radians*
* @param {number} b : second angle in *radians*
* @return {number} angular delta in *radians*
*/
function angleDelta(a, b) {
var d = b - a;
return Math.atan2(Math.sin(d), Math.cos(d));
}

/* angular distance between angle 'a' and 'b'
*
* @param {number} a : first angle in *radians*
* @param {number} b : second angle in *radians*
* @return {number} angular distance in *radians*
*/
function angleDist(a, b) {
return Math.abs(angleDelta(a, b));
}

/* is angle inside sector?
*
* @param {number} a : angle to test in *radians*
* @param {2-item array} sector : sector angles in *degrees*
* @param {boolean}
*/
function isAngleInsideSector(a, sector) {
if(isFullCircle(sector)) return true;

var s0, s1;

if(sector[0] < sector[1]) {
s0 = sector[0];
s1 = sector[1];
} else {
s0 = sector[1];
s1 = sector[0];
}

s0 = wrap360(s0);
s1 = wrap360(s1);
if(s0 > s1) s1 += 360;

var a0 = wrap360(rad2deg(a));
var a1 = a0 + 360;

return (a0 >= s0 && a0 <= s1) || (a1 >= s0 && a1 <= s1);
}

/* is pt (r,a) inside sector?
*
* @param {number} r : pt's radial coordinate
* @param {number} a : pt's angular coordinate in *radians*
* @param {2-item array} rRng : sector's radial range
* @param {2-item array} sector : sector angles in *degrees*
* @return {boolean}
*/
function isPtInsideSector(r, a, rRng, sector) {
if(!isAngleInsideSector(a, sector)) return false;

var r0, r1;

if(rRng[0] < rRng[1]) {
r0 = rRng[0];
r1 = rRng[1];
} else {
r0 = rRng[1];
r1 = rRng[0];
}

return r >= r0 && r <= r1;
}

module.exports = {
deg2rad: deg2rad,
rad2deg: rad2deg,
wrap360: wrap360,
wrap180: wrap180,
angleDelta: angleDelta,
angleDist: angleDist,
isFullCircle: isFullCircle,
isAngleInsideSector: isAngleInsideSector,
isPtInsideSector: isPtInsideSector
};
4 changes: 4 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ lib.deg2rad = anglesModule.deg2rad;
lib.rad2deg = anglesModule.rad2deg;
lib.wrap360 = anglesModule.wrap360;
lib.wrap180 = anglesModule.wrap180;
lib.angleDelta = anglesModule.angleDelta;
lib.angleDist = anglesModule.angleDist;
lib.isFullCircle = anglesModule.isFullCircle;
lib.isAngleInsideSector = anglesModule.isAngleInsideSector;
lib.isPtInsideSector = anglesModule.isPtInsideSector;

var geom2dModule = require('./geometry2d');
lib.segmentsIntersect = geom2dModule.segmentsIntersect;
Expand Down
42 changes: 10 additions & 32 deletions src/plots/polar/polar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var rad2deg = Lib.rad2deg;
var wrap360 = Lib.wrap360;
var wrap180 = Lib.wrap180;
var isFullCircle = Lib.isFullCircle;
var isAngleInsideSector = Lib.isAngleInsideSector;
var angleDelta = Lib.angleDelta;

function Polar(gd, id) {
this.id = id;
Expand Down Expand Up @@ -501,7 +503,9 @@ proto.updateAngularAxis = function(fullLayout, polarLayout) {
// the range w.r.t sector, so that sectors that cross 360 can
// show all their ticks.
if(ax.type === 'category') {
ax._tickFilter = function(d) { return isAngleInSector(t2g(d), sector); };
ax._tickFilter = function(d) {
return isAngleInsideSector(t2g(d), sector);
};
}

ax._transfn = function(d) {
Expand Down Expand Up @@ -1194,16 +1198,12 @@ proto.updateAngularDrag = function(fullLayout, polarLayout) {

proto.isPtInside = function(d) {
var sector = this.sector;
var thetag = this.angularAxis.c2g(d.theta);

if(!isAngleInSector(thetag, sector)) {
return false;
}

var vangles = this.vangles;
var thetag = this.angularAxis.c2g(d.theta);
var radialAxis = this.radialAxis;
var radialRange = radialAxis.range;
var r = radialAxis.c2r(d.r);
var rRng = radialAxis.range;

var r0, r1;
if(radialRange[1] >= radialRange[0]) {
Expand All @@ -1214,6 +1214,7 @@ proto.isPtInside = function(d) {
r1 = radialRange[0];
}

return Lib.isPtInsideSector(r, thetag, rRng, sector);
if(vangles) {
var polygonIn = polygonTester(makePolygon(r0, sector, vangles));
var polygonOut = polygonTester(makePolygon(r1, sector, vangles));
Expand Down Expand Up @@ -1291,35 +1292,12 @@ function computeSectorBBox(sector) {
return [x0, y0, x1, y1];
}

function isAngleInSector(rad, sector) {
if(isFullCircle(sector)) return true;

var s0 = wrap360(sector[0]);
var s1 = wrap360(sector[1]);
if(s0 > s1) s1 += 360;

var deg = wrap360(rad2deg(rad));
var nextTurnDeg = deg + 360;

return (deg >= s0 && deg <= s1) ||
(nextTurnDeg >= s0 && nextTurnDeg <= s1);
}

function snapToVertexAngle(a, vangles) {
function angleDeltaAbs(va) {
return Math.abs(angleDelta(a, va));
}

var ind = findIndexOfMin(vangles, angleDeltaAbs);
var fn = function(v) { return Lib.angleDist(a, v); };
var ind = findIndexOfMin(vangles, fn);
return vangles[ind];
}

// taken from https://stackoverflow.com/a/2007279
function angleDelta(a, b) {
var d = b - a;
return Math.atan2(Math.sin(d), Math.cos(d));
}

function findIndexOfMin(arr, fn) {
fn = fn || Lib.identity;

Expand Down