@@ -185,6 +185,7 @@ if (!window.Chart) {
185185 this . ohlcEnabled = false
186186 this . percentEnabled = false
187187 this . linesEnabled = false
188+ this . colorBasedOnCandle = false
188189
189190 this . div = document . createElement ( 'div' )
190191 this . div . style . position = 'absolute'
@@ -219,40 +220,61 @@ if (!window.Chart) {
219220 }
220221
221222 chart . chart . subscribeCrosshairMove ( ( param ) => {
222- if ( param . time ) {
223- let data = param . seriesData . get ( chart . series ) ;
224- let finalString = '<span style="line-height: 1.8;">'
225- if ( data ) {
226- this . candle . style . color = ''
227- let ohlc = `O ${ legendItemFormat ( data . open , chart . precision ) }
228- | H ${ legendItemFormat ( data . high , chart . precision ) }
229- | L ${ legendItemFormat ( data . low , chart . precision ) }
230- | C ${ legendItemFormat ( data . close , chart . precision ) } `
231- let percentMove = ( ( data . close - data . open ) / data . open ) * 100
232- let percent = `| ${ percentMove >= 0 ? '+' : '' } ${ percentMove . toFixed ( 2 ) } %`
233- finalString += this . ohlcEnabled ? ohlc : ''
234- finalString += this . percentEnabled ? percent : ''
235-
236- let volumeData = param . seriesData . get ( chart . volumeSeries )
237- if ( volumeData ) finalString += this . ohlcEnabled ? `<br>V ${ shorthandFormat ( volumeData . value ) } ` : ''
223+ let options = chart . series . options ( )
224+ if ( ! param . time ) {
225+ this . candle . style . color = 'transparent'
226+ this . candle . innerHTML = this . candle . innerHTML . replace ( options [ 'upColor' ] , '' ) . replace ( options [ 'downColor' ] , '' )
227+ return
228+ }
229+ let data = param . seriesData . get ( chart . series ) ;
230+ this . candle . style . color = ''
231+ let str = '<span style="line-height: 1.8;">'
232+ if ( data ) {
233+ if ( this . ohlcEnabled ) {
234+ str += `O ${ legendItemFormat ( data . open , chart . precision ) } `
235+ str += `| H ${ legendItemFormat ( data . high , chart . precision ) } `
236+ str += `| L ${ legendItemFormat ( data . low , chart . precision ) } `
237+ str += `| C ${ legendItemFormat ( data . close , chart . precision ) } `
238238 }
239- this . candle . innerHTML = finalString + '</span>'
240- this . lines . forEach ( ( line ) => {
241- if ( ! param . seriesData . get ( line . line . series ) ) return
242- let price = param . seriesData . get ( line . line . series ) . value
243239
244- if ( line . line . type === 'histogram' ) {
245- price = shorthandFormat ( price )
240+ if ( this . percentEnabled ) {
241+ let percentMove = ( ( data . close - data . open ) / data . open ) * 100
242+ let color = percentMove > 0 ? options [ 'upColor' ] : options [ 'downColor' ]
243+ let percentStr = `${ percentMove >= 0 ? '+' : '' } ${ percentMove . toFixed ( 2 ) } %`
244+
245+ if ( this . colorBasedOnCandle ) {
246+ str += `| <span style="color: ${ color } ;">${ percentStr } </span>`
246247 }
247248 else {
248- price = legendItemFormat ( price , line . line . precision )
249+ str += '| ' + percentStr
249250 }
250- line . div . innerHTML = `<span style="color: ${ line . solid } ;">▨</span> ${ line . line . name } : ${ price } `
251- } )
252-
253- } else {
254- this . candle . style . color = 'transparent'
251+ }
252+ let volumeData = param . seriesData . get ( chart . volumeSeries )
253+ if ( volumeData ) {
254+ str += this . ohlcEnabled ? `<br>V ${ shorthandFormat ( volumeData . value ) } ` : ''
255+ }
255256 }
257+ this . candle . innerHTML = str + '</span>'
258+
259+ this . lines . forEach ( ( line ) => {
260+ if ( ! this . linesEnabled ) {
261+ line . row . style . display = 'none'
262+ return
263+ }
264+ line . row . style . display = 'flex'
265+ if ( ! param . seriesData . get ( line . line . series ) ) return
266+ let price = param . seriesData . get ( line . line . series ) . value
267+
268+ if ( line . line . type === 'histogram' ) {
269+ price = shorthandFormat ( price )
270+ }
271+ else {
272+ price = legendItemFormat ( price , line . line . precision )
273+ }
274+ line . div . innerHTML = `<span style="color: ${ line . solid } ;">▨</span> ${ line . line . name } : ${ price } `
275+ } )
276+
277+
256278 } ) ;
257279 }
258280
0 commit comments