From 7ba011b707c881d4153258cbe45a1bc90f8ffaaa Mon Sep 17 00:00:00 2001 From: Aleksei Ilin Date: Wed, 9 Sep 2026 18:25:11 +0200 Subject: [PATCH] feat: add setLayoutOptions to change layout defaults after init --- docs/.vitepress/config/share.js | 1 + .../references/instance/setLayoutOptions.md | 28 +++++++++++++++++++ docs/en-US/api/instance/setLayoutOptions.md | 28 +++++++++++++++++++ src/Chart.ts | 11 +++++++- src/Store.ts | 6 ++++ src/index.ts | 3 +- 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 docs/@views/api/references/instance/setLayoutOptions.md create mode 100644 docs/en-US/api/instance/setLayoutOptions.md diff --git a/docs/.vitepress/config/share.js b/docs/.vitepress/config/share.js index d33587832..e1010ca53 100644 --- a/docs/.vitepress/config/share.js +++ b/docs/.vitepress/config/share.js @@ -34,6 +34,7 @@ export function getInstanceApiMenus (lang = '') { { text: 'getSize', link: `${prefix}/getSize` }, { text: 'setStyles', link: `${prefix}/setStyles` }, { text: 'getStyles', link: `${prefix}/getStyles` }, + { text: 'setLayoutOptions', link: `${prefix}/setLayoutOptions` }, { text: 'setFormatter', link: `${prefix}/setFormatter` }, { text: 'getFormatter', link: `${prefix}/getFormatter` }, { text: 'setLocale', link: `${prefix}/setLocale` }, diff --git a/docs/@views/api/references/instance/setLayoutOptions.md b/docs/@views/api/references/instance/setLayoutOptions.md new file mode 100644 index 000000000..e45b9ead7 --- /dev/null +++ b/docs/@views/api/references/instance/setLayoutOptions.md @@ -0,0 +1,28 @@ +```typescript +( + layout: { + barSpaceLimit?: { + min?: number + max?: number + } + pane?: { + height?: number + minHeight?: number + dragEnabled?: boolean + order?: number + state?: 'normal' | 'maximize' | 'minimize' + } + yAxis?: { + reverse?: boolean + inside?: boolean + needWidget?: boolean + position?: 'left' | 'right' + scrollZoomEnabled?: boolean + gap?: { + top?: number + bottom?: number + } + } + } +) => void +``` diff --git a/docs/en-US/api/instance/setLayoutOptions.md b/docs/en-US/api/instance/setLayoutOptions.md new file mode 100644 index 000000000..2bc058f3e --- /dev/null +++ b/docs/en-US/api/instance/setLayoutOptions.md @@ -0,0 +1,28 @@ +--- +outline: deep +--- + +# setLayoutOptions(layout) +`setLayoutOptions` set the chart default layout options. It only affects panes and y-axes created after the call, existing panes and y-axes remain unchanged. Incremental values ​​are supported. + +## Reference {#reference} + + +### Parameters {#parameters} +- `layout` Layout options. + - `barSpaceLimit` Bar space limit. + - `pane` Pane default options. + - `yAxis` Y-axis default options. + +### Returns {#returns} +`setLayoutOptions` returns `undefined` . + +## Usage {#usage} +```javascript +chart.setLayoutOptions({ + yAxis: { + inside: true, + position: 'left' + } +}) +``` diff --git a/src/Chart.ts b/src/Chart.ts index e39f68497..2014cada3 100644 --- a/src/Chart.ts +++ b/src/Chart.ts @@ -22,6 +22,7 @@ import type Crosshair from './common/Crosshair' import type { KLineData } from './common/Data' import type { DataLoader } from './common/DataLoader' import type DeepPartial from './common/DeepPartial' +import type DeepRequired from './common/DeepRequired' import type ExcludePickPartial from './common/ExcludePickPartial' import type Nullable from './common/Nullable' import type { Period } from './common/Period' @@ -45,7 +46,7 @@ import type { XAxisOverride } from './component/XAxis' import { Y_AXIS_ID_PREFIX, type YAxis, type YAxisOverride } from './component/YAxis' import Event from './Event' import { getIndicatorClass } from './extension/indicator/index' -import type { DecimalFold, Formatter, Hotkey, Options, ThousandsSeparator, ZoomAnchor, ZoomAnchorType } from './Options' +import type { DecimalFold, Formatter, Hotkey, Layout, Options, ThousandsSeparator, ZoomAnchor, ZoomAnchorType } from './Options' import CandlePane from './pane/CandlePane' import type DrawPane from './pane/DrawPane' import IndicatorPane from './pane/IndicatorPane' @@ -627,6 +628,14 @@ export default class ChartImp implements Chart { return this._chartStore.getStyles() } + setLayoutOptions(layout: DeepPartial): void { + this._chartStore.setLayoutOptions(layout) + } + + getLayoutOptions(): DeepRequired { + return this._chartStore.getLayoutOptions() + } + setFormatter(formatter: Partial): void { this._setOptions(() => { this._chartStore.setFormatter(formatter) diff --git a/src/Store.ts b/src/Store.ts index f966c91e1..f6832a4eb 100644 --- a/src/Store.ts +++ b/src/Store.ts @@ -80,6 +80,8 @@ export const DEFAULT_MIN_TIME_SPAN = 15 * 60 * 1000 export interface Store { setStyles: (value: string | DeepPartial) => void getStyles: () => Styles + setLayoutOptions: (layout: DeepPartial) => void + getLayoutOptions: () => DeepRequired setFormatter: (formatter: Partial) => void getFormatter: () => Formatter setLocale: (locale: string) => void @@ -464,6 +466,10 @@ export default class StoreImp implements Store { return this._styles } + setLayoutOptions(layout: DeepPartial): void { + merge(this._layoutOptions, layout) + } + setFormatter(formatter: Partial): void { merge(this._formatter, formatter) } diff --git a/src/index.ts b/src/index.ts index cc85c34f7..a1949060b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,7 +49,7 @@ import { getOverlayClass, getSupportedOverlays, registerOverlay } from './extens import { registerStyles } from './extension/styles/index' import { registerXAxis } from './extension/x-axis' import { registerYAxis } from './extension/y-axis' -import type { FormatDateType, Options, ZoomAnchor } from './Options' +import type { FormatDateType, Layout, Options, ZoomAnchor } from './Options' const charts = new Map() let chartBaseId = 1 @@ -163,6 +163,7 @@ export { getSupportedOverlays, type IndicatorSeries, init, + type Layout, type LineType, type OverlayDrawingMode, type OverlayMode,