Skip to content

Commit

Permalink
Merge pull request #791 from olegchernenko/master
Browse files Browse the repository at this point in the history
  • Loading branch information
timocov authored Jul 19, 2021
2 parents ec91034 + 352166a commit 6e8927d
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 45 deletions.
13 changes: 2 additions & 11 deletions src/api/iseries-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IPriceFormatter } from '../formatters/iprice-formatter';

import { BarPrice } from '../model/bar';
import { Coordinate } from '../model/coordinate';
import { PriceLineOptions } from '../model/price-line-options';
Expand All @@ -22,17 +24,6 @@ export type BarsInfo =
barsAfter: number;
};

/** Interface to be implemented by the object in order to be used as a price formatter */
export interface IPriceFormatter {
/**
* Formatting function
*
* @param price - original price to be formatted
* @returns formatted price
*/
format(price: BarPrice): string;
}

export interface ISeriesApi<TSeriesType extends SeriesType> {
/**
* Returns current price formatter
Expand Down
4 changes: 3 additions & 1 deletion src/api/series-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IPriceFormatter } from '../formatters/iprice-formatter';

import { ensureNotNull } from '../helpers/assertions';
import { clone, merge } from '../helpers/strict-type-checks';

Expand All @@ -22,7 +24,7 @@ import { convertTime } from './data-layer';
import { checkItemsAreOrdered, checkPriceLineOptions, checkSeriesValuesType } from './data-validators';
import { IPriceLine } from './iprice-line';
import { IPriceScaleApi } from './iprice-scale-api';
import { BarsInfo, IPriceFormatter, ISeriesApi } from './iseries-api';
import { BarsInfo, ISeriesApi } from './iseries-api';
import { priceLineOptionsDefaults } from './options/price-line-options-defaults';
import { PriceLine } from './price-line-api';

Expand Down
3 changes: 1 addition & 2 deletions src/formatters/date-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { formatDate } from './format-date';
import { IFormatter } from './iformatter';

export class DateFormatter implements IFormatter {
export class DateFormatter {
private readonly _locale: string;
private readonly _dateFormat: string;

Expand Down
3 changes: 1 addition & 2 deletions src/formatters/date-time-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DateFormatter } from './date-formatter';
import { IFormatter } from './iformatter';
import { TimeFormatter } from './time-formatter';

export interface DateTimeFormatterParams {
Expand All @@ -16,7 +15,7 @@ const defaultParams: DateTimeFormatterParams = {
locale: 'default',
};

export class DateTimeFormatter implements IFormatter {
export class DateTimeFormatter {
private readonly _dateFormatter: DateFormatter;
private readonly _timeFormatter: TimeFormatter;
private readonly _separator: string;
Expand Down
4 changes: 0 additions & 4 deletions src/formatters/iformatter.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/formatters/iprice-formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** Interface to be implemented by the object in order to be used as a price formatter */
export interface IPriceFormatter {
/**
* Formatting function
*
* @param price - original price to be formatted
* @returns formatted price
*/
format(price: number): string;
}
4 changes: 2 additions & 2 deletions src/formatters/price-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isInteger, isNumber } from '../helpers/strict-type-checks';

import { IFormatter } from './iformatter';
import { IPriceFormatter } from './iprice-formatter';

const formatterOptions = {
decimalSign: '.',
Expand Down Expand Up @@ -29,7 +29,7 @@ export function numberToStringWithLeadingZero(value: number, length: number): st
return (dummyString + value.toString()).slice(-length);
}

