How tradingview/lightweight-charts works
Purpose and distribution
This is the TypeScript implementation of TradingView Lightweight Charts: a browser-side HTML5 canvas library for interactive financial charts. It ships npm ES modules and standalone IIFE bundles exposing window.LightweightCharts; Rollup is the main library bundler. Public entry points are src/index.ts (module exports) and src/standalone.ts (global build), with build definition in rollup.config.js.
Public API boundary
Consumers create charts and manipulate typed chart, series, pane, price-scale, and time-scale APIs. API façade classes isolate public contracts from internal mutable model and GUI state.
• src/api/create-chart.ts
• src/api/chart-api.ts
• src/api/series-api.ts
Specialized creation paths support options charts and yield-curve charts:
• src/api/create-options-chart.ts
• src/api/create-yield-curve-chart.ts
• src/api/yield-chart-api.ts
Data ingestion and chart model
Series setData/updates flow through the data layer, which validates and converts user time/value/OHLC input into internal plot rows and ordered data structures. ChartModel owns panes, series, scales, crosshair state, invalidation, and redraw coordination. The time-based horizontal-scale behavior establishes time points/ticks; a separate yield-curve behavior supports non-time horizontal domains.
• src/model/data-layer.ts
• src/model/data-consumer.ts
• src/model/chart-model.ts
• src/model/horz-scale-behavior-time/horz-scale-behavior-time.ts
• src/model/yield-curve-horz-scale-behavior/yield-curve-horz-scale-behavior.ts
Scales, panes, and interaction
The model maps logical data to time and price coordinates, computes visible ranges/autoscaling, and partitions series among panes and price scales. Pointer/touch events are captured by GUI widgets, translated to model operations (scroll, zoom, crosshair, hit testing), then surfaced through API subscriptions.
• src/model/time-scale.ts
• src/model/price-scale.ts
• src/model/pane.ts
• src/model/crosshair.ts
• src/gui/mouse-event-handler.ts
View-to-canvas rendering pipeline
Model entities expose pane/axis views; views generate rendering data using current scale coordinates. Pane, price-axis, and time-axis widgets own DOM/canvas layout and invoke dedicated Canvas renderers for grid, series, crosshair, labels, and overlays. Invalidations from model/data/interaction drive redraws rather than a server-rendered UI cycle.
• src/gui/chart-widget.ts
• src/gui/pane-widget.ts
• src/views/pane/ipane-view.ts
• src/renderers/ipane-renderer.ts
• src/renderers/candlesticks-renderer.ts
Series and extensibility
Built-in line, area, baseline, bar, candlestick, histogram, and custom-series implementations share the model/renderer pipeline. Plugin adapters and primitive wrappers form the extension boundary: third parties can attach series or pane primitives, contribute views/renderers, autoscale information, axis labels, and hit tests without changing core model internals.
• src/model/series/series-def.ts
• src/model/series/custom-series.ts
• src/model/series-primitive-wrapper.ts
• src/plugins/series-primitive-adapter.ts
• src/plugins/primitive-wrapper.ts
Workspace packages and ecosystem
pnpm workspace packages publish reusable plugins, a plugin toolkit, and a scaffolding CLI. lwc-toolkit centralizes plugin base classes, custom-series rendering/stacking, canvas and dimension utilities. create-lwc-plugin generates plugin projects from series/pane templates. Official packages are independent extensions with their own compile scripts and browser examples.
• pnpm-workspace.yaml
• packages/lwc-toolkit/src/plugin-base.ts
• packages/create-lwc-plugin/src/index.ts
• packages/lwc-plugin-accessibility/src/accessibility.ts
Documentation, demos, and delivery infrastructure
The website is a Docusaurus documentation site, including a generated plugin catalogue and embedded examples. Standalone plugin and indicator demo sites use Vite. CI is split between CircleCI and GitHub Actions; automation builds, validates metadata/compatibility, packages plugins, deploys, and checks bundle size. Browser-level visual, interaction, coverage, and memory-leak runners use Puppeteer-oriented test infrastructure.
• website/docusaurus.config.js
• scripts/plugins/catalogue.mjs
• .circleci/config.yml
• .github/workflows/plugins.yml
• tests/e2e/runner.ts
Main entry points: chart-model.ts Chart model · chart-widget.ts Chart widget · chart-api.ts Chart API façade · pnpm-workspace.yaml pnpm workspace
Components 22
Distribution
- Rollup builds
rollup.config.js - ES module entry
src/index.ts - Standalone entry · Produces window.LightweightCharts.
src/standalone.ts
Public API
- Chart creation
src/api/create-chart.ts - Chart API façade · Isolates public contracts from mutable chart state.
src/api/chart-api.ts - Series API façade
src/api/series-api.ts - Options & yield APIs
src/api/create-options-chart.ts - Yield chart API
src/api/yield-chart-api.ts
Chart Core
- Data layer · Validates input and creates ordered internal plot rows.
src/model/data-layer.ts - Chart model · Owns panes, series, scales, crosshair, invalidation, and redraw coordination.
src/model/chart-model.ts - Horizontal-scale behaviors · Supports time-based and yield-curve domains.
src/model/horz-scale-behavior-time/horz-scale-behavior-time.ts - Scales, panes & crosshair · Maps data to coordinates and visible ranges.
src/model/time-scale.ts - Series implementations · Built-in and custom series share the model/rendering pipeline.
src/model/series/series-def.ts - Primitive extension boundary · Lets plugins contribute views, renderers, autoscale data, labels, and hit tests.
src/plugins/series-primitive-adapter.ts
GUI & Rendering
- Chart widget · Owns chart DOM/canvas layout and redraw execution.
src/gui/chart-widget.ts - Pointer/touch input
src/gui/mouse-event-handler.ts - Pane views · Generate render data from current scale coordinates.
src/views/pane/ipane-view.ts - Canvas renderers · Draw grid, series, crosshair, labels, and overlays.
src/renderers/candlesticks-renderer.ts
Workspace & Delivery
- pnpm workspace
pnpm-workspace.yaml - Plugin toolkit · Provides plugin bases and custom-series rendering utilities.
packages/lwc-toolkit/src/plugin-base.ts - Plugin scaffolder · Generates series and pane plugin projects.
packages/create-lwc-plugin/src/index.ts - Docs, catalogue & CI · Publishes docs/plugins and validates compatibility and bundles.
website/docusaurus.config.js
Connections 25
- Rollup builds → ES module entry: bundles
- Rollup builds → Standalone entry: bundles
- ES module entry → Chart creation: exports
- Standalone entry → Chart creation: exposes globally
- Chart creation → Chart API façade: creates
- Chart API façade → Series API façade: manages
- Options & yield APIs → Yield chart API: creates
- Chart API façade → Chart model: controls
- Series API façade → Data layer: setData / updates
- Data layer → Chart model: updates series state
- Chart model → Horizontal-scale behaviors: uses domain behavior
- Chart model → Scales, panes & crosshair: coordinates
- Chart model → Series implementations: owns
- Primitive extension boundary → Series implementations: attaches primitives
- Chart model → Chart widget: invalidates
- Pointer/touch input → Chart model: scrolls, zooms, hit-tests
- Chart widget → Pointer/touch input: captures events
- Chart widget → Pane views: requests view data
- Pane views → Canvas renderers: supplies rendering data
- Canvas renderers → Chart widget: draws canvases
- pnpm workspace → Plugin toolkit: contains
- pnpm workspace → Plugin scaffolder: contains
- Plugin toolkit → Primitive extension boundary: supports plugin authors
- Plugin scaffolder → Plugin toolkit: generates projects using
- Docs, catalogue & CI → pnpm workspace: builds and publishes