B4J Question How to save the chart to image file?

positrom2

Active Member
Licensed User
Longtime User
I am starting with this (it is a revised version of that available in this forum):
http://www.rwblinn.de/b4j/opensource/roavgsplit.zip
The Layout is made with SceneBuilder, and then:
Sub createlineChart(nTime As Double, nDistance As Int)
' The label is already set using the scene builder - but here to show how to change these
' XAxisJO.RunMethod("setLabel",Array As Object("Time"))
' YAxisJO.RunMethod("setLabel",Array As Object("Km/Hr for " & nDistance))

' Clear the data from the line chart by using remove the series javaobject
' Check if data is available
If LCGetDataJO.IsInitialized Then LCGetDataJO.RunMethod("remove",Array As Object(SeriesJO))

' Check distance and time. if zero then do nothing
If nTime = 0 Then Return
If nDistance = 0 Then Return

' Define the data series and set the name
SeriesJO.InitializeNewInstance("javafx.scene.chart.XYChart.Series",Null)
SeriesJO.RunMethod("setName",Array As Object("Time/Speed for " & nDistance & "m"))
I tried this but it doen't work (file not found error):
B4X:
Sub saveform
Dim Snapshot As Image
Snapshot.initialize("d:\","1.png")
Dim out As OutputStream=File.OpenOutput("d:\","1.png",False)
Snapshot.WriteToStream(out)
out.Close
End Sub
Thank you in advance.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code is wrong. You are trying to load an image and then save it back.

I'm not familiar with this library. However all nodes have a Snapshot method that returns an image from the node (assuming that the library was built correctly).

For example to save a TableView:
B4X:
Dim out As OutputStream = File.OpenOutput(...)
TableView1.Snapshot.WriteToStream(out)
out.Close
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
That was a close hit...
This does it:
B4X:
Sub savechart
Dim out As OutputStream = File.OpenOutput("d:\","2.png",False)
LineChartXY.Snapshot.WriteToStream(out)
out.Close
End Sub
Thank you.
 
Upvote 0
Top