diff --git a/website/scripts/chart/index.js b/website/scripts/chart/index.js index e172e2b30..53806dd87 100644 --- a/website/scripts/chart/index.js +++ b/website/scripts/chart/index.js @@ -190,11 +190,8 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { setup() { elements.root.classList.add("chart"); - elements.chart.classList.add("lightweight-chart"); parent.append(elements.root); - elements.root.append(legends.top.element); elements.root.append(elements.chart); - elements.root.append(legends.bottom.element); }, }; elements.setup(); @@ -220,6 +217,7 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { borderVisible: false, }, timeScale: { + // borderColor: colors.border(), borderVisible: false, enableConflation: true, ...(fitContent @@ -306,6 +304,9 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { separatorColor: borderColor, }, }, + timeScale: { + // borderColor: colors.border(), + }, crosshair: { horzLine: { color: offColor, @@ -415,28 +416,6 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { seriesByHome: new Map(), pendingVisibilityCheck: false, - /** @param {number} homePane */ - isAllHidden(homePane) { - const map = this.seriesByHome.get(homePane); - return !map || [...map.keys()].every((s) => !s.active.value); - }, - - /** - * @param {number} homePane - * @param {number} targetPane - */ - moveTo(homePane, targetPane) { - const map = this.seriesByHome.get(homePane); - if (!map) return; - for (const iseries of map.values()) { - for (const is of iseries) { - if (is.getPane().paneIndex() !== targetPane) { - is.moveToPane(targetPane); - } - } - } - }, - /** * @param {number} paneIndex * @param {VoidFunction} callback @@ -454,26 +433,55 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { } }, - updateVisibility() { - const pane0Hidden = this.isAllHidden(0); - const pane1Hidden = this.isAllHidden(1); - const bothVisible = !pane0Hidden && !pane1Hidden; + /** @param {number} targetPaneIndex @param {Legend} legend */ + injectLegend(targetPaneIndex, legend) { + const pane = ichart.panes().at(targetPaneIndex); + const parent = pane?.getHTMLElement()?.children?.item(1)?.firstChild; + if (!parent) return; + parent.appendChild(legend.element); + }, - this.moveTo(1, bothVisible ? 1 : 0); + initialized: false, - if (bothVisible) { + setup() { + if (this.initialized) return; + this.initialized = true; + + this.whenReady(0, () => { + const pane0 = ichart.panes().at(0); + fieldsets.createForPane(0); + this.injectLegend(0, legends.top); + if (pane0) injectScaleSelector(0, pane0); + this.updateSize(0); + }); + + if (this.seriesByHome.has(1)) { this.whenReady(1, () => { - fieldsets.createForPane(0); + const pane1 = ichart.panes().at(1); fieldsets.createForPane(1); + this.injectLegend(1, legends.bottom); + if (pane1) injectScaleSelector(1, pane1); + this.updateSize(1); }); + } + }, + + /** @param {number} homePane */ + isAllHidden(homePane) { + const map = this.seriesByHome.get(homePane); + return !map || [...map.keys()].every((s) => !s.active.value); + }, + + /** @param {number} paneIndex */ + updateSize(paneIndex) { + const pane = ichart.panes().at(paneIndex); + if (!pane) return; + const hidden = this.isAllHidden(paneIndex); + if (hidden) { + const chartHeight = ichart.chartElement().clientHeight; + pane.setStretchFactor(chartHeight > 0 ? 32 / (chartHeight - 32) : 0); } else { - this.whenReady(0, () => { - if (pane0Hidden && !pane1Hidden) { - fieldsets.createForPane(1, 0); - } else { - fieldsets.createForPane(0); - } - }); + pane.setStretchFactor(1); } }, @@ -494,7 +502,7 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { this.pendingVisibilityCheck = true; requestAnimationFrame(() => { this.pendingVisibilityCheck = false; - this.updateVisibility(); + this.setup(); }); } }, @@ -588,7 +596,7 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { if (value && !wasActive) { state.fetch?.(); } - panes.updateVisibility(); + panes.updateSize(paneIndex); }, setOrder, show, @@ -759,7 +767,11 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { function tryProcess() { if (seriesGeneration !== generation) return; if (!timeData || !valuesData) return; - if (valuesStamp === state.lastStamp && timeData.stamp === state.lastTimeStamp) return; + if ( + valuesStamp === state.lastStamp && + timeData.stamp === state.lastTimeStamp + ) + return; state.lastStamp = valuesStamp; state.lastTimeStamp = timeData.stamp; if (timeData.data.length && valuesData.length) { @@ -1490,47 +1502,76 @@ export function createChart({ parent, id: chartId, brk, fitContent }) { }, }; + /** + * @param {number} paneIndex + */ /** * @param {number} paneIndex */ function applyScaleForUnit(paneIndex) { + const pane = ichart.panes().at(paneIndex); + if (!pane) return; + const persisted = scalePersistedValues[paneIndex]; + if (!persisted) return; + try { + pane.priceScale("right").applyOptions({ + mode: persisted.value === "lin" ? 0 : 1, + }); + } catch {} + } + + /** @type {Record>>} */ + const scalePersistedValues = {}; + + /** + * @param {number} paneIndex + * @param {IPaneApi