-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
relative barmode to stack negative values below axis #517
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
Changes from 1 commit
253048f
0cfc5aa
39d3fe2
e8f290f
61efab5
f4c4482
21c2346
9470570
f14e4b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,10 +116,11 @@ module.exports = function setPositions(gd, plotinfo) { | |
else barposition(bl); | ||
|
||
var stack = (fullLayout.barmode === 'stack'), | ||
relative = (fullLayout.barmode ==='relative'), | ||
norm = fullLayout.barnorm; | ||
|
||
// bar size range and stacking calculation | ||
if(stack || norm) { | ||
if(stack || relative || norm) { | ||
// for stacked bars, we need to evaluate every step in every | ||
// stack, because negative bars mean the extremes could be | ||
// anywhere | ||
|
@@ -142,13 +143,15 @@ module.exports = function setPositions(gd, plotinfo) { | |
ti = gd.calcdata[bl[i]]; | ||
for(j = 0; j < ti.length; j++) { | ||
sv = Math.round(ti[j].p / sumround); | ||
// store the negative sum value for p at the same key, with sign flipped | ||
if (relative && ti[j].s < 0) sv = -Math.round(ti[j].p / sumround); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could change this to |
||
var previousSum = sums[sv] || 0; | ||
if(stack) ti[j].b = previousSum; | ||
if(stack || relative) ti[j].b = previousSum; | ||
barEnd = ti[j].b + ti[j].s; | ||
sums[sv] = previousSum + ti[j].s; | ||
|
||
// store the bar top in each calcdata item | ||
if(stack) { | ||
if(stack || relative) { | ||
ti[j][sLetter] = barEnd; | ||
if(!norm && isNumeric(sa.c2l(barEnd))) { | ||
sMax = Math.max(sMax,barEnd); | ||
|
@@ -161,13 +164,18 @@ module.exports = function setPositions(gd, plotinfo) { | |
if(norm) { | ||
padded = false; | ||
var top = norm==='fraction' ? 1 : 100, | ||
isNegative = false, | ||
tiny = top/1e9; // in case of rounding error in sum | ||
sMin = 0; | ||
sMax = stack ? top : 0; | ||
for(i = 0; i < bl.length; i++) { // trace index | ||
ti = gd.calcdata[bl[i]]; | ||
for(j = 0; j < ti.length; j++) { | ||
scale = top / sums[Math.round(ti[j].p/sumround)]; | ||
relAndNegative = relative && ti[j].s < 0; | ||
sv = Math.round(ti[j].p/sumround); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 spacing around the division |
||
if (relAndNegative) sv = -Math.round(ti[j].p/sumround); // locate negative sum amount for this p val | ||
scale = top / sums[sv]; | ||
if (relAndNegative) scale *= -1; // preserve sign if negative | ||
ti[j].b *= scale; | ||
ti[j].s *= scale; | ||
barEnd = ti[j].b + ti[j].s; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"data":[ | ||
{"name":"Col1","y":["-1","2","3","4","5"],"x":["1","2","3","4","5"],"type":"bar","uid":"aeb9ea"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐄 Fix the alignment to be typical prettyprinted json. Single-line objects are fine if they fit in < 80 characters, but otherwise break them up. |
||
{"name":"Col2","y":["2","3","4","-3","2"],"x":["1","2","3","4","5"],"type":"bar","uid":"2f201d"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the |
||
{"name":"Col3","y":["5","4","3","-2","1"],"x":["1","2","3","4","5"],"type":"bar","uid":"aef0bf"}, | ||
{"name":"Col4","y":["-3","0","1","0","-3"],"x":["1","2","3","4","5"],"type":"bar","uid":"330b4d"} | ||
], | ||
"layout":{ | ||
"height":400, | ||
"width":400, | ||
"barmode":"relative" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"data":[ | ||
{"name":"Col1","y":["-1","2","3","4","5"],"x":["1","2","3","4","5"],"type":"bar","uid":"aeb9ea"}, | ||
{"name":"Col2","y":["2","3","4","-3","2"],"x":["1","2","3","4","5"],"type":"bar","uid":"2f201d"}, | ||
{"name":"Col3","y":["5","4","3","-2","1"],"x":["1","2","3","4","5"],"type":"bar","uid":"aef0bf"}, | ||
{"name":"Col4","y":["-3","0","1","0","-3"],"x":["1","2","3","4","5"],"type":"bar","uid":"330b4d"} | ||
], | ||
"layout":{ | ||
"height":400, | ||
"width":400, | ||
"barmode":"relative", | ||
"barnorm":"percent" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