Apexcharts.js: How to Export Chart as Image using external button.

Created on 24 Jul 2019  路  8Comments  路  Source: apexcharts/apexcharts.js

I created a dashboard with multiple apex charts controls on it. I need to give users a feature to export whole page as image. Right now, i see an option to download individual component only. But How can I call this function from outside click event to export chart as image (svg or png).

Most helpful comment

I do this using the global Apex object dataURI method:
https://apexcharts.com/docs/methods/#dataURI

This will give you a base64 encoded PNG - not sure if you can get the SVG this way.

Example function:

const saveToArchive = async (chartId) => {
  const chartInstance = window.Apex._chartInstances.find(chart => chart.id === chartId);
  const base64 = await chartInstance.chart.dataURI();
  return base64;
}

All 8 comments

I do this using the global Apex object dataURI method:
https://apexcharts.com/docs/methods/#dataURI

This will give you a base64 encoded PNG - not sure if you can get the SVG this way.

Example function:

const saveToArchive = async (chartId) => {
  const chartInstance = window.Apex._chartInstances.find(chart => chart.id === chartId);
  const base64 = await chartInstance.chart.dataURI();
  return base64;
}

@georgehardy dateURI( ) method always returns PNG image , but what about SVG images , any idea ?

@prasad1791
You can do this using https://apexcharts.com/docs/methods/#paper

const paper = chartInstance.chart.paper()
const svg = paper.svg()

@georgehardy Yes, that's the correct way of getting SVG string.

Yes I already implemented that in my current project thanks @georgehardy and @junedchhipa

Hi, I tried calling paper() but the result was undefined, am I doing it in the wrong place or is the method not there anymore? I would need the svg from the chart.

var chart1 = new ApexCharts(document.querySelector("#chart1"), options1);
console.log(chart1.paper())

Adding to what @rujaraju already said above, this link (https://apexcharts.com/docs/methods/#paper) does not contain any mention of the paper method. Moreover, when I searched for paper using the search function, it did not return any result. So, I guess the method is not working anymore.

Hi

I looked at the Apex Charts source code and found a way to get the SVG.
It is not something that looks really OK, as this can change, but you can always adapt.

As the javascript is not so restrictive in messing up with internal data from the objects, I'm obtaining the svg by reading the data from:

myChartInstance.chart.w.globals.base.dom.Paper.svg()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

drew-dulgar picture drew-dulgar  路  3Comments

rudeayelo picture rudeayelo  路  3Comments

jlil picture jlil  路  3Comments

Sumon-miazi picture Sumon-miazi  路  3Comments

cstlaurent picture cstlaurent  路  3Comments