UNPKG

highcharts

Version:
148 lines (147 loc) 4.85 kB
/* * * * * */ 'use strict'; import Annotation from '../Annotation.js'; import D from '../../../Core/Defaults.js'; const { defaultOptions } = D; import MockPoint from '../MockPoint.js'; import { merge, pick } from '../../../Shared/Utilities.js'; if (defaultOptions.annotations?.types) { /** * Options for the vertical line annotation type. * * @sample highcharts/annotations-advanced/vertical-line/ * Vertical line * * @extends annotations.types.crookedLine * @excluding labels, shapes, controlPointOptions * @product highstock * @optionparent annotations.types.verticalLine */ defaultOptions.annotations.types.verticalLine = { typeOptions: { /** * @ignore */ yOffset: 10, /** * Label options. * * @extends annotations.types.crookedLine.labelOptions */ label: { offset: -40, point: function (target) { return target.annotation.points[0]; }, allowOverlap: true, backgroundColor: 'none', borderWidth: 0, crop: true, overflow: 'none', shape: 'rect', text: '{y:.2f}' }, /** * Connector options. * * @extends annotations.shapeOptions * @excluding height, r, type, width */ connector: { strokeWidth: 1, markerEnd: 'arrow' } }, labelOptions: { style: { color: "#333333" /* Palette.neutralColor80 */, fontSize: '0.7em' } } }; } /* * * * Class * * */ /** @internal */ class VerticalLine extends Annotation { /* * * * Static Functions * * */ static connectorFirstPoint(target) { const annotation = target.annotation, chart = annotation.chart, inverted = chart.inverted, point = annotation.points[0], left = pick(point.series.yAxis?.left, 0), top = pick(point.series.yAxis?.top, 0), offset = annotation.options.typeOptions?.label?.offset || 0, y = MockPoint.pointToPixels(point, true)[inverted ? 'x' : 'y']; return { x: point.x, xAxis: point.series.xAxis, y: y + offset + (inverted ? (left - chart.plotLeft) : (top - chart.plotTop)) }; } static connectorSecondPoint(target) { const annotation = target.annotation, chart = annotation.chart, inverted = chart.inverted, typeOptions = annotation.options.typeOptions, point = annotation.points[0], left = pick(point.series.yAxis && point.series.yAxis.left, 0), top = pick(point.series.yAxis && point.series.yAxis.top, 0), y = MockPoint.pointToPixels(point, true)[inverted ? 'x' : 'y']; let yOffset = typeOptions?.yOffset || 0; if ((typeOptions?.label?.offset || 0) < 0) { yOffset *= -1; } return { x: point.x, xAxis: point.series.xAxis, y: y + yOffset + (inverted ? (left - chart.plotLeft) : (top - chart.plotTop)) }; } /* * * * Functions * * */ getPointsOptions() { return this.options.typeOptions?.point ? [this.options.typeOptions.point] : []; } addShapes() { var _a; const typeOptions = this.options.typeOptions, connector = this.initShape(merge(typeOptions.connector, { type: 'path', points: [ VerticalLine.connectorFirstPoint, VerticalLine.connectorSecondPoint ], className: 'highcharts-vertical-line' }), 0); typeOptions.connector = connector.options; // Update to be able to save the chart after drag (#18584). ((_a = this.userOptions).typeOptions || (_a.typeOptions = {})).point = typeOptions.point; } addLabels() { const typeOptions = this.options.typeOptions, labelOptions = typeOptions.label, offset = labelOptions?.offset || 0; let x = 0, y = offset, verticalAlign = offset < 0 ? 'bottom' : 'top', align = 'center'; if (this.chart.inverted) { x = offset; y = 0; verticalAlign = 'middle'; align = offset < 0 ? 'right' : 'left'; } const label = this.initLabel(merge(labelOptions, { verticalAlign: verticalAlign, align: align, x: x, y: y })); typeOptions.label = label.options; } } Annotation.types.verticalLine = VerticalLine; /* * * * Default Export * * */ export default VerticalLine;