Skip to content

Commit 3024b71

Browse files
committed
plot api: move plot/update sub-routine to subroutines.js
- this move will allow us to test (with jasmine spies) that the proper subroutines are called.
1 parent b2a072a commit 3024b71

File tree

2 files changed

+343
-215
lines changed

2 files changed

+343
-215
lines changed

src/plot_api/plot_api.js

Lines changed: 4 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ var Plots = require('../plots/plots');
2222
var Fx = require('../plots/cartesian/graph_interact');
2323
var Polar = require('../plots/polar');
2424

25-
var Color = require('../components/color');
2625
var Drawing = require('../components/drawing');
2726
var ErrorBars = require('../components/errorbars');
28-
var Titles = require('../components/titles');
29-
var ModeBar = require('../components/modebar');
3027
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
3128
var svgTextUtils = require('../lib/svg_text_utils');
3229

3330
var helpers = require('./helpers');
31+
var subroutines = require('./subroutines');
3432

3533

3634
/**
@@ -196,7 +194,7 @@ Plotly.plot = function(gd, data, layout, config) {
196194
function marginPushersAgain() {
197195
// in case the margins changed, draw margin pushers again
198196
var seq = JSON.stringify(fullLayout._size) === oldmargins ?
199-
[] : [marginPushers, layoutStyles];
197+
[] : [marginPushers, subroutines.layoutStyles];
200198
return Lib.syncOrAsync(seq.concat(Fx.init), gd);
201199
}
202200

@@ -316,7 +314,7 @@ Plotly.plot = function(gd, data, layout, config) {
316314
marginPushers,
317315
marginPushersAgain,
318316
positionAndAutorange,
319-
layoutStyles,
317+
subroutines.layoutStyles,
320318
drawAxes,
321319
drawData,
322320
finalDraw
@@ -2373,7 +2371,7 @@ function makePlotFramework(gd) {
23732371

23742372
// position and style the containers, make main title
23752373
var frameWorkDone = Lib.syncOrAsync([
2376-
layoutStyles,
2374+
subroutines.layoutStyles,
23772375
function goAxes() { return Plotly.Axes.doTicks(gd, 'redraw'); },
23782376
Fx.init
23792377
], gd);
@@ -2543,212 +2541,3 @@ function makeCartesianPlotFramwork(gd, subplots) {
25432541
.classed('crisp', true);
25442542
});
25452543
}
2546-
2547-
// layoutStyles: styling for plot layout elements
2548-
function layoutStyles(gd) {
2549-
return Lib.syncOrAsync([Plots.doAutoMargin, lsInner], gd);
2550-
}
2551-
2552-
function lsInner(gd) {
2553-
var fullLayout = gd._fullLayout,
2554-
gs = fullLayout._size,
2555-
axList = Plotly.Axes.list(gd),
2556-
i;
2557-
2558-
// clear axis line positions, to be set in the subplot loop below
2559-
for(i = 0; i < axList.length; i++) axList[i]._linepositions = {};
2560-
2561-
fullLayout._paperdiv
2562-
.style({
2563-
width: fullLayout.width + 'px',
2564-
height: fullLayout.height + 'px'
2565-
})
2566-
.selectAll('.main-svg')
2567-
.call(Drawing.setSize, fullLayout.width, fullLayout.height);
2568-
2569-
gd._context.setBackground(gd, fullLayout.paper_bgcolor);
2570-
2571-
var freefinished = [];
2572-
fullLayout._paper.selectAll('g.subplot').each(function(subplot) {
2573-
var plotinfo = fullLayout._plots[subplot],
2574-
xa = Plotly.Axes.getFromId(gd, subplot, 'x'),
2575-
ya = Plotly.Axes.getFromId(gd, subplot, 'y');
2576-
xa.setScale(); // this may already be done... not sure
2577-
ya.setScale();
2578-
2579-
if(plotinfo.bg) {
2580-
plotinfo.bg
2581-
.call(Drawing.setRect,
2582-
xa._offset - gs.p, ya._offset - gs.p,
2583-
xa._length + 2 * gs.p, ya._length + 2 * gs.p)
2584-
.call(Color.fill, fullLayout.plot_bgcolor);
2585-
}
2586-
2587-
2588-
// Clip so that data only shows up on the plot area.
2589-
plotinfo.clipId = 'clip' + fullLayout._uid + subplot + 'plot';
2590-
2591-
var plotClip = fullLayout._defs.selectAll('g.clips')
2592-
.selectAll('#' + plotinfo.clipId)
2593-
.data([0]);
2594-
2595-
plotClip.enter().append('clipPath')
2596-
.attr({
2597-
'class': 'plotclip',
2598-
'id': plotinfo.clipId
2599-
})
2600-
.append('rect');
2601-
2602-
plotClip.selectAll('rect')
2603-
.attr({
2604-
'width': xa._length,
2605-
'height': ya._length
2606-
});
2607-
2608-
2609-
plotinfo.plot.call(Lib.setTranslate, xa._offset, ya._offset);
2610-
plotinfo.plot.call(Drawing.setClipUrl, plotinfo.clipId);
2611-
2612-
var xlw = Drawing.crispRound(gd, xa.linewidth, 1),
2613-
ylw = Drawing.crispRound(gd, ya.linewidth, 1),
2614-
xp = gs.p + ylw,
2615-
xpathPrefix = 'M' + (-xp) + ',',
2616-
xpathSuffix = 'h' + (xa._length + 2 * xp),
2617-
showfreex = xa.anchor === 'free' &&
2618-
freefinished.indexOf(xa._id) === -1,
2619-
freeposx = gs.h * (1 - (xa.position||0)) + ((xlw / 2) % 1),
2620-
showbottom =
2621-
(xa.anchor === ya._id && (xa.mirror || xa.side !== 'top')) ||
2622-
xa.mirror === 'all' || xa.mirror === 'allticks' ||
2623-
(xa.mirrors && xa.mirrors[ya._id + 'bottom']),
2624-
bottompos = ya._length + gs.p + xlw / 2,
2625-
showtop =
2626-
(xa.anchor === ya._id && (xa.mirror || xa.side === 'top')) ||
2627-
xa.mirror === 'all' || xa.mirror === 'allticks' ||
2628-
(xa.mirrors && xa.mirrors[ya._id + 'top']),
2629-
toppos = -gs.p - xlw / 2,
2630-
2631-
// shorten y axis lines so they don't overlap x axis lines
2632-
yp = gs.p,
2633-
// except where there's no x line
2634-
// TODO: this gets more complicated with multiple x and y axes
2635-
ypbottom = showbottom ? 0 : xlw,
2636-
yptop = showtop ? 0 : xlw,
2637-
ypathSuffix = ',' + (-yp - yptop) +
2638-
'v' + (ya._length + 2 * yp + yptop + ypbottom),
2639-
showfreey = ya.anchor === 'free' &&
2640-
freefinished.indexOf(ya._id) === -1,
2641-
freeposy = gs.w * (ya.position||0) + ((ylw / 2) % 1),
2642-
showleft =
2643-
(ya.anchor === xa._id && (ya.mirror || ya.side !== 'right')) ||
2644-
ya.mirror === 'all' || ya.mirror === 'allticks' ||
2645-
(ya.mirrors && ya.mirrors[xa._id + 'left']),
2646-
leftpos = -gs.p - ylw / 2,
2647-
showright =
2648-
(ya.anchor === xa._id && (ya.mirror || ya.side === 'right')) ||
2649-
ya.mirror === 'all' || ya.mirror === 'allticks' ||
2650-
(ya.mirrors && ya.mirrors[xa._id + 'right']),
2651-
rightpos = xa._length + gs.p + ylw / 2;
2652-
2653-
// save axis line positions for ticks, draggers, etc to reference
2654-
// each subplot gets an entry:
2655-
// [left or bottom, right or top, free, main]
2656-
// main is the position at which to draw labels and draggers, if any
2657-
xa._linepositions[subplot] = [
2658-
showbottom ? bottompos : undefined,
2659-
showtop ? toppos : undefined,
2660-
showfreex ? freeposx : undefined
2661-
];
2662-
if(xa.anchor === ya._id) {
2663-
xa._linepositions[subplot][3] = xa.side === 'top' ?
2664-
toppos : bottompos;
2665-
}
2666-
else if(showfreex) {
2667-
xa._linepositions[subplot][3] = freeposx;
2668-
}
2669-
2670-
ya._linepositions[subplot] = [
2671-
showleft ? leftpos : undefined,
2672-
showright ? rightpos : undefined,
2673-
showfreey ? freeposy : undefined
2674-
];
2675-
if(ya.anchor === xa._id) {
2676-
ya._linepositions[subplot][3] = ya.side === 'right' ?
2677-
rightpos : leftpos;
2678-
}
2679-
else if(showfreey) {
2680-
ya._linepositions[subplot][3] = freeposy;
2681-
}
2682-
2683-
// translate all the extra stuff to have the
2684-
// same origin as the plot area or axes
2685-
var origin = 'translate(' + xa._offset + ',' + ya._offset + ')',
2686-
originx = origin,
2687-
originy = origin;
2688-
if(showfreex) {
2689-
originx = 'translate(' + xa._offset + ',' + gs.t + ')';
2690-
toppos += ya._offset - gs.t;
2691-
bottompos += ya._offset - gs.t;
2692-
}
2693-
if(showfreey) {
2694-
originy = 'translate(' + gs.l + ',' + ya._offset + ')';
2695-
leftpos += xa._offset - gs.l;
2696-
rightpos += xa._offset - gs.l;
2697-
}
2698-
2699-
plotinfo.xlines
2700-
.attr('transform', originx)
2701-
.attr('d', (
2702-
(showbottom ? (xpathPrefix + bottompos + xpathSuffix) : '') +
2703-
(showtop ? (xpathPrefix + toppos + xpathSuffix) : '') +
2704-
(showfreex ? (xpathPrefix + freeposx + xpathSuffix) : '')) ||
2705-
// so it doesn't barf with no lines shown
2706-
'M0,0')
2707-
.style('stroke-width', xlw + 'px')
2708-
.call(Color.stroke, xa.showline ?
2709-
xa.linecolor : 'rgba(0,0,0,0)');
2710-
plotinfo.ylines
2711-
.attr('transform', originy)
2712-
.attr('d', (
2713-
(showleft ? ('M' + leftpos + ypathSuffix) : '') +
2714-
(showright ? ('M' + rightpos + ypathSuffix) : '') +
2715-
(showfreey ? ('M' + freeposy + ypathSuffix) : '')) ||
2716-
'M0,0')
2717-
.attr('stroke-width', ylw + 'px')
2718-
.call(Color.stroke, ya.showline ?
2719-
ya.linecolor : 'rgba(0,0,0,0)');
2720-
2721-
plotinfo.xaxislayer.attr('transform', originx);
2722-
plotinfo.yaxislayer.attr('transform', originy);
2723-
plotinfo.gridlayer.attr('transform', origin);
2724-
plotinfo.zerolinelayer.attr('transform', origin);
2725-
plotinfo.draglayer.attr('transform', origin);
2726-
2727-
// mark free axes as displayed, so we don't draw them again
2728-
if(showfreex) { freefinished.push(xa._id); }
2729-
if(showfreey) { freefinished.push(ya._id); }
2730-
});
2731-
2732-
Plotly.Axes.makeClipPaths(gd);
2733-
2734-
drawMainTitle(gd);
2735-
2736-
ModeBar.manage(gd);
2737-
2738-
return gd._promises.length && Promise.all(gd._promises);
2739-
}
2740-
2741-
function drawMainTitle(gd) {
2742-
var fullLayout = gd._fullLayout;
2743-
2744-
Titles.draw(gd, 'gtitle', {
2745-
propContainer: fullLayout,
2746-
propName: 'title',
2747-
dfltName: 'Plot',
2748-
attributes: {
2749-
x: fullLayout.width / 2,
2750-
y: fullLayout._size.t / 2,
2751-
'text-anchor': 'middle'
2752-
}
2753-
});
2754-
}

0 commit comments

Comments
 (0)