Skip to content

Commit 0fc99c6

Browse files
authored
Merge pull request #1159 from n-riesco/pr-20161024-issue-34-textposition-rebased
Implement bar labels (issue 34)
2 parents 0fc37d6 + b7bfc97 commit 0fc99c6

File tree

10 files changed

+887
-19
lines changed

10 files changed

+887
-19
lines changed

src/plot_api/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ exports.cleanData = function(data, existingData) {
298298
trace.scene = Plots.subplotsRegistry.gl3d.cleanId(trace.scene);
299299
}
300300

301-
if(!Registry.traceIs(trace, 'pie')) {
301+
if(!Registry.traceIs(trace, 'pie') && !Registry.traceIs(trace, 'bar')) {
302302
if(Array.isArray(trace.textposition)) {
303303
trace.textposition = trace.textposition.map(cleanTextPosition);
304304
}

src/traces/bar/attributes.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ var scatterAttrs = require('../scatter/attributes');
1212
var colorAttributes = require('../../components/colorscale/color_attributes');
1313
var errorBarAttrs = require('../../components/errorbars/attributes');
1414
var colorbarAttrs = require('../../components/colorbar/attributes');
15+
var fontAttrs = require('../../plots/font_attributes');
1516

1617
var extendFlat = require('../../lib/extend').extendFlat;
18+
var extendDeep = require('../../lib/extend').extendDeep;
19+
20+
var textFontAttrs = extendDeep({}, fontAttrs);
21+
textFontAttrs.family.arrayOk = true;
22+
textFontAttrs.size.arrayOk = true;
23+
textFontAttrs.color.arrayOk = true;
1724

1825
var scatterMarkerAttrs = scatterAttrs.marker;
1926
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;
@@ -40,8 +47,38 @@ module.exports = {
4047
y: scatterAttrs.y,
4148
y0: scatterAttrs.y0,
4249
dy: scatterAttrs.dy,
50+
4351
text: scatterAttrs.text,
4452

53+
textposition: {
54+
valType: 'enumerated',
55+
role: 'info',
56+
values: ['inside', 'outside', 'auto', 'none'],
57+
dflt: 'none',
58+
arrayOk: true,
59+
description: [
60+
'Specifies the location of the `text`.',
61+
'*inside* positions `text` inside, next to the bar end',
62+
'(rotated and scaled if needed).',
63+
'*outside* positions `text` outside, next to the bar end',
64+
'(scaled if needed).',
65+
'*auto* positions `text` inside or outside',
66+
'so that `text` size is maximized.'
67+
].join(' ')
68+
},
69+
70+
textfont: extendFlat({}, textFontAttrs, {
71+
description: 'Sets the font used for `text`.'
72+
}),
73+
74+
insidetextfont: extendFlat({}, textFontAttrs, {
75+
description: 'Sets the font used for `text` lying inside the bar.'
76+
}),
77+
78+
outsidetextfont: extendFlat({}, textFontAttrs, {
79+
description: 'Sets the font used for `text` lying outside the bar.'
80+
}),
81+
4582
orientation: {
4683
valType: 'enumerated',
4784
role: 'info',

src/traces/bar/defaults.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2323
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2424
}
2525

26+
var coerceFont = Lib.coerceFont;
27+
2628
var len = handleXYDefaults(traceIn, traceOut, coerce);
2729
if(!len) {
2830
traceOut.visible = false;
@@ -33,8 +35,20 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3335
coerce('base');
3436
coerce('offset');
3537
coerce('width');
38+
3639
coerce('text');
3740

41+
var textPosition = coerce('textposition');
42+
43+
var hasBoth = Array.isArray(textPosition) || textPosition === 'auto',
44+
hasInside = hasBoth || textPosition === 'inside',
45+
hasOutside = hasBoth || textPosition === 'outside';
46+
if(hasInside || hasOutside) {
47+
var textFont = coerceFont(coerce, 'textfont', layout.font);
48+
if(hasInside) coerceFont(coerce, 'insidetextfont', textFont);
49+
if(hasOutside) coerceFont(coerce, 'outsidetextfont', textFont);
50+
}
51+
3852
handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout);
3953

4054
// override defaultColor for error bars with defaultLine

0 commit comments

Comments
 (0)