@amcharts/amcharts5
Version:
amCharts 5
217 lines • 8.75 kB
JavaScript
import { __awaiter } from "tslib";
import { MovingAverage } from "./MovingAverage";
import { LineSeries } from "../../xy/series/LineSeries";
import * as $array from "../../../core/util/Array";
/**
* An implementation of a [[StockChart]] indicator.
*
* @see {@link https://www.amcharts.com/docs/v5/charts/stock/indicators/} for more info
*/
export class MovingAverageEnvelope extends MovingAverage {
constructor() {
super(...arguments);
this._editableSettings = [{
key: "period",
name: this.root.language.translateAny("Period"),
type: "number",
minValue: 2,
maxValue: 500,
step: 1
}, {
key: "maType",
name: this.root.language.translateAny("Type"),
type: "dropdown",
options: ["simple", "weighted", "exponential", "dema", "tema"]
}, {
key: "field",
name: this.root.language.translateAny("Field"),
type: "dropdown",
options: ["open", "close", "low", "high", "hl/2", "hlc/3", "hlcc/4", "ohlc/4"]
}, {
key: "shiftType",
name: this.root.language.translateAny("Shift type"),
type: "dropdown",
// No extTargetValue: switching mode keeps the user's shift value and just
// re-clamps it into the new mode's range (see _updateShiftBounds).
options: [
{ value: "percent", text: this.root.language.translateAny("percent"), extTarget: "shift", extTargetMinValue: 0.1, extTargetMaxValue: 20, extTargetStep: 0.1 },
{ value: "points", text: this.root.language.translateAny("points"), extTarget: "shift", extTargetMinValue: 1, extTargetMaxValue: 5000, extTargetStep: 1 }
]
}, {
key: "shift",
name: this.root.language.translateAny("Shift"),
type: "number",
minValue: 0.1,
maxValue: 20,
step: 0.1
}, {
key: "offset",
name: this.root.language.translateAny("Offset"),
type: "number",
minValue: -50,
maxValue: 50,
step: 1
}, {
key: "upperColor",
name: this.root.language.translateAny("Top"),
type: "color"
}, {
key: "seriesColor",
name: this.root.language.translateAny("Median"),
type: "color"
}, {
key: "lowerColor",
name: this.root.language.translateAny("Bottom"),
type: "color"
}];
}
/**
* Keeps the `shift` setting's limits in sync with `shiftType`: percent mode is
* 0.1..20 (%) with a 0.1 step; points mode is 1..5000 with a step of 1.
*/
_updateShiftBounds() {
let shiftSetting;
$array.each(this._editableSettings, (setting) => {
if (setting.key == "shift" && setting.type == "number") {
shiftSetting = setting;
}
});
if (shiftSetting) {
if (this.get("shiftType") == "points") {
shiftSetting.minValue = 1;
shiftSetting.maxValue = 5000;
shiftSetting.step = 1;
}
else {
shiftSetting.minValue = 0.1;
shiftSetting.maxValue = 20;
shiftSetting.step = 0.1;
}
}
}
_clampSettings(force = false) {
this._updateShiftBounds();
super._clampSettings(force);
}
_afterNew() {
const stockSeries = this.get("stockSeries");
const chart = stockSeries.chart;
if (chart) {
const upperBandSeries = chart.series.push(LineSeries.new(this._root, {
valueXField: "valueX",
valueYField: "upper",
openValueYField: "lower",
xAxis: stockSeries.get("xAxis"),
yAxis: stockSeries.get("yAxis"),
groupDataDisabled: true,
calculateAggregates: true,
themeTags: ["indicator", "movingaverageenvelope", "upper"]
}));
upperBandSeries.fills.template.set("visible", true);
upperBandSeries.setPrivate("baseValueSeries", stockSeries);
this.upperBandSeries = upperBandSeries;
const lowerBandSeries = chart.series.push(LineSeries.new(this._root, {
valueXField: "valueX",
valueYField: "lower",
xAxis: stockSeries.get("xAxis"),
yAxis: stockSeries.get("yAxis"),
groupDataDisabled: true,
calculateAggregates: true,
themeTags: ["indicator", "movingaverageenvelope", "lower"]
}));
lowerBandSeries.setPrivate("baseValueSeries", stockSeries);
this.lowerBandSeries = lowerBandSeries;
}
super._afterNew();
this.series.addTag("movingaverageenvelope");
this.series._applyThemes();
}
_updateChildren() {
super._updateChildren();
if (this.isDirty("upperColor")) {
const color = this.get("upperColor");
const upperBandSeries = this.upperBandSeries;
upperBandSeries.set("stroke", color);
upperBandSeries.set("fill", color);
upperBandSeries.strokes.template.set("stroke", color);
this._updateSeriesColor(upperBandSeries, color, "upperColor");
}
if (this.isDirty("lowerColor")) {
const color = this.get("lowerColor");
const lowerBandSeries = this.lowerBandSeries;
lowerBandSeries.set("stroke", color);
lowerBandSeries.strokes.template.set("stroke", color);
this._updateSeriesColor(lowerBandSeries, color, "lowerColor");
}
}
_prepareChildren() {
super._prepareChildren();
if (this.isDirty("shiftType") || this.isDirty("shift")) {
this.markDataDirty();
this.setCustomData("shift", this.get("shift"));
this.setCustomData("shiftType", this.get("shiftType"));
}
}
/**
* @ignore
*/
prepareData() {
super.prepareData();
if (this.series) {
let smaData = this.series.data.values;
let shift = Number(this.get("shift", 5));
let shiftType = this.get("shiftType");
$array.each(smaData, (dataItem) => {
let value = dataItem.ma;
if (value != null) {
let upper = value;
let lower = value;
if (shiftType == "points") {
upper += shift;
lower -= shift;
}
else {
upper += upper * shift / 100;
lower -= lower * shift / 100;
}
dataItem.upper = upper;
dataItem.lower = lower;
}
});
this.upperBandSeries.updateData(smaData);
this.lowerBandSeries.updateData(smaData);
}
}
_dispose() {
this.upperBandSeries.dispose();
this.lowerBandSeries.dispose();
super._dispose();
}
hide(duration) {
const _super = Object.create(null, {
hide: { get: () => super.hide }
});
return __awaiter(this, void 0, void 0, function* () {
return Promise.all([
_super.hide.call(this, duration),
this.upperBandSeries.hide(duration),
this.lowerBandSeries.hide(duration)
]);
});
}
show(duration) {
const _super = Object.create(null, {
show: { get: () => super.show }
});
return __awaiter(this, void 0, void 0, function* () {
return Promise.all([
_super.show.call(this, duration),
this.upperBandSeries.show(duration),
this.lowerBandSeries.show(duration)
]);
});
}
}
MovingAverageEnvelope.className = "MovingAverageEnvelope";
MovingAverageEnvelope.classNames = MovingAverage.classNames.concat([MovingAverageEnvelope.className]);
//# sourceMappingURL=MovingAverageEnvelope.js.map