Skip to content

Commit

Permalink
perf: merge pane size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Oct 15, 2024
1 parent 58642fb commit b1fc94c
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,22 @@ export default class ChartImp implements Chart {
private _xAxisPane: XAxisPane
private readonly _separatorPanes = new Map<DrawPane, SeparatorPane>()

private _layoutOptions = {
measureHeight: true,
measureWidth: true,
update: true,
buildYAxisTick: false,
forceBuildAxisTick: false
}

private _layoutPending = false

constructor (container: HTMLElement, options?: Options) {
this._initContainer(container)
this._chartEvent = new Event(this._chartContainer, this)
this._chartStore = new ChartStore(this, options)
this._initPanes(options)
this.layout({ measureWidth: true, measureHeight: true, update: true })
this._layout()
}

private _initContainer (container: HTMLElement): void {
Expand Down Expand Up @@ -360,13 +370,34 @@ export default class ChartImp implements Chart {
buildYAxisTick?: boolean
forceBuildAxisTick?: boolean
}): void {
const {
measureHeight = false,
measureWidth = false,
buildYAxisTick = false,
forceBuildAxisTick = false,
update = false
} = options
if (options.measureHeight ?? false) {
this._layoutOptions.measureHeight = options.measureHeight!
}
if (options.measureWidth ?? false) {
this._layoutOptions.measureWidth = options.measureWidth!
}
if (options.update ?? false) {
this._layoutOptions.update = options.update!
}
if (options.buildYAxisTick ?? false) {
this._layoutOptions.buildYAxisTick = options.buildYAxisTick!
}
if (options.buildYAxisTick ?? false) {
this._layoutOptions.forceBuildAxisTick = options.forceBuildAxisTick!
}
if (!this._layoutPending) {
this._layoutPending = true
Promise.resolve().then(_ => {
this._layout()
this._layoutPending = false
// eslint-disable-next-line @typescript-eslint/no-empty-function
}).catch(_ => {
})
}
}

private _layout (): void {
const { measureHeight, measureWidth, update, buildYAxisTick, forceBuildAxisTick } = this._layoutOptions
if (measureHeight) {
const totalHeight = this._chartBounding.height
const separatorSize = this._chartStore.getOptions().styles.separator.size
Expand Down Expand Up @@ -527,6 +558,13 @@ export default class ChartImp implements Chart {
(this._xAxisPane.getAxisComponent() as unknown as AxisImp).buildTicks(true)
this.updatePane(UpdateLevel.All)
}
this._layoutOptions = {
measureHeight: false,
measureWidth: false,
update: false,
buildYAxisTick: false,
forceBuildAxisTick: false
}
}

updatePane (level: UpdateLevel, paneId?: string): void {
Expand Down

0 comments on commit b1fc94c

Please sign in to comment.