0

https://jsfiddle.net/Abhi_Solanki/9yktct3z/3/ here fiddle link..

what i am trying to do is to show the same output as the highcharts show when it is exported in PDF format or SVG format, infact in this code i have used the same function that is used to generate SVG file of Highcharts the output of that seems proper to me

but the problem arises when i pass that variable into canvg.js file calling canvg at function that time the output of the image gets blurry

where i want the output to remain same as it is without pixels getting blurred as it is same in the highcharts pdf. as i have to display that image of chart in a pdf file so please help. code is as below.


var svgres = chart.getSVG();
var svgArr = [],
   top = 0,
   height = 0,
   width = 0,
   col=0;
   svgCustDim = 400;
   var svgWidth = 600,
   svg = svgres.replace('<svg', '<g transform="translate(' + col * svgWidth + ',' + top  + ')" ');
   svg = svg.replace('</svg>', '</g>');
   svg = '<svg height="' + svgCustDim + '" width="' + svgCustDim*(3)+ '" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';
canvg('canvas', svg);
var canvas = document.getElementById('canvas');    
return canvas.toDataURL("image/jpeg");
Abhishek Solanki
  • 43
  • 1
  • 1
  • 8

1 Answers1

0

The version of canvg used in the demo page on github is buggy. If i take the script from here https://github.com/canvg/canvg/blob/master/canvg.js and paste it in the fiddle it works fine.

You can read more about it here https://github.com/canvg/canvg/issues/486.

Also your canvas is a lot bigger than the image, you can take the chart width and height so that the canvas is the same dimension as the chart.

svgCustDim = chart.chartHeight;
var svgWidth = chart.chartWidth,
svg = svgres.replace('<svg', '<g');
svg = svg.replace('</svg>', '</g>');
svg = '<svg height="' + svgCustDim + '" width="' + svgWidth+ '" version="1.1" xmlns="http://www.w3.org/2000/svg">' + svg + '</svg>';

Here is a fiddle with the canvg.js taken from the url above and the changes so that your canvas is the same dimension as your chart https://jsfiddle.net/9yktct3z/6/

Note that it appear that the minified version (canvg.min.js) is

if you want a bigger resolution image remove the height and width from you chart container

<div id="container" style="margin: 0 auto"></div>

and set them in the chart option, i used 2000*2000 but you play around with the values

chart: {
    height: 2000,
    width: 2000,
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false,
    type: 'pie'
},

here is the full fiddle https://jsfiddle.net/9yktct3z/10/

Liviu Boboia
  • 1,734
  • 1
  • 10
  • 21
  • thanks @LiviuBoboia it was helpful.. and one more thing i would like to ask is that do you know any method where you display image in a PDF file and it does not get blurry because still by using this new canvg file the JPEG or PNG images are getting blurred while in SVG resolution and pixels everything remains the same...

    it will be really helpful if there is any solution to this...

    Thank you.
    – Abhishek Solanki Mar 30 '17 at 08:45
  • how are you adding generating the images? because if i put the result of the canvas in the image it looks fine https://jsfiddle.net/9yktct3z/7/ – Liviu Boboia Mar 30 '17 at 08:55
  • first i am generating SVG then passing that data into canvg to create canvas and them fetching image URL through toDataURL method and i am using pdfmake.min.js for generating PDF and in that i am using by default image tag to display images in it. – Abhishek Solanki Mar 30 '17 at 10:48
  • `var svgres = chart.getSVG(); var svgArr = [], top = 0, height = 0, width = 0, col=0; svgCustDim = 400; var svgWidth = 600, svg = svgres.replace('', ''); svg = '' + svg + ''; canvg('canvas', svgres); var canvas = document.getElementById('canvas'); return canvas.toDataURL("image/svg+xml"); }` – Abhishek Solanki Mar 30 '17 at 10:53
  • that code is in your original fiddle, please create a fiddle that generates the pdf – Liviu Boboia Mar 30 '17 at 10:55
  • [jsFiddle](https://jsfiddle.net/9yktct3z/6/)Please copy paste this actually i am new to stackoverflow question answer part so. this jsFiddle is the once in which you gave me a solution in that have a look at the canvas generated image by zooming and you will find pixelated. – Abhishek Solanki Mar 30 '17 at 10:59
  • do you want a higher resolution image? of course images get pixalated when you zoom in – Liviu Boboia Mar 30 '17 at 11:08
  • Yes thats what i am looking for and i know that image gets pixalated but for that only i am looking for a solution. and the code for PDF that is getting generated is a 3000 lines js file in angularjs so it would not run properly in js without the services. – Abhishek Solanki Mar 30 '17 at 11:18
  • i updated the answer with the code for a higher image resolution – Liviu Boboia Mar 30 '17 at 11:32