How to export the chart as an image (PNG format) without displaying UI?
In button click, you can export the chart as an image in ASP.NET Core Chart. To export the chart, you need to specify the export settings’ properties and call the export method at client-side.
The following code sample demonstrates the initialization of chart export settings and calling the export method.
<input type="button" value="PNG" onclick="chartPng()" /> <ej-chart id="container" enable-canvas-rendering="true"> </ej-chart> <script> //To call export method and settings function chartPng(sender) { var chart = $("#container").ejChart("instance"), exporting = chart.model.exportSettings; exporting.fileName = "chart"; exporting.angle = 0; exporting.action = "Home/ExportChart"; type = "png"; exporting.mode = "server"; chart.export(); }
</script>
|
At server-side, export the chart to a specific folder. You can specify your required path in which the chart needs to be saved as an image.
// To specify corresponding folder static string path = "D:\\";
// To export chart image in specified folder public void ExportChart(string Data, string ChartModel) { ChartProperties obj = ConvertChartObject(ChartModel); string type = obj.ExportSettings.Type.ToString().ToLower(); string fileName = obj.ExportSettings.FileName; string orientation = obj.ExportSettings.Orientation.ToString();
Data = Data.Remove(0, Data.IndexOf(',') + 1); MemoryStream stream = new MemoryStream(Convert.FromBase64String(Data));
using (FileStream fs = new FileStream(path + "CoreChart.png", FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(Data); bw.Write(data); bw.Dispose(); } } } // To create chart properties private ChartProperties ConvertChartObject(string ChartModel) { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; ChartProperties chartProp = new ChartProperties(); chartProp = JsonConvert.DeserializeObject<ChartProperties>(ChartModel, settings); return chartProp; }
|
Screenshot:
Sample: ExportSample
Conclusion
I hope you enjoyed learning about how to export the chart as an image (PNG format) without displaying UI.
You can refer to our ASP.NET Core Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Chart example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!
This looks promising. Would be cool to have something similar in Blazor as well: https://www.syncfusion.com/feedback/11257/possibility-to-get-a-chart-as-image-in-c-without-exporting-it-as-image-file