Skip to content

Commit a47ab4c

Browse files
committed
change drawArrowHead signature to take full annotation options
at one point I envisioned this as a more general routine, to add heads to any line and we could still do that, but other uses will just need to follow the same naming.
1 parent 2f2b300 commit a47ab4c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/components/annotations/draw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
520520
.style('stroke-width', strokewidth + 'px')
521521
.call(Color.stroke, Color.rgb(arrowColor));
522522

523-
drawArrowHead(arrow, options.arrowhead, 'end', options.arrowsize, options.standoff);
523+
drawArrowHead(arrow, 'end', options);
524524

525525
// the arrow dragger is a small square right at the head, then a line to the tail,
526526
// all expanded by a stroke width of 6px plus the arrow line width

src/components/annotations/draw_arrow_head.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@
1010
'use strict';
1111

1212
var d3 = require('d3');
13-
var isNumeric = require('fast-isnumeric');
1413

1514
var Color = require('../color');
1615
var Drawing = require('../drawing');
1716

1817
var ARROWPATHS = require('./arrow_paths');
1918

20-
// add arrowhead(s) to a path or line d3 element el3
21-
// style: 1-6, first 5 are pointers, 6 is circle, 7 is square, 8 is none
22-
// ends is 'start', 'end' (default), 'start+end'
23-
// mag is magnification vs. default (default 1)
24-
25-
module.exports = function drawArrowHead(el3, style, ends, mag, standoff) {
26-
if(!isNumeric(mag)) mag = 1;
19+
/**
20+
* Add arrowhead(s) to a path or line element
21+
*
22+
* @param {d3.selection} el3: a d3-selected line or path element
23+
*
24+
* @param {string} ends: 'start', 'end', or 'start+end' for which ends get arrowheads
25+
*
26+
* @param {object} options: style information. Must have all the following:
27+
* @param {number} options.arrowhead: head style - see ./arrow_paths
28+
* @param {number} options.arrowsize: relative size of the head vs line width
29+
* @param {number} options.standoff: distance in px to move the arrow point from its target
30+
*/
31+
module.exports = function drawArrowHead(el3, ends, options) {
2732
var el = el3.node(),
28-
headStyle = ARROWPATHS[style||0];
29-
30-
if(typeof ends !== 'string' || !ends) ends = 'end';
33+
headStyle = ARROWPATHS[options.arrowhead || 0];
3134

32-
var scale = (Drawing.getPx(el3, 'stroke-width') || 1) * mag,
35+
var scale = (Drawing.getPx(el3, 'stroke-width') || 1) * options.arrowsize,
3336
stroke = el3.style('stroke') || Color.defaultLine,
3437
opacity = el3.style('stroke-opacity') || 1,
3538
doStart = ends.indexOf('start') >= 0,
3639
doEnd = ends.indexOf('end') >= 0,
37-
backOff = headStyle.backoff * scale + standoff,
40+
backOff = headStyle.backoff * scale + options.standoff,
3841
start,
3942
end,
4043
startRot,
@@ -110,7 +113,7 @@ module.exports = function drawArrowHead(el3, style, ends, mag, standoff) {
110113

111114
function drawhead(p, rot) {
112115
if(!headStyle.path) return;
113-
if(style > 5) rot = 0; // don't rotate square or circle
116+
if(options.arrowhead > 5) rot = 0; // don't rotate square or circle
114117
d3.select(el.parentNode).append('path')
115118
.attr({
116119
'class': el3.attr('class'),

0 commit comments

Comments
 (0)