Skip to content
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

Support adding text labels to lines and shapes #6454

Merged
merged 86 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
7ee2e85
add attributes for text on shapes
emilykl Nov 17, 2022
932fa65
draw text, but not in the right place
emilykl Nov 17, 2022
1d0e471
draw text in correct place, when x0 and y0 are defined for shape
emilykl Jan 9, 2023
e46e1bc
update shape label api
emilykl Jan 9, 2023
60823dc
implement xanchor
emilykl Jan 10, 2023
2f8c810
add image test
emilykl Jan 10, 2023
e405293
fix xanchor bug
emilykl Jan 10, 2023
e31de9e
partial handling of re-render
emilykl Jan 10, 2023
0bcfff3
handle numeric angles
emilykl Jan 10, 2023
9972dc9
handle auto angle for lines, put label drawing code into func
emilykl Jan 10, 2023
6d5709a
nicer colors on image test
emilykl Jan 11, 2023
072edf6
fix weird behavior on pan
emilykl Jan 11, 2023
8bee979
disable mathjax rendering for shape text
emilykl Jan 11, 2023
8528561
clip shape text outside plot area
emilykl Jan 11, 2023
b4a4576
fix textangle bug for lines -- angle should always be correct now
emilykl Jan 23, 2023
97ee8c4
correctly handle position prop in most cases
emilykl Jan 23, 2023
674abd8
revert shapes demo to original
emilykl Jan 23, 2023
3743de4
handle label font
emilykl Jan 23, 2023
c3cc5ed
redraw text during shape move/resize
emilykl Jan 26, 2023
45a0564
handle yanchor for text
emilykl Jan 27, 2023
b8085c2
rename mock
emilykl Jan 27, 2023
8c0476e
handle multiline text and support text on arbitrary paths (sort of)
emilykl Jan 27, 2023
95dd9b6
fix jasmine tests... maybe
emilykl Jan 27, 2023
58e39cb
update plot-schema.json
emilykl Jan 30, 2023
488318f
add data-index attr to shape group
emilykl Jan 30, 2023
c8d20f2
update jasmine test
emilykl Jan 30, 2023
dad6ea5
add mocks for text on shapes
emilykl Jan 30, 2023
3134869
fix mocks
emilykl Jan 30, 2023
f17e218
add image baselines
emilykl Jan 30, 2023
01ef185
fix text positioning for lines
emilykl Feb 16, 2023
d8c4790
Apply suggestions from code review
emilykl Jan 31, 2023
5f39a62
rename position to textposition, fix some defaults
emilykl Feb 20, 2023
1ab9e24
fix docstrings for xanchor and yanchor
emilykl Feb 20, 2023
b9ea251
updated plot-schema.json
emilykl Feb 20, 2023
1f366a8
updated image baselines
emilykl Feb 20, 2023
f1087f3
updated plot-schema.json
emilykl Feb 21, 2023
f1570cf
fix Jasmine tests in shapes_test.js
emilykl Feb 21, 2023
baec66d
revert dist/plot-schema.json
emilykl Feb 21, 2023
d83e0cd
update textangle docstring and fix edge case
emilykl Feb 21, 2023
a91d12f
remove label dflt
emilykl Feb 21, 2023
4948d67
Update src/components/shapes/draw.js
emilykl Feb 23, 2023
a5e19d3
Update src/components/shapes/draw.js
emilykl Feb 23, 2023
e55953e
add new mock
emilykl Feb 24, 2023
44d5f08
fix text position for paths
emilykl Feb 28, 2023
5424ac5
fix mock
emilykl Feb 28, 2023
8769cff
add label attributes to newshape (broken)
emilykl Feb 28, 2023
f192a16
adjust edit types for newshape.label
archmoj Feb 28, 2023
7ca5cf8
fix syntax in zz-text_on_shapes_reversed_axes mock
archmoj Feb 28, 2023
822fe0d
add baseline
archmoj Feb 28, 2023
d50da33
fix circular dependency
archmoj Mar 1, 2023
8cec651
set newshape label editType
archmoj Mar 1, 2023
36771a8
change default xanchor and yanchor behavior
emilykl Mar 1, 2023
3f9727a
update mocks
emilykl Mar 1, 2023
9a4a121
Merge branch 'add-text-to-shapes' of https://github.com/plotly/plotly…
emilykl Mar 1, 2023
e493873
validate new mock
archmoj Mar 1, 2023
a17a315
update yanchor values and defaults in draw_newshape
emilykl Mar 1, 2023
d9bef04
Merge branch 'add-text-to-shapes' of https://github.com/plotly/plotly…
emilykl Mar 1, 2023
f54f102
input newshape label attributes
archmoj Mar 1, 2023
f642a52
draw labels on new shapes
archmoj Mar 1, 2023
dfb608e
provide missing editTypes
archmoj Mar 1, 2023
c20370a
merge shapes/defaults.js
emilykl Mar 1, 2023
feb2f38
standardize default yanchor behavior between shape and draw_newshape
emilykl Mar 1, 2023
11b3421
better docstrings for xanchor and yanchor
emilykl Mar 1, 2023
ea9493e
update image baselines
emilykl Mar 1, 2023
52463fa
update plot-schema
emilykl Mar 1, 2023
394b48f
improve label.textposition docstring
emilykl Mar 2, 2023
1325b7c
only coerce label properties if label text is given
emilykl Mar 2, 2023
63bb5d9
fix line xanchor behavior when x0 == x1
emilykl Mar 2, 2023
61e3df7
docstring update
emilykl Mar 2, 2023
40933d0
update plot-schema
emilykl Mar 2, 2023
8d35f6e
set default angle to auto for all shapes
emilykl Mar 2, 2023
d5d1927
set min padding to 0
emilykl Mar 2, 2023
c34d6de
update plot-schema
emilykl Mar 2, 2023
ffea962
docstring: specify padding is in px
emilykl Mar 6, 2023
ebe85c4
calculate textangle sin and cos only once
emilykl Mar 6, 2023
d113bcf
docstring updates
emilykl Mar 6, 2023
c703c11
simplify dfltYanchor implementation
emilykl Mar 6, 2023
468363e
change temp render logic
emilykl Mar 9, 2023
cdf2d37
update shape label while editing an editable shape
archmoj Mar 8, 2023
9eb7c90
test label behavior for draw_newshape
emilykl Mar 9, 2023
3d2c792
update docstring formatting
emilykl Mar 9, 2023
394555c
update plot-schema
emilykl Mar 9, 2023
eaf270d
add draft log
emilykl Mar 9, 2023
095561f
update draft log
emilykl Mar 9, 2023
b0b8ab1
Merge branch 'master' into add-text-to-shapes
emilykl Mar 10, 2023
d927b6a
run linter
emilykl Mar 10, 2023
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: 2 additions & 0 deletions draftlogs/6454_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add `label` attribute to shapes [[#6454](https://github.com/plotly/plotly.js/pull/6454)], with thanks to
the [Volkswagen](https://www.volkswagenag.com) Center of Excellence for Battery Cells for sponsoring development!
82 changes: 81 additions & 1 deletion src/components/shapes/attributes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var annAttrs = require('../annotations/attributes');
var fontAttrs = require('../../plots/font_attributes');
var scatterLineAttrs = require('../../traces/scatter/attributes').line;
var dash = require('../drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
Expand Down Expand Up @@ -224,6 +225,85 @@ module.exports = templatedArray('shape', {
'`config.editable` or `config.edits.shapePosition`.'
].join(' ')
},

label: {
archmoj marked this conversation as resolved.
Show resolved Hide resolved
text: {
valType: 'string',
dflt: '',
editType: 'arraydraw',
description: 'Sets the text to display with shape.'
},
font: fontAttrs({
editType: 'calc+arraydraw',
colorEditType: 'arraydraw',
description: 'Sets the shape label text font.'
}),
textposition: {
valType: 'enumerated',
values: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we need to have auto dflt and value in respect to src/components/shapes/defaults.js logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archmoj What would be the use case for an auto value for textposition?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archmoj What would be the use case for an auto value for textposition?

Hmm.. that's a good idea. But as far as I recall we don't have such an option for textposition in other places. So perhaps we could keep this part as is.

'top left', 'top center', 'top right',
'middle left', 'middle center', 'middle right',
'bottom left', 'bottom center', 'bottom right',
'start', 'middle', 'end',
],
editType: 'arraydraw',
description: [
'Sets the position of the label text relative to the shape.',
'Supported values for rectangles, circles and paths are',
'*top left*, *top center*, *top right*, *middle left*,',
'*middle center*, *middle right*, *bottom left*, *bottom center*,',
'and *bottom right*.',
'Supported values for lines are *start*, *middle*, and *end*.',
'Default: *middle center* for rectangles, circles, and paths; *middle* for lines.',
].join(' ')
},
textangle: {
valType: 'angle',
dflt: 'auto',
editType: 'calc+arraydraw',
description: [
'Sets the angle at which the label text is drawn',
'with respect to the horizontal. For lines, angle *auto*',
'is the same angle as the line. For all other shapes,',
'angle *auto* is horizontal.'
].join(' ')
},
xanchor: {
valType: 'enumerated',
values: ['auto', 'left', 'center', 'right'],
dflt: 'auto',
editType: 'calc+arraydraw',
description: [
'Sets the label\'s horizontal position anchor',
'This anchor binds the specified `textposition` to the *left*, *center*',
'or *right* of the label text.',
'For example, if `textposition` is set to *top right* and',
'`xanchor` to *right* then the right-most portion of the',
'label text lines up with the right-most edge of the',
'shape.',
].join(' '),
},
yanchor: {
valType: 'enumerated',
values: ['top', 'middle', 'bottom'],
editType: 'calc+arraydraw',
description: [
'Sets the label\'s vertical position anchor',
'This anchor binds the specified `textposition` to the *top*, *middle*',
'or *bottom* of the label text.',
'For example, if `textposition` is set to *top right* and',
'`yanchor` to *top* then the top-most portion of the',
'label text lines up with the top-most edge of the',
'shape.',
].join(' ')
},
padding: {
valType: 'number',
dflt: 3,
emilykl marked this conversation as resolved.
Show resolved Hide resolved
min: 0,
editType: 'arraydraw',
description: 'Sets padding (in px) between edge of label and edge of shape.'
},
editType: 'arraydraw'
},
archmoj marked this conversation as resolved.
Show resolved Hide resolved
editType: 'arraydraw'
});
21 changes: 21 additions & 0 deletions src/components/shapes/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
});
};

