Overview
TheChartManager class provides a clean interface for creating and managing candlestick charts using the lightweight-charts library. It handles chart initialization, real-time updates, and cleanup.
Class: ChartManager
Constructor
Initializes a new chart instance with the specified configuration.DOM element reference where the chart will be mounted
Array of initial candlestick data points. Each object should contain:
timestamp(number): Unix timestamp in millisecondsopen(number): Opening pricehigh(number): Highest pricelow(number): Lowest priceclose(number): Closing price
Chart layout configuration
background(string): Background color (hex or rgb)color(string): Text color (hex or rgb)
Chart Configuration
The constructor automatically configures the chart with:- Crosshair: Normal mode with dashed lines (#707070)
- Price Scale: Right-aligned with visible ticks and borders
- Grid: Horizontal and vertical lines (#262626)
- Candlestick Colors:
- Bullish (up): #34cb88 (green)
- Bearish (down): #ff615c (red)
- Wicks: #5dd5a0 (up), #ff887f (down)
Methods
update()
Updates the chart with new price data. Handles both incremental updates and new candle initialization.Price update object containing:
open(number): Opening pricehigh(number): Highest pricelow(number): Lowest priceclose(number): Closing pricenewCandleInitiated(boolean): Whether this starts a new candletime(number): Unix timestamp in milliseconds (required if newCandleInitiated is true)
The method automatically tracks the last update time. When
newCandleInitiated is true, it updates the internal timestamp to start a new candle period.destroy()
Cleans up and removes the chart instance from the DOM.Always call this method when unmounting the chart component to prevent memory leaks.
Usage Example
Integration with React
Source Location
apps/web/src/utils/chart_manager.ts