Skip to content

Commit da2a510

Browse files
committed
Enhancements & Bug Fixes
- Added the `color_based_on_candle` parameter to the legend, which will color the percentage change based on the candle color underneath the crosshair. (#210) - Fixed a bug which prevented the legend from turning off. (#216)
1 parent f798e5f commit da2a510

4 files changed

Lines changed: 60 additions & 32 deletions

File tree

docs/source/reference/abstract_chart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ ___
273273
274274
275275
276-
```{py:method} legend(visible: bool, ohlc: bool, percent: bool, lines: bool, color: COLOR, font_size: int, font_family: str, text: str)
276+
```{py:method} legend(visible: bool, ohlc: bool, percent: bool, lines: bool, color: COLOR, font_size: int, font_family: str, text: str, color_based_on_candle: bool)
277277
278278
Configures the legend of the chart.
279279
```

lightweight_charts/abstract.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,19 +904,25 @@ def watermark(self, text: str, font_size: int = 44, color: str = 'rgba(180, 180,
904904

905905
def legend(self, visible: bool = False, ohlc: bool = True, percent: bool = True, lines: bool = True,
906906
color: str = 'rgb(191, 195, 203)', font_size: int = 11, font_family: str = 'Monaco',
907-
text: str = ''):
907+
text: str = '', color_based_on_candle: bool = False):
908908
"""
909909
Configures the legend of the chart.
910910
"""
911911
l_id = f'{self.id}.legend'
912912
if not visible:
913-
self.run_script(f'{l_id}.div.style.display = "none"')
913+
self.run_script(f'''
914+
{l_id}.div.style.display = "none"
915+
{l_id}.ohlcEnabled = false
916+
{l_id}.percentEnabled = false
917+
{l_id}.linesEnabled = false
918+
''')
914919
return
915920
self.run_script(f'''
916921
{l_id}.div.style.display = 'flex'
917922
{l_id}.ohlcEnabled = {jbool(ohlc)}
918923
{l_id}.percentEnabled = {jbool(percent)}
919924
{l_id}.linesEnabled = {jbool(lines)}
925+
{l_id}.colorBasedOnCandle = {jbool(color_based_on_candle)}
920926
{l_id}.div.style.color = '{color}'
921927
{l_id}.color = '{color}'
922928
{l_id}.div.style.fontSize = '{font_size}px'

lightweight_charts/js/funcs.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='lightweight_charts',
8-
version='1.0.18.6',
8+
version='1.0.18.8',
99
packages=find_packages(),
1010
python_requires='>=3.8',
1111
install_requires=[

0 commit comments

Comments
 (0)