export class PriceFormatter implements IFormatter {
export class PriceFormatter implements IPriceFormatter {
protected _fractionalLength: number | undefined;
private readonly _priceScale: number;
private readonly _minMove: number;
Expand Down
3 changes: 1 addition & 2 deletions src/formatters/time-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IFormatter } from './iformatter';
import { numberToStringWithLeadingZero } from './price-formatter';

export class TimeFormatter implements IFormatter {
export class TimeFormatter {
private _formatStr: string;

public constructor(format?: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/volume-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IFormatter } from './iformatter';
import { IPriceFormatter } from './iprice-formatter';

export class VolumeFormatter implements IFormatter {
export class VolumeFormatter implements IPriceFormatter {
private readonly _precision: number;

public constructor(precision: number) {
Expand Down
4 changes: 2 additions & 2 deletions src/model/iprice-data-source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFormatter } from '../formatters/iformatter';
import { IPriceFormatter } from '../formatters/iprice-formatter';

import { AutoscaleInfoImpl } from './autoscale-info-impl';
import { ChartModel } from './chart-model';
Expand All @@ -12,7 +12,7 @@ export interface FirstValue {

export interface IPriceDataSource extends IDataSource {
firstValue(): FirstValue | null;
formatter(): IFormatter;
formatter(): IPriceFormatter;
priceLineColor(lastBarColor: string): string;
model(): ChartModel;
minMove(): number;
Expand Down
4 changes: 2 additions & 2 deletions src/model/price-data-source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFormatter } from '../formatters/iformatter';
import { IPriceFormatter } from '../formatters/iprice-formatter';

import { AutoscaleInfoImpl } from './autoscale-info-impl';
import { ChartModel } from './chart-model';
Expand All @@ -23,6 +23,6 @@ export abstract class PriceDataSource extends DataSource implements IPriceDataSo
public abstract autoscaleInfo(startTimePoint: TimePointIndex, endTimePoint: TimePointIndex): AutoscaleInfoImpl | null;

public abstract firstValue(): FirstValue | null;
public abstract formatter(): IFormatter;
public abstract formatter(): IPriceFormatter;
public abstract priceLineColor(lastBarColor: string): string;
}
15 changes: 7 additions & 8 deletions src/model/price-scale-conversions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { log10 } from '../helpers/mathex';

import { Coordinate } from './coordinate';
import { PriceRangeImpl } from './price-range-impl';

const enum Constants {
Expand All @@ -16,9 +15,9 @@ export function fromPercent(value: number, baseValue: number): number {
return (value / 100) * baseValue + baseValue;
}

export function toPercent(value: number, baseValue: number): Coordinate {
export function toPercent(value: number, baseValue: number): number {
const result = 100 * (value - baseValue) / baseValue;
return (baseValue < 0 ? -result : result) as Coordinate;
return (baseValue < 0 ? -result : result);
}

export function toPercentRange(priceRange: PriceRangeImpl, baseValue: number): PriceRangeImpl {
Expand All @@ -36,9 +35,9 @@ export function fromIndexedTo100(value: number, baseValue: number): number {
return (value / 100) * baseValue + baseValue;
}

export function toIndexedTo100(value: number, baseValue: number): Coordinate {
export function toIndexedTo100(value: number, baseValue: number): number {
const result = 100 * (value - baseValue) / baseValue + 100;
return (baseValue < 0 ? -result : result) as Coordinate;
return (baseValue < 0 ? -result : result);
}

export function toIndexedTo100Range(priceRange: PriceRangeImpl, baseValue: number): PriceRangeImpl {
Expand All @@ -47,14 +46,14 @@ export function toIndexedTo100Range(priceRange: PriceRangeImpl, baseValue: numbe
return new PriceRangeImpl(minPercent, maxPercent);
}

export function toLog(price: number): Coordinate {
export function toLog(price: number): number {
const m = Math.abs(price);
if (m < 1e-8) {
return 0 as Coordinate;
return 0;
}

const res = log10(m + Constants.CoordOffset) + Constants.LogicalOffset;
return ((price < 0) ? -res : res) as Coordinate;
return ((price < 0) ? -res : res);
}

export function fromLog(logical: number): number {
Expand Down
8 changes: 4 additions & 4 deletions src/model/price-scale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFormatter } from '../formatters/iformatter';
import { IPriceFormatter } from '../formatters/iprice-formatter';
import { PercentageFormatter } from '../formatters/percentage-formatter';
import { PriceFormatter } from '../formatters/price-formatter';

Expand Down Expand Up @@ -140,7 +140,7 @@ export class PriceScale {

private _scaleStartPoint: number | null = null;
private _scrollStartPoint: number | null = null;
private _formatter: IFormatter = defaultPriceFormatter;
private _formatter: IPriceFormatter = defaultPriceFormatter;

public constructor(id: string, options: PriceScaleOptions, layoutOptions: LayoutOptions, localizationOptions: LocalizationOptions) {
this._id = id;
Expand Down Expand Up @@ -636,7 +636,7 @@ export class PriceScale {
this._priceRangeSnapshot = null;
}

public formatter(): IFormatter {
public formatter(): IPriceFormatter {
if (!this._formatter) {
this.updateFormatter();
}
Expand Down Expand Up @@ -884,7 +884,7 @@ export class PriceScale {
return null;
}

private _formatPrice(price: BarPrice, fallbackFormatter?: IFormatter): string {
private _formatPrice(price: BarPrice, fallbackFormatter?: IPriceFormatter): string {
if (this._localizationOptions.priceFormatter === undefined) {
if (fallbackFormatter === undefined) {
fallbackFormatter = this.formatter();
Expand Down
6 changes: 3 additions & 3 deletions src/model/series.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFormatter } from '../formatters/iformatter';
import { IPriceFormatter } from '../formatters/iprice-formatter';
import { PercentageFormatter } from '../formatters/percentage-formatter';
import { PriceFormatter } from '../formatters/price-formatter';
import { VolumeFormatter } from '../formatters/volume-formatter';
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Series<T extends SeriesType = SeriesType> extends PriceDataSource i
private _data: SeriesPlotList<T> = createSeriesPlotList();
private readonly _priceAxisViews: IPriceAxisView[];
private readonly _panePriceAxisView: PanePriceAxisView;
private _formatter!: IFormatter;
private _formatter!: IPriceFormatter;
private readonly _priceLineView: SeriesPriceLinePaneView = new SeriesPriceLinePaneView(this);
private readonly _customPriceLines: CustomPriceLine[] = [];
private readonly _baseHorizontalLineView: SeriesHorizontalBaseLinePaneView = new SeriesHorizontalBaseLinePaneView(this);
Expand Down Expand Up @@ -395,7 +395,7 @@ export class Series<T extends SeriesType = SeriesType> extends PriceDataSource i
return this._options.priceFormat.minMove;
}

public formatter(): IFormatter {
public formatter(): IPriceFormatter {
return this._formatter;
}

Expand Down
54 changes: 54 additions & 0 deletions tests/e2e/graphics/test-cases/series/series-price-formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 = LightweightCharts.createChart(container);

const lineSeries = chart.addLineSeries({
priceFormat: {
type: 'price',
precision: 4,
minMove: 0.005,
},
});
lineSeries.setData(generateData());
console.assert(lineSeries.priceFormatter().format(12.1) === '12.1000', 'Wrong format');

lineSeries.applyOptions({
priceFormat: {
type: 'volume',
precision: 5,
minMove: 0.05,
},
});
console.assert(lineSeries.priceFormatter().format(1000) === '1K', 'Wrong format');

lineSeries.applyOptions({
priceFormat: {
type: 'percent',
precision: 3,
minMove: 0.05,
},
});
console.assert(lineSeries.priceFormatter().format(12.1) === '12.0%', 'Wrong format');

lineSeries.applyOptions({
priceFormat: {
type: 'custom',
minMove: 0.02,
formatter: price => 'price=' + price.toFixed(2),
},
});
console.assert(lineSeries.priceFormatter().format(12.1) === 'price=12.10', 'Wrong format');
}

0 comments on commit 6e8927d

Please sign in to comment.