1- import { CrosshairMode , LastPriceAnimationMode , LineSeries , LineStyle , createChart , Time , MouseEventParams } from 'lightweight-charts' ;
2- import { generateLineData } from './sample-data' ;
1+ import { LineStyle } from 'lightweight-charts' ;
32import { ShapeDrawing } from '../shape-drawing' ;
3+ import { chart , lineSeries , state , chartElement } from './data' ;
4+ import { chartCrosshairMoveEvent , chartMouseDownEvent , chartMouseMoveEvent , chartMouseUpEvent , keyUpEvent } from './helpers' ;
45
5- const priceSize = 0.01 ;
6- const pricePrecision = 2 ;
7- const chart = ( ( window as unknown as any ) . chart = createChart ( 'chart' , {
8- autoSize : true ,
9- crosshair : {
10- mode : CrosshairMode . Normal ,
11- } ,
12- layout : {
13- background : {
14- color : '#ccc' ,
15- } ,
16- } ,
17- } ) ) ;
18-
19- /************************** Statically add some shapes **************************/
20-
21- const lineSeries = chart . addSeries ( LineSeries , {
22- color : '#000000' ,
23- lastPriceAnimation : LastPriceAnimationMode . Disabled ,
24- crosshairMarkerVisible : false ,
25- priceFormat : {
26- type : 'price' ,
27- precision : pricePrecision ,
28- minMove : priceSize ,
29- } ,
30- } ) ;
31- const data = generateLineData ( ) ;
32- lineSeries . setData ( data ) ;
6+ /************************** Statically add a shape **************************/
337
34- const time1a = data [ data . length - 50 ] . time ;
35- const time2a = data [ data . length - 10 ] . time ;
8+ const time1a = lineSeries . data ( ) [ lineSeries . data ( ) . length - 50 ] . time ;
9+ const time2a = lineSeries . data ( ) [ lineSeries . data ( ) . length - 10 ] . time ;
3610
3711const shape1 = new ShapeDrawing (
3812 [
@@ -44,129 +18,26 @@ const shape1 = new ShapeDrawing(
4418 fillColor : '#ff0' ,
4519 fillOpacity : 0.5 ,
4620 borderColor : '#0ff' ,
47- borderWidth : 5 ,
21+ borderWidth : 3 ,
4822 borderStyle : LineStyle . Dashed ,
4923 showTimeAxisLabels : true ,
5024 showPriceAxisLabels : true ,
5125 labelColor : '#aaa' ,
5226 labelTextColor : '#000' ,
27+ hoveredEdgeWidth : 8 ,
28+ hoveredFillOpacity : 0.6 ,
5329 } ,
5430) ;
5531
5632lineSeries . attachPrimitive ( shape1 ) ;
5733
58- const time1b = data [ data . length - 150 ] . time ;
59- const time2b = data [ data . length - 130 ] . time ;
60- const time3b = data [ data . length - 110 ] . time ;
61- const time4b = data [ data . length - 90 ] . time ;
62-
63- const shape2 = new ShapeDrawing (
64- [
65- { price : 600 , time : time1b } ,
66- { price : 200 , time : time2b } ,
67- { price : 200 , time : time3b } ,
68- { price : 600 , time : time4b } ,
69- { price : 1000 , time : time3b } ,
70- { price : 1000 , time : time2b } ,
71- ] ,
72- {
73- fillColor : '#ff6b6b' ,
74- fillOpacity : 0.7 ,
75- borderColor : '#f00' ,
76- borderWidth : 3 ,
77- borderStyle : LineStyle . LargeDashed ,
78- showTimeAxisLabels : true ,
79- showPriceAxisLabels : true ,
80- labelColor : '#666' ,
81- labelTextColor : '#fff' ,
82- } ,
83- ) ;
84-
85- lineSeries . attachPrimitive ( shape2 ) ;
86-
87- /************************** Interactively add some shapes **************************/
34+ // Allows for interactivity with the shape
35+ state . drawnObjects [ shape1 . objectId ] = shape1 ;
8836
89- class Point {
90- price : number ;
91- time : Time ;
37+ /************************** Interactively add shapes **************************/
9238
93- constructor ( price : number , time : Time ) {
94- this . price = price ;
95- this . time = time ;
96- }
97- }
98-
99- const edgeCountElement = document . getElementById ( 'edge-count' ) as HTMLInputElement ;
100- const edgesElement = document . getElementById ( 'edges' ) as HTMLUListElement ;
101- let edgeCount = 2 ;
102- let edges : Point [ ] = [ ] ;
103-
104- edgeCountElement . addEventListener ( 'input' , ( ) => {
105- edgeCount = parseInt ( edgeCountElement . value ) ;
106- edgesElement . innerHTML = '' ;
107- } ) ;
108-
109- const chartElement = document . getElementById ( 'chart' ) ;
110- let crosshairPrice = 0 ;
111- let crosshairTime : Time | null = null ;
112-
113- window . addEventListener ( 'keyup' , ( event ) => {
114- if ( event . key === 'Escape' ) {
115- edges = [ ] ;
116- edgesElement . innerHTML = '' ;
117- }
118- } ) ;
119-
120- chartElement ! . addEventListener ( 'click' , chartClickEvent ) ;
39+ window . addEventListener ( 'keyup' , keyUpEvent ) ;
40+ chartElement ! . addEventListener ( 'mousedown' , chartMouseDownEvent ) ;
41+ chartElement ! . addEventListener ( 'mousemove' , chartMouseMoveEvent ) ;
42+ chartElement ! . addEventListener ( 'mouseup' , chartMouseUpEvent ) ;
12143chart . subscribeCrosshairMove ( chartCrosshairMoveEvent ) ;
122-
123- function chartClickEvent ( ) {
124- addEdge ( ) ;
125- }
126-
127- function chartCrosshairMoveEvent ( event : MouseEventParams < Time > ) {
128- if ( ! event . point || event . seriesData . size === 0 ) {
129- return ;
130- }
131-
132- const seriesValues = event . seriesData . entries ( ) . next ( ) . value ;
133-
134- crosshairPrice = roundNumber (
135- seriesValues ! [ 0 ] . coordinateToPrice ( event . point ! . y ) || 0 ,
136- pricePrecision ,
137- ) ;
138-
139- crosshairTime = seriesValues ! [ 1 ] . time || null ;
140- }
141-
142- function addEdge ( ) {
143- const newPoint = new Point ( crosshairPrice , crosshairTime ! ) ;
144-
145- edges . push ( newPoint ) ;
146-
147- const li = document . createElement ( 'li' ) ;
148- li . textContent = `X: ${ newPoint . price } , Y: ${ newPoint . time } ` ;
149- edgesElement . append ( li ) ;
150-
151- if ( edges . length === edgeCount ) {
152- const shape = new ShapeDrawing ( edges , {
153- fillColor : '#0f0' ,
154- fillOpacity : 0.5 ,
155- borderColor : '#f0f' ,
156- borderWidth : 5 ,
157- borderStyle : LineStyle . Dashed ,
158- } ) ;
159-
160- lineSeries . attachPrimitive ( shape ) ;
161-
162- edges = [ ] ;
163- edgesElement . innerHTML = '' ;
164- }
165- }
166-
167- function roundNumber ( n : number , dp : number ) {
168- const shiftedNumber = + `${ Number ( n ) . toFixed ( 20 ) } e+${ dp } ` ;
169- const roundedShiftedNumber = Math . round ( shiftedNumber ) ;
170- const result = + `${ roundedShiftedNumber } e-${ dp } ` ;
171- return result ;
172- }
0 commit comments