Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method priceLines in ISeriesAPI #1739

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/api/iseries-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ export interface ISeriesApi<
*/
removePriceLine(line: IPriceLine): void;

/**
* Returns an array of price lines.
*/
priceLines(): IPriceLine[];

/**
* Return current series type.
*
Expand Down
9 changes: 7 additions & 2 deletions src/api/series-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { clone, merge } from '../helpers/strict-type-checks';

import { BarPrice } from '../model/bar';
import { Coordinate } from '../model/coordinate';
import { CustomPriceLine } from '../model/custom-price-line';
import { DataUpdatesConsumer, SeriesDataItemTypeMap, WhitespaceData } from '../model/data-consumer';
import { checkItemsAreOrdered, checkPriceLineOptions, checkSeriesValuesType } from '../model/data-validators';
import { IHorzScaleBehavior } from '../model/ihorz-scale-behavior';
Expand Down Expand Up @@ -43,8 +44,8 @@ export class SeriesApi<
TOptions extends SeriesOptionsMap[TSeriesType] = SeriesOptionsMap[TSeriesType],
TPartialOptions extends SeriesPartialOptionsMap[TSeriesType] = SeriesPartialOptionsMap[TSeriesType]
> implements
ISeriesApi<TSeriesType, HorzScaleItem, TData, TOptions, TPartialOptions>,
IDestroyable {
ISeriesApi<TSeriesType, HorzScaleItem, TData, TOptions, TPartialOptions>,
IDestroyable {
signatenkov marked this conversation as resolved.
Show resolved Hide resolved
protected _series: Series<TSeriesType>;
protected _dataUpdatesConsumer: DataUpdatesConsumer<TSeriesType, HorzScaleItem>;
protected readonly _chartApi: IChartApiBase<HorzScaleItem>;
Expand Down Expand Up @@ -210,6 +211,10 @@ export class SeriesApi<
this._series.removePriceLine((line as PriceLine).priceLine());
}

public priceLines(): IPriceLine[] {
return this._series.priceLines().map((priceLine: CustomPriceLine): IPriceLine => new PriceLine(priceLine));
}

public seriesType(): TSeriesType {
return this._series.seriesType();
}
Expand Down
4 changes: 4 additions & 0 deletions src/model/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ export class Series<T extends SeriesType> extends PriceDataSource implements IDe
this.model().updateSource(this);
}

public priceLines(): CustomPriceLine[] {
return this._customPriceLines;
}

public seriesType(): T {
return this._seriesType;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/e2e/graphics/test-cases/series/price-lines-getter.js
signatenkov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function generateData() {
const res = [];
const time = new Date(Date.UTC(2018, 0, 1, 0, 0, 0, 0));
for (let i = 0; i < 60; ++i) {
res.push({
time: time.getTime() / 1000,
value: i,
});

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

function runTestCase(container) {
const chart = window.chart = LightweightCharts.createChart(container, { layout: { attributionLogo: false } });

const series = chart.addSeries(LightweightCharts.LineSeries);
series.setData(generateData());

series.createPriceLine({ price: 10 });
series.createPriceLine({ price: 20 });
series.createPriceLine({ price: 30 });

return new Promise(resolve => {
setTimeout(() => {
const priceLines = series.priceLines();
const [line1, line2] = priceLines;

// remove line 3 from array, however line3 should still be visible on the chart
priceLines.splice(2, 1);

// still check it can be removed
series.removePriceLine(line1);

// modify line 2 returned from the getter
line2.applyOptions({
price: 15,
color: 'red',
});
resolve();
}, 1000);
});
}
Loading