UNPKG

scichart

Version:

Fast WebGL JavaScript Charting Library and Framework

335 lines (334 loc) 17.4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOHLCYRange = exports.OhlcDataSeries = void 0; var NumberRange_1 = require("../../Core/NumberRange"); var SearchMode_1 = require("../../types/SearchMode"); var ValueName_1 = require("../../types/ValueName"); var YRangeMode_1 = require("../../types/YRangeMode"); var vectorToArray_1 = require("../../utils/vectorToArray"); var BaseDataSeries_1 = require("./BaseDataSeries"); var IDataSeries_1 = require("./IDataSeries"); /** * OhlcDataSeries is a DataSeries for holding Open, High, Low, Close data in SciChart's * {@link https://www.scichart.com/javascript-chart-features | JavaScript Stock Charts} * @remarks * The OhlcDataSeries is primarily used with the {@link FastCandlestickRenderableSeries | JavaScript Candlestick Chart} * but can also be used with our {@link FastOhlcRenderableSeries | JavaScript Ohlc Chart}, * used for drawing {@link https://www.scichart.com/javascript-chart-features | JavaScript Stock Charts} and Candlestick or OHLC charts. * * A DataSeries stores the data to render. This is independent from the {@link IRenderableSeries | RenderableSeries} * which defines how that data should be rendered. * * See derived types of {@link BaseDataSeries} to find out what data-series are available. * See derived types of {@link IRenderableSeries} to find out what 2D JavaScript Chart types are available. * * --- * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/data-series-api/data-series-api-overview/} */ var OhlcDataSeries = /** @class */ (function (_super) { __extends(OhlcDataSeries, _super); /** * Creates an instance of {@link OhlcDataSeries} * @param webAssemblyContext the {@link TSciChart | SciChart WebAssembly Context} containing native methods * and access to our underlying WebGL2 rendering engine * @param options the {@link IOhlcDataSeriesOptions} which can be passed to configure the DataSeries at construct time * * --- * 📚 Docs: {@link https://www.scichart.com/documentation/js/v4/2d-charts/chart-types/data-series-api/data-series-api-overview/} */ function OhlcDataSeries(webAssemblyContext, options) { var _this = this; var baseOptions = __assign(__assign({}, options), { arrayCount: 4, valueNames: [ValueName_1.EValueName.Close, ValueName_1.EValueName.Open, ValueName_1.EValueName.High, ValueName_1.EValueName.Low], includeInYRange: [true, true, true, true] }); _this = _super.call(this, webAssemblyContext, baseOptions) || this; /** @inheritDoc */ _this.type = IDataSeries_1.EDataSeriesType.Ohlc; if (baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.xValues) { _this.appendRange(baseOptions.xValues, baseOptions.openValues, baseOptions.highValues, baseOptions.lowValues, baseOptions.closeValues, baseOptions.metadata); if ((baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoCapacity) && (baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex)) { _this.xValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex); _this.yValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex); _this.openValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex); _this.highValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex); _this.lowValues.notifyAppend(baseOptions === null || baseOptions === void 0 ? void 0 : baseOptions.fifoStartIndex); } } return _this; } Object.defineProperty(OhlcDataSeries.prototype, "yValues", { get: function () { return this.getNativeYValues(); }, enumerable: false, configurable: true }); Object.defineProperty(OhlcDataSeries.prototype, "openValues", { get: function () { return this.getNativeOpenValues(); }, enumerable: false, configurable: true }); Object.defineProperty(OhlcDataSeries.prototype, "highValues", { get: function () { return this.getNativeHighValues(); }, enumerable: false, configurable: true }); Object.defineProperty(OhlcDataSeries.prototype, "lowValues", { get: function () { return this.getNativeLowValues(); }, enumerable: false, configurable: true }); /** * Gets a native / WebAssembly vector of Open-values in the DataSeries */ OhlcDataSeries.prototype.getNativeOpenValues = function () { return this.getNativeYValues(1); }; /** * Gets a native / WebAssembly vector of High-values in the DataSeries */ OhlcDataSeries.prototype.getNativeHighValues = function () { return this.getNativeYValues(2); }; /** * Gets a native / WebAssembly vector of Low-values in the DataSeries */ OhlcDataSeries.prototype.getNativeLowValues = function () { return this.getNativeYValues(3); }; /** * Gets a native / WebAssembly vector of Close-values in the DataSeries */ OhlcDataSeries.prototype.getNativeCloseValues = function () { return this.getNativeYValues(); }; /** * Appends a single X (Date), Open, High, Low, Close point to the DataSeries * @remarks * For best performance on drawing large datasets, use the {@link appendRange} method * X-value is a Date, encoded as a Unix Timestamp. * * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param x X-value is a Date, encoded as a Unix Timestamp. * @param open The Open value for this OHLC bar * @param high The High value for this OHLC bar * @param low The Low value for this OHLC bar * @param close The Close value for this OHLC bar * @param metadata The point metadata */ OhlcDataSeries.prototype.append = function (x, open, high, low, close, metadata) { _super.prototype.appendN.call(this, x, [close, open, high, low], metadata); }; /** * Appends arrays of X (Date), Open, High, Low, Close point to the DataSeries * @remarks * This method is considerably higher performance than {@link append} which appends a single point * X-value is a Date, encoded as a Unix Timestamp. * * Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param xValues X-values are Dates, encoded as a Unix Timestamp. * @param openValues The Open values for this OHLC bar * @param highValues The High values for this OHLC bar * @param lowValues The Low values for this OHLC bar * @param closeValues The Close value sfor this OHLC bar * @param metadata The array of point metadata */ OhlcDataSeries.prototype.appendRange = function (xValues, openValues, highValues, lowValues, closeValues, metadata) { _super.prototype.appendRangeN.call(this, xValues, [closeValues, openValues, highValues, lowValues], metadata); }; /** * Updates a single Open, High, Low, Close value by X-index * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param index the index to update * @param open The new Open value * @param high The new High value * @param low The new Low value * @param close The new Close value * @param metadata The point metadata */ OhlcDataSeries.prototype.update = function (index, open, high, low, close, metadata) { _super.prototype.updateN.call(this, index, [close, open, high, low], metadata); }; /** * Updates a single X, Open, High, Low, Close value by X-index. Might also need to set isSorted = false * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param index the index to update * @param x The new X value * @param open The new Open value * @param high The new High value * @param low The new Low value * @param close The new Close value * @param metadata The point metadata */ OhlcDataSeries.prototype.updateXohlc = function (index, x, open, high, low, close, metadata) { _super.prototype.updateXyN.call(this, index, x, [close, open, high, low], metadata); }; /** * Inserts a single Date, Open, High, Low, Close value at the X-index * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param startIndex the index to insert at * @param x the X-value (date) encoded as a Unix Timestamp * @param open The Open value * @param high The High value * @param low The Low value * @param close The Close value * @param metadata The point metadata */ OhlcDataSeries.prototype.insert = function (startIndex, x, open, high, low, close, metadata) { _super.prototype.insertN.call(this, startIndex, x, [close, open, high, low], metadata); }; /** * Inserts a range of Date, Open, High, Low, Close value at the X-index * @remarks Any changes of the DataSeries will trigger a redraw on the parent {@link SciChartSurface} * @param startIndex the index to insert at * @param xValues the X-values (dates) encoded as a Unix Timestamp * @param openValues The Open values * @param highValues The High values * @param lowValues The Low values * @param closeValues The Close values * @param metadata The array of point metadata */ OhlcDataSeries.prototype.insertRange = function (startIndex, xValues, openValues, highValues, lowValues, closeValues, metadata) { _super.prototype.insertRangeN.call(this, startIndex, xValues, [closeValues, openValues, highValues, lowValues], metadata); }; /** @inheritDoc */ OhlcDataSeries.prototype.getWindowedYRange = function (xRange, getPositiveRange, isXCategoryAxis, dataSeriesValueType, yRangeMode) { if (isXCategoryAxis === void 0) { isXCategoryAxis = false; } if (dataSeriesValueType === void 0) { dataSeriesValueType = IDataSeries_1.EDataSeriesValueType.Default; } if (yRangeMode === void 0) { yRangeMode = YRangeMode_1.EYRangeMode.Visible; } var _a = this.getOHLCValues(dataSeriesValueType), openValues = _a.openValues, closeValues = _a.closeValues, highValues = _a.highValues, lowValues = _a.lowValues; if (this.count() === 1) { var min = Math.min(openValues.get(0), lowValues.get(0)); var max = Math.max(closeValues.get(0), highValues.get(0)); return new NumberRange_1.NumberRange(min, max); } var indicesRange = isXCategoryAxis ? xRange : this.getIndicesRange(xRange, false, yRangeMode === YRangeMode_1.EYRangeMode.Visible ? SearchMode_1.ESearchMode.RoundUp : SearchMode_1.ESearchMode.RoundDown, yRangeMode === YRangeMode_1.EYRangeMode.Visible ? SearchMode_1.ESearchMode.RoundDown : SearchMode_1.ESearchMode.RoundUp); return getOHLCYRange(indicesRange, openValues, highValues, lowValues, closeValues); }; /** @inheritDoc */ OhlcDataSeries.prototype.getOptions = function (excludeData) { if (excludeData === void 0) { excludeData = false; } var json = _super.prototype.getOptions.call(this, excludeData); if (!excludeData) { // const dataSize = this.count(); // const xValues: number[] = new Array(dataSize); // const openValues: number[] = new Array(dataSize); // const highValues: number[] = new Array(dataSize); // const lowValues: number[] = new Array(dataSize); // const closeValues: number[] = new Array(dataSize); // if (this.fifoCapacity && this.fifoSweeping) { // for (let i = 0; i < dataSize; i++) { // xValues[i] = (this.xValues as SCRTFifoVector).getRaw(i); // openValues[i] = (this.openValues as SCRTFifoVector).getRaw(i); // closeValues[i] = (this.yValues as SCRTFifoVector).getRaw(i); // highValues[i] = (this.highValues as SCRTFifoVector).getRaw(i); // lowValues[i] = (this.lowValues as SCRTFifoVector).getRaw(i); // } // } else { // for (let i = 0; i < dataSize; i++) { // xValues[i] = this.xValues.get(i); // openValues[i] = this.openValues.get(i); // closeValues[i] = this.yValues.get(i); // highValues[i] = this.highValues.get(i); // lowValues[i] = this.lowValues.get(i); // } // } // Vroom Vroom! var xValues = (0, vectorToArray_1.vectorToArray)(this.xValues, this.webAssemblyContext); var openValues = (0, vectorToArray_1.vectorToArray)(this.openValues, this.webAssemblyContext); var highValues = (0, vectorToArray_1.vectorToArray)(this.highValues, this.webAssemblyContext); var lowValues = (0, vectorToArray_1.vectorToArray)(this.lowValues, this.webAssemblyContext); var closeValues = (0, vectorToArray_1.vectorToArray)(this.yValues, this.webAssemblyContext); var options = { xValues: xValues, openValues: openValues, highValues: highValues, lowValues: lowValues, closeValues: closeValues }; Object.assign(json, options); } return json; }; OhlcDataSeries.prototype.getOHLCValues = function (dataSeriesValueType) { var openValues; var highValues; var lowValues; var closeValues; switch (dataSeriesValueType) { case IDataSeries_1.EDataSeriesValueType.FinalAnimationValues: openValues = this.yFinalAnimationValuesArray[1]; highValues = this.yFinalAnimationValuesArray[2]; lowValues = this.yFinalAnimationValuesArray[3]; closeValues = this.yFinalAnimationValuesArray[0]; break; case IDataSeries_1.EDataSeriesValueType.InitialAnimationValues: openValues = this.yInitialAnimationValuesArray[1]; highValues = this.yInitialAnimationValuesArray[2]; lowValues = this.yInitialAnimationValuesArray[3]; closeValues = this.yInitialAnimationValuesArray[0]; break; default: openValues = this.openValues; highValues = this.highValues; lowValues = this.lowValues; closeValues = this.yValues; } return { openValues: openValues, highValues: highValues, lowValues: lowValues, closeValues: closeValues }; }; return OhlcDataSeries; }(BaseDataSeries_1.BaseDataSeries)); exports.OhlcDataSeries = OhlcDataSeries; function getOHLCYRange(indicesRange, openValues, highValues, lowValues, closeValues) { var yMin = Number.MAX_VALUE; var yMax = Number.NEGATIVE_INFINITY; var iMin = Math.max(indicesRange.min, 0); var iMax = Math.min(indicesRange.max, openValues.size() - 1); if (iMax < iMin) { return undefined; } for (var i = iMin; i <= iMax; i++) { var highVal = highValues.get(i); var lowVal = lowValues.get(i); if (lowVal < yMin) { yMin = lowVal; } if (highVal > yMax) { yMax = highVal; } } return new NumberRange_1.NumberRange(yMin, yMax); } exports.getOHLCYRange = getOHLCYRange;