Skip to content

Commit c9d0fc6

Browse files
committed
Type changing between pie and other types
1 parent 66413bb commit c9d0fc6

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

shelly/plotlyjs/static/plotlyjs/src/axes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ function swapAxisGroup(gd, xIds, yIds) {
27132713
var ann = gd._fullLayout.annotations[i];
27142714
if(xIds.indexOf(ann.xref) !== -1 &&
27152715
yIds.indexOf(ann.yref) !== -1) {
2716-
Plotly.Lib.swapXYAttrs(layout.annotations[i],['?']);
2716+
Plotly.Lib.swapAttrs(layout.annotations[i],['?']);
27172717
}
27182718
}
27192719
}

shelly/plotlyjs/static/plotlyjs/src/graph_obj.js

+63-12
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ plots.supplyDefaults = function(gd) {
13301330
if(plots.traceIs(fullTrace, 'cartesian')) newFullLayout._hasCartesian = true;
13311331
else if(plots.traceIs(fullTrace, 'gl3d')) newFullLayout._hasGL3D = true;
13321332
else if(plots.traceIs(fullTrace, 'geo')) newFullLayout._hasGeo = true;
1333+
else if(plots.traceIs(fullTrace, 'pie')) newFullLayout._hasPie = true;
13331334
else if('r' in fullTrace) newFullLayout._hasPolar = true;
13341335

13351336
module = fullTrace._module;
@@ -1596,6 +1597,10 @@ plots.layoutAttributes = {
15961597
_hasGeo: {
15971598
type: 'boolean',
15981599
dflt: false
1600+
},
1601+
_hasPie: {
1602+
type: 'boolean',
1603+
dflt: false
15991604
}
16001605
};
16011606

@@ -1635,6 +1640,7 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
16351640
coerce('_hasCartesian');
16361641
coerce('_hasGL3D');
16371642
coerce('_hasGeo');
1643+
coerce('_hasPie');
16381644
};
16391645

