B4J Question xChart Class Save chart to PNG or JPG

rasaliad

Member
Licensed User
Longtime User
Hi Community

How can I save a Chart to a image?

I am planning a program in B4J, specifically using the jTelegramBot library, where with a command I am going to read the data from a database and then generate a graph either Pie, linear or bar and then save the graph in an image in disc and then be returned to telegram.

The question is how to generate the graph in runtime and save it to disk as an image, is that possible?

I'm will use the Klaus xChart Library.

Thanks since now.
 

klaus

Expert
Licensed User
Longtime User
You can get a bitmap from a chart with the Snapshot method and do whatever you want with it.

Example code from the demo program saving a chart as a jpeg file.

B4X:
    Private bmp As B4XBitmap
    Private Out As OutputStream
    xui.SetDataFolder("ChartsDemo")
    Out = File.OpenOutput(xui.DefaultFolder, "Test.jpg", False)
    bmp = YXChart1.Snapshot
    bmp.WriteToStream(Out, 100, "JPEG")
    Out.Close
 
Upvote 1

rasaliad

Member
Licensed User
Longtime User
You can get a bitmap from a chart with the Snapshot method and do whatever you want with it.

Example code from the demo program saving a chart as a jpeg file.

B4X:
    Private bmp As B4XBitmap
    Private Out As OutputStream
    xui.SetDataFolder("ChartsDemo")
    Out = File.OpenOutput(xui.DefaultFolder, "Test.jpg", False)
    bmp = YXChart1.Snapshot
    bmp.WriteToStream(Out, 100, "JPEG")
    Out.Close

Thanks you Klaus. It do the work.
 
Upvote 0
Top