Skip to content

Commit ee974d9

Browse files
committed
autosizing in shareplots, autosize aspect ratio restrictions, and better logic for auto-axis-type for streaming
1 parent 968f9a4 commit ee974d9

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

shelly/shelly/templates/static/js/axes.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ axes.counterLetter = function(id) { return {x:'y',y:'x'}[id.charAt(0)]; };
123123

124124
function setType(ax){
125125
var axletter = ax._id.charAt(0),
126-
data = ax._td.data.filter(function(di){ return (di[axletter+'axis']||axletter)==ax._id; }),
127-
d0 = data[0]||{x:[0], y:[0]};
128-
if(!d0.type) { d0.type='scatter'; }
126+
data = (ax._td.data||[]).filter(function(di){ return (di[axletter+'axis']||axletter)==ax._id; });
129127
// backward compatibility
130128
if(!ax.type) {
131129
if(ax.isdate) { ax.type='date'; }
@@ -140,6 +138,11 @@ function setType(ax){
140138
delete ax.islog;
141139
delete ax.isdate;
142140

141+
if(!data.length) { return; }
142+
var d0 = data[0];
143+
if(!d0 || !d0.length) { return; }
144+
if(!d0.type) { d0.type='scatter'; }
145+
143146
// delete category list, if there is one, so we start over
144147
// to be filled in later by ax.d2c
145148
delete ax.categories; // obsolete (new one is private)
@@ -182,9 +185,16 @@ function setType(ax){
182185
axes.autoType = function(array) {
183186
if(axes.moreDates(array)) { return 'date'; }
184187
if(axes.category(array)) { return 'category'; }
185-
return 'linear';
188+
if(linearOK(array)) { return 'linear'; }
189+
else { return '-'; }
186190
};
187191

192+
// is there at least one number in array? If not, we should leave
193+
// ax.type empty so it can be autoset later
194+
function linearOK(array) {
195+
return array && array.some(function(v){ return $.isNumeric(v); });
196+
}
197+
188198
// does the array a have mostly dates rather than numbers?
189199
// note: some values can be neither (such as blanks, text)
190200
// 2- or 4-digit integers can be both, so require twice as many

shelly/shelly/templates/static/js/graph_obj.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,25 @@ function plotAutoSize(gd, aobj) {
12941294
var gdBB = gd.layout._container.node().getBoundingClientRect();
12951295
newheight = Math.round(gdBB.height*0.9);
12961296
newwidth = Math.round(gdBB.width*0.9);
1297+
1298+
// restrict aspect ratio to between 2:1 and 1:2, but only change height to do this
1299+
newheight = Plotly.Lib.constrain(newheight, newwidth/2, newwidth*2);
1300+
}
1301+
else if(gd.shareplot) {
1302+
newheight = $(window).height()-$('#banner').height();
1303+
newwidth = $(window).width()-parseInt($('#embedded-graph').css('padding-left')||0,10);
1304+
if(gd.standalone) {
1305+
// full-page shareplot - restrict aspect ratio to between 2:1 and 1:2,
1306+
// but only change height to do this
1307+
newheight = Plotly.Lib.constrain(newheight, newwidth/2, newwidth*2);
1308+
}
1309+
// else embedded in an iframe - just take the full iframe size if we get
1310+
// to this point, with no aspect ratio restrictions
12971311
}
12981312
else {
1313+
// plotly.js - let the developers do what they want, either provide height and width
1314+
// for the container div, specify size in layout, or take the defaults, but don't
1315+
// enforce any ratio restrictions
12991316
newheight = $(gd).height() || gd.layout.height || defaultLayout().height;
13001317
newwidth = $(gd).width() || gd.layout.width || defaultLayout().width;
13011318
// delete aobj.autosize;
@@ -1305,24 +1322,26 @@ function plotAutoSize(gd, aobj) {
13051322
gd.layout.height = newheight;
13061323
gd.layout.width = newwidth;
13071324
}
1308-
// if there's no size change, update layout but only restyle (different
1309-
// element may get margin color)
1325+
// if there's no size change, update layout but delete the autosize attr so we don't redraw
1326+
// REMOVED: call restyle (different element may get margin color)
13101327
else if(gd.layout.autosize != 'initial') { // can't call layoutStyles for initial autosize
13111328
delete(aobj.autosize);
13121329
gd.layout.autosize = true;
1313-
layoutStyles(gd);
1330+
// layoutStyles(gd);
13141331
}
13151332
return aobj;
13161333
}
13171334

13181335
// check whether to resize a tab (if it's a plot) to the container
13191336
plots.resize = function(gd) {
13201337
if(typeof gd == 'string') { gd = document.getElementById(gd); }
1321-
killPopovers();
13221338

1323-
if(gd.mainsite){ setFileAndCommentsHeight(gd); }
1339+
if(gd.mainsite){
1340+
killPopovers();
1341+
setFileAndCommentsHeight(gd);
1342+
}
13241343

1325-
if(gd && gd.tabtype=='plot' && $(gd).css('display')!='none') {
1344+
if(gd && (gd.tabtype=='plot' || gd.shareplot) && $(gd).css('display')!='none') {
13261345
if(gd.redrawTimer) { clearTimeout(gd.redrawTimer); }
13271346
gd.redrawTimer = setTimeout(function(){
13281347

@@ -1336,7 +1355,7 @@ plots.resize = function(gd) {
13361355
gd.changed = oldchanged; // autosizing doesn't count as a change
13371356
}
13381357

1339-
if(LIT) {
1358+
if(window.LIT) {
13401359
hidebox();
13411360
litebox();
13421361
}
@@ -1363,7 +1382,7 @@ function makePlotFramework(divid, layout) {
13631382
// test if this is on the main site or embedded
13641383
gd.mainsite = $('#plotlyMainMarker').length > 0;
13651384

1366-
// hook class for plots main container (in case of plotly.js this won't be #embedded_graph or .js-tab-contents)
1385+
// hook class for plots main container (in case of plotly.js this won't be #embedded-graph or .js-tab-contents)
13671386
// almost nobody actually needs this anymore, but just to be safe...
13681387
$gd.addClass('js-plotly-plot');
13691388

0 commit comments

Comments
 (0)