function dfltLabelYanchor(isLine, labelTextPosition) {
// If shape is a line, default y-anchor is 'bottom' (so that text is above line by default)
// Otherwise, default y-anchor is equal to y-component of `textposition`
// (so that text is positioned inside shape bounding box by default)
return isLine ? 'bottom' :
labelTextPosition.indexOf('top') !== -1 ? 'top' :
labelTextPosition.indexOf('bottom') !== -1 ? 'bottom' : 'middle';
}

function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
function coerce(attr, dflt) {
return Lib.coerce(shapeIn, shapeOut, attributes, attr, dflt);
Expand Down Expand Up @@ -116,4 +125,16 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {
if(noPath) {
Lib.noneOrAll(shapeIn, shapeOut, ['x0', 'x1', 'y0', 'y1']);
}

// Label options
var isLine = shapeType === 'line';
var labelText = coerce('label.text');
if(labelText) {
coerce('label.textangle');
var labelTextPosition = coerce('label.textposition', isLine ? 'middle' : 'middle center');
coerce('label.xanchor');
coerce('label.yanchor', dfltLabelYanchor(isLine, labelTextPosition));
coerce('label.padding');
Lib.coerceFont(coerce, 'label.font', fullLayout.font);
}
}
2 changes: 1 addition & 1 deletion src/components/shapes/display_outlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function displayOutlines(polygons, outlines, dragOptions, nCall
// recursive call
displayOutlines(polygons, outlines, dragOptions, nCalls++);

if(pointsOnEllipse(polygons[0])) {
if(pointsOnEllipse(polygons[0]) || dragOptions.hasText) {
update({redrawing: true});
}
}
Expand Down
Loading