Skip to content

Commit ba1b6c0

Browse files
committed
refactor bar waterfall and funnle plot - rm prefix - pass opts
1 parent 34e0a4b commit ba1b6c0

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

src/traces/bar/plot.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,23 @@ function getXY(di, xa, ya, isHorizontal) {
4949
return isHorizontal ? [s, p] : [p, s];
5050
}
5151

52-
module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
52+
module.exports = function plot(gd, plotinfo, cdModule, traceLayer, opts) {
5353
var xa = plotinfo.xaxis;
5454
var ya = plotinfo.yaxis;
5555
var fullLayout = gd._fullLayout;
5656

57+
if(!opts) {
58+
opts = {
59+
mode: fullLayout.barmode,
60+
norm: fullLayout.barmode,
61+
gap: fullLayout.bargap,
62+
groupgap: fullLayout.bargroupgap
63+
};
64+
}
65+
5766
var bartraces = Lib.makeTraceGroups(traceLayer, cdModule, 'trace bars').each(function(cd) {
5867
var plotGroup = d3.select(this);
59-
var cd0 = cd[0];
60-
var trace = cd0.trace;
68+
var trace = cd[0].trace;
6169

6270
var adjustPixel = 0;
6371
if(trace.type === 'waterfall' && trace.connector.visible && trace.connector.mode === 'between') {
@@ -66,7 +74,7 @@ module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
6674

6775
var isHorizontal = (trace.orientation === 'h');
6876

69-
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
77+
if(!plotinfo.isRangePlot) cd[0].node3 = plotGroup;
7078

7179
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
7280

@@ -111,9 +119,6 @@ module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
111119

112120
var lw;
113121
var mc;
114-
var prefix =
115-
(trace.type === 'waterfall') ? 'waterfall' :
116-
(trace.type === 'funnel') ? 'funnel' : 'bar';
117122

118123
if(trace.type === 'waterfall') {
119124
if(!isBlank) {
@@ -128,13 +133,11 @@ module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
128133
}
129134

130135
var offset = d3.round((lw / 2) % 1, 2);
131-
var bargap = fullLayout[prefix + 'gap'];
132-
var bargroupgap = fullLayout[prefix + 'groupgap'];
133136

134137
function roundWithLine(v) {
135138
// if there are explicit gaps, don't round,
136139
// it can make the gaps look crappy
137-
return (bargap === 0 && bargroupgap === 0) ?
140+
return (opts.gap === 0 && opts.groupgap === 0) ?
138141
d3.round(Math.round(v) - offset, 2) : v;
139142
}
140143

@@ -169,7 +172,7 @@ module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
169172
.attr('d', isBlank ? 'M0,0Z' : 'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z')
170173
.call(Drawing.setClipUrl, plotinfo.layerClipId, gd);
171174

172-
appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, prefix);
175+
appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, opts);
173176

174177
if(plotinfo.layerClipId) {
175178
Drawing.hideOutsideRangePoint(di, bar.select('text'), xa, ya, trace.xcalendar, trace.ycalendar);
@@ -178,15 +181,15 @@ module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
178181

179182
// lastly, clip points groups of `cliponaxis !== false` traces
180183
// on `plotinfo._hasClipOnAxisFalse === true` subplots
181-
var hasClipOnAxisFalse = cd0.trace.cliponaxis === false;
184+
var hasClipOnAxisFalse = trace.cliponaxis === false;
182185
Drawing.setClipUrl(plotGroup, hasClipOnAxisFalse ? null : plotinfo.layerClipId, gd);
183186
});
184187

185188
// error bars are on the top
186189
Registry.getComponentMethod('errorbars', 'plot')(gd, bartraces, plotinfo);
187190
};
188191

189-
function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, prefix) {
192+
function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, opts) {
190193
var xa = plotinfo.xaxis;
191194
var ya = plotinfo.yaxis;
192195

@@ -218,14 +221,18 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, prefix)
218221
textPosition = getTextPosition(trace, i);
219222

220223
// compute text position
221-
var barmode = fullLayout[prefix + 'mode'];
222-
var inStackOrRelativeMode = barmode === 'stack' || barmode === 'relative';
224+
var inStackOrRelativeMode =
225+
opts.mode === 'stack' ||
226+
opts.mode === 'relative';
223227

224228
var calcBar = calcTrace[i];
225229
var isOutmostBar = !inStackOrRelativeMode || calcBar._outmost;
226230

