Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backgroundColour not applied to full height of image with devicePixelRatio: 2 #132

Open
smarr opened this issue May 1, 2023 · 1 comment

Comments

@smarr
Copy link

smarr commented May 1, 2023

When setting the backgroundColour and devicePixelRatio: 2, the background color is applied to the correct width, but not the full hight.

I assume that the issue is that the hight is the one not scaled to the 2x ratio.

const canvasOptions: ChartJSNodeCanvasOptions = {
    width,
    height,
    backgroundColour: '#f00'
}

const chartJSNodeCanvas = new ChartJSNodeCanvas(canvasOptions);

const configuration = {
  // ...
  options: {
    devicePixelRatio: 2,
  }
};

chartJSNodeCanvas.renderToBuffer(configuration);

Versions

  • Chart.JS version: 3.9.1
  • chartj-node-canvas version: 4.1.6

I currently work around this by using the following chartCallback:

  const canvasOptions: ChartJSNodeCanvasOptions = {
    width,
    height,
    backgroundColour,
    chartCallback: (ChartJS) => {
      ChartJS.register({
        id: 'my_background_color',
        beforeDraw: (chart, _options) => {
          const ctx = chart.ctx;
          ctx.save();
          ctx.fillStyle = backgroundColour;
          ctx.fillRect(0, 0, width, chart.height);  // <- note the `chart.height` here, which gives the correct result. I am not sure why just `width` works though. It's a bit odd.
          ctx.restore();
        }
      });
    }
  };
@smarr
Copy link
Author

smarr commented May 1, 2023

And just for good measures: workaround based on #7 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant