lightweight-charts-react-components Skill
lightweight-charts-react-components 在 React 中使用 TradingView lightweight-charts 的配方。蜡烛图、折线图和面积图;成交量和指标窗格;实时 WebSocket 更新;滚动加载无限历史;十字线 OHLC 图例和提示;交易标记和价格线;按百分比收益对比多个标的;图表同步;数值选项和收益率曲线坐标轴。在 React 应用中添加或修复金融/交易图表时,或用户提到 lightweight-charts、React 中的 TradingView 图表或该包时使用。
安装方式:把技能目录放入 ~/.claude/skills/(Claude Code)或在 claude.ai 设置中启用;也可复制右侧安装命令一键添加。
技能指令原文(SKILL.md)
lightweight-charts-react-components guide
Quick Start
Let the components manage chart and series creation and cleanup. Import components
and ref types from lightweight-charts-react-components; import native data, option,
and event types from lightweight-charts.
- Check versions against the compatibility table below. Read declared ranges
from the app's package.json, then the installed versions from the version
field of node_modules//package.json for
lightweight-charts-react-components, lightweight-charts, react, and
react-dom. If the library is not installed, follow
Installation. Ensure chart rendering runs in the browser,
within the app's client boundary.
- For a basic static chart, start from the minimal chart below. Otherwise, read the
reference matching the requested behavior. Adapt its feed and UI; preserve its
data contract, component nesting, and API readiness handling.
- Choose one data owner: pass the complete dataset through
datafor React-owned
updates, or use reactive={false} and the series API for frequent ticks.
Installation
Install the wrapper together with its lightweight-charts peer dependency, using
the project's package manager. React and React DOM must already be installed.
npm install lightweight-charts-react-components lightweight-charts
Minimal chart
import type { LineData, UTCTimestamp } from "lightweight-charts";
import {
Chart,
LineSeries,
TimeScale,
TimeScaleFitContentTrigger,
} from "lightweight-charts-react-components";
// Data must be sorted ascending by time with unique times (UTC seconds here).
export function SimpleLineChart({ data }: { data: LineData<UTCTimestamp>[] }) {
return (
// autoSize follows the container, which needs a nonzero height.
<Chart
options={{ autoSize: true }}
containerProps={{ style: { height: 300, width: "100%" } }}
>
<LineSeries data={data} />
<TimeScale>
<TimeScaleFitContentTrigger deps={[]} />
</TimeScale>
</Chart>
);
}
Examples and snippets (reference files)
Read the reference matching the user's outcome. Each contains a complete TypeScript
recipe, data and integration contracts, reasons for the architecture, failure modes,
and acceptance checks. Combine references when a task spans several outcomes; do
not load every reference for a simple chart.
| User request | Reference |
| ------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
| WebSocket candles, updating the current bar, stale history, excessive chart renders | Market data and live updates |
| Older history on scroll on one series, viewport jumps when prepending | History and range selection |
| Several instruments with older history on scroll and a tooltip (read only this file) | Multi-series history |
| Symbol/interval changes, late API responses, preset viewing windows | Instrument switching |
| Candles plus volume, moving averages, oscillator panes, independent price scales | Price, volume, and indicators |
| OHLC legend, hover details, missing data, tooltip positioning | Hover legends and tooltips |
| Executions and signals, stop/target levels, click-to-place price lines | Trade markers and price levels |
| Compare assets with different prices, fixed-baseline returns, toggle comparisons | Performance comparison |
| Separate charts that share scrolling, zooming, and a crosshair | Synchronized charts |
| Trading-session shading, pane backgrounds, custom drawing that follows zoom | Session overlays and primitives |
| Options strikes, yield maturities, choosing a numeric or custom horizontal axis | Numeric axes and chart types |
If no reference matches the request, tell the user that the skill has no recipe
for it. Then build the solution from the public components and the upstream
lightweight-charts API, applying the Quick Start rules and the edge cases below.
Skill compatibility
The recipes are verified against the reference versions below. These are the
tested baseline, not the latest available releases.
If an installed version differs from the reference version, compare it with the
compatibility range. Inside the range, recipes apply as written. Outside it,
tell the user about the mismatch before writing code, because breaking API
changes can affect the recipes, and verify every API the recipe uses against
the installed package.
| Dependency or environment | Reference version | Compatibility range and checks |
| ------------------------------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| lightweight-charts-react-components | 2.6.1 | Recipe target: >=2.6.1 <3. Other versions require checking installed exports, chart API refs, and reactive data behavior before adapting a recipe. |
| lightweight-charts | 5.3.0 | Declared peer: >=5.0.8 <6. |
| React and React DOM | 19.2.6 | Declared peers: >=18.2 <20. Keep react and react-dom on matching versions and use the application's existing React instance. |
| TypeScript | 5.9.3 | Recipes typecheck in strict mode on this version. Other versions, including 7.x, are unverified: run the app's own typecheck on the adapted recipe. |
| Node.js | — | Package engines: >=18.14.0. The app's build tools can require a newer version. |
| Module system | ESM | ESM import entry only; there is no CommonJS require entry. |
| Rendering | Browser | Charts need the DOM and canvas. Render them on the client, never during server rendering. |
Common edge cases
- Chart sizing: Put DOM styles on
containerPropsand give the chart a
nonzero height.
- Required ancestors: Headless chart children need a chart wrapper ancestor.
Markers, PriceLine, and SeriesPrimitive belong inside their series;
PriceScale, Watermark, and PanePrimitive require a Pane;
TimeScaleFitContentTrigger requires a TimeScale.
- API refs and readiness: Use
ref.current?.api()for the native API and
containerRef for the div. For Chart, use ChartApiRef.
.api() can initially return null; follow the recipe's readiness step before
imperative setup.
- Viewport resets: To preserve scroll/zoom, keep the chart's key stable and
live/history updates out of fit-trigger dependencies. Fit on initial load or a
deliberate reset. See range handling.
- Data updates: Use
datafor React-owned updates, orreactive={false}and the
series API when per-tick React renders are measurably costly. Avoid mixing both.
Reactive data is always the complete dataset. The wrapper calls update() only
when every earlier item keeps the same object reference and the new array either
replaces the last item with one of the same time or appends exactly one item
with a new time. Anything else (including alwaysReplaceData) calls setData().
To keep the incremental path, copy data once at the boundary and never clone or
mutate earlier items. See live updates.