Skip to content

Commit

Permalink
Merge pull request #8 from trash-and-fire/support_baseline_series
Browse files Browse the repository at this point in the history
baseline series
  • Loading branch information
trash-and-fire authored Nov 22, 2021
2 parents 4bb38ee + 69b08fd commit 18de408
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"html-webpack-plugin": "5.3.1",
"husky": "4.3.8",
"jest": "27.3.1",
"lightweight-charts": "3.3.0",
"lightweight-charts": "3.7.0",
"lint-staged": "10.5.4",
"lodash": "4.17.21",
"mini-css-extract-plugin": "1.3.4",
Expand Down
37 changes: 33 additions & 4 deletions src/demo/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import HistogramSeries from 'svelte-lightweight-charts/components/histogram-series.svelte';
import BarSeries from 'svelte-lightweight-charts/components/bar-series.svelte';
import CandlestickSeries from 'svelte-lightweight-charts/components/candlestick-series.svelte';
import BaselineSeries from 'svelte-lightweight-charts/components/baseline-series.svelte';
import PriceLine from 'svelte-lightweight-charts/components/price-line.svelte';
type EverySeriesApi =
Expand All @@ -35,11 +36,12 @@
| ISeriesApi<'Histogram'>
| ISeriesApi<'Candlestick'>
| ISeriesApi<'Line'>
| ISeriesApi<'Baseline'>
;
export let reference: Reference<IChartApi> | undefined = undefined;
const SERIES_TYPES: SeriesType[] = ['Area', 'Bar', 'Histogram', 'Candlestick', 'Line'];
const SERIES_TYPES: SeriesType[] = ['Area', 'Bar', 'Histogram', 'Candlestick', 'Line', 'Baseline'];
const lines: PriceLineParams[] = [{
id: 'price',
Expand Down Expand Up @@ -196,15 +198,15 @@
clearTicker();
}
if (daily) {
ticker = setInterval(
ticker = window.setInterval(
() => {
day.setDate(day.getDate() + 1);
day = new Date(day);
},
1000
);
} else {
ticker = setInterval(
ticker = window.setInterval(
() => {
day.setHours(day.getHours() + 6);
day = new Date(day);
Expand Down Expand Up @@ -266,6 +268,22 @@
mainSeries = ref;
},
}
case 'Baseline':
return {
id: 'main',
type,
data: [...LINE_DATA],
options: {
baseValue: {
type: "price",
price: 38,
},
},
priceLines: lines,
reference: (ref: ISeriesApi<'Baseline'> | null) => {
mainSeries = ref;
}
}
default:
throw new Error();
}
Expand Down Expand Up @@ -361,7 +379,7 @@
</fieldset>
<fieldset name="series">
<legend>Main Series type:</legend>
{#each SERIES_TYPES as type (type) }
{#each SERIES_TYPES as type (type)}
<label>
<input type="radio" name="series-type" value={type} bind:group={seriesType}> {type}
</label>
Expand Down Expand Up @@ -452,6 +470,17 @@
{/each}
</CandlestickSeries>
{/if}
{#if mainProps.type === 'Baseline'}
<BaselineSeries
{...(mainProps.options ?? {})}
data={mainProps.data}
ref={handleMainComponentReference}
>
{#each lines as line (line.id)}
<PriceLine {...line.options}/>
{/each}
</BaselineSeries>
{/if}
{#if showVolume}
{#key volumeProps.id}
<HistogramSeries
Expand Down
2 changes: 2 additions & 0 deletions src/package/components/__tests__/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const CHART_API = {
addCandlestickSeries: jest.fn(() => SERIES_API),
addHistogramSeries: jest.fn(() => SERIES_API),
addLineSeries: jest.fn(() => SERIES_API),
addBaselineSeries: jest.fn(() => SERIES_API),
removeSeries: jest.fn(),
}

Expand All @@ -25,6 +26,7 @@ describe.each([
['Candlestick', 'candlestick-series', 'addCandlestickSeries'],
['Histogram', 'histogram-series', 'addHistogramSeries'],
['Line', 'line-series', 'addLineSeries'],
['Baseline', 'baseline-series', 'addBaselineSeries'],
])('%sSeries component', (type: SeriesType, name: string, method: keyof typeof CHART_API) => {
beforeAll(() => {
SERIES_API.seriesType = jest.fn(() => type);
Expand Down
16 changes: 16 additions & 0 deletions src/package/components/baseline-series.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {
BaselineSeriesPartialOptions,
ISeriesApi,
SeriesDataItemTypeMap
} from 'lightweight-charts';
import type {Reference} from '../types';

export interface $$PROPS extends BaselineSeriesPartialOptions {
ref?: Reference<ISeriesApi<'Baseline'>>;
data: SeriesDataItemTypeMap['Baseline'][];
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface $$EVENTS {

}
163 changes: 163 additions & 0 deletions src/package/components/baseline-series.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<svelte:options immutable={true}/>

<script lang="ts">
import type {BaselineSeriesPartialOptions, ISeriesApi} from 'lightweight-charts';
import type {$$PROPS} from './baseline-series.interface';
import type {Reference} from '../types';
import ContextProvider from './internal/context-provider.svelte';
import {useSeriesEffect} from './internal/utils';
/** Visibility of the label with the latest visible price on the price scale */
export let lastValueVisible: $$PROPS['lastValueVisible'] = undefined;
/** Title of the series. This label is placed with price axis label */
export let title: $$PROPS['title'] = undefined;
/** Target price scale to bind new series to */
export let priceScaleId: $$PROPS['priceScaleId'] = undefined;
/** Visibility of series. */
export let visible: $$PROPS['visible'] = undefined;
/** Visibility of the price line. Price line is a horizontal line indicating the last price of the series */
export let priceLineVisible: $$PROPS['priceLineVisible'] = undefined;
/** Enum of possible modes of priceLine source */
export let priceLineSource: $$PROPS['priceLineSource'] = undefined;
/** Width of the price line. Ignored if priceLineVisible is false */
export let priceLineWidth: $$PROPS['priceLineWidth'] = undefined;
/** Color of the price line. Ignored if priceLineVisible is false */
export let priceLineColor: $$PROPS['priceLineColor'] = undefined;
/** Price line style. Suitable for percentage and indexedTo100 scales */
export let priceLineStyle: $$PROPS['priceLineStyle'] = undefined;
/** Formatting settings associated with the series */
export let priceFormat: $$PROPS['priceFormat'] = undefined;
/** Visibility of base line. Suitable for percentage and indexedTo100 scales */
export let baseLineVisible: $$PROPS['baseLineVisible'] = undefined;
/** Color of the base line in IndexedTo100 mode */
export let baseLineColor: $$PROPS['baseLineColor'] = undefined;
/** Base line width. Suitable for percentage and indexedTo100 scales. Ignored if baseLineVisible is not set */
export let baseLineWidth: $$PROPS['baseLineWidth'] = undefined;
/** Base line style. Suitable for percentage and indexedTo100 scales. Ignored if baseLineVisible is not set */
export let baseLineStyle: $$PROPS['baseLineStyle'] = undefined;
/** function that overrides calculating of visible prices range */
export let autoscaleInfoProvider: $$PROPS['autoscaleInfoProvider'] = undefined;
export let scaleMargins: $$PROPS['scaleMargins'] = undefined;
/**
* Base value of the series.
*/
export let baseValue: $$PROPS['baseValue'] = undefined;
/**
* The first color of the top area.
*/
export let topFillColor1: $$PROPS['topFillColor1'] = undefined;
/**
* The second color of the top area.
*/
export let topFillColor2: $$PROPS['topFillColor2'] = undefined;
/**
* The line color of the top area.
*/
export let topLineColor: $$PROPS['topLineColor'] = undefined;
/**
* The first color of the bottom area.
*/
export let bottomFillColor1: $$PROPS['bottomFillColor1'] = undefined;
/**
* The second color of the bottom area.
*/
export let bottomFillColor2: $$PROPS['bottomFillColor2'] = undefined;
/**
* The line color of the bottom area.
*/
export let bottomLineColor: $$PROPS['bottomLineColor'] = undefined;
/**
* Line width.
*/
export let lineWidth: $$PROPS['lineWidth'] = undefined;
/**
* Line style.
*/
export let lineStyle: $$PROPS['lineStyle'] = undefined;
/**
* Show the crosshair marker.
*/
export let crosshairMarkerVisible: $$PROPS['crosshairMarkerVisible'] = undefined;
/**
* Crosshair marker radius in pixels.
*/
export let crosshairMarkerRadius: $$PROPS['crosshairMarkerRadius'] = undefined;
/**
* Crosshair marker border color. An empty string falls back to the the color of the series under the crosshair.
*/
export let crosshairMarkerBorderColor: $$PROPS['crosshairMarkerBorderColor'] = undefined;
/**
* The crosshair marker background color. An empty string falls back to the the color of the series under the crosshair.
*/
export let crosshairMarkerBackgroundColor: $$PROPS['crosshairMarkerBackgroundColor'] = undefined;
/**
* Last price animation mode.
*/
export let lastPriceAnimation: $$PROPS['lastPriceAnimation'] = undefined;
export let ref: $$PROPS['ref'] = undefined;
export let data: $$PROPS['data'] = [];
let options: BaselineSeriesPartialOptions;
$: options = {
lastValueVisible,
title,
priceScaleId,
visible,
priceLineVisible,
priceLineSource,
priceLineWidth,
priceLineColor,
priceLineStyle,
priceFormat,
baseLineVisible,
baseLineColor,
baseLineWidth,
baseLineStyle,
autoscaleInfoProvider,
scaleMargins,
baseValue,
topFillColor1,
topFillColor2,
topLineColor,
bottomFillColor1,
bottomFillColor2,
bottomLineColor,
lineStyle,
lineWidth,
crosshairMarkerBackgroundColor,
crosshairMarkerBorderColor,
crosshairMarkerRadius,
crosshairMarkerVisible,
lastPriceAnimation,
};
let reference: ISeriesApi<'Baseline'> | null = null;
let handleReference: Reference<ISeriesApi<'Baseline'>> | undefined = undefined;
$: handleReference = ((ref?: Reference<ISeriesApi<'Baseline'>>) => (series: ISeriesApi<'Baseline'> | null) => {
reference = series;
if (ref !== undefined) {
ref(series);
}
})(ref);
const id = performance.now().toString();
useSeriesEffect(() => [
{
id,
type: 'Baseline',
options,
data,
},
handleReference,
]);
</script>
{#if reference !== null}
<ContextProvider value={reference}>
<slot/>
</ContextProvider>
{/if}
5 changes: 5 additions & 0 deletions src/package/internal/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@ function createSeries<T extends SeriesActionParams>(
series.setData(params.data);
return series;
}
case 'Baseline': {
const series = chart.addBaselineSeries(params.options);
series.setData(params.data);
return series;
}
}
}
11 changes: 11 additions & 0 deletions src/package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,30 @@ export interface LineSeriesParams {
priceLines?: PriceLineParams[];
}

export type BaselineSeriesParams = 'Baseline' extends SeriesType ? {
id: string;
type: 'Baseline';
options?: SeriesPartialOptionsMap['Baseline'];
data: SeriesDataItemTypeMap['Baseline'][];
reference?: Reference<ISeriesApi<'Baseline'>>;
priceLines?: PriceLineParams[];
} : never;

export type SeriesActionParams =
| AreaSeriesParams
| BarSeriesParams
| CandlestickSeriesParams
| HistogramSeriesParams
| LineSeriesParams
| BaselineSeriesParams

export interface SeriesParamsMap extends Record<SeriesType, unknown> {
Area: AreaSeriesParams;
Bar: BarSeriesParams;
Candlestick: CandlestickSeriesParams;
Histogram: HistogramSeriesParams;
Line: LineSeriesParams;
Baseline: BaselineSeriesParams;
}

export type Reference<T> = (ref: T | null) => void;

0 comments on commit 18de408

Please sign in to comment.