LayTec-AG / Plotly.Blazor

This library packages the well-known charting library plotly.js into a razor component that can be used in a Blazor project.
MIT License
340 stars 48 forks source link

In Plotly.Blazor 4.3.0, setting layout Margin or Title causes PlotlyChart.ToImage thread to hang #424

Closed jtenney-alamar closed 2 months ago

jtenney-alamar commented 3 months ago

Simply never returns from ToImage() unless I leave Layout.Margin and Layout.Title as null.

However, it does work when I press the "Camera" to create an image.

Drawing a scatter plot with log-log X and Y axes.

sean-mcl commented 2 months ago

Will check it out next week. :)

sean-mcl commented 2 months ago

This issue is likely caused by the default settings of BlazorServer. I have mentioned it in the XML comment of the ToImage method.

Please make sure to add the following to your Program.cs:

builder.Services.AddServerSideBlazor().AddHubOptions(options => {
    options.MaximumReceiveMessageSize = null; // no limit or specify a number
});

The ToImage method returns the base64 string of the image with some prefix at the beginning.

You can use this output string directly in an image element or convert the returned string into a file using the following snippet:

var base64Image = await chart.ToImage(ImageFormat.Png, 512, 512);
var imageData = base64Image[(base64Image.IndexOf(",", StringComparison.Ordinal) + 1)..];
var imageBytes = Convert.FromBase64String(imageData);

await File.WriteAllBytesAsync("output.png", imageBytes);

This snippet removes the prefix from the base64 string and converts the remaining data into a PNG file named output.png.