From 49879f8be2086d9c3c5d8d4701124b00ac95cafc Mon Sep 17 00:00:00 2001 From: liihuu Date: Thu, 23 Nov 2023 03:25:21 +0800 Subject: [PATCH] chore: chore --- src/Chart.ts | 27 +++++++------- src/common/Action.ts | 4 +- src/common/Bounding.ts | 4 +- src/common/Eventful.ts | 4 +- src/common/SyntheticEvent.ts | 21 ++++++----- src/common/utils/canvas.ts | 4 +- src/component/Overlay.ts | 27 +++++++------- src/component/XAxis.ts | 7 ++-- src/component/YAxis.ts | 23 +++++++----- src/extension/overlay/fibonacciLine.ts | 7 ++-- src/extension/overlay/horizontalRayLine.ts | 3 +- src/extension/overlay/simpleTag.ts | 4 +- src/index.ts | 24 ++++++------ src/store/ActionStore.ts | 5 ++- src/store/ChartStore.ts | 15 ++++---- src/store/IndicatorStore.ts | 5 ++- src/store/OverlayStore.ts | 43 ++++++++++++---------- src/store/TimeScaleStore.ts | 7 ++-- src/store/TooltipStore.ts | 3 +- src/view/CandleLastPriceLabelView.ts | 6 +-- src/view/CandleTooltipView.ts | 9 ++--- src/view/CrosshairHorizontalLabelView.ts | 8 ++-- src/view/CrosshairLineView.ts | 3 +- src/view/CrosshairVerticalLabelView.ts | 4 +- src/view/IndicatorLastValueView.ts | 8 ++-- src/view/IndicatorTooltipView.ts | 21 +++++------ src/view/OverlayView.ts | 12 +++--- src/view/OverlayXAxisView.ts | 3 +- src/view/OverlayYAxisView.ts | 4 +- src/view/View.ts | 7 ++-- 30 files changed, 171 insertions(+), 151 deletions(-) diff --git a/src/Chart.ts b/src/Chart.ts index d7a5fc35c..f102c208e 100644 --- a/src/Chart.ts +++ b/src/Chart.ts @@ -30,7 +30,7 @@ import VisibleRange from './common/VisibleRange' import { createId } from './common/utils/id' import { createDom } from './common/utils/dom' import { getPixelRatio } from './common/utils/canvas' -import { isString, isArray, isValid, merge } from './common/utils/typeChecks' +import { isString, isArray, isValid, merge, isNumber } from './common/utils/typeChecks' import { logWarn } from './common/utils/logger' import { formatValue } from './common/utils/format' import { binarySearchNearest } from './common/utils/number' @@ -41,6 +41,8 @@ import CandlePane from './pane/CandlePane' import IndicatorPane from './pane/IndicatorPane' import XAxisPane from './pane/XAxisPane' import DrawPane from './pane/DrawPane' +import SeparatorPane from './pane/SeparatorPane' + import { PaneOptions, PanePosition, PANE_DEFAULT_HEIGHT, PaneIdConstants } from './pane/types' import Axis from './component/Axis' @@ -54,7 +56,6 @@ import { getStyles as getExtensionStyles } from './extension/styles/index' import Event from './Event' import { CustomApi, LayoutChildType, Options } from './Options' -import SeparatorPane from './pane/SeparatorPane' export enum DomPosition { Root = 'root', @@ -389,7 +390,7 @@ export default class ChartImp implements Chart { let shouldMeasureHeight = false if (pane !== null) { let shouldAdjust = forceShouldAdjust - if (options.id !== PaneIdConstants.CANDLE && options.height !== undefined && options.height > 0) { + if (options.id !== PaneIdConstants.CANDLE && isNumber(options.height) && options.height > 0) { const minHeight = Math.max(options.minHeight ?? pane.getOptions().minHeight, 0) const height = Math.max(minHeight, options.height) pane.setBounding({ height }) @@ -485,7 +486,7 @@ export default class ChartImp implements Chart { }) indicatorData[id] = paneIndicatorData }) - if (crosshair.paneId !== undefined) { + if (isString(crosshair.paneId)) { actionStore.execute(ActionType.OnCrosshairChange, { ...crosshair, indicatorData @@ -495,7 +496,7 @@ export default class ChartImp implements Chart { } getDom (paneId?: string, position?: DomPosition): Nullable { - if (isValid(paneId)) { + if (isString(paneId)) { const pane = this.getDrawPaneById(paneId) if (pane !== null) { const pos = position ?? DomPosition.Root @@ -806,7 +807,7 @@ export default class ChartImp implements Chart { removeOverlay (remove?: string | OverlayRemove): void { let overlayRemove - if (remove !== undefined) { + if (isValid(remove)) { if (isString(remove)) { overlayRemove = { id: remove } } else { @@ -837,7 +838,7 @@ export default class ChartImp implements Chart { } scrollByDistance (distance: number, animationDuration?: number): void { - const duration = animationDuration === undefined || animationDuration < 0 ? 0 : animationDuration + const duration = isNumber(animationDuration) && animationDuration > 0 ? animationDuration : 0 const timeScaleStore = this._chartStore.getTimeScaleStore() if (duration > 0) { timeScaleStore.startScroll() @@ -880,7 +881,7 @@ export default class ChartImp implements Chart { } zoomAtCoordinate (scale: number, coordinate?: Coordinate, animationDuration?: number): void { - const duration = animationDuration === undefined || animationDuration < 0 ? 0 : animationDuration + const duration = isNumber(animationDuration) && animationDuration > 0 ? animationDuration : 0 const timeScaleStore = this._chartStore.getTimeScaleStore() if (duration > 0) { const { bar: barSpace } = timeScaleStore.getBarSpace() @@ -926,13 +927,13 @@ export default class ChartImp implements Chart { coordinates = ps.map(point => { const coordinate: Partial = {} let dataIndex = point.dataIndex - if (point.timestamp !== undefined) { + if (isNumber(point.timestamp)) { dataIndex = timeScaleStore.timestampToDataIndex(point.timestamp) } - if (dataIndex !== undefined) { + if (isNumber(dataIndex)) { coordinate.x = xAxis?.convertToPixel(dataIndex) } - if (point.value !== undefined) { + if (isNumber(point.value)) { const y = yAxis?.convertToPixel(point.value) coordinate.y = absolute ? bounding.top + y : y } @@ -956,12 +957,12 @@ export default class ChartImp implements Chart { const yAxis = pane.getAxisComponent() points = cs.map(coordinate => { const point: Partial = {} - if (coordinate.x !== undefined) { + if (isNumber(coordinate.x)) { const dataIndex = xAxis?.convertFromPixel(coordinate.x) ?? -1 point.dataIndex = dataIndex point.timestamp = timeScaleStore.dataIndexToTimestamp(dataIndex) ?? undefined } - if (coordinate.y !== undefined) { + if (isNumber(coordinate.y)) { const y = absolute ? coordinate.y - bounding.top : coordinate.y point.value = yAxis.convertFromPixel(y) } diff --git a/src/common/Action.ts b/src/common/Action.ts index f13580692..246f45e11 100644 --- a/src/common/Action.ts +++ b/src/common/Action.ts @@ -12,6 +12,8 @@ * limitations under the License. */ +import { isFunction } from './utils/typeChecks' + export type ActionCallback = (data?: any) => void export enum ActionType { @@ -35,7 +37,7 @@ export default class Delegate { } unsubscribe (callback?: ActionCallback): void { - if (callback !== undefined) { + if (isFunction(callback)) { const index = this._callbacks.indexOf(callback) ?? -1 if (index > -1) { this._callbacks.splice(index, 1) diff --git a/src/common/Bounding.ts b/src/common/Bounding.ts index 9f6bf83a6..cb9426b5a 100644 --- a/src/common/Bounding.ts +++ b/src/common/Bounding.ts @@ -12,7 +12,7 @@ * limitations under the License. */ -import { merge } from './utils/typeChecks' +import { isValid, merge } from './utils/typeChecks' export default interface Bounding { width: number @@ -32,7 +32,7 @@ export function getDefaultBounding (bounding?: Partial): Bounding { top: 0, bottom: 0 } - if (bounding !== undefined) { + if (isValid(bounding)) { merge(defaultBounding, bounding) } return defaultBounding diff --git a/src/common/Eventful.ts b/src/common/Eventful.ts index 7eb58bfdf..3c68061fc 100644 --- a/src/common/Eventful.ts +++ b/src/common/Eventful.ts @@ -12,6 +12,8 @@ * limitations under the License. */ +import { isValid } from './utils/typeChecks' + import { EventName, MouseTouchEvent, MouseTouchEventCallback } from './SyntheticEvent' export interface EventDispatcher { @@ -30,7 +32,7 @@ export default abstract class Eventful implements EventDispatcher { onEvent (name: EventName, event: MouseTouchEvent, other?: number): boolean { const callback = this._callbacks.get(name) - if (callback !== undefined && this.checkEventOn(event)) { + if (isValid(callback) && this.checkEventOn(event)) { return callback(event, other) } return false diff --git a/src/common/SyntheticEvent.ts b/src/common/SyntheticEvent.ts index 458643ceb..454931caf 100644 --- a/src/common/SyntheticEvent.ts +++ b/src/common/SyntheticEvent.ts @@ -25,6 +25,7 @@ import Coordinate from './Coordinate' import Nullable from './Nullable' import { isFF, isIOS } from './utils/platform' +import { isValid } from './utils/typeChecks' export type MouseTouchEventCallback = (event: MouseTouchEvent, other?: number) => boolean @@ -286,7 +287,7 @@ export default class SyntheticEvent { private _mouseWheelHandler (wheelEvent: WheelEvent): void { if (Math.abs(wheelEvent.deltaX) > Math.abs(wheelEvent.deltaY)) { - if (this._handler.mouseWheelHortEvent === undefined) { + if (!isValid(this._handler.mouseWheelHortEvent)) { return } this._preventDefault(wheelEvent) @@ -295,7 +296,7 @@ export default class SyntheticEvent { } this._handler.mouseWheelHortEvent(this._makeCompatEvent(wheelEvent), -wheelEvent.deltaX) } else { - if (this._handler.mouseWheelVertEvent === undefined) { + if (!isValid(this._handler.mouseWheelVertEvent)) { return } let deltaY = -(wheelEvent.deltaY / 100) @@ -493,7 +494,7 @@ export default class SyntheticEvent { // do not fire mouse events if tap handler was executed // prevent click event on new dom element (who appeared after tap) - if (this._handler.tapEvent !== undefined) { + if (isValid(this._handler.tapEvent)) { this._preventDefault(touchEndEvent) } } @@ -733,9 +734,9 @@ export default class SyntheticEvent { } private _initPinch (): void { - if (this._handler.pinchStartEvent === undefined && - this._handler.pinchEvent === undefined && - this._handler.pinchEndEvent === undefined + if (!isValid(this._handler.pinchStartEvent) && + !isValid(this._handler.pinchEvent) && + !isValid(this._handler.pinchEndEvent) ) { return } @@ -752,7 +753,7 @@ export default class SyntheticEvent { if (event.touches.length !== 2 || this._startPinchMiddleCoordinate === null) { return } - if (this._handler.pinchEvent !== undefined) { + if (isValid(this._handler.pinchEvent)) { const currentDistance = this._getTouchDistance(event.touches[0], event.touches[1]) const scale = currentDistance / this._startPinchDistance this._handler.pinchEvent({ ...this._startPinchMiddleCoordinate, pageX: 0, pageY: 0 }, scale) @@ -788,7 +789,7 @@ export default class SyntheticEvent { this._startPinchDistance = this._getTouchDistance(touches[0], touches[1]) - if (this._handler.pinchStartEvent !== undefined) { + if (isValid(this._handler.pinchStartEvent)) { this._handler.pinchStartEvent({ x: 0, y: 0, pageX: 0, pageY: 0 }) } @@ -802,7 +803,7 @@ export default class SyntheticEvent { this._startPinchMiddleCoordinate = null - if (this._handler.pinchEndEvent !== undefined) { + if (isValid(this._handler.pinchEndEvent)) { this._handler.pinchEndEvent({ x: 0, y: 0, pageX: 0, pageY: 0 }) } } @@ -843,7 +844,7 @@ export default class SyntheticEvent { private _firesTouchEvents (e: MouseEvent): boolean { // @ts-expect-error - if (e.sourceCapabilities?.firesTouchEvents !== undefined) { + if (isValid(e.sourceCapabilities?.firesTouchEvents)) { // @ts-expect-error return e.sourceCapabilities.firesTouchEvents } diff --git a/src/common/utils/canvas.ts b/src/common/utils/canvas.ts index 3fc2d0dd3..6eec8a6e3 100644 --- a/src/common/utils/canvas.ts +++ b/src/common/utils/canvas.ts @@ -12,6 +12,8 @@ * limitations under the License. */ +import { isValid } from './typeChecks' + let measureCtx: CanvasRenderingContext2D /** @@ -33,7 +35,7 @@ export function createFont (size?: number, weight?: string | number, family?: st * @returns {number} */ export function calcTextWidth (text: string, size?: number, weight?: string | number, family?: string): number { - if (measureCtx === undefined) { + if (!isValid(measureCtx)) { const canvas = document.createElement('canvas') const pixelRatio = getPixelRatio(canvas) measureCtx = canvas.getContext('2d') as CanvasRenderingContext2D diff --git a/src/component/Overlay.ts b/src/component/Overlay.ts index 2be81c81d..fde9f82ec 100644 --- a/src/component/Overlay.ts +++ b/src/component/Overlay.ts @@ -22,8 +22,7 @@ import BarSpace from '../common/BarSpace' import Precision from '../common/Precision' import { OverlayStyle } from '../common/Styles' import { MouseTouchEvent } from '../common/SyntheticEvent' - -import { clone, isNumber, merge } from '../common/utils/typeChecks' +import { clone, isNumber, isValid, merge } from '../common/utils/typeChecks' import TimeScaleStore from '../store/TimeScaleStore' @@ -376,7 +375,7 @@ export default abstract class OverlayImp implements Overlay { } setId (id: string): boolean { - if (this.id === undefined) { + if (!isValid(this.id)) { this.id = id return true } @@ -384,7 +383,7 @@ export default abstract class OverlayImp implements Overlay { } setGroupId (groupId: string): boolean { - if (this.groupId === undefined) { + if (!isValid(this.groupId)) { this.groupId = groupId return true } @@ -623,13 +622,13 @@ export default abstract class OverlayImp implements Overlay { eventMoveForDrawing (point: Partial): void { const pointIndex = this.currentStep - 1 const newPoint: Partial = {} - if (point.timestamp !== undefined) { + if (isNumber(point.timestamp)) { newPoint.timestamp = point.timestamp } - if (point.dataIndex !== undefined) { + if (isNumber(point.dataIndex)) { newPoint.dataIndex = point.dataIndex } - if (point.value !== undefined) { + if (isNumber(point.value)) { newPoint.value = point.value } this.points[pointIndex] = newPoint @@ -643,11 +642,11 @@ export default abstract class OverlayImp implements Overlay { } eventPressedPointMove (point: Partial, pointIndex: number): void { - if (point.dataIndex !== undefined) { + if (isNumber(point.dataIndex)) { this.points[pointIndex].dataIndex = point.dataIndex this.points[pointIndex].timestamp = point.timestamp } - if (point.value !== undefined) { + if (isNumber(point.value)) { this.points[pointIndex].value = point.value } this.performEventPressedMove?.({ @@ -667,23 +666,23 @@ export default abstract class OverlayImp implements Overlay { eventPressedOtherMove (point: Partial, timeScaleStore: TimeScaleStore): void { if (this._prevPressedPoint !== null) { let difDataIndex: number - if (point.dataIndex !== undefined && this._prevPressedPoint.dataIndex !== undefined) { + if (isNumber(point.dataIndex) && isNumber(this._prevPressedPoint.dataIndex)) { difDataIndex = point.dataIndex - this._prevPressedPoint.dataIndex } let difValue: number - if (point.value !== undefined && this._prevPressedPoint.value !== undefined) { + if (isNumber(point.value) && isNumber(this._prevPressedPoint.value)) { difValue = point.value - this._prevPressedPoint.value } this.points = this._prevPressedPoints.map(p => { - if (p.dataIndex === undefined && p.timestamp !== undefined) { + if (isNumber(p.dataIndex) && isNumber(p.timestamp)) { p.dataIndex = timeScaleStore.timestampToDataIndex(p.timestamp) } const newPoint = { ...p } - if (difDataIndex !== undefined && p.dataIndex !== undefined) { + if (isNumber(difDataIndex) && isNumber(p.dataIndex)) { newPoint.dataIndex = p.dataIndex + difDataIndex newPoint.timestamp = timeScaleStore.dataIndexToTimestamp(newPoint.dataIndex) ?? undefined } - if (difValue !== undefined && p.value !== undefined) { + if (isNumber(difValue) && isNumber(p.value)) { newPoint.value = p.value + difValue } return newPoint diff --git a/src/component/XAxis.ts b/src/component/XAxis.ts index 4bd3f2495..ea3c1ea67 100644 --- a/src/component/XAxis.ts +++ b/src/component/XAxis.ts @@ -13,12 +13,13 @@ */ import Nullable from '../common/Nullable' +import { calcTextWidth } from '../common/utils/canvas' +import { isValid } from '../common/utils/typeChecks' + import { FormatDate, FormatDateType } from '../Options' import AxisImp, { Axis, AxisExtremum, AxisTick } from './Axis' -import { calcTextWidth } from '../common/utils/canvas' - export type XAxis = Axis export default class XAxisImp extends AxisImp { @@ -75,7 +76,7 @@ export default class XAxisImp extends AxisImp { } else { const firstTimestamp = optimalTicks[0].value as number const secondTimestamp = optimalTicks[1].value as number - if (optimalTicks[2] !== undefined) { + if (isValid(optimalTicks[2])) { const thirdText = optimalTicks[2].text if (/^[0-9]{2}-[0-9]{2}$/.test(thirdText)) { optimalTicks[0].text = formatDate(dateTimeFormat, firstTimestamp, 'MM-DD', FormatDateType.XAxis) diff --git a/src/component/YAxis.ts b/src/component/YAxis.ts index f23d30ff0..ee689ee1e 100644 --- a/src/component/YAxis.ts +++ b/src/component/YAxis.ts @@ -12,17 +12,16 @@ * limitations under the License. */ -import AxisImp, { Axis, AxisExtremum, AxisTick } from './Axis' - -import { IndicatorFigure } from './Indicator' - import { YAxisType, YAxisPosition, CandleType } from '../common/Styles' - -import { isValid } from '../common/utils/typeChecks' +import { isValid, isNumber } from '../common/utils/typeChecks' import { index10, log10 } from '../common/utils/number' import { calcTextWidth } from '../common/utils/canvas' import { formatPrecision, formatThousands } from '../common/utils/format' +import AxisImp, { Axis, AxisExtremum, AxisTick } from './Axis' + +import { IndicatorFigure } from './Indicator' + import { PaneIdConstants } from '../pane/types' interface FiguresResult { @@ -117,7 +116,7 @@ export default class YAxisImp extends AxisImp implements YAxis { switch (type) { case YAxisType.Percentage: { const fromData = visibleDataList[0]?.data - if (fromData?.close !== undefined) { + if (isNumber(fromData?.close)) { min = (min - fromData.close) / fromData.close * 100 max = (max - fromData.close) / fromData.close * 100 } @@ -281,7 +280,11 @@ export default class YAxisImp extends AxisImp implements YAxis { } } v = formatThousands(v, thousandsSeparator) - if (y > textHeight && y < height - textHeight && ((validY !== undefined && (Math.abs(validY - y) > textHeight * 2)) || validY === undefined)) { + const validYNumber = isNumber(validY) + if ( + y > textHeight && + y < height - textHeight && + ((validYNumber && (Math.abs(validY - y) > textHeight * 2)) || !validYNumber)) { optimalTicks.push({ text: v, coord: y, value }) validY = y } @@ -375,7 +378,7 @@ export default class YAxisImp extends AxisImp implements YAxis { const chartStore = this.getParent().getChart().getChartStore() const visibleDataList = chartStore.getVisibleDataList() const fromData = visibleDataList[0]?.data - if (fromData?.close !== undefined) { + if (isNumber(fromData?.close)) { return fromData.close * value / 100 + fromData.close } return 0 @@ -404,7 +407,7 @@ export default class YAxisImp extends AxisImp implements YAxis { const chartStore = this.getParent().getChart().getChartStore() const visibleDataList = chartStore.getVisibleDataList() const fromData = visibleDataList[0]?.data - if (fromData?.close !== undefined) { + if (isNumber(fromData?.close)) { v = (value - fromData.close) / fromData.close * 100 } break diff --git a/src/extension/overlay/fibonacciLine.ts b/src/extension/overlay/fibonacciLine.ts index 612998ec3..94feeec77 100644 --- a/src/extension/overlay/fibonacciLine.ts +++ b/src/extension/overlay/fibonacciLine.ts @@ -12,13 +12,14 @@ * limitations under the License. */ +import { formatThousands } from '../../common/utils/format' +import { isNumber } from '../../common/utils/typeChecks' + import { OverlayTemplate } from '../../component/Overlay' import { LineAttrs } from '../figure/line' import { TextAttrs } from '../figure/text' -import { formatThousands } from '../../common/utils/format' - const fibonacciLine: OverlayTemplate = { name: 'fibonacciLine', totalStep: 3, @@ -32,7 +33,7 @@ const fibonacciLine: OverlayTemplate = { const texts: TextAttrs[] = [] const startX = 0 const endX = bounding.width - if (coordinates.length > 1 && points[0].value !== undefined && points[1].value !== undefined) { + if (coordinates.length > 1 && isNumber(points[0].value) && isNumber(points[1].value)) { const percents = [1, 0.786, 0.618, 0.5, 0.382, 0.236, 0] const yDif = coordinates[0].y - coordinates[1].y const valueDif = points[0].value - points[1].value diff --git a/src/extension/overlay/horizontalRayLine.ts b/src/extension/overlay/horizontalRayLine.ts index 5add2a6bd..b2be97f01 100644 --- a/src/extension/overlay/horizontalRayLine.ts +++ b/src/extension/overlay/horizontalRayLine.ts @@ -12,6 +12,7 @@ * limitations under the License. */ +import { isValid } from '../../common/utils/typeChecks' import { OverlayTemplate } from '../../component/Overlay' const horizontalRayLine: OverlayTemplate = { @@ -22,7 +23,7 @@ const horizontalRayLine: OverlayTemplate = { needDefaultYAxisFigure: true, createPointFigures: ({ coordinates, bounding }) => { const coordinate = { x: 0, y: coordinates[0].y } - if (coordinates[1] !== undefined && coordinates[0].x < coordinates[1].x) { + if (isValid(coordinates[1]) && coordinates[0].x < coordinates[1].x) { coordinate.x = bounding.width } return [ diff --git a/src/extension/overlay/simpleTag.ts b/src/extension/overlay/simpleTag.ts index d6987a86d..26a90c57d 100644 --- a/src/extension/overlay/simpleTag.ts +++ b/src/extension/overlay/simpleTag.ts @@ -16,7 +16,7 @@ import { formatPrecision } from '../../common/utils/format' import { OverlayTemplate } from '../../component/Overlay' -import { isFunction, isValid } from '../../common/utils/typeChecks' +import { isFunction, isNumber, isValid } from '../../common/utils/typeChecks' import { LineType } from '../../common/Styles' @@ -57,7 +57,7 @@ const simpleTag: OverlayTemplate = { text = overlay.extendData(overlay) } } - if (!isValid(text) && overlay.points[0].value !== undefined) { + if (!isValid(text) && isNumber(overlay.points[0].value)) { text = formatPrecision(overlay.points[0].value, precision.price) } return { type: 'text', attrs: { x, y: coordinates[0].y, text: text ?? '', align: textAlign, baseline: 'middle' } } diff --git a/src/index.ts b/src/index.ts index 2e1c9c52b..a228e8a79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,14 +28,22 @@ import { LineType, PolygonType, TooltipShowRule, TooltipShowType, TooltipIconPosition, CandleType, YAxisPosition, YAxisType } from './common/Styles' +import Nullable from './common/Nullable' -import { Options, FormatDateType } from './Options' -import ChartImp, { Chart, DomPosition } from './Chart' +import { logError, logTag, logWarn } from './common/utils/logger' +import { + clone, merge, isString, isNumber, isValid, isObject, isArray, isFunction, isBoolean +} from './common/utils/typeChecks' +import { formatValue, formatPrecision, formatBigNumber, formatDate, formatThousands } from './common/utils/format' +import { calcTextWidth } from './common/utils/canvas' import { ActionType } from './common/Action' import { IndicatorSeries } from './component/Indicator' import { OverlayMode } from './component/Overlay' +import { Options, FormatDateType } from './Options' +import ChartImp, { Chart, DomPosition } from './Chart' + import { checkCoordinateOnArc, drawArc } from './extension/figure/arc' import { checkCoordinateOnCircle, drawCircle } from './extension/figure/circle' import { @@ -53,16 +61,6 @@ import { registerLocale, getSupportedLocales } from './extension/i18n/index' import { registerOverlay, getOverlayClass, getSupportedOverlays } from './extension/overlay/index' import { registerStyles } from './extension/styles/index' -import Nullable from './common/Nullable' - -import { logError, logTag, logWarn } from './common/utils/logger' - -import { - clone, merge, isString, isNumber, isValid, isObject, isArray, isFunction, isBoolean -} from './common/utils/typeChecks' -import { formatValue, formatPrecision, formatBigNumber, formatDate, formatThousands } from './common/utils/format' -import { calcTextWidth } from './common/utils/canvas' - const instances = new Map() let chartBaseId = 1 @@ -93,7 +91,7 @@ function init (ds: HTMLElement | string, options?: Options): Nullable { return null } let chart = instances.get(dom.id) - if (chart !== undefined) { + if (isValid(chart)) { logWarn('', '', 'The chart has been initialized on the dom!!!') return chart } diff --git a/src/store/ActionStore.ts b/src/store/ActionStore.ts index 70b057ec6..f754ce090 100644 --- a/src/store/ActionStore.ts +++ b/src/store/ActionStore.ts @@ -13,6 +13,7 @@ */ import Action, { ActionType, ActionCallback } from '../common/Action' +import { isValid } from '../common/utils/typeChecks' export default class ActionStore { /** @@ -39,7 +40,7 @@ export default class ActionStore { */ unsubscribe (type: ActionType, callback?: ActionCallback): void { const action = this._actions.get(type) - if (action !== undefined) { + if (isValid(action)) { action.unsubscribe(callback) if (action.isEmpty()) { this._actions.delete(type) @@ -49,6 +50,6 @@ export default class ActionStore { has (type: ActionType): boolean { const action = this._actions.get(type) - return action !== undefined && !action.isEmpty() + return isValid(action) && !action.isEmpty() } } diff --git a/src/store/ChartStore.ts b/src/store/ChartStore.ts index d3d916be1..1ffe5d9b7 100644 --- a/src/store/ChartStore.ts +++ b/src/store/ChartStore.ts @@ -16,11 +16,10 @@ import KLineData from '../common/KLineData' import Precision from '../common/Precision' import VisibleData from '../common/VisibleData' import { getDefaultStyles, Styles } from '../common/Styles' +import { isArray, isString, isValid, merge } from '../common/utils/typeChecks' import { getDefaultCustomApi, CustomApi, defaultLocale, Options } from '../Options' -import { isArray, isString, merge } from '../common/utils/typeChecks' - import TimeScaleStore from './TimeScaleStore' import IndicatorStore from './IndicatorStore' import TooltipStore from './TooltipStore' @@ -121,25 +120,25 @@ export default class ChartStore { } setOptions (options?: Options): ChartStore { - if (options !== undefined) { + if (isValid(options)) { const { locale, timezone, styles, customApi } = options - if (locale !== undefined) { + if (isString(locale)) { this._locale = locale } - if (timezone !== undefined) { + if (isString(timezone)) { this._timeScaleStore.setTimezone(timezone) } - if (styles !== undefined) { + if (isValid(styles)) { if (isString(styles)) { merge(this._styles, getStyles(styles)) } else { merge(this._styles, styles) } } - if (customApi !== undefined) { + if (isValid(customApi)) { merge(this._customApi, customApi) } - if (options.thousandsSeparator !== undefined) { + if (isString(options.thousandsSeparator)) { this._thousandsSeparator = options.thousandsSeparator } } diff --git a/src/store/IndicatorStore.ts b/src/store/IndicatorStore.ts index 088d79176..0dfcdc5d4 100644 --- a/src/store/IndicatorStore.ts +++ b/src/store/IndicatorStore.ts @@ -14,6 +14,7 @@ import Nullable from '../common/Nullable' import Precision from '../common/Precision' +import { isValid } from '../common/utils/typeChecks' import ChartStore from './ChartStore' @@ -196,7 +197,7 @@ export default class IndicatorStore { let instances: Map> = new Map() if (paneId !== null) { const paneInstances = this._instances.get(paneId) - if (paneInstances !== undefined) { + if (isValid(paneInstances)) { instances.set(paneId, paneInstances) } } else { @@ -206,7 +207,7 @@ export default class IndicatorStore { const tasks: Array> = [] instances.forEach(paneInstances => { const instance = paneInstances.get(name) - if (instance !== undefined) { + if (isValid(instance)) { const overrideResult = this._overrideInstance(instance, indicator) if (overrideResult[1]) { tasks.push(instance.calcIndicator(this._chartStore.getDataList())) diff --git a/src/store/OverlayStore.ts b/src/store/OverlayStore.ts index 1d7164b88..6350da878 100644 --- a/src/store/OverlayStore.ts +++ b/src/store/OverlayStore.ts @@ -15,7 +15,7 @@ import Nullable from '../common/Nullable' import { UpdateLevel } from '../common/Updater' import { MouseTouchEvent } from '../common/SyntheticEvent' -import { isFunction } from '../common/utils/typeChecks' +import { isFunction, isValid, isString, isBoolean } from '../common/utils/typeChecks' import { createId } from '../common/utils/id' import OverlayImp, { OVERLAY_ID_PREFIX, OVERLAY_ACTIVE_Z_LEVEL, OverlayCreate, OverlayRemove } from '../component/Overlay' @@ -186,9 +186,9 @@ export default class OverlayStore { getInstanceById (id: string): Nullable { for (const entry of this._instances) { const paneShapes = entry[1] - const shape = paneShapes.find(s => s.id === id) - if (shape !== undefined) { - return shape + const overlay = paneShapes.find(s => s.id === id) + if (isValid(overlay)) { + return overlay } } if (this._progressInstanceInfo !== null) { @@ -200,7 +200,7 @@ export default class OverlayStore { } private _sort (paneId?: string): void { - if (paneId !== undefined) { + if (isString(paneId)) { this._instances.get(paneId)?.sort((o1, o2) => o1.zLevel - o2.zLevel).sort((o1, o2) => o1.zLevel - o2.zLevel) } else { this._instances.forEach(paneInstances => { @@ -264,7 +264,7 @@ export default class OverlayStore { updateProgressInstanceInfo (paneId: string, appointPaneFlag?: boolean): void { if (this._progressInstanceInfo !== null) { - if (appointPaneFlag !== undefined && appointPaneFlag) { + if (isBoolean(appointPaneFlag) && appointPaneFlag) { this._progressInstanceInfo.appointPaneFlag = appointPaneFlag } this._progressInstanceInfo.paneId = paneId @@ -273,7 +273,7 @@ export default class OverlayStore { } getInstances (paneId?: string): OverlayImp[] { - if (paneId === undefined) { + if (!isString(paneId)) { let instances: OverlayImp[] = [] this._instances.forEach(paneInstances => { instances = instances.concat(paneInstances) @@ -298,18 +298,20 @@ export default class OverlayStore { } } - if (id !== undefined) { + if (isString(id)) { const instance = this.getInstanceById(id) if (instance !== null) { setFlag(instance) } } else { + const nameValid = isString(name) + const groupIdValid = isString(groupId) this._instances.forEach(paneInstances => { paneInstances.forEach(instance => { if ( - (name !== undefined && instance.name === name) || - (groupId !== undefined && instance.groupId === groupId) || - (name === undefined && groupId === undefined) + (nameValid && instance.name === name) || + (groupIdValid && instance.groupId === groupId) || + (!nameValid && !groupIdValid) ) { setFlag(instance) } @@ -318,9 +320,9 @@ export default class OverlayStore { if (this._progressInstanceInfo !== null) { const progressInstance = this._progressInstanceInfo.instance if ( - (name !== undefined && progressInstance.name === name) || - (groupId !== undefined && progressInstance.groupId === groupId) || - (name === undefined && groupId === undefined) + (nameValid && progressInstance.name === name) || + (groupIdValid && progressInstance.groupId === groupId) || + (!nameValid && !groupIdValid) ) { setFlag(progressInstance) } @@ -336,17 +338,17 @@ export default class OverlayStore { removeInstance (overlayRemove?: OverlayRemove): void { const match: ((remove: OverlayRemove, overlay: OverlayImp) => boolean) = (remove: OverlayRemove, overlay: OverlayImp) => { - if (remove.id !== undefined) { + if (isString(remove.id)) { if (overlay.id !== remove.id) { return false } } else { - if (remove.groupId !== undefined) { + if (isString(remove.groupId)) { if (overlay.groupId !== remove.groupId) { return false } } else { - if (remove.name !== undefined) { + if (isString(remove.name)) { if (overlay.name !== remove.name) { return false } @@ -357,18 +359,19 @@ export default class OverlayStore { } const updatePaneIds: string[] = [] + const overlayRemoveValid = isValid(overlayRemove) if (this._progressInstanceInfo !== null) { const { instance } = this._progressInstanceInfo if ( - overlayRemove === undefined || - (overlayRemove !== undefined && match(overlayRemove, instance)) + !overlayRemoveValid || + (overlayRemoveValid && match(overlayRemove, instance)) ) { updatePaneIds.push(this._progressInstanceInfo.paneId) instance.onRemoved?.({ overlay: instance }) this._progressInstanceInfo = null } } - if (overlayRemove !== undefined) { + if (overlayRemoveValid) { const instances = new Map() for (const entry of this._instances) { const paneInstances = entry[1] diff --git a/src/store/TimeScaleStore.ts b/src/store/TimeScaleStore.ts index f55b8ac79..5993c5e1a 100644 --- a/src/store/TimeScaleStore.ts +++ b/src/store/TimeScaleStore.ts @@ -22,6 +22,7 @@ import { ActionType } from '../common/Action' import { logWarn } from '../common/utils/logger' import { binarySearchNearest } from '../common/utils/number' +import { isNumber, isString } from '../common/utils/typeChecks' import ChartStore from './ChartStore' @@ -188,7 +189,7 @@ export default class TimeScaleStore { minute: '2-digit', second: '2-digit' } - if (timezone !== undefined) { + if (isString(timezone)) { options.timeZone = timezone } let dateTimeFormat: Nullable = null @@ -341,12 +342,12 @@ export default class TimeScaleStore { if (!this._zoomEnabled) { return } - if (coordinate?.x === undefined) { + if (!isNumber(coordinate?.x)) { const crosshair = this._chartStore.getTooltipStore().getCrosshair() coordinate = { x: crosshair?.x ?? this._totalBarSpace / 2 } } this._chartStore.getActionStore().execute(ActionType.OnZoom) - const floatIndex = this.coordinateToFloatIndex(coordinate.x as number) + const floatIndex = this.coordinateToFloatIndex(coordinate?.x as number) const barSpace = this._barSpace + scale * (this._barSpace / 10) this.setBarSpace(barSpace, () => { this._offsetRightBarCount += (floatIndex - this.coordinateToFloatIndex(coordinate?.x as number)) diff --git a/src/store/TooltipStore.ts b/src/store/TooltipStore.ts index 81f1649c8..868e892f4 100644 --- a/src/store/TooltipStore.ts +++ b/src/store/TooltipStore.ts @@ -16,6 +16,7 @@ import Nullable from '../common/Nullable' import KLineData from '../common/KLineData' import Crosshair from '../common/Crosshair' import { UpdateLevel } from '../common/Updater' +import { isNumber } from '../common/utils/typeChecks' import ChartStore from './ChartStore' @@ -44,7 +45,7 @@ export default class TooltipStore { const cr = crosshair ?? {} let realDataIndex: number let dataIndex: number - if (cr.x !== undefined) { + if (isNumber(cr.x)) { realDataIndex = this._chartStore.getTimeScaleStore().coordinateToDataIndex(cr.x) if (realDataIndex < 0) { dataIndex = 0 diff --git a/src/view/CandleLastPriceLabelView.ts b/src/view/CandleLastPriceLabelView.ts index b8e1e1f70..bb95d0bc5 100644 --- a/src/view/CandleLastPriceLabelView.ts +++ b/src/view/CandleLastPriceLabelView.ts @@ -13,13 +13,13 @@ */ import { YAxisType } from '../common/Styles' +import { formatPrecision, formatThousands } from '../common/utils/format' +import { isValid } from '../common/utils/typeChecks' import View from './View' import YAxis from '../component/YAxis' -import { formatPrecision, formatThousands } from '../common/utils/format' - export default class CandleLastPriceLabelView extends View { override drawImp (ctx: CanvasRenderingContext2D): void { const widget = this.getWidget() @@ -35,7 +35,7 @@ export default class CandleLastPriceLabelView extends View { const dataList = chartStore.getDataList() const visibleDataList = chartStore.getVisibleDataList() const data = dataList[dataList.length - 1] - if (data !== undefined) { + if (isValid(data)) { const { close, open } = data const priceY = yAxis.convertToNicePixel(close) let backgroundColor: string diff --git a/src/view/CandleTooltipView.ts b/src/view/CandleTooltipView.ts index 401c5fa35..eb736bae1 100644 --- a/src/view/CandleTooltipView.ts +++ b/src/view/CandleTooltipView.ts @@ -21,6 +21,9 @@ import { Styles, CandleStyle, TooltipData, TooltipDataChild, TooltipShowType, CandleTooltipRectPosition, CandleTooltipCustomCallbackData, YAxisPosition, PolygonType } from '../common/Styles' +import { formatPrecision, formatThousands } from '../common/utils/format' +import { createFont } from '../common/utils/canvas' +import { isFunction, isObject, isValid } from '../common/utils/typeChecks' import { CustomApi, FormatDateType } from '../Options' @@ -34,10 +37,6 @@ import { TooltipIcon } from '../store/TooltipStore' import { i18n } from '../extension/i18n/index' -import { formatPrecision, formatThousands } from '../common/utils/format' -import { createFont } from '../common/utils/canvas' -import { isFunction, isObject } from '../common/utils/typeChecks' - export default class CandleTooltipView extends IndicatorTooltipView { override drawImp (ctx: CanvasRenderingContext2D): void { const widget = this.getWidget() @@ -45,7 +44,7 @@ export default class CandleTooltipView extends IndicatorTooltipView { const paneId = pane.getId() const chartStore = pane.getChart().getChartStore() const crosshair = chartStore.getTooltipStore().getCrosshair() - if (crosshair.kLineData !== undefined) { + if (isValid(crosshair.kLineData)) { const bounding = widget.getBounding() const yAxisBounding = pane.getYAxisWidget()?.getBounding() as Bounding const dataList = chartStore.getDataList() diff --git a/src/view/CrosshairHorizontalLabelView.ts b/src/view/CrosshairHorizontalLabelView.ts index 5c0e642d0..7e42044fd 100644 --- a/src/view/CrosshairHorizontalLabelView.ts +++ b/src/view/CrosshairHorizontalLabelView.ts @@ -15,6 +15,9 @@ import Bounding from '../common/Bounding' import Crosshair from '../common/Crosshair' import { CrosshairStyle, CrosshairDirectionStyle, YAxisType, StateTextStyle } from '../common/Styles' +import { isString } from '../common/utils/typeChecks' +import { formatPrecision, formatThousands } from '../common/utils/format' +import { createFont } from '../common/utils/canvas' import Axis from '../component/Axis' import YAxis from '../component/YAxis' @@ -23,9 +26,6 @@ import { TextAttrs } from '../extension/figure/text' import ChartStore from '../store/ChartStore' -import { formatPrecision, formatThousands } from '../common/utils/format' -import { createFont } from '../common/utils/canvas' - import View from './View' export default class CrosshairHorizontalLabelView extends View { @@ -36,7 +36,7 @@ export default class CrosshairHorizontalLabelView extend const chartStore = widget.getPane().getChart().getChartStore() const crosshair = chartStore.getTooltipStore().getCrosshair() const styles = chartStore.getStyles().crosshair - if (crosshair.paneId !== undefined && this.compare(crosshair, pane.getId())) { + if (isString(crosshair.paneId) && this.compare(crosshair, pane.getId())) { if (styles.show) { const directionStyles = this.getDirectionStyles(styles) const textStyles = directionStyles.text diff --git a/src/view/CrosshairLineView.ts b/src/view/CrosshairLineView.ts index 88e4a2ff5..a6fd2a06b 100644 --- a/src/view/CrosshairLineView.ts +++ b/src/view/CrosshairLineView.ts @@ -14,6 +14,7 @@ import Coordinate from '../common/Coordinate' import { CrosshairDirectionStyle } from '../common/Styles' +import { isString } from '../common/utils/typeChecks' import View from './View' @@ -25,7 +26,7 @@ export default class CrosshairLineView extends View { const chartStore = widget.getPane().getChart().getChartStore() const crosshair = chartStore.getTooltipStore().getCrosshair() const styles = chartStore.getStyles().crosshair - if (crosshair.paneId !== undefined && styles.show) { + if (isString(crosshair.paneId) && styles.show) { if (crosshair.paneId === pane.getId()) { const y = crosshair.y as number this._drawLine( diff --git a/src/view/CrosshairVerticalLabelView.ts b/src/view/CrosshairVerticalLabelView.ts index 68cbc3a0a..446cd981a 100644 --- a/src/view/CrosshairVerticalLabelView.ts +++ b/src/view/CrosshairVerticalLabelView.ts @@ -15,6 +15,8 @@ import Bounding from '../common/Bounding' import Crosshair from '../common/Crosshair' import { CrosshairStyle, CrosshairDirectionStyle, StateTextStyle } from '../common/Styles' +import { isValid } from '../common/utils/typeChecks' + import { FormatDateType } from '../Options' import Axis from '../component/Axis' @@ -27,7 +29,7 @@ import { TextAttrs } from '../extension/figure/text' export default class CrosshairVerticalLabelView extends CrosshairHorizontalLabelView { override compare (crosshair: Crosshair): boolean { - return crosshair.kLineData !== undefined && crosshair.dataIndex === crosshair.realDataIndex + return isValid(crosshair.kLineData) && crosshair.dataIndex === crosshair.realDataIndex } override getDirectionStyles (styles: CrosshairStyle): CrosshairDirectionStyle { diff --git a/src/view/IndicatorLastValueView.ts b/src/view/IndicatorLastValueView.ts index 105668745..334c4fe57 100644 --- a/src/view/IndicatorLastValueView.ts +++ b/src/view/IndicatorLastValueView.ts @@ -12,13 +12,13 @@ * limitations under the License. */ +import { formatPrecision, formatThousands } from '../common/utils/format' +import { isValid } from '../common/utils/typeChecks' + import { eachFigures, IndicatorFigure, IndicatorFigureStyle } from '../component/Indicator' import View from './View' -import { formatPrecision, formatThousands } from '../common/utils/format' -import { isValid } from '../common/utils/typeChecks' - import YAxis from '../component/YAxis' export default class IndicatorLastValueView extends View { @@ -40,7 +40,7 @@ export default class IndicatorLastValueView extends View { indicators.forEach(indicator => { const result = indicator.result const indicatorData = result[dataIndex] - if (indicatorData !== undefined && indicator.visible) { + if (isValid(indicatorData) && indicator.visible) { const precision = indicator.precision eachFigures(dataList, indicator, dataIndex, defaultStyles, (figure: IndicatorFigure, figureStyles: Required) => { const value = indicatorData[figure.key] diff --git a/src/view/IndicatorTooltipView.ts b/src/view/IndicatorTooltipView.ts index 45ecbbf9d..2d14d8832 100644 --- a/src/view/IndicatorTooltipView.ts +++ b/src/view/IndicatorTooltipView.ts @@ -18,6 +18,9 @@ import KLineData from '../common/KLineData' import Crosshair from '../common/Crosshair' import { IndicatorStyle, TooltipStyle, TooltipIconStyle, TooltipTextStyle, TooltipData, TooltipShowRule, TooltipDataChild, TooltipIconPosition } from '../common/Styles' import { ActionType } from '../common/Action' +import { formatPrecision, formatThousands } from '../common/utils/format' +import { isValid, isObject, isString } from '../common/utils/typeChecks' +import { createFont } from '../common/utils/canvas' import { CustomApi } from '../Options' @@ -25,10 +28,6 @@ import YAxis from '../component/YAxis' import IndicatorImp, { eachFigures, Indicator, IndicatorFigure, IndicatorFigureStyle, IndicatorTooltipData } from '../component/Indicator' -import { formatPrecision, formatThousands } from '../common/utils/format' -import { isValid, isObject } from '../common/utils/typeChecks' -import { createFont } from '../common/utils/canvas' - import { TooltipIcon } from '../store/TooltipStore' import View from './View' @@ -52,7 +51,7 @@ export default class IndicatorTooltipView extends View { const pane = widget.getPane() const chartStore = pane.getChart().getChartStore() const crosshair = chartStore.getTooltipStore().getCrosshair() - if (crosshair.kLineData !== undefined) { + if (isValid(crosshair.kLineData)) { const bounding = widget.getBounding() const customApi = chartStore.getCustomApi() const thousandsSeparator = chartStore.getThousandsSeparator() @@ -276,7 +275,7 @@ export default class IndicatorTooltipView extends View { protected isDrawTooltip (crosshair: Crosshair, styles: TooltipStyle): boolean { const showRule = styles.showRule return showRule === TooltipShowRule.Always || - (showRule === TooltipShowRule.FollowCross && (crosshair.paneId !== undefined)) + (showRule === TooltipShowRule.FollowCross && isString(crosshair.paneId)) } protected getIndicatorTooltipData ( @@ -304,7 +303,7 @@ export default class IndicatorTooltipView extends View { if (indicator.visible) { const indicatorData = result[dataIndex] ?? {} eachFigures(dataList, indicator, dataIndex, styles, (figure: IndicatorFigure, figureStyles: Required) => { - if (figure.title !== undefined) { + if (isString(figure.title)) { const color = figureStyles.color let value = indicatorData[figure.key] if (isValid(value)) { @@ -333,16 +332,16 @@ export default class IndicatorTooltipView extends View { xAxis: pane.getChart().getXAxisPane().getAxisComponent(), yAxis: pane.getAxisComponent() }) - if (customName !== undefined && tooltipStyles.showName) { + if (isString(customName) && tooltipStyles.showName) { tooltipData.name = customName } - if (customCalcParamsText !== undefined && tooltipStyles.showParams) { + if (isString(customCalcParamsText) && tooltipStyles.showParams) { tooltipData.calcParamsText = customCalcParamsText } - if (customIcons !== undefined) { + if (isValid(customIcons)) { tooltipData.icons = customIcons } - if (customValues !== undefined && indicator.visible) { + if (isValid(customValues) && indicator.visible) { const optimizedValues: TooltipData[] = [] const color = styles.tooltip.text.color customValues.forEach(data => { diff --git a/src/view/OverlayView.ts b/src/view/OverlayView.ts index e5d871a20..d1bb7de5d 100644 --- a/src/view/OverlayView.ts +++ b/src/view/OverlayView.ts @@ -20,7 +20,7 @@ import BarSpace from '../common/BarSpace' import Precision from '../common/Precision' import { OverlayStyle } from '../common/Styles' import { EventHandler, EventName, MouseTouchEvent, MouseTouchEventCallback } from '../common/SyntheticEvent' -import { isBoolean } from '../common/utils/typeChecks' +import { isBoolean, isNumber, isValid } from '../common/utils/typeChecks' import { CustomApi } from '../Options' @@ -187,7 +187,7 @@ export default class OverlayView extends View { let eventHandler if (!overlay.isDrawing()) { let eventTypes: OverlayFigureIgnoreEventType[] = [] - if (ignoreEvent !== undefined) { + if (isValid(ignoreEvent)) { if (isBoolean(ignoreEvent)) { if (ignoreEvent) { eventTypes = getAllOverlayFigureIgnoreEventTypes() @@ -297,7 +297,7 @@ export default class OverlayView extends View { if (this.coordinateToPointValueFlag()) { const yAxis = pane.getAxisComponent() let value = yAxis.convertFromPixel(coordinate.y) - if (overlay.mode !== OverlayMode.Normal && paneId === PaneIdConstants.CANDLE && point.dataIndex !== undefined) { + if (overlay.mode !== OverlayMode.Normal && paneId === PaneIdConstants.CANDLE && isNumber(point.dataIndex)) { const kLineData = timeScaleStore.getDataByDataIndex(point.dataIndex) if (kLineData !== null) { const modeSensitivity = overlay.modeSensitivity @@ -432,14 +432,14 @@ export default class OverlayView extends View { const { points } = overlay const coordinates = points.map(point => { let dataIndex = point.dataIndex - if (point.timestamp !== undefined) { + if (isNumber(point.timestamp)) { dataIndex = timeScaleStore.timestampToDataIndex(point.timestamp) } const coordinate = { x: 0, y: 0 } - if (dataIndex !== undefined) { + if (isNumber(dataIndex)) { coordinate.x = xAxis?.convertToPixel(dataIndex) ?? 0 } - if (point.value !== undefined) { + if (isNumber(point.value)) { coordinate.y = yAxis?.convertToPixel(point.value) ?? 0 } return coordinate diff --git a/src/view/OverlayXAxisView.ts b/src/view/OverlayXAxisView.ts index 8fb39b952..6150e9fc5 100644 --- a/src/view/OverlayXAxisView.ts +++ b/src/view/OverlayXAxisView.ts @@ -18,6 +18,7 @@ import Bounding from '../common/Bounding' import BarSpace from '../common/BarSpace' import Precision from '../common/Precision' import { OverlayStyle } from '../common/Styles' +import { isNumber } from '../common/utils/typeChecks' import { CustomApi, FormatDateType } from '../Options' @@ -66,7 +67,7 @@ export default class OverlayXAxisView extends OverlayYAxisView { leftX = Math.min(leftX, coordinate.x) rightX = Math.max(rightX, coordinate.x) const point = overlay.points[index] - if (point.timestamp !== undefined) { + if (isNumber(point.timestamp)) { const text = customApi.formatDate(dateTimeFormat, point.timestamp, 'YYYY-MM-DD HH:mm', FormatDateType.Crosshair) figures.push({ type: 'text', attrs: { x: coordinate.x, y: 0, text, align: 'center' }, ignoreEvent: true }) } diff --git a/src/view/OverlayYAxisView.ts b/src/view/OverlayYAxisView.ts index 16f5dd56e..a5df2ada7 100644 --- a/src/view/OverlayYAxisView.ts +++ b/src/view/OverlayYAxisView.ts @@ -19,8 +19,8 @@ import BarSpace from '../common/BarSpace' import Precision from '../common/Precision' import { OverlayStyle } from '../common/Styles' import { CustomApi } from '../Options' - import { formatPrecision, formatThousands } from '../common/utils/format' +import { isNumber } from '../common/utils/typeChecks' import Axis from '../component/Axis' import XAxis from '../component/XAxis' @@ -91,7 +91,7 @@ export default class OverlayYAxisView extends OverlayVie } coordinates.forEach((coordinate, index) => { const point = overlay.points[index] - if (point.value !== undefined) { + if (isNumber(point.value)) { topY = Math.min(topY, coordinate.y) bottomY = Math.max(bottomY, coordinate.y) const text = formatThousands(formatPrecision(point.value, precision.price), thousandsSeparator) diff --git a/src/view/View.ts b/src/view/View.ts index 1e0854c7d..e4ed2c5fe 100644 --- a/src/view/View.ts +++ b/src/view/View.ts @@ -15,15 +15,16 @@ import Nullable from '../common/Nullable' import { EventHandler, EventName } from '../common/SyntheticEvent' import Eventful from '../common/Eventful' +import { isValid } from '../common/utils/typeChecks' import Figure from '../component/Figure' +import Axis from '../component/Axis' + import { getInnerFigureClass } from '../extension/figure/index' import DrawWidget from '../widget/DrawWidget' import DrawPane from '../pane/DrawPane' -import Axis from '../component/Axis' - export default abstract class View extends Eventful { /** * Parent widget @@ -41,7 +42,7 @@ export default abstract class View extends Eventful { const FigureClazz = getInnerFigureClass(name) if (FigureClazz !== null) { const figure = new FigureClazz({ name, attrs, styles }) - if (eventHandler !== undefined) { + if (isValid(eventHandler)) { for (const key in eventHandler) { // eslint-disable-next-line no-prototype-builtins if (eventHandler.hasOwnProperty(key)) {