mirror of
https://github.com/klinecharts/KLineChart.git
synced 2024-11-25 16:22:43 +08:00
refactor: some axis styles are changed to axis properties
This commit is contained in:
parent
d5779483f1
commit
b25f15df81
94
src/Chart.ts
94
src/Chart.ts
@ -19,7 +19,7 @@ import { type KLineData } from './common/Data'
|
||||
import type Coordinate from './common/Coordinate'
|
||||
import type Point from './common/Point'
|
||||
import { UpdateLevel } from './common/Updater'
|
||||
import { type Styles, YAxisPosition, YAxisType } from './common/Styles'
|
||||
import { type Styles } from './common/Styles'
|
||||
import type Crosshair from './common/Crosshair'
|
||||
import { ActionType, type ActionCallback } from './common/Action'
|
||||
import type LoadMoreCallback from './common/LoadMoreCallback'
|
||||
@ -49,15 +49,15 @@ import SeparatorPane from './pane/SeparatorPane'
|
||||
import { type PaneOptions, PanePosition, PANE_DEFAULT_HEIGHT, PaneIdConstants } from './pane/types'
|
||||
|
||||
import type AxisImp from './component/Axis'
|
||||
import { type Axis } from './component/Axis'
|
||||
import { AxisPosition, type Axis } from './component/Axis'
|
||||
|
||||
import { type Indicator, type IndicatorCreate } from './component/Indicator'
|
||||
import { type Overlay, type OverlayCreate, type OverlayRemove } from './component/Overlay'
|
||||
|
||||
import { getIndicatorClass } from './extension/indicator/index'
|
||||
import { getStyles as getExtensionStyles } from './extension/styles/index'
|
||||
|
||||
import Event from './Event'
|
||||
import { type YAxis } from './component/YAxis'
|
||||
|
||||
export enum DomPosition {
|
||||
Root = 'root',
|
||||
@ -154,9 +154,6 @@ export default class ChartImp implements Chart {
|
||||
this._chartStore = new ChartStore(this, options)
|
||||
this._initPanes(options)
|
||||
this.adjustPaneViewport(true, true, true)
|
||||
if (isValid(options?.styles)) {
|
||||
this.setStyles(options?.styles)
|
||||
}
|
||||
}
|
||||
|
||||
private _initContainer (container: HTMLElement): void {
|
||||
@ -350,55 +347,60 @@ export default class ChartImp implements Chart {
|
||||
private _measurePaneWidth (): void {
|
||||
const totalWidth = Math.floor(this._container.clientWidth)
|
||||
const styles = this._chartStore.getStyles()
|
||||
const yAxisStyles = styles.yAxis
|
||||
const isYAxisLeft = yAxisStyles.position === YAxisPosition.Left
|
||||
const isOutside = !yAxisStyles.inside
|
||||
let mainWidth = 0
|
||||
let yAxisWidth = 0
|
||||
let yAxisLeft = 0
|
||||
let mainLeft = 0
|
||||
|
||||
let leftYAxisWidth = 0
|
||||
let leftYAxisOutside = true
|
||||
let rightYAxisWidth = 0
|
||||
let rightYAxisOutside = true
|
||||
|
||||
this._drawPanes.forEach(pane => {
|
||||
if (pane.getId() !== PaneIdConstants.X_AXIS) {
|
||||
yAxisWidth = Math.max(yAxisWidth, pane.getAxisComponent().getAutoSize())
|
||||
const yAxis = pane.getAxisComponent() as YAxis
|
||||
const inside = yAxis.inside ?? false
|
||||
const yAxisWidth = yAxis.getAutoSize()
|
||||
if (yAxis.position === AxisPosition.Left) {
|
||||
leftYAxisWidth = Math.max(leftYAxisWidth, yAxisWidth)
|
||||
if (inside) {
|
||||
leftYAxisOutside = false
|
||||
}
|
||||
} else {
|
||||
rightYAxisWidth = Math.max(rightYAxisWidth, yAxisWidth)
|
||||
if (inside) {
|
||||
rightYAxisOutside = false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
if (yAxisWidth > totalWidth) {
|
||||
yAxisWidth = totalWidth
|
||||
|
||||
let mainWidth = totalWidth
|
||||
let mainLeft = 0
|
||||
let mainRight = 0
|
||||
if (leftYAxisOutside) {
|
||||
mainWidth -= leftYAxisWidth
|
||||
mainLeft = leftYAxisWidth
|
||||
}
|
||||
if (isOutside) {
|
||||
mainWidth = totalWidth - yAxisWidth
|
||||
if (isYAxisLeft) {
|
||||
yAxisLeft = 0
|
||||
mainLeft = yAxisWidth
|
||||
} else {
|
||||
yAxisLeft = totalWidth - yAxisWidth
|
||||
mainLeft = 0
|
||||
}
|
||||
} else {
|
||||
mainWidth = totalWidth
|
||||
mainLeft = 0
|
||||
if (isYAxisLeft) {
|
||||
yAxisLeft = 0
|
||||
} else {
|
||||
yAxisLeft = totalWidth - yAxisWidth
|
||||
}
|
||||
|
||||
if (rightYAxisOutside) {
|
||||
mainWidth -= rightYAxisWidth
|
||||
mainRight = rightYAxisWidth
|
||||
}
|
||||
|
||||
this._chartStore.getTimeScaleStore().setTotalBarSpace(mainWidth)
|
||||
|
||||
const paneBounding = { width: totalWidth }
|
||||
const mainBounding = { width: mainWidth, left: mainLeft }
|
||||
const yAxisBounding = { width: yAxisWidth, left: yAxisLeft }
|
||||
const mainBounding = { width: mainWidth, left: mainLeft, right: mainRight }
|
||||
const leftYAxisBounding = { width: leftYAxisWidth }
|
||||
const rightYAxisBounding = { width: rightYAxisWidth }
|
||||
const separatorFill = styles.separator.fill
|
||||
let separatorBounding: Partial<Bounding>
|
||||
if (isOutside && !separatorFill) {
|
||||
if (!separatorFill) {
|
||||
separatorBounding = mainBounding
|
||||
} else {
|
||||
separatorBounding = paneBounding
|
||||
}
|
||||
this._drawPanes.forEach((pane) => {
|
||||
this._separatorPanes.get(pane)?.setBounding(separatorBounding)
|
||||
pane.setBounding(paneBounding, mainBounding, yAxisBounding)
|
||||
pane.setBounding(paneBounding, mainBounding, leftYAxisBounding, rightYAxisBounding)
|
||||
})
|
||||
}
|
||||
|
||||
@ -415,7 +417,7 @@ export default class ChartImp implements Chart {
|
||||
shouldAdjust = true
|
||||
shouldMeasureHeight = true
|
||||
}
|
||||
if (isString(options.axisOptions?.name) || isValid(options.gap)) {
|
||||
if (isString(options.axis)) {
|
||||
shouldAdjust = true
|
||||
}
|
||||
pane.setOptions(options)
|
||||
@ -569,22 +571,6 @@ export default class ChartImp implements Chart {
|
||||
|
||||
setStyles (styles: string | DeepPartial<Styles>): void {
|
||||
this._chartStore.setOptions({ styles })
|
||||
let realStyles: Nullable<DeepPartial<Styles>>
|
||||
if (isString(styles)) {
|
||||
realStyles = getExtensionStyles(styles)
|
||||
} else {
|
||||
realStyles = styles
|
||||
}
|
||||
if (isValid(realStyles?.yAxis?.type)) {
|
||||
(this._candlePane?.getAxisComponent() as unknown as AxisImp).setAutoCalcTickFlag(true)
|
||||
const type = realStyles?.yAxis?.type
|
||||
this.setPaneOptions({
|
||||
id: 'candle_pane',
|
||||
axisOptions: {
|
||||
name: type === YAxisType.Log ? 'logarithm' : type
|
||||
}
|
||||
})
|
||||
}
|
||||
this.adjustPaneViewport(true, true, true, true, true)
|
||||
}
|
||||
|
||||
|
@ -241,10 +241,10 @@ export default class Event implements EventHandler {
|
||||
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
|
||||
if (!consumed && this._startScrollCoordinate !== null) {
|
||||
const yAxis = (pane as DrawPane<YAxis>).getAxisComponent()
|
||||
if (this._prevYAxisRange !== null && !yAxis.getAutoCalcTickFlag() && yAxis.getScrollZoomEnabled()) {
|
||||
if (this._prevYAxisRange !== null && !yAxis.getAutoCalcTickFlag() && yAxis.scrollZoomEnabled) {
|
||||
const { from, to, range } = this._prevYAxisRange
|
||||
let distance: number
|
||||
if (yAxis?.isReverse() ?? false) {
|
||||
if (yAxis.reverse) {
|
||||
distance = this._startScrollCoordinate.y - event.y
|
||||
} else {
|
||||
distance = event.y - this._startScrollCoordinate.y
|
||||
@ -279,7 +279,7 @@ export default class Event implements EventHandler {
|
||||
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
|
||||
if (!consumed) {
|
||||
const xAxis = (pane as DrawPane<XAxis>).getAxisComponent()
|
||||
if ((xAxis?.getScrollZoomEnabled() ?? true)) {
|
||||
if ((xAxis?.scrollZoomEnabled ?? true)) {
|
||||
const scale = this._xAxisStartScaleDistance / event.pageX
|
||||
if (Number.isFinite(scale)) {
|
||||
const zoomScale = (scale - this._xAxisScale) * 10
|
||||
@ -296,7 +296,7 @@ export default class Event implements EventHandler {
|
||||
const consumed = widget.dispatchEvent('pressedMouseMoveEvent', event)
|
||||
if (!consumed) {
|
||||
const yAxis = (pane as DrawPane<YAxis>).getAxisComponent()
|
||||
if (this._prevYAxisRange !== null && yAxis.getScrollZoomEnabled()) {
|
||||
if (this._prevYAxisRange !== null && yAxis.scrollZoomEnabled) {
|
||||
const { from, to, range } = this._prevYAxisRange
|
||||
const scale = event.pageY / this._yAxisStartScaleDistance
|
||||
const newRange = range * scale
|
||||
|
@ -318,26 +318,6 @@ export interface AxisStyle {
|
||||
tickText: AxisTickTextStyle
|
||||
}
|
||||
|
||||
export type XAxisStyle = AxisStyle
|
||||
|
||||
export enum YAxisPosition {
|
||||
Left = 'left',
|
||||
Right = 'right'
|
||||
}
|
||||
|
||||
export enum YAxisType {
|
||||
Normal = 'normal',
|
||||
Percentage = 'percentage',
|
||||
Log = 'log'
|
||||
}
|
||||
|
||||
export interface YAxisStyle extends AxisStyle {
|
||||
type: YAxisType
|
||||
position: YAxisPosition
|
||||
inside: boolean
|
||||
reverse: boolean
|
||||
}
|
||||
|
||||
export interface CrosshairDirectionStyle {
|
||||
show: boolean
|
||||
line: StateLineStyle
|
||||
@ -388,8 +368,8 @@ export interface Styles {
|
||||
grid: GridStyle
|
||||
candle: CandleStyle
|
||||
indicator: IndicatorStyle
|
||||
xAxis: XAxisStyle
|
||||
yAxis: YAxisStyle
|
||||
xAxis: AxisStyle
|
||||
yAxis: AxisStyle
|
||||
separator: SeparatorStyle
|
||||
crosshair: CrosshairStyle
|
||||
overlay: OverlayStyle
|
||||
@ -636,7 +616,7 @@ function getDefaultIndicatorStyle (): IndicatorStyle {
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultXAxisStyle (): XAxisStyle {
|
||||
function getDefaultAxisStyle (): AxisStyle {
|
||||
return {
|
||||
show: true,
|
||||
size: 'auto',
|
||||
@ -663,15 +643,6 @@ function getDefaultXAxisStyle (): XAxisStyle {
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultYAxisStyle (): YAxisStyle {
|
||||
const style = getDefaultXAxisStyle() as YAxisStyle
|
||||
style.type = YAxisType.Normal
|
||||
style.position = YAxisPosition.Right
|
||||
style.inside = false
|
||||
style.reverse = false
|
||||
return style
|
||||
}
|
||||
|
||||
function getDefaultCrosshairStyle (): CrosshairStyle {
|
||||
function item (): CrosshairDirectionStyle {
|
||||
return {
|
||||
@ -801,8 +772,8 @@ export function getDefaultStyles (): Styles {
|
||||
grid: getDefaultGridStyle(),
|
||||
candle: getDefaultCandleStyle(),
|
||||
indicator: getDefaultIndicatorStyle(),
|
||||
xAxis: getDefaultXAxisStyle(),
|
||||
yAxis: getDefaultYAxisStyle(),
|
||||
xAxis: getDefaultAxisStyle(),
|
||||
yAxis: getDefaultAxisStyle(),
|
||||
separator: getDefaultSeparatorStyle(),
|
||||
crosshair: getDefaultCrosshairStyle(),
|
||||
overlay: getDefaultOverlayStyle()
|
||||
|
@ -39,6 +39,11 @@ export interface AxisGap {
|
||||
bottom?: number
|
||||
}
|
||||
|
||||
export enum AxisPosition {
|
||||
Left = 'left',
|
||||
Right = 'right'
|
||||
}
|
||||
|
||||
export interface AxisValueToValueParams {
|
||||
range: AxisRange
|
||||
}
|
||||
@ -76,6 +81,8 @@ export interface Axis {
|
||||
export interface AxisTemplate {
|
||||
name: string
|
||||
reverse?: boolean
|
||||
inside?: boolean
|
||||
position?: AxisPosition
|
||||
scrollZoomEnabled?: boolean
|
||||
gap?: AxisGap
|
||||
valueToRealValue?: AxisValueToValueCallback
|
||||
@ -88,7 +95,7 @@ export interface AxisTemplate {
|
||||
createTicks?: AxisCreateTicksCallback
|
||||
}
|
||||
|
||||
export type AxisCreate = Omit<AxisTemplate, 'valueToRealValue' | 'realValueToDisplayValue' | 'displayValueToRealValue' | 'realValueToValue' | 'minSpan'>
|
||||
export type AxisCreate = Omit<AxisTemplate, 'displayValueToText' | 'valueToRealValue' | 'realValueToDisplayValue' | 'displayValueToRealValue' | 'realValueToValue' | 'minSpan'>
|
||||
|
||||
function getDefaultAxisRange (): AxisRange {
|
||||
return {
|
||||
@ -139,10 +146,6 @@ export default abstract class AxisImp implements Axis {
|
||||
return this._ticks
|
||||
}
|
||||
|
||||
getScrollZoomEnabled (): boolean {
|
||||
return this.getParent().getOptions().axisOptions.scrollZoomEnabled ?? true
|
||||
}
|
||||
|
||||
setRange (range: AxisRange): void {
|
||||
this._autoCalcTickFlag = false
|
||||
this._range = range
|
||||
|
@ -12,7 +12,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { YAxisPosition, CandleType } from '../common/Styles'
|
||||
import { CandleType } from '../common/Styles'
|
||||
import type Bounding from '../common/Bounding'
|
||||
import { isNumber, isString, isValid, merge } from '../common/utils/typeChecks'
|
||||
import { index10, getPrecision, nice, round } from '../common/utils/number'
|
||||
@ -22,7 +22,8 @@ import { formatPrecision, formatThousands, formatFoldDecimal } from '../common/u
|
||||
import AxisImp, {
|
||||
type AxisTemplate, type Axis, type AxisRange,
|
||||
type AxisTick, type AxisValueToValueCallback,
|
||||
type AxisMinSpanCallback, type AxisCreateRangeCallback
|
||||
type AxisMinSpanCallback, type AxisCreateRangeCallback,
|
||||
AxisPosition
|
||||
} from './Axis'
|
||||
|
||||
import type DrawPane from '../pane/DrawPane'
|
||||
@ -43,6 +44,8 @@ export type YAxisConstructor = new (parent: DrawPane<Axis>) => YAxis
|
||||
|
||||
export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
reverse = false
|
||||
inside = false
|
||||
position = AxisPosition.Right
|
||||
gap = {
|
||||
top: 0.2,
|
||||
bottom: 0.1
|
||||
@ -64,7 +67,9 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
override (yAxis: YAxisTemplate): void {
|
||||
const {
|
||||
name,
|
||||
position,
|
||||
reverse,
|
||||
inside,
|
||||
scrollZoomEnabled,
|
||||
gap,
|
||||
minSpan,
|
||||
@ -79,8 +84,10 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
if (!isString(name)) {
|
||||
this.name = name
|
||||
}
|
||||
this.scrollZoomEnabled = scrollZoomEnabled ?? this.scrollZoomEnabled
|
||||
this.position = position ?? this.position
|
||||
this.reverse = reverse ?? this.reverse
|
||||
this.inside = inside ?? this.inside
|
||||
this.scrollZoomEnabled = scrollZoomEnabled ?? this.scrollZoomEnabled
|
||||
merge(this.gap, gap)
|
||||
this.displayValueToText = displayValueToText ?? this.displayValueToText
|
||||
this.minSpan = minSpan ?? this.minSpan
|
||||
@ -240,31 +247,14 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
return this.getParent().getId() === PaneIdConstants.CANDLE
|
||||
}
|
||||
|
||||
getPosition (): string {
|
||||
return this.getParent().getChart().getStyles().yAxis.position
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否反转
|
||||
* @return {boolean}
|
||||
*/
|
||||
isReverse (): boolean {
|
||||
if (this.isInCandle()) {
|
||||
return this.getParent().getChart().getStyles().yAxis.reverse
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否从y轴0开始
|
||||
* @return {boolean}
|
||||
*/
|
||||
isFromZero (): boolean {
|
||||
const yAxisStyles = this.getParent().getChart().getStyles().yAxis
|
||||
const inside = yAxisStyles.inside
|
||||
return (
|
||||
(yAxisStyles.position === YAxisPosition.Left && inside) ||
|
||||
(yAxisStyles.position === YAxisPosition.Right && !inside)
|
||||
(this.position === AxisPosition.Left && this.inside) ||
|
||||
(this.position === AxisPosition.Right && !this.inside)
|
||||
)
|
||||
}
|
||||
|
||||
@ -373,10 +363,10 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
crosshairStyles.horizontal.text.show
|
||||
) {
|
||||
const indicators = chartStore.getIndicatorStore().getInstances(pane.getId())
|
||||
let techPrecision = 0
|
||||
let indicatorPrecision = 0
|
||||
let shouldFormatBigNumber = false
|
||||
indicators.forEach(indicator => {
|
||||
techPrecision = Math.max(indicator.precision, techPrecision)
|
||||
indicatorPrecision = Math.max(indicator.precision, indicatorPrecision)
|
||||
if (!shouldFormatBigNumber) {
|
||||
shouldFormatBigNumber = indicator.shouldFormatBigNumber
|
||||
}
|
||||
@ -386,14 +376,14 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
const { price: pricePrecision } = chartStore.getPrecision()
|
||||
const lastValueMarkStyles = styles.indicator.lastValueMark
|
||||
if (lastValueMarkStyles.show && lastValueMarkStyles.text.show) {
|
||||
precision = Math.max(techPrecision, pricePrecision)
|
||||
precision = Math.max(indicatorPrecision, pricePrecision)
|
||||
} else {
|
||||
precision = pricePrecision
|
||||
}
|
||||
} else {
|
||||
precision = techPrecision
|
||||
precision = indicatorPrecision
|
||||
}
|
||||
let valueText = formatPrecision(this.getRange().to, precision)
|
||||
let valueText = formatPrecision(this.getRange().displayTo, precision)
|
||||
if (shouldFormatBigNumber) {
|
||||
valueText = customApi.formatBigNumber(valueText)
|
||||
}
|
||||
@ -421,7 +411,7 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
const height = this.getBounding().height
|
||||
const range = this.getRange()
|
||||
const { realFrom, realRange } = range
|
||||
const rate = this.isReverse() ? pixel / height : 1 - pixel / height
|
||||
const rate = this.reverse ? pixel / height : 1 - pixel / height
|
||||
const realValue = rate * realRange + realFrom
|
||||
return this.realValueToValue(realValue, { range })
|
||||
}
|
||||
@ -432,7 +422,7 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
const height = this.getParent().getYAxisWidget()?.getBounding().height ?? 0
|
||||
const { realFrom, realRange } = range
|
||||
const rate = (realValue - realFrom) / realRange
|
||||
return this.isReverse() ? Math.round(rate * height) : Math.round((1 - rate) * height)
|
||||
return this.reverse ? Math.round(rate * height) : Math.round((1 - rate) * height)
|
||||
}
|
||||
|
||||
convertToNicePixel (value: number): number {
|
||||
|
@ -19,21 +19,21 @@ const logarithm: AxisTemplate = {
|
||||
name: 'logarithm',
|
||||
minSpan: (precision) => 0.05 * index10(-precision),
|
||||
valueToRealValue: (value) => {
|
||||
return log10(value)
|
||||
return value < 0 ? -log10(Math.abs(value)) : log10(value)
|
||||
},
|
||||
realValueToDisplayValue: (value) => {
|
||||
return index10(value)
|
||||
return value < 0 ? -index10(Math.abs(value)) : index10(value)
|
||||
},
|
||||
displayValueToRealValue: (value) => {
|
||||
return log10(value)
|
||||
return value < 0 ? -log10(Math.abs(value)) : log10(value)
|
||||
},
|
||||
realValueToValue: (value) => {
|
||||
return index10(value)
|
||||
return value < 0 ? -index10(Math.abs(value)) : index10(value)
|
||||
},
|
||||
createRange: ({ defaultRange }) => {
|
||||
const { from, to, range } = defaultRange
|
||||
const realFrom = log10(from)
|
||||
const realTo = log10(to)
|
||||
const realFrom = from < 0 ? -log10(Math.abs(from)) : log10(from)
|
||||
const realTo = to < 0 ? -log10(Math.abs(to)) : log10(to)
|
||||
return {
|
||||
from,
|
||||
to,
|
||||
|
@ -26,7 +26,7 @@
|
||||
|
||||
import {
|
||||
LineType, PolygonType, TooltipShowRule, TooltipShowType, TooltipIconPosition,
|
||||
CandleType, YAxisPosition, YAxisType, CandleTooltipRectPosition
|
||||
CandleType, CandleTooltipRectPosition
|
||||
} from './common/Styles'
|
||||
import type Nullable from './common/Nullable'
|
||||
|
||||
@ -173,6 +173,6 @@ export {
|
||||
registerXAxis, registerYAxis,
|
||||
utils,
|
||||
LineType, PolygonType, TooltipShowRule, TooltipShowType, TooltipIconPosition, CandleTooltipRectPosition,
|
||||
CandleType, YAxisPosition, YAxisType, FormatDateType,
|
||||
CandleType, FormatDateType,
|
||||
DomPosition, ActionType, IndicatorSeries, OverlayMode
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ import type Bounding from '../common/Bounding'
|
||||
|
||||
import { isString, isValid, merge } from '../common/utils/typeChecks'
|
||||
|
||||
import { type Axis } from '../component/Axis'
|
||||
import { AxisPosition, type Axis } from '../component/Axis'
|
||||
|
||||
import type DrawWidget from '../widget/DrawWidget'
|
||||
import type YAxisWidget from '../widget/YAxisWidget'
|
||||
@ -32,6 +32,7 @@ import type Chart from '../Chart'
|
||||
import { createDom } from '../common/utils/dom'
|
||||
import { getPixelRatio } from '../common/utils/canvas'
|
||||
import type PickPartial from '../common/PickPartial'
|
||||
import YAxisImp, { type YAxis } from '../component/YAxis'
|
||||
|
||||
export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
private readonly _mainWidget: DrawWidget<DrawPane<C>>
|
||||
@ -39,7 +40,7 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
|
||||
private _axis: C
|
||||
|
||||
private readonly _options: PickPartial<DeepRequired<Omit<PaneOptions, 'id' | 'height'>>, 'position'> = { minHeight: PANE_MIN_HEIGHT, dragEnabled: true, gap: { top: 0.2, bottom: 0.1 }, axisOptions: { name: 'normal', scrollZoomEnabled: true } }
|
||||
private readonly _options: PickPartial<DeepRequired<Omit<PaneOptions, 'id' | 'height'>>, 'position'> = { minHeight: PANE_MIN_HEIGHT, dragEnabled: true, axis: { name: 'normal', scrollZoomEnabled: true } }
|
||||
|
||||
constructor (rootContainer: HTMLElement, afterElement: Nullable<HTMLElement>, chart: Chart, id: string, options: Omit<PaneOptions, 'id' | 'height'>) {
|
||||
super(rootContainer, afterElement, chart, id)
|
||||
@ -50,18 +51,20 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
}
|
||||
|
||||
setOptions (options: Omit<PaneOptions, 'id' | 'height'>): this {
|
||||
const name = options.axisOptions?.name
|
||||
const name = options.axis?.name
|
||||
if (
|
||||
(this._options.axisOptions.name !== name && isString(name)) ||
|
||||
(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)
|
||||
}
|
||||
}
|
||||
merge(this._options, options)
|
||||
this._axis.override({
|
||||
name: name ?? 'normal',
|
||||
gap: this._options.gap,
|
||||
...this._options.axisOptions
|
||||
...this._options.axis
|
||||
})
|
||||
let container: HTMLElement
|
||||
let cursor: string
|
||||
@ -72,7 +75,7 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
container = this.getYAxisWidget()!.getContainer()
|
||||
cursor = 'ns-resize'
|
||||
}
|
||||
if (options.axisOptions?.scrollZoomEnabled ?? true) {
|
||||
if (options.axis?.scrollZoomEnabled ?? true) {
|
||||
container.style.cursor = cursor
|
||||
} else {
|
||||
container.style.cursor = 'default'
|
||||
@ -86,7 +89,12 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
return this._axis
|
||||
}
|
||||
|
||||
override setBounding (rootBounding: Partial<Bounding>, mainBounding?: Partial<Bounding>, yAxisBounding?: Partial<Bounding>): this {
|
||||
override setBounding (
|
||||
rootBounding: Partial<Bounding>,
|
||||
mainBounding?: Partial<Bounding>,
|
||||
leftYAxisBounding?: Partial<Bounding>,
|
||||
rightYAxisBounding?: Partial<Bounding>
|
||||
): this {
|
||||
merge(this.getBounding(), rootBounding)
|
||||
const contentBounding: Partial<Bounding> = {}
|
||||
if (isValid(rootBounding.height)) {
|
||||
@ -96,12 +104,30 @@ export default abstract class DrawPane<C extends Axis = Axis> extends Pane {
|
||||
contentBounding.top = rootBounding.top
|
||||
}
|
||||
this._mainWidget.setBounding(contentBounding)
|
||||
this._yAxisWidget?.setBounding(contentBounding)
|
||||
if (isValid(mainBounding)) {
|
||||
const mainBoundingValid = isValid(mainBounding)
|
||||
if (mainBoundingValid) {
|
||||
this._mainWidget.setBounding(mainBounding)
|
||||
}
|
||||
if (isValid(yAxisBounding)) {
|
||||
this._yAxisWidget?.setBounding(yAxisBounding)
|
||||
if (isValid(this._yAxisWidget)) {
|
||||
this._yAxisWidget.setBounding(contentBounding)
|
||||
const yAxis = this._axis as unknown as YAxis
|
||||
if (yAxis.position === AxisPosition.Left) {
|
||||
if (isValid(leftYAxisBounding)) {
|
||||
this._yAxisWidget.setBounding({ ...leftYAxisBounding, left: 0 })
|
||||
}
|
||||
} else {
|
||||
if (isValid(rightYAxisBounding)) {
|
||||
this._yAxisWidget.setBounding(rightYAxisBounding)
|
||||
if (mainBoundingValid) {
|
||||
this._yAxisWidget.setBounding({
|
||||
left: (mainBounding.left ?? 0) +
|
||||
(mainBounding.width ?? 0) +
|
||||
(mainBounding.right ?? 0) -
|
||||
(rightYAxisBounding.width ?? 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
@ -19,8 +19,6 @@ export interface PaneGap {
|
||||
bottom?: number
|
||||
}
|
||||
|
||||
export type PaneAxisOptions = Partial<AxisCreate>
|
||||
|
||||
export const enum PanePosition {
|
||||
Top = 'top',
|
||||
Bottom = 'bottom'
|
||||
@ -32,8 +30,7 @@ export interface PaneOptions {
|
||||
minHeight?: number
|
||||
dragEnabled?: boolean
|
||||
position?: PanePosition
|
||||
gap?: PaneGap
|
||||
axisOptions?: PaneAxisOptions
|
||||
axis?: Partial<AxisCreate>
|
||||
}
|
||||
|
||||
export const PANE_MIN_HEIGHT = 30
|
||||
|
@ -12,6 +12,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { isValid } from '../common/utils/typeChecks'
|
||||
import type YAxis from '../component/YAxis'
|
||||
|
||||
import View from './View'
|
||||
@ -29,7 +30,7 @@ export default class CandleLastPriceView extends View {
|
||||
const yAxis = pane.getAxisComponent() as YAxis
|
||||
const dataList = chartStore.getDataList()
|
||||
const data = dataList[dataList.length - 1]
|
||||
if (data != null) {
|
||||
if (isValid(data)) {
|
||||
const { close, open } = data
|
||||
const priceY = yAxis.convertToNicePixel(close)
|
||||
let color: string
|
||||
|
@ -19,7 +19,7 @@ import type Precision from '../common/Precision'
|
||||
import type Crosshair from '../common/Crosshair'
|
||||
import {
|
||||
type Styles, type CandleStyle, type TooltipLegend, type TooltipLegendChild, TooltipShowType, CandleTooltipRectPosition,
|
||||
type CandleTooltipCustomCallbackData, YAxisPosition, PolygonType
|
||||
type CandleTooltipCustomCallbackData, PolygonType
|
||||
} from '../common/Styles'
|
||||
import { formatPrecision, formatThousands, formatFoldDecimal } from '../common/utils/format'
|
||||
import { createFont } from '../common/utils/canvas'
|
||||
@ -36,6 +36,8 @@ 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 {
|
||||
@ -298,14 +300,15 @@ export default class CandleTooltipView extends IndicatorTooltipView {
|
||||
rectX = realX + rectOffsetLeft
|
||||
}
|
||||
} else {
|
||||
const yAxis = this.getWidget().getPane().getAxisComponent() as YAxisImp
|
||||
if (isLeft) {
|
||||
rectX = rectOffsetLeft + offsetLeft
|
||||
if (styles.yAxis.inside && styles.yAxis.position === YAxisPosition.Left) {
|
||||
if (yAxis.inside && yAxis.position === AxisPosition.Left) {
|
||||
rectX += yAxisBounding.width
|
||||
}
|
||||
} else {
|
||||
rectX = bounding.width - rectOffsetRight - rectWidth - offsetRight
|
||||
if (styles.yAxis.inside && styles.yAxis.position === YAxisPosition.Right) {
|
||||
if (yAxis.inside && yAxis.position === AxisPosition.Right) {
|
||||
rectX -= yAxisBounding.width
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user