I need to export a dojo chart to an image format preferably PNG. Unfortunality there is no in-built feature but I have found a way around for this. As dojo charts are based on SVG graphics:
I will first get the SVG string after the chart is rendered, then send this string to server via ajax and then use ImageMagick php library to convert it into PNG. Now the whole process is working properly but there is an issue:
The SVG string i get from dojo Charts does not contain full details, the chart title and axis values and markers are not present in SVG string, what can be the possible issue?
Following is the code in which I have re-printed the extracted SVG string from dojo Chart into another div, you can see the difference visually.
var mychart;
require(["dojo/ready", "dojox/charting/Chart2D", "dojox/gfx/utils", "dojox/charting/action2d/Tooltip", "dojox/charting/action2d/Highlight", "dojox/charting/themes/Claro"], function (ready, Chart, Utils, Tooltip, Highlight, ClaroTheme) {
ready(function () {
mychart = Chart("mychart");
mychart.title = "My Chart";
mychart.addPlot("column_plot", {
type: "Columns",
lines: true,
areas: false,
markers: true
});
var column_tooltip = new Tooltip(mychart, "column_plot");
var column_highlight = new Highlight(mychart, "column_plot");
mychart.addAxis("x", {vertical: false});
mychart.addAxis("y", {vertical: true});
mychart.addSeries("column_series", [1, 3, 5, 2, 6, 1, 0, 4, 6, 4, 1], {
plot: "column_plot"
});
mychart.setTheme(ClaroTheme);
mychart.render();
//this function will be called after render to send SVG to server via AJAX
document.getElementById("syncbutton").onclick = function (e) {
var svgString = Utils.toSvg(mychart.surface).results[0];
document.getElementById("svgchart").innerHTML = svgString;
};
});
});