lightweight-charts-drawing 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,92 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 (26/09/2026)
4
+
5
+ A new library built on a shared drawing core (`src/tv`) that is also used by
6
+ [OpenTrader](https://github.com/deepentropy/opentrader), with a canvas
7
+ runtime (`src/runtime`). Breaking: the 0.1 API is removed.
8
+
9
+ ### Breaking changes
10
+
11
+ - The 0.1 runtime is removed: `Drawing` and every tool class (`TrendLine`,
12
+ `FibRetracement`, …) with their pane views, the 0.1 `DrawingManager`
13
+ (`attach`, `addDrawing`, `selectDrawing`, …), `InteractionHandler`,
14
+ `ToolRegistry` / `getToolRegistry` / `TOOL_DEFINITIONS`, the geometry and
15
+ canvas helpers, `PreviewRenderer`.
16
+ - New drawing model: a drawing is plain data `{ id, kind, points: [{ time,
17
+ price }], style, … }` (types `Drawing`, `NewDrawing`, `DrawingStyle`) with
18
+ TradingView field names and factory defaults. The 0.1 JSON format
19
+ (`{ type, anchors, style: { lineColor, … }, options }`) is not read by
20
+ `importJSON`.
21
+ - Tool names follow TradingView / OpenTrader:
22
+
23
+ | 0.1 | 0.2 |
24
+ |---|---|
25
+ | fib-extension | trend-based-fib-extension |
26
+ | fib-time-extension | trend-based-fib-time |
27
+ | fib-arcs | fib-speed-resistance-arcs |
28
+ | fib-speed-fan | fib-speed-resistance-fan |
29
+ | forecast | position-forecast |
30
+ | andrews-pitchfork | pitchfork |
31
+ | date-price-range | date-and-price-range |
32
+ | bars-pattern | bar-pattern |
33
+ | text-annotation | text |
34
+ | projection | sector |
35
+
36
+ - `anchored-text` is removed. TradingView has retired its Anchored text tool
37
+ in favour of the Text tool with "Anchor drawing" on
38
+ (`toggleAnchored`).
39
+ - `fancy-canvas` is no longer a dependency (types only, at build time).
40
+
41
+ ### New
42
+
43
+ - `DrawingManager(chart, series, options)`: one series primitive draws every
44
+ drawing, the placement preview, the anchors (TradingView style) and the
45
+ drawings' price / time axis labels.
46
+ - Placement by TradingView's rules: clicks, 1-click tools, press-drag for
47
+ brush / highlighter, polyline / path / ghost feed finished by a
48
+ double-click or a click on the last point, the data TradingView computes
49
+ at placement (position levels, bar-pattern snapshot, ghost-feed seed,
50
+ curve controls, trend angle, Gann square), text tools ask the host for
51
+ an editor (`textEdit` event).
52
+ - Hover, multi-select (Ctrl / Cmd), anchor / body / group drags with each
53
+ tool's anchor rules, Ctrl + drag clone, magnet (weak / strong, Shift /
54
+ Ctrl modifiers), Shift 45° / square constraints, Escape / Delete, z-order,
55
+ per-interval visibility, locked drawings, cursors.
56
+ - "Anchor drawing" for Text, Pin and Table (`toggleAnchored`).
57
+ - Table cell editing (`tableEdit` event, `setTableCellText`, `tableOp`),
58
+ images (`addImage` + the image cache hooks).
59
+ - JSON export / import of the drawing model (`exportJSON`, `importJSON`
60
+ with schema check and point-model migration).
61
+ - 86 tools with TradingView factory defaults, labels and hit tests
62
+ ([docs/TOOLS.md](docs/TOOLS.md)): lines, channels, pitchforks, Fibonacci
63
+ and Gann tools, chart patterns, Elliott waves, cycles, forecasting and
64
+ measuring tools (positions, ranges, forecast, bar pattern, ghost feed),
65
+ volume-based tools (anchored VWAP, fixed range / anchored volume
66
+ profile), shapes, text and notes, table, image, font icon.
67
+ - For hosts that draw their own way: `sceneOf` (renderer-neutral scene of a
68
+ drawing), `drawScene` (canvas), `hitTestKind`, `drawingAxisLabels`,
69
+ `finishPlacement` / `applyDrag` (interaction rules), `makeCoords` (chart ↔
70
+ price / time bridge), `parseDrawings` / `migrateDrawing`.
71
+ - The package imports nothing from lightweight-charts at run time (types
72
+ only), so a host keeps a single lightweight-charts copy.
73
+
74
+ ### Notes
75
+
76
+ - The volume-based tools need bars with a volume: pass them with the
77
+ `bars` option (series data usually has none).
78
+ - Checks behind this release (in `docs/port-opentrader/`): the renderers
79
+ match OpenTrader's on 430 golden fixtures, the canvas renderer matches the
80
+ SVG output on 774 fixtures (0.05 % of drawn pixels differ, text
81
+ anti-aliasing), and all 86 tools pass a real-mouse survey (place, select,
82
+ anchor drag, body drag, delete) in the demo.
83
+
84
+ ## 0.1.1 (26/02/2026)
85
+
86
+ - Description without "professional"; screenshots and the outdated
87
+ changelog removed from the README and docs.
88
+
89
+ ## 0.1.0
90
+
91
+ - First release: tool classes with canvas pane views, `DrawingManager`,
92
+ tool registry, demo on GitHub Pages.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 deepentropy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,7 +2,14 @@
2
2
 
