Skip to content

Commit

Permalink
opt: merge tooltip store and crosshair store
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Aug 20, 2023
1 parent 8a8e5cf commit 9813e53
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 156 deletions.
6 changes: 3 additions & 3 deletions src/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,15 +472,15 @@ export default class ChartImp implements Chart {
}

clearData (): void {
this._chartStore.clearDataList()
this._chartStore.clear()
}

getDataList (): KLineData[] {
return this._chartStore.getDataList()
}

applyNewData (dataList: KLineData[], more?: boolean, callback?: () => void): void {
this._chartStore.clearDataList()
this._chartStore.clear()
if (dataList.length === 0) {
this.adjustPaneViewport(false, true, true, true)
} else {
Expand Down Expand Up @@ -803,7 +803,7 @@ export default class ChartImp implements Chart {
case ActionType.OnCrosshairChange: {
const crosshair = { ...data }
crosshair.paneId = crosshair.paneId ?? PaneIdConstants.CANDLE
this._chartStore.getCrosshairStore().set(crosshair)
this._chartStore.getTooltipStore().setCrosshair(crosshair)
break
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/ChartEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,20 @@ export default class ChartEvent implements EventHandler {
const consumed = widget.dispatchEvent('mouseMoveEvent', event)
const chartStore = this._chart.getChartStore()
let crosshair: Crosshair | undefined = { x: event.x, y: event.y, paneId: pane?.getId() }
if (consumed && chartStore.getTooltipStore().getActiveIconInfo() !== null) {
if (consumed && chartStore.getTooltipStore().getActiveIcon() !== null) {
crosshair = undefined
if (widget !== null) {
widget.getContainer().style.cursor = 'pointer'
}
}
this._chart.getChartStore().getCrosshairStore().set(crosshair)
this._chart.getChartStore().getTooltipStore().setCrosshair(crosshair)
return consumed
}
case WidgetNameConstants.SEPARATOR:
case WidgetNameConstants.XAXIS:
case WidgetNameConstants.YAXIS: {
const consumed = widget.dispatchEvent('mouseMoveEvent', event)
this._chart.getChartStore().getCrosshairStore().set()
this._chart.getChartStore().getTooltipStore().setCrosshair()
return consumed
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ export default class ChartEvent implements EventHandler {
const distance = event.x - this._startScrollCoordinate.x
this._chart.getChartStore().getTimeScaleStore().scroll(distance)
}
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
this._chart.getChartStore().getTooltipStore().setCrosshair({ x: event.x, y: event.y, paneId: pane?.getId() })
return consumed
}
case WidgetNameConstants.XAXIS: {
Expand Down Expand Up @@ -414,7 +414,7 @@ export default class ChartEvent implements EventHandler {
}

mouseLeaveEvent (): boolean {
this._chart.getChartStore().getCrosshairStore().set()
this._chart.getChartStore().getTooltipStore().setCrosshair()
return true
}

Expand All @@ -426,11 +426,11 @@ export default class ChartEvent implements EventHandler {
switch (name) {
case WidgetNameConstants.MAIN: {
const chartStore = this._chart.getChartStore()
const crosshairStore = chartStore.getCrosshairStore()
const tooltipStore = chartStore.getTooltipStore()
if (widget.dispatchEvent('mouseDownEvent', event)) {
this._touchCancelCrosshair = true
this._touchCoordinate = null
crosshairStore.set(undefined, true)
tooltipStore.setCrosshair(undefined, true)
this._chart.updatePane(UpdateLevel.Overlay)
return true
}
Expand All @@ -448,11 +448,11 @@ export default class ChartEvent implements EventHandler {
const radius = Math.sqrt(xDif * xDif + yDif * yDif)
if (radius < TOUCH_MIN_RADIUS) {
this._touchCoordinate = { x: event.x, y: event.y }
crosshairStore.set({ x: event.x, y: event.y, paneId: pane?.getId() })
tooltipStore.setCrosshair({ x: event.x, y: event.y, paneId: pane?.getId() })
} else {
this._touchCoordinate = null
this._touchCancelCrosshair = true
crosshairStore.set()
tooltipStore.setCrosshair()
}
}
return true
Expand All @@ -476,18 +476,18 @@ export default class ChartEvent implements EventHandler {
const event = this._makeWidgetEvent(e, widget)
const name = widget.getName()
const chartStore = this._chart.getChartStore()
const crosshairStore = chartStore.getCrosshairStore()
const tooltipStore = chartStore.getTooltipStore()
switch (name) {
case WidgetNameConstants.MAIN: {
if (widget.dispatchEvent('pressedMouseMoveEvent', event)) {
event.preventDefault?.()
crosshairStore.set(undefined, true)
tooltipStore.setCrosshair(undefined, true)
this._chart.updatePane(UpdateLevel.Overlay)
return true
}
if (this._touchCoordinate !== null) {
event.preventDefault?.()
crosshairStore.set({ x: event.x, y: event.y, paneId: pane?.getId() })
tooltipStore.setCrosshair({ x: event.x, y: event.y, paneId: pane?.getId() })
} else {
if (
this._startScrollCoordinate !== null &&
Expand Down Expand Up @@ -568,16 +568,16 @@ export default class ChartEvent implements EventHandler {
if (widget.getName() === WidgetNameConstants.MAIN) {
const event = this._makeWidgetEvent(e, widget)
const chartStore = this._chart.getChartStore()
const crosshairStore = chartStore.getCrosshairStore()
const tooltipStore = chartStore.getTooltipStore()
if (result) {
this._touchCancelCrosshair = true
this._touchCoordinate = null
crosshairStore.set(undefined, true)
tooltipStore.setCrosshair(undefined, true)
consumed = true
} else {
if (!this._touchCancelCrosshair && !this._touchZoomed) {
this._touchCoordinate = { x: event.x, y: event.y }
crosshairStore.set({ x: event.x, y: event.y, paneId: pane?.getId() }, true)
tooltipStore.setCrosshair({ x: event.x, y: event.y, paneId: pane?.getId() }, true)
consumed = true
}
this._touchCancelCrosshair = false
Expand All @@ -599,7 +599,7 @@ export default class ChartEvent implements EventHandler {
if (widget !== null && widget.getName() === WidgetNameConstants.MAIN) {
const event = this._makeWidgetEvent(e, widget)
this._touchCoordinate = { x: event.x, y: event.y }
this._chart.getChartStore().getCrosshairStore().set({ x: event.x, y: event.y, paneId: pane?.getId() })
this._chart.getChartStore().getTooltipStore().setCrosshair({ x: event.x, y: event.y, paneId: pane?.getId() })
return true
}
return false
Expand Down
17 changes: 4 additions & 13 deletions src/store/ChartStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { isArray, isString, merge } from '../common/utils/typeChecks'

import TimeScaleStore from './TimeScaleStore'
import IndicatorStore from './IndicatorStore'
import CrosshairStore from './CrosshairStore'
import TooltipStore from './TooltipStore'
import OverlayStore from './OverlayStore'
import ActionStore from './ActionStore'
Expand Down Expand Up @@ -81,15 +80,10 @@ export default class ChartStore {
*/
private readonly _overlayStore = new OverlayStore(this)

/**
* Crosshair store
*/
private readonly _crosshairStore = new CrosshairStore(this)

/**
* Tooltip store
*/
private readonly _tooltipStore = new TooltipStore()
private readonly _tooltipStore = new TooltipStore(this)

/**
* Chart action store
Expand Down Expand Up @@ -208,13 +202,14 @@ export default class ChartStore {
this.adjustVisibleDataList()
}
}
this._crosshairStore.recalculate(true)
this._tooltipStore.recalculateCrosshair(true)
}

clearDataList (): void {
clear (): void {
this._dataList = []
this._visibleDataList = []
this._timeScaleStore.clear()
this._tooltipStore.clear()
}

getTimeScaleStore (): TimeScaleStore {
Expand All @@ -229,10 +224,6 @@ export default class ChartStore {
return this._overlayStore
}

getCrosshairStore (): CrosshairStore {
return this._crosshairStore
}

getTooltipStore (): TooltipStore {
return this._tooltipStore
}
Expand Down
85 changes: 0 additions & 85 deletions src/store/CrosshairStore.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/store/TimeScaleStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ export default class TimeScaleStore {
this._gapBarSpace = this._calcGapBarSpace()
adjustBeforeFunc?.()
this.adjustVisibleRange()
this._chartStore.getCrosshairStore().recalculate(true)
this._chartStore.getTooltipStore().recalculateCrosshair(true)
this._chartStore.getChart().adjustPaneViewport(false, true, true, true)
}

setTotalBarSpace (totalSpace: number): TimeScaleStore {
if (this._totalBarSpace !== totalSpace) {
this._totalBarSpace = totalSpace
this.adjustVisibleRange()
this._chartStore.getCrosshairStore().recalculate(true)
this._chartStore.getTooltipStore().recalculateCrosshair(true)
}
return this
}
Expand All @@ -246,7 +246,7 @@ export default class TimeScaleStore {
this._offsetRightBarCount = distance / this._barSpace
if (isUpdate ?? false) {
this.adjustVisibleRange()
this._chartStore.getCrosshairStore().recalculate(true)
this._chartStore.getTooltipStore().recalculateCrosshair(true)
this._chartStore.getChart().adjustPaneViewport(false, true, true, true)
}
return this
Expand Down Expand Up @@ -299,7 +299,7 @@ export default class TimeScaleStore {
this._chartStore.getActionStore().execute(ActionType.OnScroll)
this._offsetRightBarCount = this._startScrollOffsetRightBarCount - distanceBarCount
this.adjustVisibleRange()
this._chartStore.getCrosshairStore().recalculate(true)
this._chartStore.getTooltipStore().recalculateCrosshair(true)
this._chartStore.getChart().adjustPaneViewport(false, true, true, true)
}

Expand Down Expand Up @@ -342,7 +342,7 @@ export default class TimeScaleStore {
return
}
if (coordinate?.x === undefined) {
const crosshair = this._chartStore.getCrosshairStore().get()
const crosshair = this._chartStore.getTooltipStore().getCrosshair()
coordinate = { x: crosshair?.x ?? this._totalBarSpace / 2 }
}
this._chartStore.getActionStore().execute(ActionType.OnZoom)
Expand Down
Loading

0 comments on commit 9813e53

Please sign in to comment.