diff --git a/README.md b/README.md index 9846502..58c1e90 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ if __name__ == '__main__': chart.show() - last_close = df1.iloc[-1] + last_close = df1.iloc[-1]['close'] for i, series in df2.iterrows(): chart.update(series) diff --git a/docs/source/reference/abstract_chart.md b/docs/source/reference/abstract_chart.md index dcca55c..ee3f6f7 100644 --- a/docs/source/reference/abstract_chart.md +++ b/docs/source/reference/abstract_chart.md @@ -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. 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. diff --git a/examples/2_live_data/live_data.py b/examples/2_live_data/live_data.py index b4c0164..2563fc5 100644 --- a/examples/2_live_data/live_data.py +++ b/examples/2_live_data/live_data.py @@ -13,7 +13,7 @@ if __name__ == '__main__': chart.show() - last_close = df1.iloc[-1] + last_close = df1.iloc[-1]['close'] for i, series in df2.iterrows(): chart.update(series) diff --git a/lightweight_charts/abstract.py b/lightweight_charts/abstract.py index 41575e9..9518bb3 100644 --- a/lightweight_charts/abstract.py +++ b/lightweight_charts/abstract.py @@ -357,12 +357,15 @@ class SeriesCommon(Pane): ''') 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 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. """ + 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) @@ -590,7 +593,7 @@ class Candlestick(SeriesCommon): {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; 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()) {{ {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}) ''') if 'volume' not in series: diff --git a/lightweight_charts/js/toolbox.js b/lightweight_charts/js/toolbox.js index 34bd86c..c33296b 100644 --- a/lightweight_charts/js/toolbox.js +++ b/lightweight_charts/js/toolbox.js @@ -10,6 +10,7 @@ if (!window.ToolBox) { this.drawings = [] this.chart.cursor = 'default' this.makingDrawing = false + this.mouseDown = false this.hoverBackgroundColor = 'rgba(80, 86, 94, 0.7)' this.clickBackgroundColor = 'rgba(90, 106, 104, 0.7)' @@ -270,7 +271,7 @@ if (!window.ToolBox) { document.body.style.cursor = this.chart.cursor hoveringOver = null contextMenu.listen(false) - if (!mouseDown) { + if (!this.mouseDown) { document.removeEventListener('mousedown', checkForClick) document.removeEventListener('mouseup', checkForRelease) } @@ -281,11 +282,11 @@ if (!window.ToolBox) { let originalIndex let originalTime let originalPrice - let mouseDown = false + this.mouseDown = false let clickedEnd = false let labelColor let checkForClick = (event) => { - mouseDown = true + this.mouseDown = true document.body.style.cursor = 'grabbing' this.chart.chart.applyOptions({handleScroll: false}) this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false}) @@ -312,7 +313,7 @@ if (!window.ToolBox) { document.removeEventListener('mousedown', checkForClick) } let checkForRelease = (event) => { - mouseDown = false + this.mouseDown = false document.body.style.cursor = this.chart.cursor this.chart.chart.applyOptions({handleScroll: true}) @@ -330,7 +331,7 @@ if (!window.ToolBox) { let checkForDrag = (param) => { if (!param.point) return this.chart.chart.unsubscribeCrosshairMove(checkForDrag) - if (!mouseDown) return + if (!this.mouseDown) return let priceAtCursor = this.chart.series.coordinateToPrice(param.point.y) @@ -363,7 +364,7 @@ if (!window.ToolBox) { let crosshairHandlerTrend = (param) => { if (!param.point) return this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerTrend) - if (!mouseDown) return + if (!this.mouseDown) return let currentPrice = this.chart.series.coordinateToPrice(param.point.y) let currentTime = this.chart.chart.timeScale().coordinateToTime(param.point.x) @@ -391,7 +392,7 @@ if (!window.ToolBox) { let crosshairHandlerHorz = (param) => { if (!param.point) return this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerHorz) - if (!mouseDown) return + if (!this.mouseDown) return hoveringOver.updatePrice(this.chart.series.coordinateToPrice(param.point.y)) setTimeout(() => { this.chart.chart.subscribeCrosshairMove(crosshairHandlerHorz) @@ -401,6 +402,7 @@ if (!window.ToolBox) { } renderDrawings() { + if (this.mouseDown) return this.drawings.forEach((item) => { if ('price' in item) return 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) } else { + let range = this.chart.chart.timeScale().getVisibleLogicalRange() + this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: false}) this.chart.chart.removeSeries(drawing.line); this.chart.chart.timeScale().applyOptions({shiftVisibleRangeOnNewBar: true}) + + this.chart.chart.timeScale().setVisibleLogicalRange(range) } this.drawings.splice(this.drawings.indexOf(drawing), 1) this.saveDrawings() diff --git a/setup.py b/setup.py index 9359e59..106f5e7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r', encoding='utf-8') as f: setup( name='lightweight_charts', - version='1.0.18.8', + version='1.0.19', packages=find_packages(), python_requires='>=3.8', install_requires=[