3
3
  **[Live Demo](https://deepentropy.github.io/lightweight-charts-drawing/)**
4
4
 
5
- 68 drawing tools for TradingView's lightweight-charts library. Includes trend lines, Fibonacci tools, Gann analysis, channels, pitchforks, shapes, annotations, and forecasting tools.
5
+ TradingView-style drawing tools for [lightweight-charts](https://github.com/tradingview/lightweight-charts) v5:
6
+ 86 tools with TradingView's factory defaults, labels, hit tests, placement
7
+ and anchor rules (trend lines, channels, pitchforks, Fibonacci and Gann
8
+ tools, patterns, Elliott waves, cycles, forecasting and measuring tools,
9
+ volume-based tools, shapes, text and notes). List: [docs/TOOLS.md](docs/TOOLS.md).
10
+
11
+ > Version 0.2 breaks the 0.1 API: the tool classes of 0.1 are replaced by one
12
+ > `DrawingManager` on a shared drawing core. See [CHANGELOG.md](CHANGELOG.md).
6
13
 
7
14
  ## Installation
8
15
 
@@ -10,216 +17,97 @@
10
17
  npm install lightweight-charts-drawing lightweight-charts
11
18
  ```
12
19
 
13
- ## Quick Start
20
+ ## Quick start
14
21
 
15
22
  ```typescript
16
23
  import { createChart, CandlestickSeries } from 'lightweight-charts';
17
- import { DrawingManager, TrendLine, FibRetracement } from 'lightweight-charts-drawing';
18
-
19
- // Create chart
20
- const chart = createChart(document.getElementById('chart')!, {
21
- width: 800,
22
- height: 400,
23
- });
24
+ import { DrawingManager } from 'lightweight-charts-drawing';
24
25
 
26
+ const chart = createChart(document.getElementById('chart')!, { autoSize: true });
25
27
  const series = chart.addSeries(CandlestickSeries);
26
- series.setData([/* ... your OHLC data ... */]);
27
-
28
- // Set up drawing manager
29
- const manager = new DrawingManager();
30
- manager.attach(chart, series, document.getElementById('chart')!);
28
+ series.setData(bars); // your OHLC data
31
29
 
32
- // Add a trend line
33
- const trendLine = new TrendLine('tl-1', [
34
- { time: '2024-01-15', price: 100 },
35
- { time: '2024-02-15', price: 110 },
36
- ], { lineColor: '#2962FF', lineWidth: 2 });
30
+ const drawings = new DrawingManager(chart, series, { magnet: 'weak' });
37
31
 
38
- manager.addDrawing(trendLine);
32
+ // Arm a tool: the user places it with the mouse (TV clicks / drag rules).
33
+ drawings.setTool('fib-retracement');
39
34
 
40
- // Add a Fibonacci retracement
41
- const fib = new FibRetracement('fib-1', [
42
- { time: '2024-01-01', price: 95 },
43
- { time: '2024-03-01', price: 115 },
44
- ]);
35
+ // Or add one in code (the tool's TradingView defaults fill the style).
36
+ drawings.add({ kind: 'trend-line', points: [
37
+ { time: bars[10].time, price: bars[10].low },
38
+ { time: bars[40].time, price: bars[40].high },
39
+ ] });
45
40
 
46
- manager.addDrawing(fib);
41
+ // Save / restore (the core Drawing model, checked and migrated on import).
42
+ localStorage.setItem('drawings', drawings.exportJSON());
43
+ drawings.importJSON(localStorage.getItem('drawings') ?? '[]');
47
44
  ```
48
45
 
49
- ## Lightweight-Charts Integration Example
50
-
51
- ```typescript
52
- import { createChart, CandlestickSeries, ColorType } from 'lightweight-charts';
53
- import {
54
- DrawingManager,
55
- TrendLine,
56
- HorizontalLine,
57
- Rectangle,
58
- TextAnnotation,
59
- getToolRegistry,
60
- } from 'lightweight-charts-drawing';
61
-
62
- // Create chart
63
- const container = document.getElementById('chart')!;
64
- const chart = createChart(container, {
65
- layout: {
66
- background: { type: ColorType.Solid, color: '#131722' },
67
- textColor: '#d1d4dc',
68
- },
69
- width: 1000,
70
- height: 500,
71
- });
72
-
73
- const series = chart.addSeries(CandlestickSeries);
74
- series.setData([/* ... OHLC data ... */]);
75
-
76
- // Initialize drawing manager
77
- const manager = new DrawingManager();
78
- manager.attach(chart, series, container);
79
-
80
- // Listen for events
81
- manager.on('drawing:selected', (event) => {
82
- console.log('Selected:', event.drawingId);
83
- });
84
-
85
- // Add drawings programmatically
86
- const support = new HorizontalLine('support', [
87
- { time: '2024-01-01' as any, price: 95 },
88
- ], { lineColor: '#26a69a', lineWidth: 1 });
46
+ ## DrawingManager
47
+
48
+ `new DrawingManager(chart, series, options?)` attaches one series primitive
49
+ that draws every drawing, the placement preview, the anchors and the
50
+ drawings' axis labels.
51
+
52
+ Options: `bars` (bar getter with volume; default the series data, which
53
+ usually has no volume: the volume profiles then show no rows and the
54
+ anchored VWAP uses equal weights), `fontFamily`
55
+ (default the chart font), `magnet` (`'off' | 'weak' | 'strong'`), `interval`
56
+ (per-interval visibility, TV Visibility tab), `timeInfo` (time zone /
57
+ intraday of date labels), `stayInDrawingMode` (TV Keep drawing).
58
+
59
+ | Method | |
60
+ |---|---|
61
+ | `setTool(kind \| null, { glyph? })`, `tool()` | arm / disarm a tool (`kind` from docs/TOOLS.md) |
62
+ | `setMagnet(mode)`, `setInterval(i)`, `setStayInDrawingMode(on)` | settings |
63
+ | `add(drawing)`, `update(drawing)`, `remove(id)`, `clear()`, `get(id)`, `drawings()` | drawings |
64
+ | `select(ids)`, `selection()` | selection (multi-select with Ctrl / Cmd) |
65
+ | `bringToFront(id)`, `sendToBack(id)` | z-order |
66
+ | `toggleAnchored(id)` | TV "Anchor drawing" for Text, Pin and Table: the drawing keeps its pane position when the chart scrolls (`isAnchorable(kind)`) |
67
+ | `exportJSON()`, `importJSON(json)` | save / restore |
68
+ | `addImage({ name, width, height })` | place an image (file put in the image cache by the host: `cacheImage` / `setImageReader`) |
69
+ | `setTableCellText(id, cell, text)`, `endTableEdit()`, `tableOp(id, op)`, `tableState()` | table cell editing (after a `tableEdit` event) and row / column operations |
70
+ | `on(event, cb)` → unsubscribe | events |
71
+ | `redraw()`, `destroy()` | |
72
+
73
+ Events: `change`, `add`, `update`, `remove`, `selection`, `tool`,
74
+ `textEdit` (a text tool was placed or double-clicked: open your editor at
75
+ the given pane point and `update` the drawing's `text`), `tableEdit` (a click
76
+ on a cell of the selected table: open your editor over the given cell box),
77
+ `gestureEnd` (end of a drag / placement, for undo grouping).
78
+
79
+ Mouse and keys (TradingView behaviour): placement by clicks, press-drag for
80
+ brush / highlighter, double-click (or a click on the last point) finishes a
81
+ path / polyline; anchor drags, body drag of a selected drawing, Ctrl / Cmd
82
+ click to multi-select and group drag, Ctrl + body drag clones; Shift = 45°
83
+ steps / square boxes and magnet off, Ctrl / Cmd inverts the magnet; Escape
84
+ drops pending points, then the tool, then the selection; Delete removes the
85
+ selection (locked drawings stay).
86
+
87
+ The host keeps what is not drawing logic: undo stack, storage, settings
88
+ dialogs, text / table editors, toolbar, context menu.
89
89
 
90
- manager.addDrawing(support);
91
-
92
- // Select, deselect, remove
93
- manager.selectDrawing('support');
94
- manager.deselectAll();
95
- manager.removeDrawing('support');
96
-
97
- // Export/import drawings as JSON
98
- const json = manager.exportDrawings();
99
- // manager.importDrawings(json, factory);
90
+ ## Architecture
100
91
 
101
- // Access the tool registry for all 68 tool definitions
102
- const registry = getToolRegistry();
103
- const allTools = registry.getAllTools();
92
+ ```
93
+ src/tv/ shared drawing core, no UI and no lightweight-charts runtime:
94
+ tool model + TradingView factory defaults (specs), hit tests,
95
+ placement and drag rules (interact), saved-drawing migration
96
+ (serialize), renderer-neutral scenes per tool (scene)
97
+ src/runtime/ canvas runtime: DrawingManager, scene -> canvas renderer,
98
+ chart <-> price / time bridge (makeCoords)
104
99
  ```
105
100
 
106
- ## Available Drawing Tools (68)
107
-
108
- ### Lines
109
-
110
- | Tool | Export | Anchors |
111
- |------|--------|---------|
112
- | Trend Line | `TrendLine` | 2 |
113
- | Ray | `Ray` | 2 |
114
- | Info Line | `InfoLine` | 2 |
115
- | Extended Line | `ExtendedLine` | 2 |
116
- | Trend Angle | `TrendAngle` | 2 |
117
- | Horizontal Line | `HorizontalLine` | 1 |
118
- | Horizontal Ray | `HorizontalRay` | 1 |
119
- | Vertical Line | `VerticalLine` | 1 |
120
- | Cross Line | `CrossLine` | 1 |
121
-
122
- ### Channels
123
-
124
- | Tool | Export | Anchors |
125
- |------|--------|---------|
126
- | Parallel Channel | `ParallelChannel` | 3 |
127
- | Regression Trend | `RegressionTrend` | 2 |
128
- | Flat Top/Bottom | `FlatTopBottom` | 3 |
129
- | Disjoint Channel | `DisjointChannel` | 4 |
130
-
131
- ### Pitchforks
132
-
133
- | Tool | Export | Anchors |
134
- |------|--------|---------|
135
- | Andrews Pitchfork | `AndrewsPitchfork` | 3 |
136
- | Schiff Pitchfork | `SchiffPitchfork` | 3 |
137
- | Modified Schiff Pitchfork | `ModifiedSchiffPitchfork` | 3 |
138
- | Inside Pitchfork | `InsidePitchfork` | 3 |
139
-
140
- ### Fibonacci
141
-
142
- | Tool | Export | Anchors |
143
- |------|--------|---------|
144
- | Fib Retracement | `FibRetracement` | 2 |
145
- | Fib Extension | `FibExtension` | 3 |
146
- | Fib Channel | `FibChannel` | 3 |
147
- | Fib Time Zone | `FibTimeZone` | 2 |
148
- | Fib Speed Fan | `FibSpeedFan` | 2 |
149
- | Fib Time Extension | `FibTimeExtension` | 3 |
150
- | Fib Circles | `FibCircles` | 2 |
151
- | Fib Spiral | `FibSpiral` | 2 |
152
- | Fib Arcs | `FibArcs` | 2 |
153
- | Fib Wedge | `FibWedge` | 3 |
154
- | Pitchfan | `Pitchfan` | 3 |
155
-
156
- ### Gann
157
-
158
- | Tool | Export | Anchors |
159
- |------|--------|---------|
160
- | Gann Box | `GannBox` | 2 |
161
- | Gann Fan | `GannFan` | 2 |
162
- | Gann Square Fixed | `GannSquareFixed` | 1 |
163
- | Gann Square | `GannSquare` | 2 |
164
-
165
- ### Forecasting & Measurement
166
-
167
- | Tool | Export | Anchors |
168
- |------|--------|---------|
169
- | Long Position | `LongPosition` | 3 |
170
- | Short Position | `ShortPosition` | 3 |
171
- | Forecast | `Forecast` | 2 |
172
- | Bars Pattern | `BarsPattern` | 3 |
173
- | Projection | `Projection` | 3 |
174
- | Price Range | `PriceRange` | 2 |
175
- | Date Range | `DateRange` | 2 |
176
- | Date & Price Range | `DatePriceRange` | 2 |
177
-
178
- ### Shapes
179
-
180
- | Tool | Export | Anchors |
181
- |------|--------|---------|
182
- | Rectangle | `Rectangle` | 2 |
183
- | Rotated Rectangle | `RotatedRectangle` | 3 |
184
- | Circle | `Circle` | 2 |
185
- | Triangle | `Triangle` | 3 |
186
- | Ellipse | `Ellipse` | 2 |
187
- | Arc | `Arc` | 3 |
188
- | Path | `Path` | 2+ |
189
- | Polyline | `Polyline` | 2+ |
190
- | Curve | `Curve` | 4 |
191
- | Double Curve | `DoubleCurve` | 3 |
192
-
193
- ### Annotations
194
-
195
- | Tool | Export | Anchors |
196
- |------|--------|---------|
197
- | Text | `TextAnnotation` | 1 |
198
- | Callout | `Callout` | 2 |
199
- | Anchored Text | `AnchoredText` | 2 |
200
- | Note | `Note` | 1 |
201
- | Price Note | `PriceNote` | 1 |
202
- | Price Label | `PriceLabel` | 1 |
203
- | Flag Mark | `FlagMark` | 1 |
204
- | Pin | `Pin` | 1 |
205
- | Comment | `Comment` | 1 |
206
- | Signpost | `Signpost` | 1 |
207
- | Table | `Table` | 1 |
208
- | Brush | `Brush` | 2+ |
209
- | Highlighter | `Highlighter` | 2+ |
210
- | Arrow | `Arrow` | 2 |
211
- | Arrow Marker | `ArrowMarker` | 1 |
212
- | Arrow Mark Up | `ArrowMarkUp` | 1 |
213
- | Arrow Mark Down | `ArrowMarkDown` | 1 |
214
-
215
- ## Architecture
216
-
217
- Each drawing tool follows a two-class pattern:
101
+ The core is shared with [OpenTrader](https://github.com/deepentropy/opentrader),
102
+ which draws the same scenes as SVG. The package imports only types from
103
+ lightweight-charts, so the chart library is not bundled twice.
218
104
 
219
- - **Tool class** (e.g. `TrendLine`) - extends `Drawing`, holds anchors/state, implements hit testing and geometry computation
220
- - **Pane view** (e.g. `TrendLinePaneView`) - implements `IPrimitivePaneView` from lightweight-charts, handles canvas rendering
105
+ ## Demo
221
106
 
222
- The `DrawingManager` orchestrates lifecycle, selection, drag-editing, and event emission.
107
+ ```bash
108
+ npm run demo # dev server, demo/
109
+ npm run build:demo # dist-demo/ (GitHub Pages)
110
+ ```
223
111
 
224
112
  ## License
225
113