WebChart and MobileChart to Picture

I want to use the following cross-platform code to convert my Xojo Chart to a Picture. This works fine on Desktop, but WebChart.DrawInto(…) and MobileChart.DrawInto(…) are not available for Web/iOS.

Var tempPicture As New Picture(myChartXojo.Width, myChartXojo.Height)
myChartXojo.ChartXojo.DrawInto(tempPicture.Graphics, 0, 0)

How can I convert my WebChart and MobileChart into a Picture?

There is a Feature Request for WebChart:
Issue #72195

In the meantime, here is a way to do it (you can wrap it as an Extension). I’ve posted it a while back in this forum thread in the Spanish section.

Here is the code for a WebButton.Pressed event:

Var js() As String

js.Add("Chart.helpers.each(Chart.instances, (instance) => {")
js.Add("  var parent = instance.canvas.closest('.XojoCharts').id || '';")
js.Add("  if (parent === '" + Chart1.ControlID + "') {")
js.Add("    XojoWeb.session.storage.setItem('chart-png', instance.toBase64Image(), true);")
js.Add("    XojoWeb.session.storage.getItem('chart-png', true);")
js.Add("  }")
js.Add("});")

ExecuteJavaScript(String.FromArray(js))

You’ll have the picture available in the Session.RequestedData event:

Select Case Key
Case "chart-png"
  Var imageData As String = DecodeBase64(Value.Replace("data:image/png;base64,", ""))
  WebPage1.ImageViewer1.Picture = New WebPicture(imageData, "chart.png")
End Select

I hope that helps.

Hi @David_Cox

MobileUIControl lacks the DrawInto method that is available on the Desktop target.

Please, feel free to file a Feature Request to retrieve a Chart instance as Picture.

@Ricardo_Cruz I have this working, thank you.

@Javier_Menendez I have added a Feature Request:

https://tracker.xojo.com/xojoinc/xojo/-/issues/77063