Skip to content

Commit a27d62c

Browse files
committed
Merge pull request #329 from plotly/break-up-legend
Break up legend module
2 parents 7882ba5 + dbaf340 commit a27d62c

File tree

16 files changed

+1201
-812
lines changed

16 files changed

+1201
-812
lines changed

build/plotcss.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var rules = {
88
"X a": "text-decoration:none;",
99
"X a:hover": "text-decoration:none;",
1010
"X .crisp": "shape-rendering:crispEdges;",
11+
"X .user-select-none": "-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;",
1112
"X svg": "overflow:hidden;",
1213
"X svg a": "fill:#447adb;",
1314
"X svg a:hover": "fill:#3c6dc5;",

src/components/legend/anchor_utils.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
13+
/**
14+
* Determine the position anchor property of x/y xanchor/yanchor components.
15+
*
16+
* - values < 1/3 align the low side at that fraction,
17+
* - values [1/3, 2/3] align the center at that fraction,
18+
* - values > 2/3 align the right at that fraction.
19+
*/
20+
21+
exports.isRightAnchor = function isRightAnchor(opts) {
22+
return (
23+
opts.xanchor === 'right' ||
24+
(opts.xanchor === 'auto' && opts.x >= 2 / 3)
25+
);
26+
};
27+
28+
exports.isCenterAnchor = function isCenterAnchor(opts) {
29+
return (
30+
opts.xanchor === 'center' ||
31+
(opts.xanchor === 'auto' && opts.x > 1 / 3 && opts.x < 2 / 3)
32+
);
33+
};
34+
35+
exports.isBottomAnchor = function isBottomAnchor(opts) {
36+
return (
37+
opts.yanchor === 'bottom' ||
38+
(opts.yanchor === 'auto' && opts.y <= 1 / 3)
39+
);
40+
};
41+
42+
exports.isMiddleAnchor = function isMiddleAnchor(opts) {
43+
return (
44+
opts.yanchor === 'middle' ||
45+
(opts.yanchor === 'auto' && opts.y > 1 / 3 && opts.y < 2 / 3)
46+
);
47+
};

src/components/legend/defaults.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Lib = require('../../lib');
13+
var Plots = require('../../plots/plots');
14+
15+
var attributes = require('./attributes');
16+
var helpers = require('./helpers');
17+
18+
19+
module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
20+
var containerIn = layoutIn.legend || {},
21+
containerOut = layoutOut.legend = {};
22+
23+
var visibleTraces = 0,
24+
defaultOrder = 'normal';
25+
26+
for(var i = 0; i < fullData.length; i++) {
27+
var trace = fullData[i];
28+
29+
if(helpers.legendGetsTrace(trace)) {
30+
visibleTraces++;
31+
// always show the legend by default if there's a pie
32+
if(Plots.traceIs(trace, 'pie')) visibleTraces++;
33+
}
34+
35+
if((Plots.traceIs(trace, 'bar') && layoutOut.barmode==='stack') ||
36+
['tonextx','tonexty'].indexOf(trace.fill)!==-1) {
37+
defaultOrder = helpers.isGrouped({traceorder: defaultOrder}) ?
38+
'grouped+reversed' : 'reversed';
39+
}
40+
41+
if(trace.legendgroup !== undefined && trace.legendgroup !== '') {
42+
defaultOrder = helpers.isReversed({traceorder: defaultOrder}) ?
43+
'reversed+grouped' : 'grouped';
44+
}
45+
}
46+
47+
function coerce(attr, dflt) {
48+
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
49+
}
50+
51+
var showLegend = Lib.coerce(layoutIn, layoutOut,
52+
Plots.layoutAttributes, 'showlegend', visibleTraces > 1);
53+
54+
if(showLegend === false) return;
55+
56+
coerce('bgcolor', layoutOut.paper_bgcolor);
57+
coerce('bordercolor');
58+
coerce('borderwidth');
59+
Lib.coerceFont(coerce, 'font', layoutOut.font);
60+
61+
coerce('traceorder', defaultOrder);
62+
if(helpers.isGrouped(layoutOut.legend)) coerce('tracegroupgap');
63+
64+
coerce('x');
65+
coerce('xanchor');
66+
coerce('y');
67+
coerce('yanchor');
68+
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
69+
};

0 commit comments

Comments
 (0)