Skip to content

Commit

Permalink
Merge pull request #1412 from tradingview/minimum-scale-dimensions
Browse files Browse the repository at this point in the history
Add option to set minimum dimension for scales
  • Loading branch information
edew committed Sep 19, 2023
2 parents 06fc780 + 1210430 commit 6569b3e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ module.exports = [
{
name: 'CJS',
path: 'dist/lightweight-charts.production.cjs',
limit: '47.62 KB',
limit: '47.66 KB',
},
{
name: 'ESM',
path: 'dist/lightweight-charts.production.mjs',
limit: '47.56 KB',
limit: '47.60 KB',
},
{
name: 'Standalone-ESM',
path: 'dist/lightweight-charts.standalone.production.mjs',
limit: '49.29 KB',
limit: '49.32 KB',
},
{
name: 'Standalone',
path: 'dist/lightweight-charts.standalone.production.js',
limit: '49.33 KB',
limit: '49.37 KB',
},
];
1 change: 1 addition & 0 deletions src/api/options/price-scale-options-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export const priceScaleOptionsDefaults: PriceScaleOptions = {
bottom: 0.1,
top: 0.2,
},
minimumWidth: 0,
};
1 change: 1 addition & 0 deletions src/api/options/time-scale-options-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const timeScaleOptionsDefaults: HorzScaleOptions = {
shiftVisibleRangeOnNewBar: true,
ticksVisible: false,
uniformDistribution: false,
minimumHeight: 0,
};
14 changes: 11 additions & 3 deletions src/gui/chart-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,18 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas

for (const paneWidget of this._paneWidgets) {
if (this._isLeftAxisVisible()) {
leftPriceAxisWidth = Math.max(leftPriceAxisWidth, ensureNotNull(paneWidget.leftPriceAxisWidget()).optimalWidth());
leftPriceAxisWidth = Math.max(
leftPriceAxisWidth,
ensureNotNull(paneWidget.leftPriceAxisWidget()).optimalWidth(),
this._options.leftPriceScale.minimumWidth
);
}
if (this._isRightAxisVisible()) {
rightPriceAxisWidth = Math.max(rightPriceAxisWidth, ensureNotNull(paneWidget.rightPriceAxisWidget()).optimalWidth());
rightPriceAxisWidth = Math.max(
rightPriceAxisWidth,
ensureNotNull(paneWidget.rightPriceAxisWidget()).optimalWidth(),
this._options.rightPriceScale.minimumWidth
);
}
totalStretch += paneWidget.stretchFactor();
}
Expand All @@ -464,7 +472,7 @@ export class ChartWidget<HorzScaleItem> implements IDestroyable, IChartWidgetBas
// const separatorHeight = SEPARATOR_HEIGHT;
const separatorsHeight = 0; // separatorHeight * separatorCount;
const timeAxisVisible = this._options.timeScale.visible;
let timeAxisHeight = timeAxisVisible ? this._timeAxisWidget.optimalHeight() : 0;
let timeAxisHeight = timeAxisVisible ? Math.max(this._timeAxisWidget.optimalHeight(), this._options.timeScale.minimumHeight) : 0;
timeAxisHeight = suggestTimeScaleHeight(timeAxisHeight);

const otherWidgetHeight = separatorsHeight + timeAxisHeight;
Expand Down
14 changes: 14 additions & 0 deletions src/model/price-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ export interface PriceScaleOptions {
* @defaultValue `false`
*/
ticksVisible: boolean;

/**
* Define a minimum width for the price scale.
* Note: This value will be exceeded if the
* price scale needs more space to display it's contents.
*
* Setting a minimum width could be useful for ensuring that
* multiple charts positioned in a vertical stack each have
* an identical price scale width, or for plugins which
* require a bit more space within the price scale pane.
*
* @defaultValue 0
*/
minimumWidth: number;
}

interface RangeCache {
Expand Down
14 changes: 14 additions & 0 deletions src/model/time-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ export interface HorzScaleOptions {
* With this flag equal to `true`, marks of the same weight are either all drawn or none are drawn at all.
*/
uniformDistribution: boolean;

/**
* Define a minimum height for the time scale.
* Note: This value will be exceeded if the
* time scale needs more space to display it's contents.
*
* Setting a minimum height could be useful for ensuring that
* multiple charts positioned in a horizontal stack each have
* an identical time scale height, or for plugins which
* require a bit more space within the time scale pane.
*
* @defaultValue 0
*/
minimumHeight: number;
}

export interface ITimeScale {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function generateData(startAmount) {
const res = [];
const time = new Date(Date.UTC(2018, 0, 1, 0, 0, 0, 0));
for (let i = startAmount; i < 500; ++i) {
res.push({
time: time.getTime() / 1000,
value: i,
});

time.setUTCDate(time.getUTCDate() + 1);
}

return res;
}

function runTestCase(container) {
const chartOptions = {
height: 500,
width: 600,
leftPriceScale: {
visible: true,
minimumWidth: 100,
},
rightPriceScale: {
visible: true,
minimumWidth: 150,
},
timeScale: {
minimumHeight: 50,
},
};

const chart = (window.chart = LightweightCharts.createChart(
container,
chartOptions
));

const mainSeries = chart.addLineSeries({});
mainSeries.setData(generateData(0));

const mainSeries2 = chart.addLineSeries({
color: '#000000',
priceScaleId: 'left',
});
mainSeries2.setData(generateData(30));
}

0 comments on commit 6569b3e

Please sign in to comment.