Skip to content

Commit

Permalink
opt: opt overlay callback params precision
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed May 13, 2024
1 parent dc982f2 commit 109b0c9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/component/Overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@ export interface OverlayFigure {
ignoreEvent?: boolean | OverlayFigureIgnoreEventType[]
}

export interface OverlayPrecision extends Precision {
max: number
min: number
excludePriceVolumeMax: number
excludePriceVolumeMin: number
[key: string]: number
}

export interface OverlayCreateFiguresCallbackParams {
overlay: Overlay
coordinates: Coordinate[]
bounding: Bounding
barSpace: BarSpace
precision: Precision
precision: OverlayPrecision
thousandsSeparator: string
decimalFoldThreshold: number
dateTimeFormat: Intl.DateTimeFormat
Expand Down
1 change: 1 addition & 0 deletions src/component/YAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface FiguresResult {

export interface YAxis extends Axis {
isFromZero: () => boolean
isInCandle: () => boolean
}

export type YAxisConstructor = new (parent: DrawPane<AxisImp>) => YAxisImp
Expand Down
5 changes: 3 additions & 2 deletions src/extension/overlay/fibonacciLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const fibonacciLine: OverlayTemplate = {
needDefaultPointFigure: true,
needDefaultXAxisFigure: true,
needDefaultYAxisFigure: true,
createPointFigures: ({ coordinates, bounding, overlay, precision, thousandsSeparator, decimalFoldThreshold }) => {
createPointFigures: ({ coordinates, bounding, overlay, precision, thousandsSeparator, decimalFoldThreshold, yAxis }) => {
const points = overlay.points
if (coordinates.length > 0) {
const currentPrecision = (yAxis?.isInCandle() ?? true) ? precision.price : precision.excludePriceVolumeMax
const lines: LineAttrs[] = []
const texts: TextAttrs[] = []
const startX = 0
Expand All @@ -39,7 +40,7 @@ const fibonacciLine: OverlayTemplate = {
const valueDif = points[0].value - points[1].value
percents.forEach(percent => {
const y = coordinates[1].y + yDif * percent
const value = formatFoldDecimal(formatThousands(((points[1].value ?? 0) + valueDif * percent).toFixed(precision.price), thousandsSeparator), decimalFoldThreshold)
const value = formatFoldDecimal(formatThousands(((points[1].value ?? 0) + valueDif * percent).toFixed(currentPrecision), thousandsSeparator), decimalFoldThreshold)
lines.push({ coordinates: [{ x: startX, y }, { x: endX, y }] })
texts.push({
x: startX,
Expand Down
5 changes: 3 additions & 2 deletions src/extension/overlay/priceLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const priceLine: OverlayTemplate = {
needDefaultPointFigure: true,
needDefaultXAxisFigure: true,
needDefaultYAxisFigure: true,
createPointFigures: ({ coordinates, bounding, precision, overlay, thousandsSeparator, decimalFoldThreshold }) => {
createPointFigures: ({ coordinates, bounding, precision, overlay, thousandsSeparator, decimalFoldThreshold, yAxis }) => {
const { value = 0 } = (overlay.points)[0]
const currentPrecision = (yAxis?.isInCandle() ?? true) ? precision.price : precision.excludePriceVolumeMax
return [
{
type: 'line',
Expand All @@ -35,7 +36,7 @@ const priceLine: OverlayTemplate = {
attrs: {
x: coordinates[0].x,
y: coordinates[0].y,
text: formatFoldDecimal(formatThousands(value.toFixed(precision.price), thousandsSeparator), decimalFoldThreshold),
text: formatFoldDecimal(formatThousands(value.toFixed(currentPrecision), thousandsSeparator), decimalFoldThreshold),
baseline: 'bottom'
}
}
Expand Down
29 changes: 22 additions & 7 deletions src/view/OverlayView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type Coordinate from '../common/Coordinate'
import type Point from '../common/Point'
import type Bounding from '../common/Bounding'
import type BarSpace from '../common/BarSpace'
import type Precision from '../common/Precision'
import { type OverlayStyle } from '../common/Styles'
import { type EventHandler, type EventName, type MouseTouchEvent, type MouseTouchEventCallback } from '../common/SyntheticEvent'
import { isBoolean, isNumber, isValid } from '../common/utils/typeChecks'
Expand All @@ -27,7 +26,7 @@ import { type CustomApi } from '../Options'
import type Axis from '../component/Axis'
import type XAxis from '../component/XAxis'
import type YAxis from '../component/YAxis'
import { type OverlayFigure, type OverlayFigureIgnoreEventType } from '../component/Overlay'
import { type OverlayPrecision, type OverlayFigure, type OverlayFigureIgnoreEventType } from '../component/Overlay'
import type Overlay from '../component/Overlay'
import { OVERLAY_FIGURE_KEY_PREFIX, OverlayMode, getAllOverlayFigureIgnoreEventTypes } from '../component/Overlay'

Expand Down Expand Up @@ -393,10 +392,26 @@ export default class OverlayView<C extends Axis = YAxis> extends View<C> {
const hoverInstanceInfo = overlayStore.getHoverInstanceInfo()
const clickInstanceInfo = overlayStore.getClickInstanceInfo()
const overlays = this.getCompleteOverlays(overlayStore, paneId)
const paneIndicators = chartStore.getIndicatorStore().getInstances(paneId)
const overlayPrecision = paneIndicators.reduce((prev, indicator) => {
const precision = indicator.precision
prev[indicator.name] = precision
prev.max = Math.max(prev.max, precision)
prev.min = Math.min(prev.min, precision)
prev.excludePriceVolumeMax = Math.max(prev.excludePriceVolumeMax, precision)
prev.excludePriceVolumeMin = Math.min(prev.excludePriceVolumeMin, precision)
return prev
}, {
...precision,
max: Math.max(precision.price, precision.volume),
min: Math.min(precision.price, precision.volume),
excludePriceVolumeMax: Number.MIN_SAFE_INTEGER,
excludePriceVolumeMin: Number.MAX_SAFE_INTEGER
})
overlays.forEach(overlay => {
if (overlay.visible) {
this._drawOverlay(
ctx, overlay, bounding, barSpace, precision,
ctx, overlay, bounding, barSpace, overlayPrecision,
dateTimeFormat, customApi, thousandsSeparator, decimalFoldThreshold,
defaultStyles, xAxis, yAxis,
hoverInstanceInfo, clickInstanceInfo, timeScaleStore
Expand All @@ -410,7 +425,7 @@ export default class OverlayView<C extends Axis = YAxis> extends View<C> {
if (overlay !== null && overlay.visible) {
this._drawOverlay(
ctx, overlay, bounding, barSpace,
precision, dateTimeFormat, customApi, thousandsSeparator, decimalFoldThreshold,
overlayPrecision, dateTimeFormat, customApi, thousandsSeparator, decimalFoldThreshold,
defaultStyles, xAxis, yAxis,
hoverInstanceInfo, clickInstanceInfo, timeScaleStore
)
Expand All @@ -423,7 +438,7 @@ export default class OverlayView<C extends Axis = YAxis> extends View<C> {
overlay: Overlay,
bounding: Bounding,
barSpace: BarSpace,
precision: Precision,
precision: OverlayPrecision,
dateTimeFormat: Intl.DateTimeFormat,
customApi: CustomApi,
thousandsSeparator: string,
Expand Down Expand Up @@ -512,7 +527,7 @@ export default class OverlayView<C extends Axis = YAxis> extends View<C> {
coordinates: Coordinate[],
bounding: Bounding,
barSpace: BarSpace,
precision: Precision,
precision: OverlayPrecision,
thousandsSeparator: string,
decimalFoldThreshold: number,
dateTimeFormat: Intl.DateTimeFormat,
Expand All @@ -528,7 +543,7 @@ export default class OverlayView<C extends Axis = YAxis> extends View<C> {
overlay: Overlay,
coordinates: Coordinate[],
_bounding: Bounding,
_precision: Precision,
_precision: OverlayPrecision,
_dateTimeFormat: Intl.DateTimeFormat,
_customApi: CustomApi,
_thousandsSeparator: string,
Expand Down
4 changes: 2 additions & 2 deletions src/view/OverlayXAxisView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { type CustomApi, FormatDateType } from '../Options'

import type XAxis from '../component/XAxis'
import type YAxis from '../component/YAxis'
import { type OverlayFigure } from '../component/Overlay'
import { type OverlayPrecision, type OverlayFigure } from '../component/Overlay'
import type Overlay from '../component/Overlay'

import { type EventOverlayInfo, type ProgressOverlayInfo } from '../store/OverlayStore'
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class OverlayXAxisView extends OverlayYAxisView<XAxis> {
coordinates: Coordinate[],
bounding: Bounding,
barSpace: BarSpace,
precision: Precision,
precision: OverlayPrecision,
thousandsSeparator: string,
decimalFoldThreshold: number,
dateTimeFormat: Intl.DateTimeFormat,
Expand Down
9 changes: 4 additions & 5 deletions src/view/OverlayYAxisView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type Nullable from '../common/Nullable'
import type Coordinate from '../common/Coordinate'
import type Bounding from '../common/Bounding'
import type BarSpace from '../common/BarSpace'
import type Precision from '../common/Precision'
import { type OverlayStyle } from '../common/Styles'
import { type CustomApi } from '../Options'
import { formatPrecision, formatThousands, formatFoldDecimal } from '../common/utils/format'
Expand All @@ -25,7 +24,7 @@ import { isNumber } from '../common/utils/typeChecks'
import type Axis from '../component/Axis'
import type XAxis from '../component/XAxis'
import type YAxis from '../component/YAxis'
import { type OverlayFigure } from '../component/Overlay'
import { type OverlayPrecision, type OverlayFigure } from '../component/Overlay'
import type Overlay from '../component/Overlay'

import { type EventOverlayInfo } from '../store/OverlayStore'
Expand All @@ -42,7 +41,7 @@ export default class OverlayYAxisView<C extends Axis = YAxis> extends OverlayVie
overlay: Overlay,
coordinates: Coordinate[],
bounding: Bounding,
precision: Precision,
precision: OverlayPrecision,
dateTimeFormat: Intl.DateTimeFormat,
customApi: CustomApi,
thousandsSeparator: string,
Expand All @@ -65,7 +64,7 @@ export default class OverlayYAxisView<C extends Axis = YAxis> extends OverlayVie
overlay: Overlay,
coordinates: Coordinate[],
bounding: Bounding,
precision: Precision,
precision: OverlayPrecision,
_dateTimeFormat: Intl.DateTimeFormat,
_customApi: CustomApi,
thousandsSeparator: string,
Expand Down Expand Up @@ -113,7 +112,7 @@ export default class OverlayYAxisView<C extends Axis = YAxis> extends OverlayVie
coordinates: Coordinate[],
bounding: Bounding,
barSpace: BarSpace,
precision: Precision,
precision: OverlayPrecision,
thousandsSeparator: string,
decimalFoldThreshold: number,
dateTimeFormat: Intl.DateTimeFormat,
Expand Down

0 comments on commit 109b0c9

Please sign in to comment.