Exporting Morris Plots to PDF?

I am using Morris.js to create graphs. I have a requirement to export pdf charts. I can see that graphs are svg elements. What should I do to achieve this.

+3


source to share


2 answers


I took one of Morris's samples and made a violin for you:

http://jsfiddle.net/1roLdqte/48/

I added a simple call to PDF formatting an existing div with just a morris chart:



$('#print').click(function () {
printMe();
});
function printMe() {
  xepOnline.Formatter.Format('line-example',{render:'download', srctype:'svg'});
 }

      

Run the fiddle and click the PDF button.

Note that there are many more options available here, you can format a lot more content than just the morris.js diagram, control page sizes, add headers and footers, etc. This only formats only the chart (srctype: 'svg') for PDF as a vector image (not a raster).

+3


source


It works. I've tried with morris.js v0.5.0 and Raphael 2.1.2.

Add this to where you have your diagram (like your controller):

$scope.pdf = function(chartName){
    printMorris(chartName);
};

function printMorris(chartName) {
    xepOnline.Formatter.Format(chartName, {render:'download', srctype:'svg'});
}

      

xepOnline.jqPlugin.008.js is incorrect. To resolve the error: "Uncaught TypeError: Unable to read property 'length' of null value on xepOnline.jqPlugin.008.js: 166", modify the code in xepOnline.jqPlugin.008.js.



Add this to line 166. This will skip the length when "rules" is null.

        if(rules === null)
            continue;

      

Now the code in the togglePrintMediaStyle function in xepOnline.jqPlugin.008.js:

togglePrintMediaStyle: function() {
    if($('head style[data-xeponline-formatting]').length > 0) {
        $('head style[data-xeponline-formatting]').remove();
        return;
    }
    var printrules = [];
    for(var x=0;x<document.styleSheets.length;x++) { 
        var rules=document.styleSheets[x].cssRules;
        var rule=[];
        if(rules === null)
            continue;
        for(var x2=0;x2<rules.length;x2++) {

            if(rules[x2].media && rules[x2].media && (rules[x2].media[0] === 'print' || 
                rules[x2].media && rules[x2].media.mediaText === 'print')) {
                for(var x3=0;x3<rules[x2].cssRules.length; x3++) {
                    rule.push(rules[x2].cssRules[x3]);
                }
            }  else if (rules[x2].parentStyleSheet.media[0] && 
                    rules[x2].parentStyleSheet.media[0] === 'print' ||
                    (rules[x2].parentStyleSheet.media && 
                        rules[x2].parentStyleSheet.media.mediaText === 'print')) {
                rule.push(rules[x2]);
            }
        }
        for(var x2=0;x2<rule.length;x2++) {
            printrules.push(rule[x2].cssText);  
        }
    }

    // write print media styles to head
    var html = '\n<style type="text/css" data-xeponline-formatting="true">\n';
    for(var x=0; x<printrules.length; x++) {
        html+='.xeponline-container ' + printrules[x] + '\n';
    }
    html += '</style>\n';
    $('head').append(html);
},

      

0


source







All Articles