score:8

Accepted answer

add viewportsize and zoomfactor in your phantomjs page:

await page.property('viewportsize', { height: 1600, width: 3600 });
await page.property('zoomfactor', 4);

and add in your html head template

<script>
  window.devicepixelratio = 4;
</script>

score:-1

i can confirm that the @devtrong response is working with phantomjs 2.1.1

the only difference is that i set in my settings file:

    page.viewportsize = { width: 3600, height: 1600 };
    page.zoomfactor = 4;


note: its very important to set in you html this part:

<//script>
  window.devicepixelratio = 4;
<//script> (fix the script tag)

score:0

chart.js now has the parameter "devicepixelratio". this allows you to increase the resolution directly in chart.js. (normal 96dpi. target 300dpi; 300/96 = 3.125)

options:{
      devicepixelratio: 3
}

documentation: https://www.chartjs.org/docs/3.0.2/configuration/device-pixel-ratio.html

score:2

try setting the zoom factor using a higher dpi for paper in relation to screen dpi:

page.zoomfactor = 300 / 96;   // or use / 72

must be set after page size is defined.

you could also check out this answer:
poor quality of png images drawn into html canvas

score:2

for phantom 2, i rendered the charts with a large canvas to get the resolution up and then converted it into a png finally destroying and removing the old canvas and replacing them with an image with responsive css classes. adjusting the knobes on the canvas width and height in addition to chart.js options will get you a perfect render. we were able to get our rendering speed up with the approach (alternative to svg renders) and the file size down.

html:

<div class="container">
  <!-- generated images -->
  <img id="someidimage" class="img-responsive"></img>

  <!-- temporary canvas -->
  <canvas id="someid" width="2000" height="600"></canvas>    
</div>

javascript:

/**
 * _plot() plot with chart.js
 *
 * @param {function} callback
 */
 function _plot(callback) {
  var config = {}; // some chart.js config
  var id = 'someid';
  var el = document.queryselector('#' + id);
  var el2d = el.getcontext('2d');

  // plot instance
  var instance = new chart(el2d, config);

  // generate and append image
  document.queryselector('#' + id + 'image').setattribute('src', el.todataurl('image/png'));

  // destroy instance
  instance.destroy();
  el.parentelement.removechild(el);

  // callback
  if (callback) {
    callback();
  }
}

Related Query

More Query from same tag