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

Extract markers into plugin #1687

Merged
merged 16 commits into from
Sep 4, 2024
12 changes: 10 additions & 2 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,25 @@ export default [
{
name: 'Plugin: Text Watermark',
path: 'dist/lightweight-charts.production.mjs',
import: '{ TextWatermark }',
import: '{ createTextWatermark }',
ignore: ['fancy-canvas'],
limit: '2.00 KB',
brotli: true,
},
{
name: 'Plugin: Image Watermark',
path: 'dist/lightweight-charts.production.mjs',
import: '{ ImageWatermark }',
import: '{ createImageWatermark }',
ignore: ['fancy-canvas'],
limit: '2.00 KB',
brotli: true,
},
{
name: 'Plugin: Series Markers',
path: 'dist/lightweight-charts.production.mjs',
import: '{ createSeriesMarkers }',
ignore: ['fancy-canvas'],
limit: '4.08 KB',
brotli: true,
},
];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"tslint-eslint-rules": "~5.4.0",
"tslint-microsoft-contrib": "~6.2.0",
"tsx": "~4.16.2",
"typescript": "~5.4.5",
"typescript": "~5.5.4",
"yargs": "~17.7.2"
},
"scripts": {
Expand Down
48 changes: 0 additions & 48 deletions src/api/iseries-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SeriesDataItemTypeMap } from '../model/data-consumer';
import { Time } from '../model/horz-scale-behavior-time/types';
import { MismatchDirection } from '../model/plot-list';
import { CreatePriceLineOptions } from '../model/price-line-options';
import { SeriesMarker } from '../model/series-markers';
import {
SeriesOptionsMap,
SeriesPartialOptionsMap,
Expand Down Expand Up @@ -230,53 +229,6 @@ export interface ISeriesApi<
*/
unsubscribeDataChanged(handler: DataChangedHandler): void;

/**
* Allows to set/replace all existing series markers with new ones.
*
* @param data - An array of series markers. This array should be sorted by time. Several markers with same time are allowed.
* @example
* ```js
* series.setMarkers([
* {
* time: '2019-04-09',
* position: 'aboveBar',
* color: 'black',
* shape: 'arrowDown',
* },
* {
* time: '2019-05-31',
* position: 'belowBar',
* color: 'red',
* shape: 'arrowUp',
* id: 'id3',
* },
* {
* time: '2019-05-31',
* position: 'belowBar',
* color: 'orange',
* shape: 'arrowUp',
* id: 'id4',
* text: 'example',
* size: 2,
* },
* ]);
*
* chart.subscribeCrosshairMove(param => {
* console.log(param.hoveredObjectId);
* });
*
* chart.subscribeClick(param => {
* console.log(param.hoveredObjectId);
* });
* ```
*/
setMarkers(data: SeriesMarker<HorzScaleItem>[]): void;

/**
* Returns an array of series markers.
*/
markers(): SeriesMarker<HorzScaleItem>[];

/**
* Creates a new price line
*
Expand Down
10 changes: 9 additions & 1 deletion src/api/itime-scale-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeepPartial } from '../helpers/strict-type-checks';

import { Coordinate } from '../model/coordinate';
import { IRange, Logical, LogicalRange } from '../model/time-data';
import { IRange, Logical, LogicalRange, TimePointIndex } from '../model/time-data';
import { HorzScaleOptions } from '../model/time-scale';

/**
Expand Down Expand Up @@ -110,6 +110,14 @@ export interface ITimeScaleApi<HorzScaleItem> {
*/
coordinateToLogical(x: number): Logical | null;

/**
* Converts a time to local x coordinate.
*
* @param time - Time needs to be converted
* @returns X coordinate of that time or `null` if no time found on time scale
*/
timeToIndex(time: HorzScaleItem, findNearest?: boolean): TimePointIndex | null;

/**
* Converts a time to local x coordinate.
*
Expand Down
18 changes: 1 addition & 17 deletions src/api/series-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import { BarPrice } from '../model/bar';
import { Coordinate } from '../model/coordinate';
import { DataUpdatesConsumer, SeriesDataItemTypeMap, WhitespaceData } from '../model/data-consumer';
import { checkItemsAreOrdered, checkPriceLineOptions, checkSeriesValuesType } from '../model/data-validators';
import { IHorzScaleBehavior, InternalHorzScaleItem } from '../model/ihorz-scale-behavior';
import { IHorzScaleBehavior } from '../model/ihorz-scale-behavior';
import { ISeriesPrimitiveBase } from '../model/iseries-primitive';
import { Pane } from '../model/pane';
import { MismatchDirection } from '../model/plot-list';
import { CreatePriceLineOptions, PriceLineOptions } from '../model/price-line-options';
import { RangeImpl } from '../model/range-impl';
import { Series } from '../model/series';
import { SeriesPlotRow } from '../model/series-data';
import { convertSeriesMarker, SeriesMarker } from '../model/series-markers';
import {
SeriesOptionsMap,
SeriesPartialOptionsMap,
Expand Down Expand Up @@ -187,21 +186,6 @@ export class SeriesApi<
this._dataChangedDelegate.unsubscribe(handler);
}

public setMarkers(data: SeriesMarker<HorzScaleItem>[]): void {
checkItemsAreOrdered(data, this._horzScaleBehavior, true);

const convertedMarkers = data.map((marker: SeriesMarker<HorzScaleItem>) =>
convertSeriesMarker<HorzScaleItem, InternalHorzScaleItem>(marker, this._horzScaleBehavior.convertHorzItemToInternal(marker.time), marker.time)
);
this._series.setMarkers(convertedMarkers);
}

public markers(): SeriesMarker<HorzScaleItem>[] {
return this._series.markers().map<SeriesMarker<HorzScaleItem>>((internalItem: SeriesMarker<InternalHorzScaleItem>) => {
return convertSeriesMarker<InternalHorzScaleItem, HorzScaleItem>(internalItem, internalItem.originalTime as HorzScaleItem, undefined);
});
}

public applyOptions(options: TPartialOptions): void {
this._series.applyOptions(options);
}
Expand Down
8 changes: 6 additions & 2 deletions src/api/time-scale-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ export class TimeScaleApi<HorzScaleItem> implements ITimeScaleApi<HorzScaleItem>
}
}

public timeToCoordinate(time: HorzScaleItem): Coordinate | null {
public timeToIndex(time: HorzScaleItem, findNearest: boolean): TimePointIndex | null {
const timePoint = this._horzScaleBehavior.convertHorzItemToInternal(time);
const timePointIndex = this._timeScale.timeToIndex(timePoint, false);
return this._timeScale.timeToIndex(timePoint, findNearest);
}

public timeToCoordinate(time: HorzScaleItem): Coordinate | null {
const timePointIndex = this.timeToIndex(time, false);
if (timePointIndex === null) {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export { createChart, createChartEx, defaultHorzScaleBehavior } from './api/crea
/*
Plugins
*/
export { TextWatermark } from './plugins/text-watermark/primitive';
export { ImageWatermark } from './plugins/image-watermark/primitive';
export { createTextWatermark } from './plugins/text-watermark/primitive';
export { createImageWatermark } from './plugins/image-watermark/primitive';
export { createSeriesMarkers } from './plugins/series-markers/wrapper';

/**
* Returns the current version as a string. For example `'3.3.0'`.
Expand Down
7 changes: 2 additions & 5 deletions src/model/autoscale-info-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ export class AutoscaleInfoImpl {
return this._margins;
}

public toRaw(): AutoscaleInfo | null {
if (this._priceRange === null) {
return null;
}
public toRaw(): AutoscaleInfo {
return {
priceRange: this._priceRange.toRaw(),
priceRange: this._priceRange === null ? null : this._priceRange.toRaw(),
SlicedSilver marked this conversation as resolved.
Show resolved Hide resolved
margins: this._margins || undefined,
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/model/data-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { assert } from '../helpers/assertions';
import { isFulfilledData, SeriesDataItemTypeMap } from './data-consumer';
import { IHorzScaleBehavior } from './ihorz-scale-behavior';
import { CreatePriceLineOptions } from './price-line-options';
import { SeriesMarker } from './series-markers';
import { SeriesType } from './series-options';

export function checkPriceLineOptions(options: CreatePriceLineOptions): void {
Expand All @@ -16,7 +15,7 @@ export function checkPriceLineOptions(options: CreatePriceLineOptions): void {
assert(typeof options.price === 'number', `the type of 'price' price line's property must be a number, got '${typeof options.price}'`);
}

export function checkItemsAreOrdered<HorzScaleItem>(data: readonly (SeriesMarker<HorzScaleItem> | SeriesDataItemTypeMap<HorzScaleItem>[SeriesType])[], bh: IHorzScaleBehavior<HorzScaleItem>, allowDuplicates: boolean = false): void {
export function checkItemsAreOrdered<HorzScaleItem>(data: readonly (SeriesDataItemTypeMap<HorzScaleItem>[SeriesType])[], bh: IHorzScaleBehavior<HorzScaleItem>, allowDuplicates: boolean = false): void {
if (process.env.NODE_ENV === 'production') {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/series-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export interface AutoscaleInfo {
/**
* Price range.
*/
priceRange: PriceRange;
priceRange: PriceRange | null;

/**
* Scale margins.
Expand Down
Loading
Loading