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

Position - a new annotation type for stock charts #224

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion src/annotations/PlotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ goog.require('anychart.annotationsModule.InfiniteLine');
goog.require('anychart.annotationsModule.Label');
goog.require('anychart.annotationsModule.Line');
goog.require('anychart.annotationsModule.Marker');
goog.require('anychart.annotationsModule.Position');
goog.require('anychart.annotationsModule.Ray');
goog.require('anychart.annotationsModule.Rectangle');
goog.require('anychart.annotationsModule.TrendChannel');
Expand Down Expand Up @@ -857,6 +858,20 @@ anychart.annotationsModule.PlotController.prototype.ellipse = function(opt_confi
};


/**
* Creates and returns a position annotation.
* @param {Object=} opt_config
* @return {anychart.annotationsModule.Position}
*/
anychart.annotationsModule.PlotController.prototype.position = function(opt_config) {
var annotation = /** @type {anychart.annotationsModule.Position} */(
this.controller_.createAnnotationByType(anychart.enums.AnnotationTypes.POSITION));
annotation.setup(opt_config);
this.bindAnnotation(annotation, true);
return annotation;
};


/**
* Creates and returns a triangle annotation.
* @param {Object=} opt_config
Expand Down Expand Up @@ -887,7 +902,7 @@ anychart.annotationsModule.PlotController.prototype.trendChannel = function(opt_

/**
* Creates and returns a finiteTrendChannel annotation.
* @param {Object=} opt_config
* @param {Object=} opt_config
* @return {anychart.annotationsModule.FiniteTrendChannel}
*/
anychart.annotationsModule.PlotController.prototype.finiteTrendChannel = function(opt_config) {
Expand Down Expand Up @@ -1332,6 +1347,7 @@ anychart.annotationsModule.PlotController.AnchorDragger.prototype.handleDragEnd_
proto['horizontalRange'] = proto.horizontalRange;
proto['rectangle'] = proto.rectangle;
proto['ellipse'] = proto.ellipse;
proto['position'] = proto.position;
proto['triangle'] = proto.triangle;
proto['trendChannel'] = proto.trendChannel;
proto['finiteTrendChannel'] = proto.finiteTrendChannel;
Expand Down
291 changes: 291 additions & 0 deletions src/annotations/Position.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
goog.provide('anychart.annotationsModule.Position');
goog.require('anychart.annotationsModule');
goog.require('anychart.annotationsModule.Base');
goog.require('anychart.core.settings');
goog.require('anychart.enums');



/**
* Position annotation.
* @param {!anychart.annotationsModule.ChartController} chartController
* @constructor
* @extends {anychart.annotationsModule.Base}
*/
anychart.annotationsModule.Position = function(chartController) {
anychart.annotationsModule.Position.base(this, 'constructor', chartController);

/**
* Paths long array.
* @type {Array.<acgraph.vector.Path>}
* @private
*/
this.pathsLong_ = null;

/**
* Paths short array.
* @type {Array.<acgraph.vector.Path>}
* @private
*/
this.pathsShort_ = null;

/**
* Stroke resolver.
* @param {anychart.annotationsModule.Base} annotation
* @param {number} state
* @return {acgraph.vector.Stroke}
* @private
*/
this.strokeResolver_ = /** @type {function(anychart.annotationsModule.Base,number):acgraph.vector.Stroke} */(
anychart.annotationsModule.Base.getColorResolver('stroke', anychart.enums.ColorType.STROKE, true));

/**
* Long Fill resolver.
* @param {anychart.annotationsModule.Base} annotation
* @param {number} state
* @return {acgraph.vector.Fill}
* @private
*/
this.winFillResolver_ = /** @type {function(anychart.annotationsModule.Base,number):acgraph.vector.Fill} */(
anychart.annotationsModule.Base.getColorResolver('winFill', anychart.enums.ColorType.FILL, true));

/**
* Short Fill resolver.
* @param {anychart.annotationsModule.Base} annotation
* @param {number} state
* @return {acgraph.vector.Fill}
* @private
*/
this.lossFillResolver_ = /** @type {function(anychart.annotationsModule.Base,number):acgraph.vector.Fill} */(
anychart.annotationsModule.Base.getColorResolver('lossFill', anychart.enums.ColorType.FILL, true));

anychart.core.settings.createDescriptorsMeta(this.descriptorsMeta, anychart.annotationsModule.X_ANCHOR_DESCRIPTORS_META);
anychart.core.settings.createDescriptorsMeta(this.descriptorsMeta, anychart.annotationsModule.VALUE_ANCHOR_DESCRIPTORS_META);
anychart.core.settings.createDescriptorsMeta(this.descriptorsMeta, anychart.annotationsModule.SECOND_ANCHOR_POINT_DESCRIPTORS_META);
anychart.core.settings.createDescriptorsMeta(this.descriptorsMeta, anychart.annotationsModule.THIRD_ANCHOR_POINT_DESCRIPTORS_META);
};
goog.inherits(anychart.annotationsModule.Position, anychart.annotationsModule.Base);
anychart.core.settings.populateAliases(anychart.annotationsModule.Position, ['winFill', 'lossFill', 'stroke'], 'normal');
anychart.core.settings.populate(anychart.annotationsModule.Position, anychart.annotationsModule.X_ANCHOR_DESCRIPTORS);
anychart.core.settings.populate(anychart.annotationsModule.Position, anychart.annotationsModule.VALUE_ANCHOR_DESCRIPTORS);
anychart.core.settings.populate(anychart.annotationsModule.Position, anychart.annotationsModule.SECOND_ANCHOR_POINT_DESCRIPTORS);
anychart.core.settings.populate(anychart.annotationsModule.Position, anychart.annotationsModule.THIRD_ANCHOR_POINT_DESCRIPTORS);
anychart.annotationsModule.AnnotationTypes[anychart.enums.AnnotationTypes.POSITION] = anychart.annotationsModule.Position;


//region Properties
//----------------------------------------------------------------------------------------------------------------------
//
// Properties
//
//----------------------------------------------------------------------------------------------------------------------
/** @inheritDoc */
anychart.annotationsModule.Position.prototype.type = anychart.enums.AnnotationTypes.POSITION;


/**
* Supported anchors.
* @type {anychart.annotationsModule.AnchorSupport}
*/
anychart.annotationsModule.Position.prototype.SUPPORTED_ANCHORS = anychart.annotationsModule.AnchorSupport.THREE_POINTS;


//endregion
//region State settings
/** @inheritDoc */
anychart.annotationsModule.Position.prototype.getNormalDescriptorsMeta = function() {
var base = anychart.annotationsModule.Position.base(this, 'getNormalDescriptorsMeta');
return goog.array.concat(
base,
anychart.annotationsModule.STROKE_DESCRIPTORS_META,
[
['winFill', anychart.ConsistencyState.APPEARANCE, anychart.Signal.NEEDS_REDRAW],
['lossFill', anychart.ConsistencyState.APPEARANCE, anychart.Signal.NEEDS_REDRAW]
]
);
};


//endregion
//region Drawing
//----------------------------------------------------------------------------------------------------------------------
//
// Drawing
//
//----------------------------------------------------------------------------------------------------------------------
/** @inheritDoc */
anychart.annotationsModule.Position.prototype.ensureCreated = function() {
anychart.annotationsModule.Position.base(this, 'ensureCreated');

// LONG
if (!this.pathsLong_) {
// main, hover
this.pathsLong_ = [this.rootLayer.path(), this.rootLayer.path(), this.rootLayer.path()];

this.pathsLong_[0].zIndex(anychart.annotationsModule.Base.STROKE_ZINDEX); // stroke
this.pathsLong_[1].zIndex(anychart.annotationsModule.Base.SHAPES_ZINDEX); // fill
this.pathsLong_[2].zIndex(anychart.annotationsModule.Base.HOVER_SHAPE_ZINDEX);
}

// SHORT
if (!this.pathsShort_) {
this.pathsShort_ = [this.rootLayer.path(), this.rootLayer.path(), this.rootLayer.path()];

this.pathsShort_[0].zIndex(anychart.annotationsModule.Base.STROKE_ZINDEX); // stroke
this.pathsShort_[1].zIndex(anychart.annotationsModule.Base.SHAPES_ZINDEX); // fill
this.pathsShort_[2].zIndex(anychart.annotationsModule.Base.HOVER_SHAPE_ZINDEX);
}
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.drawOnePointShape = function(x, y) {
for (var i = 0; i < this.pathsLong_.length; i++) {
var path = this.pathsLong_[i];
path.clear();
path.moveTo(x, y).lineTo(x, y);
}
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.drawTwoPointsShape = function(x1, y1, x2, y2) {
var thickness = anychart.utils.extractThickness(/** @type {acgraph.vector.Stroke} */(this.pathsLong_[0].stroke()));
x1 = anychart.utils.applyPixelShift(x1, thickness);
y1 = anychart.utils.applyPixelShift(y1, thickness);
x2 = anychart.utils.applyPixelShift(x2, thickness);
y2 = anychart.utils.applyPixelShift(y2, thickness);

this.pathsLong_[0].clear();

// stroke direction for long
this.pathsLong_[0].moveTo(x1, y1).lineTo(x2, y2);
// draw separator on long layer
this.pathsLong_[0].moveTo(x1, y1).lineTo(x2, y1);

// draw rectangle
for (var i = 1; i < this.pathsLong_.length; i++) {
var path = this.pathsLong_[i];
path.clear();
path.moveTo(x1, y1)
.lineTo(x2, y1)
.lineTo(x2, y2)
.lineTo(x1, y2)
.close();
}
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.drawThreePointsShape = function(x1, y1, x2, y2, x3, y3) {
var thickness = anychart.utils.extractThickness(/** @type {acgraph.vector.Stroke} */(this.pathsLong_[0].stroke()));
x1 = anychart.utils.applyPixelShift(x1, thickness);
y1 = anychart.utils.applyPixelShift(y1, thickness);
x2 = anychart.utils.applyPixelShift(x2, thickness);
y2 = anychart.utils.applyPixelShift(y2, thickness);

var lx, rx;
lx = Math.max(x1, x2, x3);
rx = Math.min(x1, x2, x3);

this.pathsLong_[0].clear();
this.pathsShort_[0].clear();

// stroke direction for long
this.pathsLong_[0].moveTo(x1, y1).lineTo(x2, y2);
// stroke direction for short
this.pathsShort_[0].moveTo(x1, y1).lineTo(x3, y3);
// draw separator on long layer
this.pathsLong_[0].moveTo(lx, y1).lineTo(rx, y1);

// draw rectangle for long
for (var i = 1; i < this.pathsLong_.length; i++) {
var path = this.pathsLong_[i];
path.clear();
path.moveTo(lx, y1)
.lineTo(rx, y1)
.lineTo(rx, y2)
.lineTo(lx, y2)
.close();
}

// draw rectangle for short
for (var i = 1; i < this.pathsShort_.length; i++) {
var path = this.pathsShort_[i];
path.clear();
path.moveTo(lx, y1)
.lineTo(rx, y1)
.lineTo(rx, y3)
.lineTo(lx, y3)
.close();
}
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.colorize = function(state) {
anychart.annotationsModule.Position.base(this, 'colorize', state);
this.pathsLong_[0]
.fill(null)
.stroke(this.strokeResolver_(this, state));
this.pathsLong_[1]
.stroke(null)
.fill(this.winFillResolver_(this, state));
this.pathsLong_[2]
.fill(anychart.color.TRANSPARENT_HANDLER)
.stroke(/** @type {acgraph.vector.SolidFill} */(anychart.color.TRANSPARENT_HANDLER), this['hoverGap']() * 2);

this.pathsShort_[0]
.fill(null)
.stroke(this.strokeResolver_(this, state));
this.pathsShort_[1]
.stroke(null)
.fill(this.lossFillResolver_(this, state));
this.pathsShort_[2]
.fill(anychart.color.TRANSPARENT_HANDLER)
.stroke(/** @type {acgraph.vector.SolidFill} */(anychart.color.TRANSPARENT_HANDLER), this['hoverGap']() * 2);
};


//endregion
//region Serialization / Deserialization / Disposing
//----------------------------------------------------------------------------------------------------------------------
//
// Serialization / Deserialization / Disposing
//
//----------------------------------------------------------------------------------------------------------------------
/** @inheritDoc */
anychart.annotationsModule.Position.prototype.serialize = function() {
var json = anychart.annotationsModule.Position.base(this, 'serialize');

anychart.core.settings.serialize(this, anychart.annotationsModule.X_ANCHOR_DESCRIPTORS, json, 'Annotation');
anychart.core.settings.serialize(this, anychart.annotationsModule.VALUE_ANCHOR_DESCRIPTORS, json, 'Annotation');
anychart.core.settings.serialize(this, anychart.annotationsModule.SECOND_ANCHOR_POINT_DESCRIPTORS, json, 'Annotation');
anychart.core.settings.serialize(this, anychart.annotationsModule.THIRD_ANCHOR_POINT_DESCRIPTORS, json, 'Annotation');

return json;
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.setupByJSON = function(config, opt_default) {
anychart.annotationsModule.Position.base(this, 'setupByJSON', config, opt_default);

anychart.core.settings.deserialize(this, anychart.annotationsModule.X_ANCHOR_DESCRIPTORS, config);
anychart.core.settings.deserialize(this, anychart.annotationsModule.VALUE_ANCHOR_DESCRIPTORS, config);
anychart.core.settings.deserialize(this, anychart.annotationsModule.SECOND_ANCHOR_POINT_DESCRIPTORS, config);
anychart.core.settings.deserialize(this, anychart.annotationsModule.THIRD_ANCHOR_POINT_DESCRIPTORS, config);
};


/** @inheritDoc */
anychart.annotationsModule.Position.prototype.disposeInternal = function() {
anychart.annotationsModule.Position.base(this, 'disposeInternal');

goog.disposeAll(this.pathsLong_);
goog.disposeAll(this.pathsShort_);
delete this.strokeResolver_;
delete this.winFillResolver_;
delete this.lossFillResolver_;
};
//endregion
25 changes: 25 additions & 0 deletions src/annotations/defaultTheme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
goog.provide('anychart.annotationsModule.defaultTheme');
goog.require('anychart.core.defaultTheme');

/**
* @this {{
* sourceColor: (acgraph.vector.SolidFill|string)
* }}
* @return {*}
*/
anychart.core.defaultTheme.returnRisingColor20 = function() {
return anychart.color.setOpacity(anychart.core.defaultTheme.risingColor, 0.2, true);
};

/**
* @this {{
* sourceColor: (acgraph.vector.SolidFill|string)
* }}
* @return {*}
*/
anychart.core.defaultTheme.returnFallingColor20 = function() {
return anychart.color.setOpacity(anychart.core.defaultTheme.fallingColor, 0.2, true);
};

goog.mixin(goog.global['anychart']['themes']['defaultTheme']['chart'], {
'defaultAnnotationSettings': {
Expand Down Expand Up @@ -61,6 +80,12 @@ goog.mixin(goog.global['anychart']['themes']['defaultTheme']['chart'], {
'horizontalLine': {},
'rectangle': {},
'ellipse': {},
'position': {
'normal': {
'winFill': anychart.core.defaultTheme.returnRisingColor20,
'lossFill': anychart.core.defaultTheme.returnFallingColor20
}
},
'triangle': {},
'trendChannel': {},
'finiteTrendChannel': {
Expand Down
2 changes: 2 additions & 0 deletions src/core/StateSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ anychart.core.StateSettings.prototype.PROPERTY_DESCRIPTORS = (function() {
descriptors.HIGH_STROKE,
descriptors.LOW_FILL,
descriptors.HIGH_FILL,
descriptors.WIN_FILL,
descriptors.LOSS_FILL,
descriptors.NEGATIVE_STROKE,
descriptors.RISING_STROKE,
descriptors.FALLING_STROKE,
Expand Down
2 changes: 2 additions & 0 deletions src/core/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,8 @@ anychart.core.settings.descriptors = (function() {
map.FALLING_FILL = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'fallingFill', anychart.core.settings.fillOrFunctionNormalizer];
map.LOW_FILL = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'lowFill', anychart.core.settings.fillOrFunctionNormalizer];
map.HIGH_FILL = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'highFill', anychart.core.settings.fillOrFunctionNormalizer];
map.WIN_FILL = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'winFill', anychart.core.settings.fillOrFunctionNormalizer];
map.LOSS_FILL = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'lossFill', anychart.core.settings.fillOrFunctionNormalizer];

map.STROKE = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'stroke', anychart.core.settings.strokeNormalizer];
map.STROKE_FUNCTION = [anychart.enums.PropertyHandlerType.MULTI_ARG, 'stroke', anychart.core.settings.strokeOrFunctionNormalizer];
Expand Down
Loading