Skip to content

Commit

Permalink
Merge pull request #505 from wdstdev/main
Browse files Browse the repository at this point in the history
add `currentData` for the figure `attrs` callback
  • Loading branch information
liihuu authored Jun 4, 2024
2 parents 9efcd01 + 773da8f commit 0be7ec6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/component/Indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export interface IndicatorFigureCallbackBrother<PCN> {

export type IndicatorFigureAttrsCallbackCoordinate<D> = IndicatorFigureCallbackBrother<Record<keyof D, number> & { x: number }>

export type IndicatorFigureAttrsCallbackData<D> = IndicatorFigureCallbackBrother<D>

export interface IndicatorFigureAttrsCallbackParams<D> {
data: IndicatorFigureAttrsCallbackData<Nullable<D>>
coordinate: IndicatorFigureAttrsCallbackCoordinate<D>
bounding: Bounding
barSpace: BarSpace
Expand Down
21 changes: 11 additions & 10 deletions src/view/IndicatorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,31 @@ export default class IndicatorView extends CandleBarView {
const { dataIndex, x } = data
const prevX = xAxis.convertToPixel(dataIndex - 1)
const nextX = xAxis.convertToPixel(dataIndex + 1)
const prevIndicatorData = result[dataIndex - 1] ?? {}
const currentIndicatorData = result[dataIndex] ?? {}
const nextIndicatorData = result[dataIndex + 1] ?? {}
const prevData = result[dataIndex - 1] ?? null
const currentData = result[dataIndex] ?? null
const nextData = result[dataIndex + 1] ?? null
const prevCoordinate = { x: prevX }
const currentCoordinate = { x }
const nextCoordinate = { x: nextX }
indicator.figures.forEach(({ key }) => {
const prevValue = prevIndicatorData[key]
const prevValue = prevData?.[key]
if (isNumber(prevValue)) {
prevCoordinate[key] = yAxis.convertToPixel(prevValue)
}
const currentValue = currentIndicatorData[key]
const currentValue = currentData?.[key]
if (isNumber(currentValue)) {
currentCoordinate[key] = yAxis.convertToPixel(currentValue)
}
const nextValue = nextIndicatorData[key]
const nextValue = nextData?.[key]
if (isNumber(nextValue)) {
nextCoordinate[key] = yAxis.convertToPixel(nextValue)
}
})
eachFigures(dataList, indicator, dataIndex, defaultStyles, (figure: IndicatorFigure, figureStyles: IndicatorFigureStyle, figureIndex: number) => {
if (isValid(currentIndicatorData[figure.key])) {
eachFigures(dataList, indicator, dataIndex, defaultStyles, (figure: IndicatorFigure, figureStyles: IndicatorFigureStyle) => {
if (isValid(currentData?.[figure.key])) {
const valueY = currentCoordinate[figure.key]
let attrs = figure.attrs?.({
data: { prev: prevData, current: currentData, next: nextData },
coordinate: { prev: prevCoordinate, current: currentCoordinate, next: nextCoordinate },
bounding,
barSpace,
Expand All @@ -147,7 +148,7 @@ export default class IndicatorView extends CandleBarView {
const baseValue = figure.baseValue ?? yAxis.getRange().from
const baseValueY = yAxis.convertToPixel(baseValue)
let height = Math.abs(baseValueY - (valueY as number))
if (baseValue !== currentIndicatorData[figure.key]) {
if (baseValue !== currentData?.[figure.key]) {
height = Math.max(1, height)
}
let y: number
Expand All @@ -159,7 +160,7 @@ export default class IndicatorView extends CandleBarView {
attrs = {
x: x - halfGapBar,
y,
width: gapBar,
width: Math.max(1, halfGapBar * 2),
height
}
break
Expand Down

0 comments on commit 0be7ec6

Please sign in to comment.