Skip to content

Commit

Permalink
Added the render_drawings parameter to update
Browse files Browse the repository at this point in the history
Added the `round` parameter to `vertical_span`
  • Loading branch information
louisnw01 committed Jan 21, 2024
1 parent f31db04 commit 5861da2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/source/reference/abstract_chart.md
Expand Up @@ -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).
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/2_live_data/live_data.py
Expand Up @@ -13,7 +13,7 @@

chart.show()

last_close = df1.iloc[-1]
last_close = df1.iloc[-1]['close']

for i, series in df2.iterrows():
chart.update(series)
Expand Down
12 changes: 9 additions & 3 deletions lightweight_charts/abstract.py
Expand Up @@ -357,12 +357,15 @@ def _toggle_data(self, arg):
''')

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)


Expand Down Expand Up @@ -590,7 +593,7 @@ def set(self, df: pd.DataFrame = None, render_drawings=False):
{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
Expand All @@ -607,7 +610,10 @@ def update(self, series: pd.Series, _from_tick=False):
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:
Expand Down
20 changes: 13 additions & 7 deletions lightweight_charts/js/toolbox.js
Expand Up @@ -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)'
Expand Down Expand Up @@ -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)
}
Expand All @@ -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})
Expand All @@ -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})
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@

setup(
name='lightweight_charts',
version='1.0.18.8',
version='1.0.19',
packages=find_packages(),
python_requires='>=3.8',
install_requires=[
Expand Down

0 comments on commit 5861da2

Please sign in to comment.