B4J Code Snippet [PyBridge] Pygal - nice charting library

1740031896532.png



This library produces interactive SVG, which can be loaded with WebView.
It has many features. Small example:
B4X:
Dim PyGal As PyWrapper = Py.ImportModule("pygal")
Dim PyGalStyle As PyWrapper = Py.ImportModule("pygal.style")
Dim Chart As PyWrapper = PyGal.Run("StackedLine") _
    .ArgNamed("style", PyGalStyle.GetField("CleanStyle")).ArgNamed("fill", True).ArgNamed("interpolate", "cubic")
Chart.Run("add").Arg("A").Arg(Array(1, 3,  5, 16, 13, 3,  7))
Chart.Run("add").Arg("B").Arg(Array(5, 2,  3,  2,  5, 7, 17))
Chart.Run("add").Arg("C").Arg(Array(6, 10, 9,  7,  3, 1,  0))
Chart.Run("add").Arg("D").Arg(Array(2,  3, 5,  9, 12, 9,  5))
Chart.Run("add").Arg("E").Arg(Array(7,  4, 2,  1,  2, 10, 0))
'Chart.SetField("title", "This is the title") 'uncomment with BETA #3+
Wait For (Chart.Run("render").Fetch) Complete (Result As PyWrapper)
File.WriteBytes(xui.DefaultFolder, "temp.svg", Result.Value)
WebView1.LoadUrl(xui.FileUri(xui.DefaultFolder, "temp.svg"))

PyWrapper.SetField is a new method that will be added in the next beta.

Dependencies:
pip install pygal
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1740032710344.png


B4X:
Dim PyGal As PyWrapper = Py.ImportModule("pygal")
Dim PyGalStyle As PyWrapper = Py.ImportModule("pygal.style")
Dim Chart As PyWrapper = PyGal.Run("Radar") _
    .ArgNamed("style", PyGalStyle.GetField("NeonStyle")).ArgNamed("fill", True)
Chart.SetField("x_labels", Array("Richards", "DeltaBlue", "Crypto", "RayTrace", "EarleyBoyer", "RegExp", "Splay", "NavierStokes"))
Chart.Run("add").Arg("Chrome").Arg(Array(6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607))
Chart.Run("add").Arg("Firefox").Arg(Array(7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450))
Chart.Run("add").Arg("Opera").Arg(Array(3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669))
Chart.Run("add").Arg("IE").Arg(Array(43, 41, 59, 79, 144, 136, 34, 102))
Chart.SetField("title", "V8 benchmark results")
Wait For (Chart.Run("render").Fetch) Complete (Result As PyWrapper)
File.WriteBytes(xui.DefaultFolder, "temp.svg", Result.Value)
WebView1.LoadUrl(xui.FileUri(xui.DefaultFolder, "temp.svg"))

Online version of this chart: https://www.b4x.com/basic4android/images/temp.svg
 
Last edited:
Top