mirror of
https://github.com/klinecharts/KLineChart.git
synced 2024-11-25 16:22:43 +08:00
opt: opt axis type
This commit is contained in:
parent
5086d071b8
commit
4dc9d7e72e
@ -11,6 +11,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import type VisibleRange from '../common/VisibleRange'
|
||||
|
||||
import type DrawPane from '../pane/DrawPane'
|
||||
@ -29,6 +30,7 @@ export interface AxisRange extends VisibleRange {
|
||||
}
|
||||
|
||||
export interface Axis {
|
||||
getTicks: () => AxisTick[]
|
||||
convertToPixel: (value: number) => number
|
||||
convertFromPixel: (px: number) => number
|
||||
}
|
||||
@ -39,14 +41,21 @@ export interface AxisCreateTicksParams {
|
||||
defaultTicks: AxisTick[]
|
||||
}
|
||||
|
||||
export interface AxisCreateRangeParams {
|
||||
defaultRange: AxisRange
|
||||
}
|
||||
|
||||
export type AxisCreateRangeCallback = (params: AxisCreateRangeParams) => AxisRange
|
||||
|
||||
export type AxisCreateTicksCallback = (params: AxisCreateTicksParams) => AxisTick[]
|
||||
|
||||
export interface AxisTemplate {
|
||||
name: string
|
||||
createTicks: AxisCreateTicksCallback
|
||||
createRange?: AxisCreateRangeCallback
|
||||
createTicks?: AxisCreateTicksCallback
|
||||
}
|
||||
|
||||
export default abstract class AxisImp implements Pick<AxisTemplate, 'createTicks'>, Axis {
|
||||
export default abstract class AxisImp implements AxisTemplate, Axis {
|
||||
private readonly _parent: DrawPane<AxisImp>
|
||||
|
||||
private _range: AxisRange = { from: 0, to: 0, range: 0, realFrom: 0, realTo: 0, realRange: 0 }
|
||||
@ -59,20 +68,23 @@ export default abstract class AxisImp implements Pick<AxisTemplate, 'createTicks
|
||||
this._parent = parent
|
||||
}
|
||||
|
||||
name: string
|
||||
|
||||
getParent (): DrawPane<AxisImp> { return this._parent }
|
||||
|
||||
buildTicks (force: boolean): boolean {
|
||||
if (this._autoCalcTickFlag) {
|
||||
this._range = this.calcRange()
|
||||
const defaultRange = this.createDefaultRange()
|
||||
this._range = this.createRange?.({ defaultRange }) ?? defaultRange
|
||||
}
|
||||
if (this._prevRange.from !== this._range.from || this._prevRange.to !== this._range.to || force) {
|
||||
this._prevRange = this._range
|
||||
const defaultTicks = this.calcTicks()
|
||||
this._ticks = this.createTicks({
|
||||
const defaultTicks = this.createDefaultTicks()
|
||||
this._ticks = this.createTicks?.({
|
||||
range: this._range,
|
||||
bounding: this.getSelfBounding(),
|
||||
bounding: this.getBounding(),
|
||||
defaultTicks
|
||||
})
|
||||
}) ?? defaultTicks
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -99,16 +111,18 @@ export default abstract class AxisImp implements Pick<AxisTemplate, 'createTicks
|
||||
|
||||
getAutoCalcTickFlag (): boolean { return this._autoCalcTickFlag }
|
||||
|
||||
protected abstract calcRange (): AxisRange
|
||||
protected abstract createDefaultRange (): AxisRange
|
||||
|
||||
protected abstract calcTicks (): AxisTick[]
|
||||
protected abstract createDefaultTicks (): AxisTick[]
|
||||
|
||||
protected abstract getBounding (): Bounding
|
||||
|
||||
abstract createRange (params: AxisCreateRangeParams): AxisRange
|
||||
|
||||
abstract createTicks (params: AxisCreateTicksParams): AxisTick[]
|
||||
|
||||
abstract getAutoSize (): number
|
||||
|
||||
abstract getSelfBounding (): Bounding
|
||||
|
||||
abstract convertToPixel (value: number): number
|
||||
abstract convertFromPixel (px: number): number
|
||||
}
|
||||
|
@ -15,17 +15,20 @@
|
||||
import type Nullable from '../common/Nullable'
|
||||
import type Bounding from '../common/Bounding'
|
||||
|
||||
import AxisImp, { type AxisTemplate, type Axis, type AxisRange, type AxisTick, type AxisCreateTicksParams } from './Axis'
|
||||
import AxisImp, { type AxisTemplate, type Axis, type AxisRange, type AxisTick, type AxisCreateTicksParams, type AxisCreateRangeParams } from './Axis'
|
||||
|
||||
import type DrawPane from '../pane/DrawPane'
|
||||
import { TimeWeightConstants } from '../store/TimeScaleStore'
|
||||
|
||||
export type XAxis = Axis
|
||||
export interface XAxis extends Axis {
|
||||
convertTimestampFromPixel: (pixel: number) => Nullable<number>
|
||||
convertTimestampToPixel: (timestamp: number) => number
|
||||
}
|
||||
|
||||
export type XAxisConstructor = new (parent: DrawPane<AxisImp>) => XAxisImp
|
||||
|
||||
export default abstract class XAxisImp extends AxisImp {
|
||||
protected calcRange (): AxisRange {
|
||||
export default abstract class XAxisImp extends AxisImp implements XAxis {
|
||||
protected override createDefaultRange (): AxisRange {
|
||||
const chartStore = this.getParent().getChart().getChartStore()
|
||||
const { from, to } = chartStore.getTimeScaleStore().getVisibleRange()
|
||||
const af = from
|
||||
@ -36,7 +39,7 @@ export default abstract class XAxisImp extends AxisImp {
|
||||
}
|
||||
}
|
||||
|
||||
protected override calcTicks (): AxisTick[] {
|
||||
protected override createDefaultTicks (): AxisTick[] {
|
||||
const timeTickList = this.getParent().getChart().getChartStore().getTimeScaleStore().getVisibleTimeTickList()
|
||||
return timeTickList.map(({ dataIndex, dateTime, weight, timestamp }) => {
|
||||
let text = ''
|
||||
@ -107,7 +110,7 @@ export default abstract class XAxisImp extends AxisImp {
|
||||
return Math.max(xAxisHeight, crosshairVerticalTextHeight)
|
||||
}
|
||||
|
||||
getSelfBounding (): Bounding {
|
||||
protected override getBounding (): Bounding {
|
||||
return this.getParent().getMainWidget().getBounding()
|
||||
}
|
||||
|
||||
@ -133,8 +136,12 @@ export default abstract class XAxisImp extends AxisImp {
|
||||
|
||||
static extend (template: AxisTemplate): XAxisConstructor {
|
||||
class Custom extends XAxisImp {
|
||||
createRange (params: AxisCreateRangeParams): AxisRange {
|
||||
return template.createRange?.(params) ?? params.defaultRange
|
||||
}
|
||||
|
||||
createTicks (params: AxisCreateTicksParams): AxisTick[] {
|
||||
return template.createTicks(params)
|
||||
return template.createTicks?.(params) ?? params.defaultTicks
|
||||
}
|
||||
}
|
||||
return Custom
|
||||
|
@ -19,7 +19,7 @@ import { index10, log10, getPrecision, nice, round } from '../common/utils/numbe
|
||||
import { calcTextWidth } from '../common/utils/canvas'
|
||||
import { formatPrecision, formatThousands, formatFoldDecimal } from '../common/utils/format'
|
||||
|
||||
import AxisImp, { type AxisTemplate, type Axis, type AxisRange, type AxisTick, type AxisCreateTicksParams } from './Axis'
|
||||
import AxisImp, { type AxisTemplate, type Axis, type AxisRange, type AxisTick, type AxisCreateTicksParams, type AxisCreateRangeParams } from './Axis'
|
||||
|
||||
import { type IndicatorFigure } from './Indicator'
|
||||
|
||||
@ -35,12 +35,13 @@ interface FiguresResult {
|
||||
export interface YAxis extends Axis {
|
||||
isFromZero: () => boolean
|
||||
isInCandle: () => boolean
|
||||
convertToNicePixel: (value: number) => number
|
||||
}
|
||||
|
||||
export type YAxisConstructor = new (parent: DrawPane<AxisImp>) => YAxisImp
|
||||
|
||||
export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
protected calcRange (): AxisRange {
|
||||
protected override createDefaultRange (): AxisRange {
|
||||
const parent = this.getParent()
|
||||
const chart = parent.getChart()
|
||||
const chartStore = chart.getChartStore()
|
||||
@ -247,7 +248,7 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
)
|
||||
}
|
||||
|
||||
calcTicks (): AxisTick[] {
|
||||
protected override createDefaultTicks (): AxisTick[] {
|
||||
const { realFrom, realTo, realRange } = this.getRange()
|
||||
const ticks: AxisTick[] = []
|
||||
|
||||
@ -407,7 +408,7 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
return Math.max(yAxisWidth, crosshairVerticalTextWidth)
|
||||
}
|
||||
|
||||
getSelfBounding (): Bounding {
|
||||
protected override getBounding (): Bounding {
|
||||
return this.getParent().getYAxisWidget()!.getBounding()
|
||||
}
|
||||
|
||||
@ -470,8 +471,12 @@ export default abstract class YAxisImp extends AxisImp implements YAxis {
|
||||
|
||||
static extend (template: AxisTemplate): YAxisConstructor {
|
||||
class Custom extends YAxisImp {
|
||||
createRange (params: AxisCreateRangeParams): AxisRange {
|
||||
return template.createRange?.(params) ?? params.defaultRange
|
||||
}
|
||||
|
||||
createTicks (params: AxisCreateTicksParams): AxisTick[] {
|
||||
return template.createTicks(params)
|
||||
return template.createTicks?.(params) ?? params.defaultTicks
|
||||
}
|
||||
}
|
||||
return Custom
|
||||
|
Loading…
Reference in New Issue
Block a user