B4J Code Snippet [PyBridge] Visualization of hierarchical data with Plotly

Plotly is a very rich and powerful Python library for the generation of interactive charts: https://plotly.com/graphing-libraries/
The output of Plotly, in this case, is a html string that is loaded to a WebView.

As a first example of Plotly I'm demonstrating how to visualize hierarchical data:

1753689080213.png
1753689097066.png

1753689111517.png
1753700668586-png.165625

The (outdated) data source: https://gist.github.com/tiagodealmeida/0b97ccf117252d742dddf098bc6cc58a

Dependencies:
B4X:
pip install plotly
pip install "plotly[express]"
pip install pandas
The data is loaded into a 2d table - a list of lists:
B4X:
Dim Data As List = B4XCollections.CreateList(Null)
    For Each c As Map In RawData
        Dim Country As String = c.Get("countryName")
        Dim Population As Long = c.Get("population")
        Dim Continent As String = c.Get("continentName")
        Data.Add(Array(Continent, Country, Population))
    Next
It is then converted to Python Dataframe and the html is generated by the provided code.

The Path parameter specified the hierarchical order. As we don't have a single root node in the data it is added with the Constant method:
B4X:
Dim Path As List = Array(px.Run("Constant").Arg("World"), "Continent", "Country")

Same example in a server configuration: https://www.b4x.com/android/forum/threads/pybridge-server-online-charts-with-plotly.167983/
 

Attachments

  • Plotly.zip
    12.2 KB · Views: 39
Last edited:

thetrueman

Active Member
Thanks for great addition... I just like to see Real Time Data Graphing as I searched that Plotly has ability to do so.
Can we have an example as I want to use it with B4R sensors data plotting in real time. Thanks.
 
Top