227-
if(!text || textPosition === 'none' ||
228-
(calcBar.isBlank && (textPosition === 'auto' || textPosition === 'inside'))) {
231+
if(!text ||
232+
textPosition === 'none' ||
233+
(calcBar.isBlank && (
234+
textPosition === 'auto' ||
235+
textPosition === 'inside'))) {
229236
bar.select('text').remove();
230237
return;
231238
}
@@ -286,8 +293,11 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, prefix)
286293
(barWidth >= textWidth * (barHeight / textHeight)) :
287294
(barHeight >= textHeight * (barWidth / textWidth));
288295

289-
if(textHasSize &&
290-
(fitsInside || fitsInsideIfRotated || fitsInsideIfShrunk)) {
296+
if(textHasSize && (
297+
fitsInside ||
298+
fitsInsideIfRotated ||
299+
fitsInsideIfShrunk)
300+
) {
291301
textPosition = 'inside';
292302
} else {
293303
textPosition = 'outside';
@@ -317,11 +327,17 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, prefix)
317327
// compute text transform
318328
var transform, constrained;
319329
if(textPosition === 'outside') {
320-
constrained = trace.constraintext === 'both' || trace.constraintext === 'outside';
330+
constrained =
331+
trace.constraintext === 'both' ||
332+
trace.constraintext === 'outside';
333+
321334
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB,
322335
orientation, constrained);
323336
} else {
324-
constrained = trace.constraintext === 'both' || trace.constraintext === 'inside';
337+
constrained =
338+
trace.constraintext === 'both' ||
339+
trace.constraintext === 'inside';
340+
325341
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB,
326342
orientation, constrained, trace.insidetextanchor, trace.insidetextrotate === 'none');
327343
}

src/traces/funnel/plot.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ var Drawing = require('../../components/drawing');
1414
var barPlot = require('../bar/plot');
1515

1616
module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
17-
barPlot(gd, plotinfo, cdModule, traceLayer);
17+
var fullLayout = gd._fullLayout;
18+
19+
barPlot(gd, plotinfo, cdModule, traceLayer, {
20+
mode: fullLayout.funnelmode,
21+
norm: fullLayout.funnelmode,
22+
gap: fullLayout.funnelgap,
23+
groupgap: fullLayout.funnelgroupgap
24+
});
25+
1826
plotConnectorRegions(gd, plotinfo, cdModule, traceLayer);
1927
plotConnectorLines(gd, plotinfo, cdModule, traceLayer);
2028
};
@@ -25,8 +33,7 @@ function plotConnectorRegions(gd, plotinfo, cdModule, traceLayer) {
2533

2634
Lib.makeTraceGroups(traceLayer, cdModule, 'trace bars').each(function(cd) {
2735
var plotGroup = d3.select(this);
28-
var cd0 = cd[0];
29-
var trace = cd0.trace;
36+
var trace = cd[0].trace;
3037

3138
var group = Lib.ensureSingle(plotGroup, 'g', 'regions');
3239

@@ -77,8 +84,7 @@ function plotConnectorLines(gd, plotinfo, cdModule, traceLayer) {
7784

7885
Lib.makeTraceGroups(traceLayer, cdModule, 'trace bars').each(function(cd) {
7986
var plotGroup = d3.select(this);
80-
var cd0 = cd[0];
81-
var trace = cd0.trace;
87+
var trace = cd[0].trace;
8288

8389
var group = Lib.ensureSingle(plotGroup, 'g', 'lines');
8490

src/traces/waterfall/plot.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ var Drawing = require('../../components/drawing');
1414
var barPlot = require('../bar/plot');
1515

1616
module.exports = function plot(gd, plotinfo, cdModule, traceLayer) {
17-
barPlot(gd, plotinfo, cdModule, traceLayer);
17+
var fullLayout = gd._fullLayout;
18+
19+
barPlot(gd, plotinfo, cdModule, traceLayer, {
20+
mode: fullLayout.waterfallmode,
21+
norm: fullLayout.waterfallmode,
22+
gap: fullLayout.waterfallgap,
23+
groupgap: fullLayout.waterfallgroupgap
24+
});
25+
1826
plotConnectors(gd, plotinfo, cdModule, traceLayer);
1927
};
2028

@@ -24,8 +32,7 @@ function plotConnectors(gd, plotinfo, cdModule, traceLayer) {
2432

2533
Lib.makeTraceGroups(traceLayer, cdModule, 'trace bars').each(function(cd) {
2634
var plotGroup = d3.select(this);
27-
var cd0 = cd[0];
28-
var trace = cd0.trace;
35+
var trace = cd[0].trace;
2936

3037
var group = Lib.ensureSingle(plotGroup, 'g', 'lines');
3138

0 commit comments

Comments
 (0)