diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a4dada..d944876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- **Time-varying reference lines.** `ReferenceLine.series` draws a historical + `LiveChartPoint[]` benchmark over line or candle charts, with the existing + color, dash, width, label, and range-fitting behavior. The final value extends + flat to the live edge by default; `extendToNow: false` stops at the last point. - **Custom threshold badges.** `LiveChart.renderThresholdBadge` replaces the built-in Skia threshold pill with any React Native element while the chart continues to position it from the live threshold value on the UI thread. Its diff --git a/app/demo/candlestick.tsx b/app/demo/candlestick.tsx index 3dc01e5..70a9a75 100644 --- a/app/demo/candlestick.tsx +++ b/app/demo/candlestick.tsx @@ -1,6 +1,10 @@ import { useState } from "react"; -import type { CandlePoint } from "react-native-livechart"; -import { LiveChart } from "react-native-livechart"; +import { + LiveChart, + type CandlePoint, + type LiveChartPoint, + type ReferenceLine, +} from "react-native-livechart"; import { useSharedValue } from "react-native-reanimated"; import { DemoScreen } from "../../demo-lib/DemoScreen"; @@ -75,6 +79,24 @@ const VOLUME_ROUND_OPTIONS: { value: number; label: string }[] = [ /** Distinct violet bars so the volume color override reads as a feature. */ const CUSTOM_VOLUME_COLORS = { upColor: "#8b5cf6", downColor: "#4c1d95" }; +/** + * Historical average-cost changes from a realistic averaging-in flow. Adjacent + * points around each fill make the cost basis step at that timestamp instead of + * drifting diagonally between fills. + */ +function buildAverageCostSeries(anchor: number): LiveChartPoint[] { + return [ + { time: anchor - 3_600, value: 100.8 }, + { time: anchor - 780.01, value: 100.8 }, + { time: anchor - 780, value: 99.6 }, + { time: anchor - 360.01, value: 99.6 }, + { time: anchor - 360, value: 101.2 }, + { time: anchor - 90.01, value: 101.2 }, + { time: anchor - 90, value: 100.4 }, + { time: anchor - 30, value: 100.4 }, + ]; +} + export default function CandlestickScreen() { const [tfLabel, setTfLabel] = useState("15m · 1m"); const [stripCandles, setStripCandles] = useState(false); @@ -86,6 +108,9 @@ export default function CandlestickScreen() { const [volumeRound, setVolumeRound] = useState(2); const [customVolumeColors, setCustomVolumeColors] = useState(false); const [snapToCandles, setSnapToCandles] = useState(false); + const [averageCost, setAverageCost] = useState(true); + const [extendAverageCost, setExtendAverageCost] = useState(true); + const [referenceAnchor] = useState(() => Date.now() / 1000); const tf = TIMEFRAMES.find((t) => t.label === tfLabel) ?? TIMEFRAMES[1]; const candleWidthSecs = tf.candleWidthSecs; @@ -111,11 +136,28 @@ export default function CandlestickScreen() { maxPoints: 10000, }); + const referenceLines: ReferenceLine[] = averageCost + ? [ + { + id: "average-cost", + series: buildAverageCostSeries(referenceAnchor), + extendToNow: extendAverageCost, + label: "Avg cost", + showValue: true, + labelPosition: "right", + color: "#f59e0b", + labelColor: "#b45309", + strokeWidth: 2, + intervals: [6, 4], + }, + ] + : []; + return ( } > + + + {averageCost ? ( + + ) : null} + + - Reference lines / bands (horizontal line, value band, or time band). Form-A lines - can be `draggable` (drag to set a price, with `snap` / `bounds` and `onChange` / + Reference lines / bands (horizontal line, time-varying `series`, value band, or + time band). Series lines work in both line and candle mode and extend their last + value to the live edge unless `extendToNow` is `false`. Form-A lines can be + `draggable` (drag to set a price, with `snap` / `bounds` and `onChange` / `onCommit` / `onDragIn` / `onDragOut` callbacks). See [Reference lines](/guides/reference-lines-and-bands). diff --git a/docs/api-reference/types.mdx b/docs/api-reference/types.mdx index 6fa0231..200268f 100644 --- a/docs/api-reference/types.mdx +++ b/docs/api-reference/types.mdx @@ -179,10 +179,13 @@ interface ReferenceLine { id?: string; // stable identity; supply when lines may reorder // Form A — horizontal line: value?: number; - // Form B — horizontal band: + // Form B — time-varying line (sorted unix-second points): + series?: LiveChartPoint[]; + extendToNow?: boolean; // flat-extend the last value to live; default true + // Form C — horizontal band: valueFrom?: number; valueTo?: number; - // Form C — vertical time band (unix seconds): + // Form D — vertical time band (unix seconds): from?: number; to?: number; // Shared: @@ -192,8 +195,8 @@ interface ReferenceLine { color?: string; fillColor?: string; // band fill; defaults to color, then palette refLine fillOpacity?: number; // default 0.16 - strokeOpacity?: number; // line / band-border opacity; default 1 - fullWidth?: boolean; // span edge-to-edge through the Y-axis gutter (Form A/B); + strokeOpacity?: number; // line / series / band-border opacity; default 1 + fullWidth?: boolean; // span edge-to-edge through the Y-axis gutter (Form A/C); // label/badge stay in the plot. default false labelColor?: string; labelPosition?: "left" | "center" | "right"; diff --git a/docs/guides/candlestick.mdx b/docs/guides/candlestick.mdx index 3415d8c..e29ca79 100644 --- a/docs/guides/candlestick.mdx +++ b/docs/guides/candlestick.mdx @@ -141,6 +141,31 @@ Configure colors, band height, rounding, and opacity with a by candle direction (up/down) unless you override `upColor` / `downColor`. +## Historical reference series + +Overlay a changing average cost, VWAP, peg, or other historical benchmark with the +[`referenceLines.series`](/guides/reference-lines-and-bands#time-varying-reference-line-series) +form. It draws a Skia polyline over the candles using the same time and Y scales, supports the +normal reference-line color, dash, width, and label fields, and extends its last value flat to +the live edge by default. + +```tsx + +``` + ## Scrubbing & the OHLC tooltip Pass `scrub` to enable the crosshair: the built-in tooltip shows the scrubbed candle's O/H/L/C diff --git a/docs/guides/reference-lines-and-bands.mdx b/docs/guides/reference-lines-and-bands.mdx index 174a01e..41900aa 100644 --- a/docs/guides/reference-lines-and-bands.mdx +++ b/docs/guides/reference-lines-and-bands.mdx @@ -1,7 +1,7 @@ --- title: Reference lines & bands icon: "ruler-horizontal" -description: "Draw horizontal lines, value/time bands, pill badges, and draggable working orders." +description: "Draw horizontal or time-varying lines, value/time bands, pill badges, and draggable working orders." --- @@ -16,9 +16,10 @@ description: "Draw horizontal lines, value/time bands, pill badges, and draggabl