Added the render_drawings parameter to update

Added the `round` parameter to `vertical_span`
This commit is contained in:
louisnw
2024-01-21 14:42:54 +00:00
parent f31db04464
commit 5861da2123
6 changed files with 27 additions and 15 deletions

View File

@ -71,7 +71,7 @@ if __name__ == '__main__':
chart.show() chart.show()
last_close = df1.iloc[-1] last_close = df1.iloc[-1]['close']
for i, series in df2.iterrows(): for i, series in df2.iterrows():
chart.update(series) chart.update(series)

View File

@ -27,7 +27,7 @@ ___
```{py:method} update(series: pd.Series) ```{py:method} update(series: pd.Series, render_drawings: bool = False)
Updates the chart data from a bar. Updates the chart data from a bar.
Series labels should be akin to [`set`](#AbstractChart.set). Series labels should be akin to [`set`](#AbstractChart.set).
@ -105,7 +105,7 @@ ___
```{py:method} vertical_span(start_time: TIME | list | tuple, end_time: TIME = None, color: COLOR = 'rgba(252, 219, 3, 0.2)') ```{py:method} vertical_span(start_time: TIME | list | tuple, end_time: TIME = None, color: COLOR = 'rgba(252, 219, 3, 0.2)', round: bool = False)
Creates and returns a `VerticalSpan` object. Creates and returns a `VerticalSpan` object.

View File

@ -13,7 +13,7 @@ if __name__ == '__main__':
chart.show() chart.show()
last_close = df1.iloc[-1] last_close = df1.iloc[-1]['close']
for i, series in df2.iterrows(): for i, series in df2.iterrows():
chart.update(series) chart.update(series)

View File

@ -357,12 +357,15 @@ class SeriesCommon(Pane):
''') ''')
def vertical_span(self, start_time: Union[TIME, tuple, list], end_time: TIME = None, def vertical_span(self, start_time: Union[TIME, tuple, list], end_time: TIME = None,
color: str = 'rgba(252, 219, 3, 0.2)'): color: str = 'rgba(252, 219, 3, 0.2)', round: bool = False):
""" """
Creates a vertical line or span across the chart.\n Creates a vertical line or span across the chart.\n
Start time and end time can be used together, or end_time can be Start time and end time can be used together, or end_time can be
omitted and a single time or a list of times can be passed to start_time. omitted and a single time or a list of times can be passed to start_time.
""" """
if round:
start_time = self._single_datetime_format(start_time)
end_time = self._single_datetime_format(end_time) if end_time else None
return VerticalSpan(self, start_time, end_time, color) return VerticalSpan(self, start_time, end_time, color)
@ -590,7 +593,7 @@ class Candlestick(SeriesCommon):
{self.id}.chart.priceScale("right").applyOptions({{autoScale: true}}) {self.id}.chart.priceScale("right").applyOptions({{autoScale: true}})
''') ''')
def update(self, series: pd.Series, _from_tick=False): def update(self, series: pd.Series, render_drawings=False, _from_tick=False):
""" """
Updates the data from a bar; Updates the data from a bar;
if series['time'] is the same time as the last bar, the last bar will be overwritten.\n if series['time'] is the same time as the last bar, the last bar will be overwritten.\n
@ -607,7 +610,10 @@ class Candlestick(SeriesCommon):
if (stampToDate(lastBar({self.id}.data).time).getTime() === stampToDate({series['time']}).getTime()) {{ if (stampToDate(lastBar({self.id}.data).time).getTime() === stampToDate({series['time']}).getTime()) {{
{self.id}.data[{self.id}.data.length-1] = {bar} {self.id}.data[{self.id}.data.length-1] = {bar}
}} }}
else {self.id}.data.push({bar}) else {{
{self.id}.data.push({bar})
{f'{self.id}.toolBox.renderDrawings()' if render_drawings else ''}
}}
{self.id}.series.update({bar}) {self.id}.series.update({bar})
''') ''')
if 'volume' not in series: if 'volume' not in series:

View File

