Skip to content

Commit f0b99ee

Browse files
committed
revamp the mode bar's createIcon to better handle custom icons
1 parent 0673823 commit f0b99ee

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/components/modebar/modebar.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,13 @@ proto.createButton = function(config) {
155155
button.setAttribute('data-toggle', config.toggle || false);
156156
if(config.toggle) d3.select(button).classed('active', true);
157157

158-
button.appendChild(this.createIcon(config.icon || Icons.question, config.name));
158+
var icon = config.icon;
159+
if(typeof icon === 'function') {
160+
button.appendChild(icon());
161+
}
162+
else {
163+
button.appendChild(this.createIcon(icon || Icons.question, config.name));
164+
}
159165
button.setAttribute('data-gravity', config.gravity || 'n');
160166

161167
return button;
@@ -169,7 +175,9 @@ proto.createButton = function(config) {
169175
* @Return {HTMLelement}
170176
*/
171177
proto.createIcon = function(thisIcon, name) {
172-
var iconHeight = thisIcon.ascent - thisIcon.descent,
178+
var iconHeight = thisIcon.height !== undefined ?
179+
thisIcon.height :
180+
thisIcon.ascent - thisIcon.descent,
173181
svgNS = 'http://www.w3.org/2000/svg',
174182
icon = document.createElementNS(svgNS, 'svg'),
175183
path = document.createElementNS(svgNS, 'path');
@@ -178,12 +186,19 @@ proto.createIcon = function(thisIcon, name) {
178186
icon.setAttribute('width', (thisIcon.width / iconHeight) + 'em');
179187
icon.setAttribute('viewBox', [0, 0, thisIcon.width, iconHeight].join(' '));
180188

181-
var transform = name === 'toggleSpikelines' ?
182-
'matrix(1.5 0 0 -1.5 0 ' + thisIcon.ascent + ')' :
183-
'matrix(1 0 0 -1 0 ' + thisIcon.ascent + ')';
184-
185189
path.setAttribute('d', thisIcon.path);
186-
path.setAttribute('transform', transform);
190+
191+
if(thisIcon.transform) {
192+
path.setAttribute('transform', thisIcon.transform);
193+
}
194+
else if(thisIcon.ascent) {
195+
// Legacy icon transform calculation
196+
var transform = name === 'toggleSpikelines' ?
197+
'matrix(1.5 0 0 -1.5 0 ' + thisIcon.ascent + ')' :
198+
'matrix(1 0 0 -1 0 ' + thisIcon.ascent + ')';
199+
path.setAttribute('transform', transform);
200+
}
201+
187202
icon.appendChild(path);
188203

189204
return icon;

0 commit comments

Comments
 (0)