Skip to content

Implement bar labels (issue 34) #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ exports.cleanData = function(data, existingData) {
trace.scene = Plots.subplotsRegistry.gl3d.cleanId(trace.scene);
}

if(!Registry.traceIs(trace, 'pie')) {
if(!Registry.traceIs(trace, 'pie') && !Registry.traceIs(trace, 'bar')) {
if(Array.isArray(trace.textposition)) {
trace.textposition = trace.textposition.map(cleanTextPosition);
}
Expand Down
37 changes: 37 additions & 0 deletions src/traces/bar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var errorBarAttrs = require('../../components/errorbars/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var fontAttrs = require('../../plots/font_attributes');

var extendFlat = require('../../lib/extend').extendFlat;
var extendDeep = require('../../lib/extend').extendDeep;

var textFontAttrs = extendDeep({}, fontAttrs);
textFontAttrs.family.arrayOk = true;
textFontAttrs.size.arrayOk = true;
textFontAttrs.color.arrayOk = true;

var scatterMarkerAttrs = scatterAttrs.marker;
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;
Expand All @@ -40,8 +47,38 @@ module.exports = {
y: scatterAttrs.y,
y0: scatterAttrs.y0,
dy: scatterAttrs.dy,

text: scatterAttrs.text,

textposition: {
valType: 'enumerated',
role: 'info',
values: ['inside', 'outside', 'auto', 'none'],
dflt: 'none',
arrayOk: true,
description: [
'Specifies the location of the `text`.',
'*inside* positions `text` inside, next to the bar end',
'(rotated and scaled if needed).',
'*outside* positions `text` outside, next to the bar end',
'(scaled if needed).',
'*auto* positions `text` inside or outside',
'so that `text` size is maximized.'
].join(' ')
},

textfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text`.'
}),

insidetextfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text` lying inside the bar.'
}),

outsidetextfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text` lying outside the bar.'
}),

orientation: {
valType: 'enumerated',
role: 'info',
Expand Down
14 changes: 14 additions & 0 deletions src/traces/bar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}

var coerceFont = Lib.coerceFont;

var len = handleXYDefaults(traceIn, traceOut, coerce);
if(!len) {
traceOut.visible = false;
Expand All @@ -33,8 +35,20 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce('base');
coerce('offset');
coerce('width');

coerce('text');

var textPosition = coerce('textposition');

var hasBoth = Array.isArray(textPosition) || textPosition === 'auto',
hasInside = hasBoth || textPosition === 'inside',
hasOutside = hasBoth || textPosition === 'outside';
if(hasInside || hasOutside) {
var textFont = coerceFont(coerce, 'textfont', layout.font);
if(hasInside) coerceFont(coerce, 'insidetextfont', textFont);
if(hasOutside) coerceFont(coerce, 'outsidetextfont', textFont);
}

handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout);

// override defaultColor for error bars with defaultLine
Expand Down
Loading