@ -10,6 +10,7 @@ if (!window.ToolBox) {
this.drawings = [] this.drawings = []
this.chart.cursor = 'default' this.chart.cursor = 'default'
this.makingDrawing = false this.makingDrawing = false
this.mouseDown = false
this.hoverBackgroundColor = 'rgba(80, 86, 94, 0.7)' this.hoverBackgroundColor = 'rgba(80, 86, 94, 0.7)'
this.clickBackgroundColor = 'rgba(90, 106, 104, 0.7)' this.clickBackgroundColor = 'rgba(90, 106, 104, 0.7)'
@ -270,7 +271,7 @@ if (!window.ToolBox) {
document.body.style.cursor = this.chart.cursor document.body.style.cursor = this.chart.cursor
hoveringOver = null hoveringOver = null
contextMenu.listen(false) contextMenu.listen(false)
if (!mouseDown) { if (!this.mouseDown) {
document.removeEventListener('mousedown', checkForClick) document.removeEventListener('mousedown', checkForClick)
document.removeEventListener('mouseup', checkForRelease) document.removeEventListener('mouseup', checkForRelease)
} }
@ -281,11 +282,11 @@ if (!window.ToolBox) {
let originalIndex let originalIndex
let originalTime let originalTime
let originalPrice let originalPrice
let mouseDown = false this.mouseDown = false
let clickedEnd = false let clickedEnd = false
let labelColor let labelColor
let checkForClick = (event) => { let checkForClick = (event) => {
mouseDown = true this.mouseDown = true
document.body.style.cursor = 'grabbing' document.body.style.cursor = 'grabbing'
this.chart.chart.applyOptions({handleScroll: false}) this.chart.chart.applyOptions({handleScroll: false})
this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false}) this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false})
@ -312,7 +313,7 @@ if (!window.ToolBox) {
document.removeEventListener('mousedown', checkForClick) document.removeEventListener('mousedown', checkForClick)
} }
let checkForRelease = (event) => { let checkForRelease = (event) => {
mouseDown = false this.mouseDown = false
document.body.style.cursor = this.chart.cursor document.body.style.cursor = this.chart.cursor
this.chart.chart.applyOptions({handleScroll: true}) this.chart.chart.applyOptions({handleScroll: true})
@ -330,7 +331,7 @@ if (!window.ToolBox) {
let checkForDrag = (param) => { let checkForDrag = (param) => {
if (!param.point) return if (!param.point) return
this.chart.chart.unsubscribeCrosshairMove(checkForDrag) this.chart.chart.unsubscribeCrosshairMove(checkForDrag)
if (!mouseDown) return if (!this.mouseDown) return
let priceAtCursor = this.chart.series.coordinateToPrice(param.point.y) let priceAtCursor = this.chart.series.coordinateToPrice(param.point.y)
@ -363,7 +364,7 @@ if (!window.ToolBox) {
let crosshairHandlerTrend = (param) => { let crosshairHandlerTrend = (param) => {
if (!param.point) return if (!param.point) return
this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerTrend) this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerTrend)
if (!mouseDown) return if (!this.mouseDown) return
let currentPrice = this.chart.series.coordinateToPrice(param.point.y) let currentPrice = this.chart.series.coordinateToPrice(param.point.y)
let currentTime = this.chart.chart.timeScale().coordinateToTime(param.point.x) let currentTime = this.chart.chart.timeScale().coordinateToTime(param.point.x)
@ -391,7 +392,7 @@ if (!window.ToolBox) {
let crosshairHandlerHorz = (param) => { let crosshairHandlerHorz = (param) => {
if (!param.point) return if (!param.point) return
this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerHorz) this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerHorz)
if (!mouseDown) return if (!this.mouseDown) return
hoveringOver.updatePrice(this.chart.series.coordinateToPrice(param.point.y)) hoveringOver.updatePrice(this.chart.series.coordinateToPrice(param.point.y))
setTimeout(() => { setTimeout(() => {
this.chart.chart.subscribeCrosshairMove(crosshairHandlerHorz) this.chart.chart.subscribeCrosshairMove(crosshairHandlerHorz)
@ -401,6 +402,7 @@ if (!window.ToolBox) {
} }
renderDrawings() { renderDrawings() {
if (this.mouseDown) return
this.drawings.forEach((item) => { this.drawings.forEach((item) => {
if ('price' in item) return if ('price' in item) return
let startDate = Math.round(item.from[0]/this.chart.interval)*this.chart.interval let startDate = Math.round(item.from[0]/this.chart.interval)*this.chart.interval
@ -414,9 +416,13 @@ if (!window.ToolBox) {
this.chart.series.removePriceLine(drawing.line) this.chart.series.removePriceLine(drawing.line)
} }
else { else {
let range = this.chart.chart.timeScale().getVisibleLogicalRange()
this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false}) this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false})
this.chart.chart.removeSeries(drawing.line); this.chart.chart.removeSeries(drawing.line);
this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: true}) this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: true})
this.chart.chart.timeScale().setVisibleLogicalRange(range)
} }
this.drawings.splice(this.drawings.indexOf(drawing), 1) this.drawings.splice(this.drawings.indexOf(drawing), 1)
this.saveDrawings() this.saveDrawings()

View File

@ -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.8', version='1.0.19',
packages=find_packages(), packages=find_packages(),
python_requires='>=3.8', python_requires='>=3.8',
install_requires=[ install_requires=[