16401646
plots.supplyLayoutModuleDefaults = function(layoutIn, layoutOut, fullData) {
@@ -2487,7 +2493,8 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
24872493
// for the undo / redo queue
24882494
var redoit = {},
24892495
undoit = {},
2490-
axlist;
2496+
axlist,
2497+
flagAxForDelete = [];
24912498

24922499
// for now, if we detect 3D or geo stuff, just re-do the plot
24932500
if(fullLayout._hasGL3D || fullLayout._hasGeo) doplot = true;
@@ -2506,6 +2513,7 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
25062513
// for attrs that interact (like scales & autoscales), save the
25072514
// old vals before making the change
25082515
// val=undefined will not set a value, just record what the value was.
2516+
// val=null will delete the attribute
25092517
// attr can be an array to set several at once (all to the same val)
25102518
function doextra(cont,attr,val,i) {
25112519
if(Array.isArray(attr)) {
@@ -2536,7 +2544,8 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
25362544
var vi = aobj[ai],
25372545
cont,
25382546
contFull,
2539-
param;
2547+
param,
2548+
oldVal;
25402549
redoit[ai] = vi;
25412550

25422551
if(layoutAttrs.indexOf(ai.replace(/[xyz]axis[0-9]*/g, '?axis'))!==-1){
@@ -2557,6 +2566,7 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
25572566
cont = gd.data[traces[i]];
25582567
contFull = gd._fullData[traces[i]];
25592568
param = Plotly.Lib.nestedProperty(cont,ai);
2569+
oldVal = param.get();
25602570

25612571
// setting bin or z settings should turn off auto
25622572
// and setting auto should save bin or z settings
@@ -2638,8 +2648,29 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
26382648
doextra(cont, ['colorbar.tick0', 'colorbar.dtick'], undefined);
26392649
}
26402650

2641-
// save the old value
2642-
undoit[ai][i] = param.get();
2651+
2652+
if(ai === 'type' && (vi === 'pie') !== (oldVal === 'pie')) {
2653+
var labelsTo = 'x',
2654+
valuesTo = 'y';
2655+
if((vi === 'bar' || oldVal === 'bar') && cont.orientation === 'h') {
2656+
labelsTo = 'y';
2657+
valuesTo = 'x';
2658+
}
2659+
Plotly.Lib.swapAttrs(cont, ['?', '?src'], 'labels', labelsTo);
2660+
Plotly.Lib.swapAttrs(cont, ['d?', '?0'], 'label', labelsTo);
2661+
Plotly.Lib.swapAttrs(cont, ['?', '?src'], 'values', valuesTo);
2662+
2663+
if(oldVal === 'pie') {
2664+
// super kludgy - but if all pies are gone we won't remove them otherwise
2665+
fullLayout._pielayer.selectAll('g.trace').remove();
2666+
} else if(plots.traceIs(cont, 'cartesian')) {
2667+
//look for axes that are no longer in use and delete them
2668+
flagAxForDelete.push(cont.xaxis || 'x');
2669+
flagAxForDelete.push(cont.yaxis || 'y');
2670+
}
2671+
}
2672+
2673+
undoit[ai][i] = oldVal;
26432674
// set the new value - if val is an array, it's one el per trace
26442675
// first check for attributes that get more complex alterations
26452676
var swapAttrs = [
@@ -2706,12 +2737,14 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
27062737
for(i=0; i<traces.length; i++) {
27072738
var trace = gd.data[traces[i]];
27082739

2709-
addToAxlist(trace.xaxis||'x');
2710-
addToAxlist(trace.yaxis||'y');
2740+
if(plots.traceIs(trace, 'cartesian')) {
2741+
addToAxlist(trace.xaxis||'x');
2742+
addToAxlist(trace.yaxis||'y');
27112743

2712-
if(astr==='type') {
2713-
doextra(gd.data[traces[i]],
2714-
['autobinx','autobiny'],true,i);
2744+
if(astr==='type') {
2745+
doextra(gd.data[traces[i]],
2746+
['autobinx','autobiny'],true,i);
2747+
}
27152748
}
27162749
}
27172750

@@ -2723,6 +2756,24 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
27232756
else if(replotAttrs.indexOf(ai)!==-1) doplot = true;
27242757
else if(autorangeAttrs.indexOf(ai)!==-1) docalcAutorange = true;
27252758
}
2759+
2760+
// check axes we've flagged for possible deletion
2761+
axisLoop:
2762+
for(i = 0; i < flagAxForDelete.length; i++) {
2763+
var axId = flagAxForDelete[i],
2764+
axLetter = axId.charAt(0),
2765+
axAttr = axLetter + 'axis';
2766+
for(var j = 0; j < gd.data.length; j++) {
2767+
if(plots.traceIs(gd.data[j], 'cartesian') &&
2768+
(gd.data[j][axAttr] || axLetter) === axId) {
2769+
continue axisLoop;
2770+
}
2771+
}
2772+
2773+
// no data on this axis - delete it.
2774+
doextra(gd.layout, Plotly.Axes.id2name(axId), null, 0);
2775+
}
2776+
27262777
// now all attribute mods are done, as are redo and undo
27272778
// so we can save them
27282779
if(Plotly.Queue) {
@@ -2809,7 +2860,7 @@ Plotly.restyle = function restyle (gd,astr,val,traces) {
28092860
// swap all the data and data attributes associated with x and y
28102861
function swapXYData(trace) {
28112862
var i;
2812-
Plotly.Lib.swapXYAttrs(trace, ['?', '?0', 'd?', '?bins', 'nbins?', 'autobin?', '?src', 'error_?']);
2863+
Plotly.Lib.swapAttrs(trace, ['?', '?0', 'd?', '?bins', 'nbins?', 'autobin?', '?src', 'error_?']);
28132864
if(Array.isArray(trace.z) && Array.isArray(trace.z[0])) {
28142865
if(trace.transpose) delete trace.transpose;
28152866
else trace.transpose = true;
@@ -2818,9 +2869,9 @@ function swapXYData(trace) {
28182869
var errorY = trace.error_y,
28192870
copyYstyle = ('copy_ystyle' in errorY) ? errorY.copy_ystyle :
28202871
!(errorY.color || errorY.thickness || errorY.width);
2821-
Plotly.Lib.swapXYAttrs(trace, ['error_?.copy_ystyle']);
2872+
Plotly.Lib.swapAttrs(trace, ['error_?.copy_ystyle']);
28222873
if(copyYstyle) {
2823-
Plotly.Lib.swapXYAttrs(trace, ['error_?.color', 'error_?.thickness', 'error_?.width']);
2874+
Plotly.Lib.swapAttrs(trace, ['error_?.color', 'error_?.thickness', 'error_?.width']);
28242875
}
28252876
}
28262877
if(trace.hoverinfo) {

shelly/plotlyjs/static/plotlyjs/src/lib.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,15 @@ function badContainer(container, propStr, propParts) {
662662
/**
663663
* swap x and y of the same attribute in container cont
664664
* specify attr with a ? in place of x/y
665+
* you can also swap other things than x/y by providing part1 and part2
665666
*/
666-
lib.swapXYAttrs = function(cont,attrList) {
667+
lib.swapAttrs = function(cont, attrList, part1, part2) {
668+
if(!part1) part1 = 'x';
669+
if(!part2) part2 = 'y';
667670
for(var i = 0; i < attrList.length; i++) {
668671
var attr = attrList[i],
669-
xp = lib.nestedProperty(cont, attr.replace('?', 'x')),
670-
yp = lib.nestedProperty(cont, attr.replace('?', 'y')),
672+
xp = lib.nestedProperty(cont, attr.replace('?', part1)),
673+
yp = lib.nestedProperty(cont, attr.replace('?', part2)),
671674
temp = xp.get();
672675
xp.set(yp.get());
673676
yp.set(temp);

0 commit comments

Comments
 (0)