highcharts
Version:
JavaScript charting framework
1,514 lines (1,312 loc) • 244 kB
JavaScript
/**
* @license Highcharts JS v8.0.0 (2019-12-10)
*
* Annotations module
*
* (c) 2009-2019 Torstein Honsi
*
* License: www.highcharts.com/license
*/
'use strict';
(function (factory) {
if (typeof module === 'object' && module.exports) {
factory['default'] = factory;
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
define('highcharts/modules/annotations', ['highcharts'], function (Highcharts) {
factory(Highcharts);
factory.Highcharts = Highcharts;
return factory;
});
} else {
factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
}
}(function (Highcharts) {
var _modules = Highcharts ? Highcharts._modules : {};
function _registerModule(obj, path, args, fn) {
if (!obj.hasOwnProperty(path)) {
obj[path] = fn.apply(null, args);
}
}
_registerModule(_modules, 'annotations/eventEmitterMixin.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
var objectEach = U.objectEach,
pick = U.pick;
var fireEvent = H.fireEvent;
/**
* It provides methods for:
* - adding and handling DOM events and a drag event,
* - mapping a mouse move event to the distance between two following events.
* The units of the distance are specific to a transformation,
* e.g. for rotation they are radians, for scaling they are scale factors.
*
* @private
* @mixin
* @memberOf Annotation
*/
var eventEmitterMixin = {
/**
* Add emitter events.
*/
addEvents: function () {
var emitter = this;
H.addEvent(
emitter.graphic.element,
'mousedown',
function (e) {
emitter.onMouseDown(e);
}
);
objectEach(emitter.options.events, function (event, type) {
var eventHandler = function (e) {
if (type !== 'click' || !emitter.cancelClick) {
event.call(
emitter,
emitter.chart.pointer.normalize(e),
emitter.target
);
}
};
if (H.inArray(type, emitter.nonDOMEvents || []) === -1) {
emitter.graphic.on(type, eventHandler);
} else {
H.addEvent(emitter, type, eventHandler);
}
});
if (emitter.options.draggable) {
H.addEvent(emitter, 'drag', emitter.onDrag);
if (!emitter.graphic.renderer.styledMode) {
emitter.graphic.css({
cursor: {
x: 'ew-resize',
y: 'ns-resize',
xy: 'move'
}[emitter.options.draggable]
});
}
}
if (!emitter.isUpdating) {
fireEvent(emitter, 'add');
}
},
/**
* Remove emitter document events.
*/
removeDocEvents: function () {
if (this.removeDrag) {
this.removeDrag = this.removeDrag();
}
if (this.removeMouseUp) {
this.removeMouseUp = this.removeMouseUp();
}
},
/**
* Mouse down handler.
*
* @param {Object} e event
*/
onMouseDown: function (e) {
var emitter = this,
pointer = emitter.chart.pointer,
prevChartX,
prevChartY;
if (e.preventDefault) {
e.preventDefault();
}
// On right click, do nothing:
if (e.button === 2) {
return;
}
e = pointer.normalize(e);
prevChartX = e.chartX;
prevChartY = e.chartY;
emitter.cancelClick = false;
emitter.chart.hasDraggedAnnotation = true;
emitter.removeDrag = H.addEvent(
H.doc,
'mousemove',
function (e) {
emitter.hasDragged = true;
e = pointer.normalize(e);
e.prevChartX = prevChartX;
e.prevChartY = prevChartY;
fireEvent(emitter, 'drag', e);
prevChartX = e.chartX;
prevChartY = e.chartY;
}
);
emitter.removeMouseUp = H.addEvent(
H.doc,
'mouseup',
function (e) {
emitter.cancelClick = emitter.hasDragged;
emitter.hasDragged = false;
emitter.chart.hasDraggedAnnotation = false;
// ControlPoints vs Annotation:
fireEvent(pick(emitter.target, emitter), 'afterUpdate');
emitter.onMouseUp(e);
}
);
},
/**
* Mouse up handler.
*
* @param {Object} e event
*/
onMouseUp: function () {
var chart = this.chart,
annotation = this.target || this,
annotationsOptions = chart.options.annotations,
index = chart.annotations.indexOf(annotation);
this.removeDocEvents();
annotationsOptions[index] = annotation.options;
},
/**
* Drag and drop event. All basic annotations should share this
* capability as well as the extended ones.
*
* @param {Object} e event
*/
onDrag: function (e) {
if (
this.chart.isInsidePlot(
e.chartX - this.chart.plotLeft,
e.chartY - this.chart.plotTop
)
) {
var translation = this.mouseMoveToTranslation(e);
if (this.options.draggable === 'x') {
translation.y = 0;
}
if (this.options.draggable === 'y') {
translation.x = 0;
}
if (this.points.length) {
this.translate(translation.x, translation.y);
} else {
this.shapes.forEach(function (shape) {
shape.translate(translation.x, translation.y);
});
this.labels.forEach(function (label) {
label.translate(translation.x, translation.y);
});
}
this.redraw(false);
}
},
/**
* Map mouse move event to the radians.
*
* @param {Object} e event
* @param {number} cx center x
* @param {number} cy center y
*/
mouseMoveToRadians: function (e, cx, cy) {
var prevDy = e.prevChartY - cy,
prevDx = e.prevChartX - cx,
dy = e.chartY - cy,
dx = e.chartX - cx,
temp;
if (this.chart.inverted) {
temp = prevDx;
prevDx = prevDy;
prevDy = temp;
temp = dx;
dx = dy;
dy = temp;
}
return Math.atan2(dy, dx) - Math.atan2(prevDy, prevDx);
},
/**
* Map mouse move event to the distance between two following events.
*
* @param {Object} e event
*/
mouseMoveToTranslation: function (e) {
var dx = e.chartX - e.prevChartX,
dy = e.chartY - e.prevChartY,
temp;
if (this.chart.inverted) {
temp = dy;
dy = dx;
dx = temp;
}
return {
x: dx,
y: dy
};
},
/**
* Map mouse move to the scale factors.
*
* @param {Object} e event
* @param {number} cx center x
* @param {number} cy center y
**/
mouseMoveToScale: function (e, cx, cy) {
var prevDx = e.prevChartX - cx,
prevDy = e.prevChartY - cy,
dx = e.chartX - cx,
dy = e.chartY - cy,
sx = (dx || 1) / (prevDx || 1),
sy = (dy || 1) / (prevDy || 1),
temp;
if (this.chart.inverted) {
temp = sy;
sy = sx;
sx = temp;
}
return {
x: sx,
y: sy
};
},
/**
* Destroy the event emitter.
*/
destroy: function () {
this.removeDocEvents();
H.removeEvent(this);
this.hcEvents = null;
}
};
return eventEmitterMixin;
});
_registerModule(_modules, 'annotations/ControlPoint.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['annotations/eventEmitterMixin.js']], function (H, U, eventEmitterMixin) {
var extend = U.extend,
pick = U.pick;
/**
* A control point class which is a connection between controllable
* transform methods and a user actions.
*
* @constructor
* @mixes eventEmitterMixin
* @memberOf Annotation
*
* @param {Highcharts.Chart} chart a chart instance
* @param {Object} target a controllable instance which is a target for
* a control point
* @param {Annotation.ControlPoint.Options} options an options object
* @param {number} [index]
*/
function ControlPoint(chart, target, options, index) {
this.chart = chart;
this.target = target;
this.options = options;
this.index = pick(options.index, index);
}
/**
* @typedef {Object} Annotation.ControlPoint.Position
* @property {number} x
* @property {number} y
*/
/**
* @callback Annotation.ControlPoint.Positioner
* @param {Object} e event
* @param {Controllable} target
* @return {Annotation.ControlPoint.Position} position
*/
/**
* @typedef {Object} Annotation.ControlPoint.Options
* @property {string} symbol
* @property {number} width
* @property {number} height
* @property {Object} style
* @property {boolean} visible
* @property {Annotation.ControlPoint.Positioner} positioner
* @property {Object} events
*/
extend(
ControlPoint.prototype,
eventEmitterMixin
);
/**
* List of events for `anntation.options.events` that should not be
* added to `annotation.graphic` but to the `annotation`.
*
* @type {Array<string>}
*/
ControlPoint.prototype.nonDOMEvents = ['drag'];
/**
* Set the visibility.
*
* @param {boolean} [visible]
**/
ControlPoint.prototype.setVisibility = function (visible) {
this.graphic.attr('visibility', visible ? 'visible' : 'hidden');
this.options.visible = visible;
};
/**
* Render the control point.
*/
ControlPoint.prototype.render = function () {
var chart = this.chart,
options = this.options;
this.graphic = chart.renderer
.symbol(
options.symbol,
0,
0,
options.width,
options.height
)
.add(chart.controlPointsGroup)
.css(options.style);
this.setVisibility(options.visible);
this.addEvents();
};
/**
* Redraw the control point.
*
* @param {boolean} [animation]
*/
ControlPoint.prototype.redraw = function (animation) {
this.graphic[animation ? 'animate' : 'attr'](
this.options.positioner.call(this, this.target)
);
};
/**
* Destroy the control point.
*/
ControlPoint.prototype.destroy = function () {
eventEmitterMixin.destroy.call(this);
if (this.graphic) {
this.graphic = this.graphic.destroy();
}
this.chart = null;
this.target = null;
this.options = null;
};
/**
* Update the control point.
*/
ControlPoint.prototype.update = function (userOptions) {
var chart = this.chart,
target = this.target,
index = this.index,
options = H.merge(true,
this.options,
userOptions);
this.destroy();
this.constructor(chart, target, options, index);
this.render(chart.controlPointsGroup);
this.redraw();
};
return ControlPoint;
});
_registerModule(_modules, 'annotations/MockPoint.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
var defined = U.defined,
extend = U.extend;
/**
* A mock point label configuration.
*
* @interface Annotation.MockLabelOptionsObject
*//**
* X value translated to x axis scale
* @name Annotation.MockLabelOptionsObject#x
* @type {number|undefined}
*//**
* Y value translated to y axis scale
* @name Annotation.MockLabelOptionsObject#y
* @type {number|undefined}
*//**
* @name Annotation.MockLabelOptionsObject#point
* @type {Highcharts.Point}
*/
/**
* A mock point configuration.
*
* @interface Highcharts.MockPointOptionsObject
*//**
* x value for the point in xAxis scale or pixels
* @name Highcharts.MockPointOptionsObject#x
* @type {number}
*//**
* y value for the point in yAxis scale or pixels
* @name Highcharts.MockPointOptionsObject#y
* @type {number}
*//**
* xAxis index or id
* @name Highcharts.MockPointOptionsObject#xAxis
* @type {Highcharts.Axis|number|string|undefined}
*//**
* yAxis index or id
* @name Highcharts.MockPointOptionsObject#yAxis
* @property {Highcharts.Axis|number|string|undefined}
*/
/**
* A point-like object, a mock point or a point uses in series.
*
* @private
* @typedef {Highcharts.Point|Highcharts.MockPoint} Highcharts.PointLike
*/
/**
* A trimmed point object which imitates {@link Highchart.Point} class.
* It is created when there is a need of pointing to some chart's position
* using axis values or pixel values
*
* @private
* @class
* @name Highcharts.MockPoint
*
* @param {Highcharts.Chart} chart
* The chart object
*
* @param {Highcharts.MockPointOptionsObject} options
* The options object
*/
function MockPoint(chart, target, options) {
/**
* A mock series instance imitating a real series from a real point.
*
* @type {Object}
* @property {boolean} series.visible=true - whether a series is visible
* @property {Chart} series.chart - a chart instance
* @property {function} series.getPlotBox
*/
this.series = {
visible: true,
chart: chart,
getPlotBox: H.Series.prototype.getPlotBox
};
/**
* @type {?Controllable}
*/
this.target = target || null;
/**
* Options for the mock point.
*
* @type {Highcharts.MockPointOptionsObject}
*/
this.options = options;
/**
* If an xAxis is set it represents the point's value in terms of the xAxis.
*
* @name Annotation.MockPoint#x
* @type {?number}
*/
/**
* If an yAxis is set it represents the point's value in terms of the yAxis.
*
* @name Annotation.MockPoint#y
* @type {?number}
*/
/**
* It represents the point's pixel x coordinate relative to its plot box.
*
* @name Annotation.MockPoint#plotX
* @type {?number}
*/
/**
* It represents the point's pixel y position relative to its plot box.
*
* @name Annotation.MockPoint#plotY
* @type {?number}
*/
/**
* Whether the point is inside the plot box.
*
* @name Annotation.MockPoint#isInside
* @type {boolean}
*/
this.applyOptions(this.getOptions());
}
/**
* Create a mock point from a real Highcharts point.
*
* @param {Point} point
*
* @return {Annotation.MockPoint} a mock point instance.
*/
MockPoint.fromPoint = function (point) {
return new MockPoint(point.series.chart, null, {
x: point.x,
y: point.y,
xAxis: point.series.xAxis,
yAxis: point.series.yAxis
});
};
/**
* @typedef Annotation.MockPoint.Position
* @property {number} x
* @property {number} y
*/
/**
* Get the pixel position from the point like object.
*
* @param {Annotation.PointLike} point
* @param {boolean} [paneCoordinates]
* whether the pixel position should be relative
*
* @return {Annotation.MockPoint.Position} pixel position
*/
MockPoint.pointToPixels = function (point, paneCoordinates) {
var series = point.series,
chart = series.chart,
x = point.plotX,
y = point.plotY,
plotBox;
if (chart.inverted) {
if (point.mock) {
x = point.plotY;
y = point.plotX;
} else {
x = chart.plotWidth - point.plotY;
y = chart.plotHeight - point.plotX;
}
}
if (series && !paneCoordinates) {
plotBox = series.getPlotBox();
x += plotBox.translateX;
y += plotBox.translateY;
}
return {
x: x,
y: y
};
};
/**
* Get fresh mock point options from the point like object.
*
* @param {Annotation.PointLike} point
*
* @return {Annotation.MockPoint.Options} mock point's options
*/
MockPoint.pointToOptions = function (point) {
return {
x: point.x,
y: point.y,
xAxis: point.series.xAxis,
yAxis: point.series.yAxis
};
};
extend(MockPoint.prototype, /** @lends Annotation.MockPoint# */ {
/**
* A flag indicating that a point is not the real one.
*
* @type {boolean}
* @default true
*/
mock: true,
/**
* Check if the point has dynamic options.
*
* @return {boolean} A positive flag if the point has dynamic options.
*/
hasDynamicOptions: function () {
return typeof this.options === 'function';
},
/**
* Get the point's options.
*
* @return {Annotation.MockPoint.Options} the mock point's options.
*/
getOptions: function () {
return this.hasDynamicOptions() ?
this.options(this.target) :
this.options;
},
/**
* Apply options for the point.
*
* @param {Annotation.MockPoint.Options} options
*/
applyOptions: function (options) {
this.command = options.command;
this.setAxis(options, 'x');
this.setAxis(options, 'y');
this.refresh();
},
/**
* Set x or y axis.
*
* @param {Annotation.MockPoint.Options} options
* @param {string} xOrY 'x' or 'y' string literal
*/
setAxis: function (options, xOrY) {
var axisName = xOrY + 'Axis',
axisOptions = options[axisName],
chart = this.series.chart;
this.series[axisName] =
axisOptions instanceof H.Axis ?
axisOptions :
defined(axisOptions) ?
chart[axisName][axisOptions] || chart.get(axisOptions) :
null;
},
/**
* Transform the mock point to an anchor
* (relative position on the chart).
*
* @return {Array<number>} A quadruple of numbers which denotes x, y,
* width and height of the box
**/
toAnchor: function () {
var anchor = [this.plotX,
this.plotY, 0, 0];
if (this.series.chart.inverted) {
anchor[0] = this.plotY;
anchor[1] = this.plotX;
}
return anchor;
},
/**
* @typedef {Object} Annotation.MockPoint.LabelConfig
* @property {number|undefined} x x value translated to x axis scale
* @property {number|undefined} y y value translated to y axis scale
* @property {Annotation.MockPoint} point instance of the point
*/
/**
* Returns a label config object -
* the same as Highcharts.Point.prototype.getLabelConfig
*
* @return {Annotation.MockPoint.LabelConfig} the point's label config
*/
getLabelConfig: function () {
return {
x: this.x,
y: this.y,
point: this
};
},
/**
* Check if the point is inside its pane.
*
* @return {boolean} A flag indicating whether the point is inside the pane.
*/
isInsidePane: function () {
var plotX = this.plotX,
plotY = this.plotY,
xAxis = this.series.xAxis,
yAxis = this.series.yAxis,
isInside = true;
if (xAxis) {
isInside = defined(plotX) && plotX >= 0 && plotX <= xAxis.len;
}
if (yAxis) {
isInside =
isInside &&
defined(plotY) &&
plotY >= 0 && plotY <= yAxis.len;
}
return isInside;
},
/**
* Refresh point values and coordinates based on its options.
*/
refresh: function () {
var series = this.series,
xAxis = series.xAxis,
yAxis = series.yAxis,
options = this.getOptions();
if (xAxis) {
this.x = options.x;
this.plotX = xAxis.toPixels(options.x, true);
} else {
this.x = null;
this.plotX = options.x;
}
if (yAxis) {
this.y = options.y;
this.plotY = yAxis.toPixels(options.y, true);
} else {
this.y = null;
this.plotY = options.y;
}
this.isInside = this.isInsidePane();
},
/**
* Translate the point.
*
* @param {number} [cx] origin x transformation
* @param {number} [cy] origin y transformation
* @param {number} dx translation for x coordinate
* @param {number} dy translation for y coordinate
**/
translate: function (cx, cy, dx, dy) {
if (!this.hasDynamicOptions()) {
this.plotX += dx;
this.plotY += dy;
this.refreshOptions();
}
},
/**
* Scale the point.
*
* @param {number} cx origin x transformation
* @param {number} cy origin y transformation
* @param {number} sx scale factor x
* @param {number} sy scale factor y
*/
scale: function (cx, cy, sx, sy) {
if (!this.hasDynamicOptions()) {
var x = this.plotX * sx,
y = this.plotY * sy,
tx = (1 - sx) * cx,
ty = (1 - sy) * cy;
this.plotX = tx + x;
this.plotY = ty + y;
this.refreshOptions();
}
},
/**
* Rotate the point.
*
* @param {number} cx origin x rotation
* @param {number} cy origin y rotation
* @param {number} radians
*/
rotate: function (cx, cy, radians) {
if (!this.hasDynamicOptions()) {
var cos = Math.cos(radians),
sin = Math.sin(radians),
x = this.plotX,
y = this.plotY,
tx,
ty;
x -= cx;
y -= cy;
tx = x * cos - y * sin;
ty = x * sin + y * cos;
this.plotX = tx + cx;
this.plotY = ty + cy;
this.refreshOptions();
}
},
/**
* Refresh point options based on its plot coordinates.
*/
refreshOptions: function () {
var series = this.series,
xAxis = series.xAxis,
yAxis = series.yAxis;
this.x = this.options.x = xAxis ?
this.options.x = xAxis.toValue(this.plotX, true) :
this.plotX;
this.y = this.options.y = yAxis ?
yAxis.toValue(this.plotY, true) :
this.plotY;
}
});
return MockPoint;
});
_registerModule(_modules, 'annotations/controllable/controllableMixin.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js'], _modules['annotations/ControlPoint.js'], _modules['annotations/MockPoint.js']], function (H, U, ControlPoint, MockPoint) {
var isObject = U.isObject,
isString = U.isString,
splat = U.splat;
/**
* It provides methods for handling points, control points
* and points transformations.
*
* @private
* @mixin
* @memberOf Annotation
*/
var controllableMixin = {
/**
* Init the controllable
*
* @param {Annotation} annotation - an annotation instance
* @param {Object} options - options specific for controllable
* @param {number} index - index of the controllable element
**/
init: function (annotation,
options,
index) {
this.annotation = annotation;
this.chart = annotation.chart;
this.options = options;
this.points = [];
this.controlPoints = [];
this.index = index;
this.linkPoints();
this.addControlPoints();
},
/**
* Redirect attr usage on the controllable graphic element.
**/
attr: function () {
this.graphic.attr.apply(this.graphic, arguments);
},
/**
* Get the controllable's points options.
*
* @return {Array<PointLikeOptions>} - an array of points' options.
*
*/
getPointsOptions: function () {
var options = this.options;
return options.points || (options.point && splat(options.point));
},
/**
* Utility function for mapping item's options
* to element's attribute
*
* @param {Object} options
* @return {Object} mapped options
**/
attrsFromOptions: function (options) {
var map = this.constructor.attrsMap,
attrs = {},
key,
mappedKey,
styledMode = this.chart.styledMode;
for (key in options) {
mappedKey = map[key];
if (
mappedKey &&
(
!styledMode ||
['fill', 'stroke', 'stroke-width']
.indexOf(mappedKey) === -1
)
) {
attrs[mappedKey] = options[key];
}
}
return attrs;
},
/**
* @typedef {Object} Annotation.controllableMixin.Position
* @property {number} x
* @property {number} y
*/
/**
* An object which denotes an anchor position
*
* @typedef Annotation.controllableMixin.AnchorPosition
* Annotation.controllableMixin.Position
* @property {number} height
* @property {number} width
*/
/**
* An object which denots a controllable's anchor positions
* - relative and absolute.
*
* @typedef {Object} Annotation.controllableMixin.Anchor
* @property {Annotation.controllableMixin.AnchorPosition} relativePosition
* @property {Annotation.controllableMixin.AnchorPosition} absolutePosition
*/
/**
* Returns object which denotes anchor position - relative and absolute.
*
* @param {Annotation.PointLike} point a point like object
* @return {Annotation.controllableMixin.Anchor} a controllable anchor
*/
anchor: function (point) {
var plotBox = point.series.getPlotBox(),
box = point.mock ?
point.toAnchor() :
H.Tooltip.prototype.getAnchor.call({
chart: point.series.chart
},
point),
anchor = {
x: box[0] + (this.options.x || 0),
y: box[1] + (this.options.y || 0),
height: box[2] || 0,
width: box[3] || 0
};
return {
relativePosition: anchor,
absolutePosition: H.merge(anchor, {
x: anchor.x + plotBox.translateX,
y: anchor.y + plotBox.translateY
})
};
},
/**
* Map point's options to a point-like object.
*
* @param {Highcharts.MockPointOptionsObject} pointOptions
* point's options
* @param {Highcharts.PointLike} point
* a point like instance
*
* @return {Highcharts.PointLike|null}
* if the point is found/set returns this point, otherwise null
*/
point: function (pointOptions, point) {
if (pointOptions && pointOptions.series) {
return pointOptions;
}
if (!point || point.series === null) {
if (isObject(pointOptions)) {
point = new MockPoint(
this.chart,
this,
pointOptions
);
} else if (isString(pointOptions)) {
point = this.chart.get(pointOptions) || null;
} else if (typeof pointOptions === 'function') {
var pointConfig = pointOptions.call(point,
this);
point = pointConfig.series ?
pointConfig :
new MockPoint(
this.chart,
this,
pointOptions
);
}
}
return point;
},
/**
* Find point-like objects based on points options.
*
* @return {Array<Annotation.PointLike>} an array of point-like objects
*/
linkPoints: function () {
var pointsOptions = this.getPointsOptions(),
points = this.points,
len = (pointsOptions && pointsOptions.length) || 0,
i,
point;
for (i = 0; i < len; i++) {
point = this.point(pointsOptions[i], points[i]);
if (!point) {
points.length = 0;
return;
}
if (point.mock) {
point.refresh();
}
points[i] = point;
}
return points;
},
/**
* Add control points to a controllable.
*/
addControlPoints: function () {
var controlPointsOptions = this.options.controlPoints;
(controlPointsOptions || []).forEach(
function (controlPointOptions, i) {
var options = H.merge(
this.options.controlPointOptions,
controlPointOptions
);
if (!options.index) {
options.index = i;
}
controlPointsOptions[i] = options;
this.controlPoints.push(
new ControlPoint(this.chart, this, options)
);
},
this
);
},
/**
* Check if a controllable should be rendered/redrawn.
*
* @return {boolean} whether a controllable should be drawn.
*/
shouldBeDrawn: function () {
return Boolean(this.points.length);
},
/**
* Render a controllable.
**/
render: function () {
this.controlPoints.forEach(function (controlPoint) {
controlPoint.render();
});
},
/**
* Redraw a controllable.
*
* @param {boolean} animation
**/
redraw: function (animation) {
this.controlPoints.forEach(function (controlPoint) {
controlPoint.redraw(animation);
});
},
/**
* Transform a controllable with a specific transformation.
*
* @param {string} transformation a transformation name
* @param {number} cx origin x transformation
* @param {number} cy origin y transformation
* @param {number} p1 param for the transformation
* @param {number} p2 param for the transformation
**/
transform: function (transformation, cx, cy, p1, p2) {
if (this.chart.inverted) {
var temp = cx;
cx = cy;
cy = temp;
}
this.points.forEach(function (point, i) {
this.transformPoint(transformation, cx, cy, p1, p2, i);
}, this);
},
/**
* Transform a point with a specific transformation
* If a transformed point is a real point it is replaced with
* the mock point.
*
* @param {string} transformation a transformation name
* @param {number} cx origin x transformation
* @param {number} cy origin y transformation
* @param {number} p1 param for the transformation
* @param {number} p2 param for the transformation
* @param {number} i index of the point
*
**/
transformPoint: function (transformation, cx, cy, p1, p2, i) {
var point = this.points[i];
if (!point.mock) {
point = this.points[i] = MockPoint.fromPoint(point);
}
point[transformation](cx, cy, p1, p2);
},
/**
* Translate a controllable.
*
* @param {number} dx translation for x coordinate
* @param {number} dy translation for y coordinate
**/
translate: function (dx, dy) {
this.transform('translate', null, null, dx, dy);
},
/**
* Translate a specific point within a controllable.
*
* @param {number} dx translation for x coordinate
* @param {number} dy translation for y coordinate
* @param {number} i index of the point
**/
translatePoint: function (dx, dy, i) {
this.transformPoint('translate', null, null, dx, dy, i);
},
/**
* Translate shape within controllable item.
* Replaces `controllable.translate` method.
*
* @param {number} dx translation for x coordinate
* @param {number} dy translation for y coordinate
*/
translateShape: function (dx, dy) {
var chart = this.annotation.chart,
// Annotation.options
shapeOptions = this.annotation.userOptions,
// Chart.options.annotations
annotationIndex = chart.annotations.indexOf(this.annotation),
chartOptions = chart.options.annotations[annotationIndex];
this.translatePoint(dx, dy, 0);
// Options stored in:
// - chart (for exporting)
// - current config (for redraws)
chartOptions[this.collection][this.index].point = this.options.point;
shapeOptions[this.collection][this.index].point = this.options.point;
},
/**
* Rotate a controllable.
*
* @param {number} cx origin x rotation
* @param {number} cy origin y rotation
* @param {number} radians
**/
rotate: function (cx, cy, radians) {
this.transform('rotate', cx, cy, radians);
},
/**
* Scale a controllable.
*
* @param {number} cx origin x rotation
* @param {number} cy origin y rotation
* @param {number} sx scale factor x
* @param {number} sy scale factor y
*/
scale: function (cx, cy, sx, sy) {
this.transform('scale', cx, cy, sx, sy);
},
/**
* Set control points' visibility.
*
* @param {boolean} [visible]
*/
setControlPointsVisibility: function (visible) {
this.controlPoints.forEach(function (controlPoint) {
controlPoint.setVisibility(visible);
});
},
/**
* Destroy a controllable.
*/
destroy: function () {
if (this.graphic) {
this.graphic = this.graphic.destroy();
}
if (this.tracker) {
this.tracker = this.tracker.destroy();
}
this.controlPoints.forEach(function (controlPoint) {
controlPoint.destroy();
});
this.chart = null;
this.points = null;
this.controlPoints = null;
this.options = null;
if (this.annotation) {
this.annotation = null;
}
},
/**
* Update a controllable.
*
* @param {Object} newOptions
*/
update: function (newOptions) {
var annotation = this.annotation,
options = H.merge(true,
this.options,
newOptions),
parentGroup = this.graphic.parentGroup;
this.destroy();
this.constructor(annotation, options);
this.render(parentGroup);
this.redraw();
}
};
return controllableMixin;
});
_registerModule(_modules, 'annotations/controllable/markerMixin.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
var defined = U.defined,
objectEach = U.objectEach,
splat = U.splat;
/**
* Options for configuring markers for annotations.
*
* An example of the arrow marker:
* <pre>
* {
* arrow: {
* id: 'arrow',
* tagName: 'marker',
* refY: 5,
* refX: 5,
* markerWidth: 10,
* markerHeight: 10,
* children: [{
* tagName: 'path',
* attrs: {
* d: 'M 0 0 L 10 5 L 0 10 Z',
* strokeWidth: 0
* }
* }]
* }
* }
* </pre>
* @type {Object}
* @sample highcharts/annotations/custom-markers/
* Define a custom marker for annotations
* @sample highcharts/css/annotations-markers/
* Define markers in a styled mode
* @since 6.0.0
* @apioption defs
*/
var defaultMarkers = {
arrow: {
tagName: 'marker',
render: false,
id: 'arrow',
refY: 5,
refX: 9,
markerWidth: 10,
markerHeight: 10,
children: [{
tagName: 'path',
d: 'M 0 0 L 10 5 L 0 10 Z', // triangle (used as an arrow)
strokeWidth: 0
}]
},
'reverse-arrow': {
tagName: 'marker',
render: false,
id: 'reverse-arrow',
refY: 5,
refX: 1,
markerWidth: 10,
markerHeight: 10,
children: [{
tagName: 'path',
// reverse triangle (used as an arrow)
d: 'M 0 5 L 10 0 L 10 10 Z',
strokeWidth: 0
}]
}
};
H.SVGRenderer.prototype.addMarker = function (id, markerOptions) {
var options = { id: id };
var attrs = {
stroke: markerOptions.color || 'none',
fill: markerOptions.color || 'rgba(0, 0, 0, 0.75)'
};
options.children = markerOptions.children.map(function (child) {
return H.merge(attrs, child);
});
var marker = this.definition(H.merge(true, {
markerWidth: 20,
markerHeight: 20,
refX: 0,
refY: 0,
orient: 'auto'
},
markerOptions,
options));
marker.id = id;
return marker;
};
var createMarkerSetter = function (markerType) {
return function (value) {
this.attr(markerType, 'url(#' + value + ')');
};
};
/**
* @private
* @mixin
*/
var markerMixin = {
markerEndSetter: createMarkerSetter('marker-end'),
markerStartSetter: createMarkerSetter('marker-start'),
/*
* Set markers.
*
* @param {Controllable} item
*/
setItemMarkers: function (item) {
var itemOptions = item.options,
chart = item.chart,
defs = chart.options.defs,
fill = itemOptions.fill,
color = defined(fill) && fill !== 'none' ?
fill :
itemOptions.stroke,
setMarker = function (markerType) {
var markerId = itemOptions[markerType],
def,
predefinedMarker,
key,
marker;
if (markerId) {
for (key in defs) {
def = defs[key];
if (
markerId === def.id && def.tagName === 'marker'
) {
predefinedMarker = def;
break;
}
}
if (predefinedMarker) {
marker = item[markerType] = chart.renderer
.addMarker(
(itemOptions.id || H.uniqueKey()) + '-' +
predefinedMarker.id,
H.merge(predefinedMarker, { color: color })
);
item.attr(mark