KlineChart Rendering Engine
Relevant source files
The following files were used as context for generating this wiki page:
- frontend/src/klinechart/README.md
- frontend/src/klinechart/package.json
- frontend/src/klinechart/types/index.d.ts
- frontend/src/pages/chart/index.tsx
- frontend/src/klinechart/tests/technical-indicator-template2.html
- frontend/src/klinechart/tests/shape-template.html
- frontend/src/klinechart/tests/styles1.html
The KlineChart Rendering Engine is a lightweight, high-performance visualization system built using the HTML5 Canvas API. Its primary purpose is to render financial time-series data, specifically K-line (candlestick) charts, and provide a extensible framework for technical indicators and interactive overlays.
The engine follows a modular architecture where the core logic manages data flow and viewport calculations, while specific visual elements like indicators and shapes are handled through a template-based system. This allow developers to define custom rendering logic by interacting directly with the Canvas 2D context.
Sources: [frontend/src/klinechart/README.md:1-5](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/README.md#L1-L5), [frontend/src/klinechart/package.json:2-15](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/package.json#L2-L15)
Core Architecture and Initialization
The engine is initialized by targeting a DOM container and applying a set of configuration options. The init function creates a Chart instance which serves as the primary controller for data management and layout.
Initialization Workflow
The following diagram illustrates the lifecycle of a chart instance from initialization to data rendering.
This flow ensures that all styling and custom indicator definitions are registered before the initial data paint.
Sources: [frontend/src/pages/chart/index.tsx:86-105](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/pages/chart/index.tsx#L86-L105), [frontend/src/klinechart/types/index.d.ts:44](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/types/index.d.ts#L44)
Key Data Structures
The engine relies on several core interfaces to synchronize the visual state with the underlying data:
| Interface | Purpose | Derived From |
|---|---|---|
KLineData | Defines the structure of a single candle (OHLCV). | types/index.d.ts |
Viewport | Represents the dimensions of the visible drawing area. | types/index.d.ts |
DataSource | Provides calculated indicator data for rendering. | types/index.d.ts |
Precision | Controls the decimal formatting for prices and volumes. | types/index.d.ts |
Sources: [frontend/src/klinechart/types/index.d.ts:5-8](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/types/index.d.ts#L5-L8)
Technical Indicator Rendering
The engine implements a specialized pipeline for technical indicators. Indicators are defined via a TechnicalIndicatorTemplate, which separates data calculation from visual rendering.
Rendering Pipeline Logic
- Calculation (
calcTechnicalIndicator): Processes the raw K-line list to produce indicator values. - Tooltip Creation (
createToolTipDataSource): Formats data for the crosshair overlay. - Visual Render (
render): Executes Canvas API commands to draw lines, bars, or custom graphics.
Sources: [frontend/src/klinechart/tests/technical-indicator-template2.html:42-70](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/technical-indicator-template2.html#L42-L70)
Custom Render Implementation
Developers can override the default drawing logic using the render hook. The engine provides a TechnicalIndicatorRenderParams object containing the ctx (CanvasRenderingContext2D) and dataSource.
// Example of custom volume-based bar rendering
render: function ({ ctx, dataSource, viewport }) {
const result = dataSource.technicalIndicatorDataList
for (let i = dataSource.from; i < dataSource.to; i++) {
const barWidth = Math.floor(result[i].volume / max * 150)
ctx.fillRect(viewport.width - barWidth, startY, barWidth, realBarHeight)
}
}
Sources: [frontend/src/klinechart/tests/technical-indicator-template2.html:56-69](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/technical-indicator-template2.html#L56-L69)
Shape and Overlay System
Interactive elements such as trend lines, segments, and geometric shapes are managed through a "Shape" system. This system uses a state machine to track multi-step drawing processes (e.g., clicking twice to define a line).
Shape Template Components
A ShapeTemplate requires three main handlers:
totalStep: Number of clicks required to complete the shape.checkEventCoordinateOnShape: Hit testing logic to determine if a mouse event interacts with the shape.createShapeDataSource: Maps coordinates to Canvas primitives like 'arc', 'line', or 'text'.
Sources: [frontend/src/klinechart/tests/shape-template.html:43-65](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/shape-template.html#L43-L65), [frontend/src/klinechart/types/index.d.ts:20-27](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/types/index.d.ts#L20-L27)
Coordinate Mapping
The engine translates geographical data (timestamps and price values) into screen coordinates before passing them to the shape's data source generator. This ensures shapes remain anchored correctly during scrolling or scaling.
Sources: [frontend/src/pages/chart/index.tsx:160-185](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/pages/chart/index.tsx#L160-L185), [frontend/src/klinechart/tests/shape-template.html:52-56](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/shape-template.html#L52-L56)
Styling and Viewport Configuration
The visual appearance of the engine is controlled via a centralized style object. This includes configuration for the grid, axes, candles, and crosshairs.
Style Configuration Categories
| Category | Configurable Elements |
|---|---|
| Grid | Horizontal/Vertical line colors. |
| Candle | Price marks (high/low), tooltip text colors, candle colors. |
| xAxis / yAxis | Axis lines, tick lines, and tick text styles. |
| Crosshair | Line color, label background color. |
Sources: [frontend/src/klinechart/tests/styles1.html:43-108](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/styles1.html#L43-L108)
Dynamic Updates
The engine supports real-time style updates via setStyleOptions. This is used to implement features like dark/light mode switching or highlighting specific chart regions.
Sources: [frontend/src/pages/chart/index.tsx:107-118](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/pages/chart/index.tsx#L107-L118), [frontend/src/klinechart/tests/styles2.html:43-45](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/tests/styles2.html#L43-L45)
Conclusion
The KlineChart Rendering Engine provides a robust foundation for financial visualization by abstracting the complexities of Canvas management behind a template-driven API. By decoupling data calculation from rendering logic, it enables the creation of complex technical indicators and interactive drawing tools while maintaining high performance across various web environments.
Sources: [frontend/src/klinechart/README.md:1-10](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/README.md#L1-L10), [frontend/src/klinechart/package.json:16-25](https://github.com/marlonsc/technical-analysis/blob/e47edb2ad8be7b90d347cc556190f9958a74be1c/frontend/src/klinechart/package.json#L16-L25)