fix: fix axis errors

This commit is contained in:
liihuu 2024-09-04 01:58:08 +08:00
parent 5fe227fcc4
commit 535eabdb21
7 changed files with 37 additions and 18 deletions

View File

@ -51,6 +51,7 @@ export interface AxisValueToValueParams {
export type AxisValueToValueCallback = (value: number, params: AxisValueToValueParams) => number
export interface AxisCreateRangeParams {
paneId: string
kLineDataList: KLineData[]
indicators: Indicator[]
visibleDataRange: VisibleRange

View File

@ -362,6 +362,7 @@ export default class IndicatorImp<D = any> implements Indicator<D> {
constructor (indicator: IndicatorTemplate<D>) {
this.override(indicator)
this._lockSeriesPrecision = false
}
override (indicator: Partial<Indicator<D>>): void {

View File

@ -24,7 +24,7 @@ import { FormatDateType } from '../Options'
export type XAxisTemplate = Pick<AxisTemplate, 'name' | 'scrollZoomEnabled' | 'createTicks'>
export interface XAxis extends Axis, AxisTemplate {
export interface XAxis extends Axis, Required<XAxisTemplate> {
convertTimestampFromPixel: (pixel: number) => Nullable<number>
convertTimestampToPixel: (timestamp: number) => number
}

View File

@ -34,7 +34,7 @@ export type YAxisTemplate = AxisTemplate
const TICK_COUNT = 8
export interface YAxis extends Axis, YAxisTemplate {
export interface YAxis extends Axis, Required<YAxisTemplate> {
isFromZero: () => boolean
isInCandle: () => boolean
convertToNicePixel: (value: number) => number
@ -103,13 +103,14 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
const parent = this.getParent()
const chart = parent.getChart()
const chartStore = chart.getChartStore()
const paneId = parent.getId()
let min = Number.MAX_SAFE_INTEGER
let max = Number.MIN_SAFE_INTEGER
let shouldOhlc = false
let specifyMin = Number.MAX_SAFE_INTEGER
let specifyMax = Number.MIN_SAFE_INTEGER
let indicatorPrecision = Number.MAX_SAFE_INTEGER
const indicators = chartStore.getIndicatorStore().getInstanceByPaneId(parent.getId())
const indicators = chartStore.getIndicatorStore().getInstanceByPaneId(paneId)
indicators.forEach(indicator => {
if (!shouldOhlc) {
shouldOhlc = indicator.shouldOhlc ?? false
@ -189,6 +190,7 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
}
const range = this.createRange?.({
paneId,
kLineDataList: chartStore.getDataList(),
visibleDataRange: chartStore.getTimeScaleStore().getVisibleRange(),
indicators,

View File

@ -17,7 +17,7 @@ import type Nullable from '../common/Nullable'
import { type UpdateLevel } from '../common/Updater'
import type Bounding from '../common/Bounding'
import { isString, isValid, merge } from '../common/utils/typeChecks'
import { isValid, merge } from '../common/utils/typeChecks'
import { AxisPosition, type Axis } from '../component/Axis'
@ -51,19 +51,27 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
}
setOptions (options: Omit<PaneOptions, 'id' | 'height'>): this {
const name = options.axis?.name
if (
(this._options.axis.name !== name && isString(name)) ||
!isValid(this._axis)
) {
this._axis = this.createAxisComponent(name ?? 'normal')
if (this._axis instanceof YAxisImp) {
this._axis.setAutoCalcTickFlag(true)
const paneId = this.getId()
let name = options.axis?.name ?? 'normal'
if (paneId === PaneIdConstants.CANDLE) {
if (
!isValid(this._axis) ||
(this._options.axis.name !== name)
) {
this._axis = this.createAxisComponent(name)
}
} else {
name = 'normal'
if (!isValid(this._axis)) {
this._axis = this.createAxisComponent(name)
}
}
if (this._axis instanceof YAxisImp) {
this._axis.setAutoCalcTickFlag(true)
}
merge(this._options, options)
this._axis.override({
name: name ?? 'normal',
name,
...this._options.axis
})
let container: HTMLElement

View File

@ -30,14 +30,13 @@ import { type CustomApi, FormatDateType } from '../Options'
import { PaneIdConstants } from '../pane/types'
import type Indicator from '../component/Indicator'
import { AxisPosition } from '../component/Axis'
import IndicatorTooltipView from './IndicatorTooltipView'
import { type TooltipIcon } from '../store/TooltipStore'
import { i18n } from '../extension/i18n/index'
import type YAxisImp from '../component/YAxis'
import { AxisPosition } from '../component/Axis'
export default class CandleTooltipView extends IndicatorTooltipView {
override drawImp (ctx: CanvasRenderingContext2D): void {
@ -300,7 +299,7 @@ export default class CandleTooltipView extends IndicatorTooltipView {
rectX = realX + rectOffsetLeft
}
} else {
const yAxis = this.getWidget().getPane().getAxisComponent() as YAxisImp
const yAxis = this.getWidget().getPane().getAxisComponent()
if (isLeft) {
rectX = rectOffsetLeft + offsetLeft
if (yAxis.inside && yAxis.position === AxisPosition.Left) {

View File

@ -12,7 +12,7 @@
* limitations under the License.
*/
import { formatPrecision, formatThousands, formatFoldDecimal } from '../common/utils/format'
import { formatThousands, formatFoldDecimal } from '../common/utils/format'
import { isNumber, isValid } from '../common/utils/typeChecks'
import { eachFigures, type IndicatorFigure, type IndicatorFigureStyle } from '../component/Indicator'
@ -33,6 +33,7 @@ export default class IndicatorLastValueView extends View<YAxis> {
const lastValueMarkTextStyles = lastValueMarkStyles.text
if (lastValueMarkStyles.show) {
const yAxis = pane.getAxisComponent()
const yAxisRange = yAxis.getRange()
const dataList = chartStore.getDataList()
const dataIndex = dataList.length - 1
const indicators = chartStore.getIndicatorStore().getInstanceByPaneId(pane.getId())
@ -47,7 +48,14 @@ export default class IndicatorLastValueView extends View<YAxis> {
const value = indicatorData[figure.key]
if (isNumber(value)) {
const y = yAxis.convertToNicePixel(value)
let text = formatPrecision(value, precision)
// let text = formatPrecision(value, precision)
let text = yAxis.displayValueToText(
yAxis.realValueToDisplayValue(
yAxis.valueToRealValue(value, { range: yAxisRange }),
{ range: yAxisRange }
),
precision
)
if (indicator.shouldFormatBigNumber) {
text = customApi.formatBigNumber(text)
}