Files
lightweight-charts-python/docs/source/charts.md
louisnw 34ce3f7199 Refactoring/Enhancements/Fixes
Breaking Changes:
- Removed the `api` parameter; callbacks no longer need to be in a specific class.
- Topbar callbacks now take a chart as an argument (see updated callback examples)
- Removed the `topbar` parameter from chart declaration. The Topbar will be automatically created upon declaration of a topbar widget.
- Removed the `searchbox` parameter from charts. It will be created upon subscribing to it in `chart.events`.
- Removed `dynamic_loading`.
- Removed ‘volume_enabled’ parameter. Volume will be enabled if the volumn column is present in the dataframe.
- Widgets’ `func` parameter is now declared last.
- Switchers take a tuple of options rather than a variable number of arguments.
- `add_hotkey` renamed to `hotkey`
- Horizontal lines now take a `func` argument rather than `interactive`. This event will emit the Line object that was moved.
- Removed the `name` parameter from `line.set`. Line object names are now declared upon creation.

Enhancements:
- Added the `button` widget to the Topbar.
- Added the color picker to the drawing context menu.
- Charts now have a `candle_data` method, which returns the current data displayed on the chart as a DataFrame.
- Fixed callbacks are now located in the `chart.events` object:
    - search (e.g `chart.events.search += on_search`)
    - new_bar
    - range_change
- Added volume to the legend
- Drawings can now be accessed through `chart.toolbox.drawings`
- added the `style` and `name` parameters to `create_line`

Bug Fixes:
- Fixed a bug causing new charts not to load after `exit` was called.
- Refactored rayline placement to ensure they do not move the visible range.
- Fixed a bug causing the visible range to shift when trendlines are moved past the final candlestick.
- Fixed a bug preventing trendlines and raylines on irregular timeframes.
- Fixed a bug causing the legend to prevent mouse input into the chart.
2023-08-14 16:06:16 +01:00

4.7 KiB

Charts

This page contains a reference to all chart objects that can be used within the library. They all have access to the common methods.


Chart

width: int | height: int | x: int | y: int | on_top: bool | maximize: bool | debug: bool | toolbox: bool

The main object used for the normal functionality of lightweight-charts-python, built on the pywebview library.

The `Chart` object should be defined within an `if __name__ == '__main__'` block.

show

block: bool

Shows the chart window, blocking until the chart has loaded. If block is enabled, the method will block code execution until the window is closed.


hide

Hides the chart window, which can be later shown by calling chart.show().


exit

Exits and destroys the chart window.


show_async

block: bool

Show the chart asynchronously.


screenshot

-> bytes

Takes a screenshot of the chart, and returns a bytes object containing the image. For example:

if __name__ == '__main__':
    chart = Chart()
    df = pd.read_csv('ohlcv.csv')
    chart.set(df)
    chart.show()
    
    img = chart.screenshot()
    with open('screenshot.png', 'wb') as f:
        f.write(img)
This method should be called after the chart window has loaded.

QtChart

widget: QWidget

The QtChart object allows the use of charts within a QMainWindow object, and has similar functionality to the Chart object for manipulating data, configuring and styling.

Either the PyQt5 or PySide6 libraries will work with this chart.

Callbacks can be received through the Qt event loop.


get_webview

-> QWebEngineView

Returns the QWebEngineView object.


Example:

import pandas as pd
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget

from lightweight_charts.widgets import QtChart

app = QApplication([])
window = QMainWindow()
layout = QVBoxLayout()
widget = QWidget()
widget.setLayout(layout)

window.resize(800, 500)
layout.setContentsMargins(0, 0, 0, 0)

chart = QtChart(widget)

df = pd.read_csv('ohlcv.csv')
chart.set(df)

layout.addWidget(chart.get_webview())

window.setCentralWidget(widget)
window.show()

app.exec_()

WxChart

parent: wx.Panel

The WxChart object allows the use of charts within a wx.Frame object, and has similar functionality to the Chart object for manipulating data, configuring and styling.

Callbacks can be received through the Wx event loop.


get_webview

-> wx.html2.WebView

Returns a wx.html2.WebView object which can be used to for positioning and styling within wxPython.


Example:

import wx
import pandas as pd

from lightweight_charts.widgets import WxChart


class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None)
        self.SetSize(1000, 500)

        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(sizer)

        chart = WxChart(panel)

        df = pd.read_csv('ohlcv.csv')
        chart.set(df)

        sizer.Add(chart.get_webview(), 1, wx.EXPAND | wx.ALL)
        sizer.Layout()
        self.Show()


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()


StreamlitChart

The StreamlitChart object allows the use of charts within a Streamlit app, and has similar functionality to the Chart object for manipulating data, configuring and styling.

This object only supports the displaying of static data, and should not be used with the update_from_tick or update methods. Every call to the chart object must occur before calling load.


load

Loads the chart into the Streamlit app. This should be called after setting, styling, and configuring the chart, as no further calls to the StreamlitChart will be acknowledged.


Example:

import pandas as pd
from lightweight_charts.widgets import StreamlitChart

chart = StreamlitChart(width=900, height=600)

df = pd.read_csv('ohlcv.csv')
chart.set(df)

chart.load()

JupyterChart

The JupyterChart object allows the use of charts within a notebook, and has similar functionality to the Chart object for manipulating data, configuring and styling.

This object only supports the displaying of static data, and should not be used with the update_from_tick or update methods. Every call to the chart object must occur before calling load.


load

Renders the chart. This should be called after setting, styling, and configuring the chart, as no further calls to the JupyterChart will be acknowledged.


Example:

import pandas as pd
from lightweight_charts import JupyterChart

chart = JupyterChart()

df = pd.read_csv('ohlcv.csv')
chart.set(df)

chart.load()