Skip to content

Commit f55e769

Browse files
committed
pre-collate counteraxes and subplot ids for each axis
this means Axes.getSubplots and Axes.findSubplotsWithAxis are essentially unused internally, but we have some outside callers using getSubplots
1 parent 68993c3 commit f55e769

File tree

5 files changed

+41
-35
lines changed

5 files changed

+41
-35
lines changed

src/components/rangeslider/draw.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var Color = require('../color');
1919
var Titles = require('../titles');
2020

2121
var Cartesian = require('../../plots/cartesian');
22-
var Axes = require('../../plots/cartesian/axes');
22+
var axisIDs = require('../../plots/cartesian/axis_ids');
2323

2424
var dragElement = require('../dragelement');
2525
var setCursor = require('../../lib/setcursor');
@@ -77,8 +77,8 @@ module.exports = function(gd) {
7777
rangeSliders.each(function(axisOpts) {
7878
var rangeSlider = d3.select(this);
7979
var opts = axisOpts[constants.name];
80-
var oppAxisOpts = fullLayout[Axes.id2name(axisOpts.anchor)];
81-
var oppAxisRangeOpts = opts[Axes.id2name(axisOpts.anchor)];
80+
var oppAxisOpts = fullLayout[axisIDs.id2name(axisOpts.anchor)];
81+
var oppAxisRangeOpts = opts[axisIDs.id2name(axisOpts.anchor)];
8282

8383
// update range
8484
// Expand slider range to the axis range
@@ -102,12 +102,7 @@ module.exports = function(gd) {
102102
var domain = axisOpts.domain;
103103
var tickHeight = (axisOpts._boundingBox || {}).height || 0;
104104

105-
var oppBottom = Infinity;
106-
var subplotData = Axes.getSubplots(gd, axisOpts);
107-
for(var i = 0; i < subplotData.length; i++) {
108-
var oppAxis = Axes.getFromId(gd, subplotData[i].substr(subplotData[i].indexOf('y')));
109-
oppBottom = Math.min(oppBottom, oppAxis.domain[0]);
110-
}
105+
var oppBottom = opts._oppBottom;
111106

112107
opts._width = graphSize.w * (domain[1] - domain[0]);
113108

@@ -366,11 +361,10 @@ function addClipPath(rangeSlider, gd, axisOpts, opts) {
366361
}
367362

368363
function drawRangePlot(rangeSlider, gd, axisOpts, opts) {
369-
var subplotData = Axes.getSubplots(gd, axisOpts),
370-
calcData = gd.calcdata;
364+
var calcData = gd.calcdata;
371365

372366
var rangePlots = rangeSlider.selectAll('g.' + constants.rangePlotClassName)
373-
.data(subplotData, Lib.identity);
367+
.data(axisOpts._subplotsWith, Lib.identity);
374368

375369
rangePlots.enter().append('g')
376370
.attr('class', function(id) { return constants.rangePlotClassName + ' ' + id; })
@@ -386,7 +380,7 @@ function drawRangePlot(rangeSlider, gd, axisOpts, opts) {
386380
var plotgroup = d3.select(this),
387381
isMainPlot = (i === 0);
388382

389-
var oppAxisOpts = Axes.getFromId(gd, id, 'y'),
383+
var oppAxisOpts = axisIDs.getFromId(gd, id, 'y'),
390384
oppAxisName = oppAxisOpts._name,
391385
oppAxisRangeOpts = opts[oppAxisName];
392386

src/components/rangeslider/helpers.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
var Axes = require('../../plots/cartesian/axes');
11+
var axisIDs = require('../../plots/cartesian/axis_ids');
1212
var constants = require('./constants');
1313
var name = constants.name;
1414

@@ -19,7 +19,7 @@ function isVisible(ax) {
1919
exports.isVisible = isVisible;
2020

2121
exports.makeData = function(fullLayout) {
22-
var axes = Axes.list({ _fullLayout: fullLayout }, 'x', true);
22+
var axes = axisIDs.list({ _fullLayout: fullLayout }, 'x', true);
2323
var margin = fullLayout.margin;
2424
var rangeSliderData = [];
2525

@@ -45,12 +45,13 @@ exports.autoMarginOpts = function(gd, ax) {
4545
var opts = ax[name];
4646

4747
var oppBottom = Infinity;
48-
var subplotData = Axes.getSubplots(gd, ax);
49-
for(var j = 0; j < subplotData.length; j++) {
50-
var subplotj = subplotData[j];
51-
var oppAxis = Axes.getFromId(gd, subplotj.substr(subplotj.indexOf('y')));
48+
var counterAxes = ax._counterAxes;
49+
for(var j = 0; j < counterAxes.length; j++) {
50+
var counterId = counterAxes[j];
51+
var oppAxis = axisIDs.getFromId(gd, counterId);
5252
oppBottom = Math.min(oppBottom, oppAxis.domain[0]);
5353
}
54+
opts._oppBottom = oppBottom;
5455

5556
var tickHeight = (ax.side === 'bottom' && ax._boundingBox.height) || 0;
5657

src/plots/cartesian/axes.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,9 @@ axes.getTickFormat = function(ax) {
14671467
// as an array of items like 'xy', 'x2y', 'x2y2'...
14681468
// sorted by x (x,x2,x3...) then y
14691469
// optionally restrict to only subplots containing axis object ax
1470+
//
1471+
// NOTE: this is currently only used OUTSIDE plotly.js (toolpanel, webapp)
1472+
// ideally we get rid of it there (or just copy this there) and remove it here
14701473
axes.getSubplots = function(gd, ax) {
14711474
var subplotObj = gd._fullLayout._subplots;
14721475
var allSubplots = subplotObj.cartesian.concat(subplotObj.gl2d || []);
@@ -1485,6 +1488,8 @@ axes.getSubplots = function(gd, ax) {
14851488
};
14861489

14871490
// find all subplots with axis 'ax'
1491+
// NOTE: this is only used in axes.getSubplots (only used outside plotly.js) and
1492+
// gl2d/convert (where it restricts axis subplots to only those with gl2d)
14881493
axes.findSubplotsWithAxis = function(subplots, ax) {
14891494
var axMatch = new RegExp(
14901495
(ax._id.charAt(0) === 'x') ? ('^' + ax._id + 'y') : (ax._id + '$')
@@ -1631,7 +1636,7 @@ axes.drawOne = function(gd, ax, opts) {
16311636
var mainMirrorPosition = ax._mainMirrorPosition;
16321637
var mainPlotinfo = fullLayout._plots[mainSubplot];
16331638
var mainAxLayer = mainPlotinfo[axLetter + 'axislayer'];
1634-
var subplotsWithAx = axes.getSubplots(gd, ax);
1639+
var subplotsWithAx = ax._subplotsWith;
16351640

16361641
var vals = ax._vals = axes.calcTicks(ax);
16371642

src/plots/cartesian/layout_defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
156156
axLayoutOut._traceIndices = traces.map(function(t) { return t._expandedIndex; });
157157
axLayoutOut._annIndices = [];
158158
axLayoutOut._shapeIndices = [];
159+
axLayoutOut._subplotsWith = [];
160+
axLayoutOut._counterAxes = [];
159161

160162
// set up some private properties
161163
axLayoutOut._name = axLayoutOut._attr = axName;

src/plots/plots.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,12 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
811811
plotinfo.id = id;
812812
}
813813

814+
// add these axis ids to each others' subplot lists
815+
xaxis._counterAxes.push(yaxis._id);
816+
yaxis._counterAxes.push(xaxis._id);
817+
xaxis._subplotsWith.push(id);
818+
yaxis._subplotsWith.push(id);
819+
814820
// update x and y axis layout object refs
815821
plotinfo.xaxis = xaxis;
816822
plotinfo.yaxis = yaxis;
@@ -873,11 +879,13 @@ plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLa
873879
// (on which the ticks & labels are drawn)
874880
for(i = 0; i < axList.length; i++) {
875881
ax = axList[i];
876-
ax._mainSubplot = findMainSubplot(ax, newFullLayout, ids);
882+
ax._counterAxes.sort(axisIDs.idSort);
883+
ax._subplotsWith.sort(Lib.subplotSort);
884+
ax._mainSubplot = findMainSubplot(ax, newFullLayout);
877885
}
878886
};
879887

880-
function findMainSubplot(ax, fullLayout, ids) {
888+
function findMainSubplot(ax, fullLayout) {
881889
var mockGd = {_fullLayout: fullLayout};
882890

883891
var isX = ax._id.charAt(0) === 'x';
@@ -897,19 +905,15 @@ function findMainSubplot(ax, fullLayout, ids) {
897905
if(!mainSubplotID || !fullLayout._plots[mainSubplotID]) {
898906
mainSubplotID = '';
899907

900-
for(var j = 0; j < ids.length; j++) {
901-
var id = ids[j];
902-
var yIndex = id.indexOf('y');
903-
var idPart = isX ? id.substr(0, yIndex) : id.substr(yIndex);
904-
var counterPart = isX ? id.substr(yIndex) : id.substr(0, yIndex);
905-
906-
if(idPart === ax._id) {
907-
if(!nextBestMainSubplotID) nextBestMainSubplotID = id;
908-
var counterAx = axisIDs.getFromId(mockGd, counterPart);
909-
if(anchorID && counterAx.overlaying === anchorID) {
910-
mainSubplotID = id;
911-
break;
912-
}
908+
var counterIDs = ax._counterAxes;
909+
for(var j = 0; j < counterIDs.length; j++) {
910+
var counterPart = counterIDs[j];
911+
var id = isX ? (ax._id + counterPart) : (counterPart + ax._id);
912+
if(!nextBestMainSubplotID) nextBestMainSubplotID = id;
913+
var counterAx = axisIDs.getFromId(mockGd, counterPart);
914+
if(anchorID && counterAx.overlaying === anchorID) {
915+
mainSubplotID = id;
916+
break;
913917
}
914918
}
915919
}

0 commit comments

Comments
 (0)