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)
This commit is contained in:
@ -273,7 +273,7 @@ ___
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
```{py:method} legend(visible: bool, ohlc: bool, percent: bool, lines: bool, color: COLOR, font_size: int, font_family: str, text: str)
|
```{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)
|
||||||
|
|
||||||
Configures the legend of the chart.
|
Configures the legend of the chart.
|
||||||
```
|
```
|
||||||
|
|||||||
@ -904,19 +904,25 @@ class AbstractChart(Candlestick, Pane):
|
|||||||
|
|
||||||
def legend(self, visible: bool = False, ohlc: bool = True, percent: bool = True, lines: bool = True,
|
def legend(self, visible: bool = False, ohlc: bool = True, percent: bool = True, lines: bool = True,
|
||||||
color: str = 'rgb(191, 195, 203)', font_size: int = 11, font_family: str = 'Monaco',
|
color: str = 'rgb(191, 195, 203)', font_size: int = 11, font_family: str = 'Monaco',
|
||||||
text: str = ''):
|
text: str = '', color_based_on_candle: bool = False):
|
||||||
"""
|
"""
|
||||||
Configures the legend of the chart.
|
Configures the legend of the chart.
|
||||||
"""
|
"""
|
||||||
l_id = f'{self.id}.legend'
|
l_id = f'{self.id}.legend'
|
||||||
if not visible:
|
if not visible:
|
||||||
self.run_script(f'{l_id}.div.style.display = "none"')
|
self.run_script(f'''
|
||||||
|
{l_id}.div.style.display = "none"
|
||||||
|
{l_id}.ohlcEnabled = false
|
||||||
|
{l_id}.percentEnabled = false
|
||||||
|
{l_id}.linesEnabled = false
|
||||||
|
''')
|
||||||
return
|
return
|
||||||
self.run_script(f'''
|
self.run_script(f'''
|
||||||
{l_id}.div.style.display = 'flex'
|
{l_id}.div.style.display = 'flex'
|
||||||
{l_id}.ohlcEnabled = {jbool(ohlc)}
|
{l_id}.ohlcEnabled = {jbool(ohlc)}
|
||||||
{l_id}.percentEnabled = {jbool(percent)}
|
{l_id}.percentEnabled = {jbool(percent)}
|
||||||
{l_id}.linesEnabled = {jbool(lines)}
|
{l_id}.linesEnabled = {jbool(lines)}
|
||||||
|
{l_id}.colorBasedOnCandle = {jbool(color_based_on_candle)}
|
||||||
{l_id}.div.style.color = '{color}'
|
{l_id}.div.style.color = '{color}'
|
||||||
{l_id}.color = '{color}'
|
{l_id}.color = '{color}'
|
||||||
{l_id}.div.style.fontSize = '{font_size}px'
|
{l_id}.div.style.fontSize = '{font_size}px'
|
||||||
|
|||||||
@ -185,6 +185,7 @@ if (!window.Chart) {
|
|||||||
this.ohlcEnabled = false
|
this.ohlcEnabled = false
|
||||||
this.percentEnabled = false
|
this.percentEnabled = false
|
||||||
this.linesEnabled = false
|
this.linesEnabled = false
|
||||||
|
this.colorBasedOnCandle = false
|
||||||
|
|
||||||
this.div = document.createElement('div')
|
this.div = document.createElement('div')
|
||||||
this.div.style.position = 'absolute'
|
this.div.style.position = 'absolute'
|
||||||
@ -219,40 +220,61 @@ if (!window.Chart) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
chart.chart.subscribeCrosshairMove((param) => {
|
chart.chart.subscribeCrosshairMove((param) => {
|
||||||
if (param.time) {
|
let options = chart.series.options()
|
||||||
let data = param.seriesData.get(chart.series);
|
if (!param.time) {
|
||||||
let finalString = '<span style="line-height: 1.8;">'
|
this.candle.style.color = 'transparent'
|
||||||
if (data) {
|
this.candle.innerHTML = this.candle.innerHTML.replace(options['upColor'], '').replace(options['downColor'], '')
|
||||||
this.candle.style.color = ''
|
return
|
||||||
let ohlc = `O ${legendItemFormat(data.open, chart.precision)}
|
}
|
||||||
| H ${legendItemFormat(data.high, chart.precision)}
|
let data = param.seriesData.get(chart.series);
|
||||||
| L ${legendItemFormat(data.low, chart.precision)}
|
this.candle.style.color = ''
|
||||||
| C ${legendItemFormat(data.close, chart.precision)} `
|
let str = '<span style="line-height: 1.8;">'
|
||||||
let percentMove = ((data.close - data.open) / data.open) * 100
|
if (data) {
|
||||||
let percent = `| ${percentMove >= 0 ? '+' : ''}${percentMove.toFixed(2)} %`
|
if (this.ohlcEnabled) {
|
||||||
finalString += this.ohlcEnabled ? ohlc : ''
|
str += `O ${legendItemFormat(data.open, chart.precision)} `
|
||||||
finalString += this.percentEnabled ? percent : ''
|
str += `| H ${legendItemFormat(data.high, chart.precision)} `
|
||||||
|
str += `| L ${legendItemFormat(data.low, chart.precision)} `
|
||||||
let volumeData = param.seriesData.get(chart.volumeSeries)
|
str += `| C ${legendItemFormat(data.close, chart.precision)} `
|
||||||
if (volumeData) finalString += this.ohlcEnabled ? `<br>V ${shorthandFormat(volumeData.value)}` : ''
|
|
||||||
}
|
}
|
||||||
this.candle.innerHTML = finalString + '</span>'
|
|
||||||
this.lines.forEach((line) => {
|
|
||||||
if (!param.seriesData.get(line.line.series)) return
|
|
||||||
let price = param.seriesData.get(line.line.series).value
|
|
||||||
|
|
||||||
if (line.line.type === 'histogram') {
|
if (this.percentEnabled) {
|
||||||
price = shorthandFormat(price)
|
let percentMove = ((data.close - data.open) / data.open) * 100
|
||||||
|
let color = percentMove > 0 ? options['upColor'] : options['downColor']
|
||||||
|
let percentStr = `${percentMove >= 0 ? '+' : ''}${percentMove.toFixed(2)} %`
|
||||||
|
|
||||||
|
if (this.colorBasedOnCandle) {
|
||||||
|
str += `| <span style="color: ${color};">${percentStr}</span>`
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
price = legendItemFormat(price, line.line.precision)
|
str += '| ' + percentStr
|
||||||
}
|
}
|
||||||
line.div.innerHTML = `<span style="color: ${line.solid};">▨</span> ${line.line.name} : ${price}`
|
}
|
||||||
})
|
let volumeData = param.seriesData.get(chart.volumeSeries)
|
||||||
|
if (volumeData) {
|
||||||
} else {
|
str += this.ohlcEnabled ? `<br>V ${shorthandFormat(volumeData.value)}` : ''
|
||||||
this.candle.style.color = 'transparent'
|
}
|
||||||
}
|
}
|
||||||
|
this.candle.innerHTML = str + '</span>'
|
||||||
|
|
||||||
|
this.lines.forEach((line) => {
|
||||||
|
if (!this.linesEnabled) {
|
||||||
|
line.row.style.display = 'none'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
line.row.style.display = 'flex'
|
||||||
|
if (!param.seriesData.get(line.line.series)) return
|
||||||
|
let price = param.seriesData.get(line.line.series).value
|
||||||
|
|
||||||
|
if (line.line.type === 'histogram') {
|
||||||
|
price = shorthandFormat(price)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
price = legendItemFormat(price, line.line.precision)
|
||||||
|
}
|
||||||
|
line.div.innerHTML = `<span style="color: ${line.solid};">▨</span> ${line.line.name} : ${price}`
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@ -5,7 +5,7 @@ with open('README.md', 'r', encoding='utf-8') as f:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='lightweight_charts',
|
name='lightweight_charts',
|
||||||
version='1.0.18.6',
|
version='1.0.18.8',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
python_requires='>=3.8',
|
python_requires='>=3.8',
|
||||||
install_requires=[
|
install_requires=[
|
||||||
|
|||||||
Reference in New Issue
Block a user