-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add option for rounded corners on bar charts #6761
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 all commits
024f05f
31c3ac2
b0c6258
d71fe49
bb78c78
5dfd686
64912d4
cc1e6ff
dc2c54c
3b725fa
870d4c9
aa8ba1e
5845e0e
85f47c6
578712a
8f77cde
45689ae
137e185
795235b
dd717b6
35e0c8f
7aa7496
250bf31
495d950
be0ddff
819e8bf
ed69422
4a44291
65d3b55
e8cdde1
9c4227e
dabcaf0
701fd8c
29589e8
d0b9a90
3e5a957
582ce13
a238c87
ac329f7
8a607e6
338b4e6
60c918e
9dd0896
1628b5c
bfbd397
71e4896
4e794d0
dedce2a
b137b25
fdd6d03
8fcc3a5
71b53c3
90cf5c3
510c66b
5599ea6
8dffbbe
c165f6a
ee2451a
256a87e
b122521
015b881
8b57fb5
ec44d87
2913c6e
9e3c249
f986349
99352d0
6f685c0
f093ee7
e3cd5a5
98356b4
a2befea
781fbe2
97f0b0e
c52890c
10e38f4
f7d4b4a
4335ba0
e9043e9
4624132
8c45d15
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add `layout.barcornerradius` and `trace.marker.cornerradius` properties to support rounding the corners of bar traces [[#6761](https://github.com/plotly/plotly.js/pull/6761)], with thanks to [Displayr](https://www.displayr.com) for sponsoring development! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,10 @@ function setGroupPositions(gd, pa, sa, calcTraces, opts) { | |
else excluded.push(calcTrace); | ||
} | ||
|
||
// If any trace in `included` has a cornerradius, set cornerradius of all bars | ||
// in `included` to match the first trace which has a cornerradius | ||
standardizeCornerradius(included); | ||
|
||
if(included.length) { | ||
setGroupPositionsInStackOrRelativeMode(gd, pa, sa, included, opts); | ||
} | ||
|
@@ -119,10 +123,57 @@ function setGroupPositions(gd, pa, sa, calcTraces, opts) { | |
} | ||
break; | ||
} | ||
|
||
setCornerradius(calcTraces); | ||
collectExtents(calcTraces, pa); | ||
} | ||
|
||
// Set cornerradiusvalue and cornerradiusform in calcTraces[0].t | ||
function setCornerradius(calcTraces) { | ||
var i, calcTrace, fullTrace, t, cr, crValue, crForm; | ||
|
||
for(i = 0; i < calcTraces.length; i++) { | ||
calcTrace = calcTraces[i]; | ||
fullTrace = calcTrace[0].trace; | ||
t = calcTrace[0].t; | ||
|
||
if(t.cornerradiusvalue === undefined) { | ||
cr = fullTrace.marker ? fullTrace.marker.cornerradius : undefined; | ||
if(cr !== undefined) { | ||
crValue = isNumeric(cr) ? +cr : +cr.slice(0, -1); | ||
crForm = isNumeric(cr) ? 'px' : '%'; | ||
t.cornerradiusvalue = crValue; | ||
t.cornerradiusform = crForm; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Make sure all traces in a stack use the same cornerradius | ||
function standardizeCornerradius(calcTraces) { | ||
if(calcTraces.length < 2) return; | ||
var i, calcTrace, fullTrace, t; | ||
var cr, crValue, crForm; | ||
for(i = 0; i < calcTraces.length; i++) { | ||
calcTrace = calcTraces[i]; | ||
fullTrace = calcTrace[0].trace; | ||
cr = fullTrace.marker ? fullTrace.marker.cornerradius : undefined; | ||
if(cr !== undefined) break; | ||
} | ||
// If any trace has cornerradius, store first cornerradius | ||
// in calcTrace[0].t so that all traces in stack use same cornerradius | ||
if(cr !== undefined) { | ||
crValue = isNumeric(cr) ? +cr : +cr.slice(0, -1); | ||
crForm = isNumeric(cr) ? 'px' : '%'; | ||
for(i = 0; i < calcTraces.length; i++) { | ||
calcTrace = calcTraces[i]; | ||
t = calcTrace[0].t; | ||
|
||
t.cornerradiusvalue = crValue; | ||
t.cornerradiusform = crForm; | ||
} | ||
} | ||
} | ||
|
||
function initBase(sa, calcTraces) { | ||
var i, j; | ||
|
||
|
@@ -713,6 +764,23 @@ function normalizeBars(sa, sieve, opts) { | |
} | ||
} | ||
|
||
// Add an `_sMin` and `_sMax` value for each bar representing the min and max size value | ||
// across all bars sharing the same position as that bar. These values are used for rounded | ||
// bar corners, to carry rounding down to lower bars in the stack as needed. | ||
function setHelperValuesForRoundedCorners(calcTraces, sMinByPos, sMaxByPos, pa) { | ||
var pLetter = getAxisLetter(pa); | ||
// Set `_sMin` and `_sMax` value for each bar | ||
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. We only need these when having bars with rounded corners. 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. @archmoj Yes good point. Would it make sense then to check whether any of the traces in 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. Yes that's a good idea. |
||
for(var i = 0; i < calcTraces.length; i++) { | ||
var calcTrace = calcTraces[i]; | ||
for(var j = 0; j < calcTrace.length; j++) { | ||
var bar = calcTrace[j]; | ||
var pos = bar[pLetter]; | ||
bar._sMin = sMinByPos[pos]; | ||
bar._sMax = sMaxByPos[pos]; | ||
} | ||
} | ||
} | ||
|
||
// find the full position span of bars at each position | ||
// for use by hover, to ensure labels move in if bars are | ||
// narrower than the space they're in. | ||
|
@@ -745,6 +813,18 @@ function collectExtents(calcTraces, pa) { | |
return String(Math.round(roundFactor * (p - pMin))); | ||
}; | ||
|
||
// Find min and max size axis extent for each position | ||
// This is used for rounded bar corners, to carry rounding | ||
// down to lower bars in the case of stacked bars | ||
var sMinByPos = {}; | ||
var sMaxByPos = {}; | ||
|
||
// Check whether any trace has rounded corners | ||
var anyTraceHasCornerradius = calcTraces.some(function(x) { | ||
var trace = x[0].trace; | ||
return 'marker' in trace && trace.marker.cornerradius; | ||
}); | ||
|
||
for(i = 0; i < calcTraces.length; i++) { | ||
cd = calcTraces[i]; | ||
cd[0].t.extents = extents; | ||
|
@@ -770,8 +850,19 @@ function collectExtents(calcTraces, pa) { | |
di.p1 = di.p0 + di.w; | ||
di.s0 = di.b; | ||
di.s1 = di.s0 + di.s; | ||
|
||
if(anyTraceHasCornerradius) { | ||
var sMin = Math.min(di.s0, di.s1) || 0; | ||
var sMax = Math.max(di.s0, di.s1) || 0; | ||
var pos = di[pLetter]; | ||
sMinByPos[pos] = (pos in sMinByPos) ? Math.min(sMinByPos[pos], sMin) : sMin; | ||
sMaxByPos[pos] = (pos in sMaxByPos) ? Math.max(sMaxByPos[pos], sMax) : sMax; | ||
} | ||
} | ||
} | ||
if(anyTraceHasCornerradius) { | ||
setHelperValuesForRoundedCorners(calcTraces, sMinByPos, sMaxByPos, pa); | ||
} | ||
} | ||
|
||
function getAxisLetter(ax) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.