Skip to content

Commit 8e6ab7d

Browse files
committed
split annotation drawOne into drawRaw
- which expects an option object, xaxis and yaxis - generalize className given to annotation to group and base attribute string (for editable: true) to accommodate 3d annotations - use `_input` ref instead of `gd.layout` to grab user layout options
1 parent 4dc16e4 commit 8e6ab7d

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/components/annotations/draw.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ var drawArrowHead = require('./draw_arrow_head');
3636

3737
module.exports = {
3838
draw: draw,
39-
drawOne: drawOne
39+
drawOne: drawOne,
40+
drawRaw: drawRaw
4041
};
4142

4243
/*
@@ -62,40 +63,59 @@ function draw(gd) {
6263
* index (int): the annotation to draw
6364
*/
6465
function drawOne(gd, index) {
65-
var layout = gd.layout,
66-
fullLayout = gd._fullLayout,
67-
gs = gd._fullLayout._size;
66+
var fullLayout = gd._fullLayout;
67+
var options = fullLayout.annotations[index] || {};
68+
var xa = Axes.getFromId(gd, options.xref);
69+
var ya = Axes.getFromId(gd, options.yref);
6870

69-
// remove the existing annotation if there is one
70-
fullLayout._infolayer.selectAll('.annotation[data-index="' + index + '"]').remove();
71+
drawRaw(gd, options, index, xa, ya);
72+
}
73+
74+
/*
75+
* drawRaw: draw a single annotation, potentially with modifications
76+
*
77+
* options (object): this annotation's options
78+
* index (int): the annotation to draw
79+
* xa (object | undefined): full x-axis object to compute subplot pos-to-px
80+
* ya (object | undefined): ... y-axis
81+
*/
82+
function drawRaw(gd, options, index, xa, ya) {
83+
var fullLayout = gd._fullLayout;
84+
var gs = gd._fullLayout._size;
7185

72-
// remember a few things about what was already there,
73-
var optionsIn = (layout.annotations || [])[index],
74-
options = fullLayout.annotations[index];
86+
var className = options._scene ?
87+
'annotation-' + options._scene :
88+
'annotation';
89+
90+
var annbase = options._scene ?
91+
options._scene + '.annotations[' + index + ']' :
92+
'annotations[' + index + ']';
93+
94+
// remove the existing annotation if there is one
95+
fullLayout._infolayer
96+
.selectAll('.' + className + '[data-index="' + index + '"]')
97+
.remove();
7598

7699
var annClipID = 'clip' + fullLayout._uid + '_ann' + index;
77100

78101
// this annotation is gone - quit now after deleting it
79102
// TODO: use d3 idioms instead of deleting and redrawing every time
80-
if(!optionsIn || options.visible === false) {
103+
if(!options._input || options.visible === false) {
81104
d3.selectAll('#' + annClipID).remove();
82105
return;
83106
}
84107

85-
var xa = Axes.getFromId(gd, options.xref),
86-
ya = Axes.getFromId(gd, options.yref),
87-
88-
// calculated pixel positions
89-
// x & y each will get text, head, and tail as appropriate
90-
annPosPx = {x: {}, y: {}},
108+
// calculated pixel positions
109+
// x & y each will get text, head, and tail as appropriate
110+
var annPosPx = {x: {}, y: {}},
91111
textangle = +options.textangle || 0;
92112

93113
// create the components
94114
// made a single group to contain all, so opacity can work right
95115
// with border/arrow together this could handle a whole bunch of
96116
// cleanup at this point, but works for now
97117
var annGroup = fullLayout._infolayer.append('g')
98-
.classed('annotation', true)
118+
.classed(className, true)
99119
.attr('data-index', String(index))
100120
.style('opacity', options.opacity);
101121

@@ -110,7 +130,7 @@ function drawOne(gd, index) {
110130
gd._dragging = false;
111131
gd.emit('plotly_clickannotation', {
112132
index: index,
113-
annotation: optionsIn,
133+
annotation: options._input,
114134
fullAnnotation: options
115135
});
116136
});
@@ -405,8 +425,6 @@ function drawOne(gd, index) {
405425
annTextGroup.attr({transform: 'rotate(' + textangle + ',' +
406426
annPosPx.x.text + ',' + annPosPx.y.text + ')'});
407427

408-
var annbase = 'annotations[' + index + ']';
409-
410428
/*
411429
* add the arrow
412430
* uses options[arrowwidth,arrowcolor,arrowhead] for styling
@@ -646,14 +664,18 @@ function drawOne(gd, index) {
646664
options.text = _text;
647665
this.attr({'data-unformatted': options.text});
648666
this.call(textLayout);
667+
649668
var update = {};
650-
update['annotations[' + index + '].text'] = options.text;
669+
update[annbase + '.text'] = options.text;
670+
671+
// TODO
651672
if(xa && xa.autorange) {
652673
update[xa._name + '.autorange'] = true;
653674
}
654675
if(ya && ya.autorange) {
655676
update[ya._name + '.autorange'] = true;
656677
}
678+
657679
Plotly.relayout(gd, update);
658680
});
659681
}

src/components/annotations/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
calcAutorange: require('./calc_autorange'),
2323
draw: drawModule.draw,
2424
drawOne: drawModule.drawOne,
25+
drawRaw: drawModule.drawRaw,
2526

2627
hasClickToShow: clickModule.hasClickToShow,
2728
onClick: clickModule.onClick,

0 commit comments

Comments
 (0)