Skip to content

Commit 450ceda

Browse files
committed
export of few things in Pie.plot that sunburst will reuse
1 parent 4549692 commit 450ceda

File tree

2 files changed

+63
-46
lines changed

2 files changed

+63
-46
lines changed

src/traces/pie/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var calcModule = require('./calc');
1919
Pie.calc = calcModule.calc;
2020
Pie.crossTraceCalc = calcModule.crossTraceCalc;
2121

22-
Pie.plot = require('./plot');
22+
Pie.plot = require('./plot').plot;
2323
Pie.style = require('./style');
2424
Pie.styleOne = require('./style_one');
2525

src/traces/pie/plot.js

+62-45
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var svgTextUtils = require('../../lib/svg_text_utils');
1919
var helpers = require('./helpers');
2020
var eventData = require('./event_data');
2121

22-
module.exports = function plot(gd, cdpie) {
22+
function plot(gd, cdpie) {
2323
var fullLayout = gd._fullLayout;
2424

2525
prerenderTitles(cdpie, gd);
@@ -235,50 +235,8 @@ module.exports = function plot(gd, cdpie) {
235235

236236
// now make sure no labels overlap (at least within one pie)
237237
if(hasOutsideText) scootLabels(quadrants, trace);
238-
slices.each(function(pt) {
239-
if(pt.labelExtraX || pt.labelExtraY) {
240-
// first move the text to its new location
241-
var sliceTop = d3.select(this);
242-
var sliceText = sliceTop.select('g.slicetext text');
243-
244-
sliceText.attr('transform', 'translate(' + pt.labelExtraX + ',' + pt.labelExtraY + ')' +
245-
sliceText.attr('transform'));
246-
247-
// then add a line to the new location
248-
var lineStartX = pt.cxFinal + pt.pxmid[0];
249-
var lineStartY = pt.cyFinal + pt.pxmid[1];
250-
var textLinePath = 'M' + lineStartX + ',' + lineStartY;
251-
var finalX = (pt.yLabelMax - pt.yLabelMin) * (pt.pxmid[0] < 0 ? -1 : 1) / 4;
252-
253-
if(pt.labelExtraX) {
254-
var yFromX = pt.labelExtraX * pt.pxmid[1] / pt.pxmid[0];
255-
var yNet = pt.yLabelMid + pt.labelExtraY - (pt.cyFinal + pt.pxmid[1]);
256-
257-
if(Math.abs(yFromX) > Math.abs(yNet)) {
258-
textLinePath +=
259-
'l' + (yNet * pt.pxmid[0] / pt.pxmid[1]) + ',' + yNet +
260-
'H' + (lineStartX + pt.labelExtraX + finalX);
261-
} else {
262-
textLinePath += 'l' + pt.labelExtraX + ',' + yFromX +
263-
'v' + (yNet - yFromX) +
264-
'h' + finalX;
265-
}
266-
} else {
267-
textLinePath +=
268-
'V' + (pt.yLabelMid + pt.labelExtraY) +
269-
'h' + finalX;
270-
}
271238

272-
sliceTop.append('path')
273-
.classed('textline', true)
274-
.call(Color.stroke, trace.outsidetextfont.color)
275-
.attr({
276-
'stroke-width': Math.min(2, trace.outsidetextfont.size / 8),
277-
d: textLinePath,
278-
fill: 'none'
279-
});
280-
}
281-
});
239+
plotTextLines(slices, trace);
282240
});
283241
});
284242

@@ -294,7 +252,57 @@ module.exports = function plot(gd, cdpie) {
294252
if(s.attr('dy')) s.attr('dy', s.attr('dy'));
295253
});
296254
}, 0);
297-
};
255+
}
256+
257+
function plotTextLines(slices, trace) {
258+
slices.each(function(pt) {
259+
var sliceTop = d3.select(this);
260+
261+
if(!pt.labelExtraX && !pt.labelExtraY) {
262+
sliceTop.select('path.textline').remove();
263+
return;
264+
}
265+
266+
// first move the text to its new location
267+
var sliceText = sliceTop.select('g.slicetext text');
268+
269+
sliceText.attr('transform', 'translate(' + pt.labelExtraX + ',' + pt.labelExtraY + ')' +
270+
sliceText.attr('transform'));
271+
272+
// then add a line to the new location
273+
var lineStartX = pt.cxFinal + pt.pxmid[0];
274+
var lineStartY = pt.cyFinal + pt.pxmid[1];
275+
var textLinePath = 'M' + lineStartX + ',' + lineStartY;
276+
var finalX = (pt.yLabelMax - pt.yLabelMin) * (pt.pxmid[0] < 0 ? -1 : 1) / 4;
277+
278+
if(pt.labelExtraX) {
279+
var yFromX = pt.labelExtraX * pt.pxmid[1] / pt.pxmid[0];
280+
var yNet = pt.yLabelMid + pt.labelExtraY - (pt.cyFinal + pt.pxmid[1]);
281+
282+
if(Math.abs(yFromX) > Math.abs(yNet)) {
283+
textLinePath +=
284+
'l' + (yNet * pt.pxmid[0] / pt.pxmid[1]) + ',' + yNet +
285+
'H' + (lineStartX + pt.labelExtraX + finalX);
286+
} else {
287+
textLinePath += 'l' + pt.labelExtraX + ',' + yFromX +
288+
'v' + (yNet - yFromX) +
289+
'h' + finalX;
290+
}
291+
} else {
292+
textLinePath +=
293+
'V' + (pt.yLabelMid + pt.labelExtraY) +
294+
'h' + finalX;
295+
}
296+
297+
Lib.ensureSingle(sliceTop, 'path', 'textline')
298+
.call(Color.stroke, trace.outsidetextfont.color)
299+
.attr({
300+
'stroke-width': Math.min(2, trace.outsidetextfont.size / 8),
301+
d: textLinePath,
302+
fill: 'none'
303+
});
304+
});
305+
}
298306

299307
function attachFxHandlers(sliceTop, gd, cd) {
300308
var cd0 = cd[0];
@@ -879,3 +887,12 @@ function setCoords(cd) {
879887
cdi.largeArc = (cdi.v > cd0.vTotal / 2) ? 1 : 0;
880888
}
881889
}
890+
891+
module.exports = {
892+
plot: plot,
893+
plotTextLines: plotTextLines,
894+
895+
transformInsideText: transformInsideText,
896+
transformOutsideText: transformOutsideText,
897+
scootLabels: scootLabels
898+
};

0 commit comments

Comments
 (0)