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

Bug: NodeJS Canvas Render Aligns Labels Wrongly #422

Open
USMortality opened this issue Sep 6, 2024 · 0 comments
Open

Bug: NodeJS Canvas Render Aligns Labels Wrongly #422

USMortality opened this issue Sep 6, 2024 · 0 comments

Comments

@USMortality
Copy link

With this example: https://codepen.io/usmortality/pen/KKjbzrj the datalabels render correctly.
Screenshot 2024-09-05 at 6 23 50 PM

And the same rendered via simple nodejs/canvas server side renderer do not:
localhost

import express from 'express'
import path from 'path'
import { createCanvas } from 'canvas'
import { Chart, registerables } from 'chart.js'
import ChartDataLabels from 'chartjs-plugin-datalabels'

import { fileURLToPath } from 'url'

global.window = Object.assign(globalThis, {})

Chart.register(...registerables, ChartDataLabels)

const app = express()
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
app.use(
  express.static(path.resolve(__dirname, '../../client/dist/'), {
    index: false,
  })
)

app.get('/', async (_, res) => {
  const canvas = createCanvas(600, 600)
  const ctx = canvas.getContext('2d')

  Chart.register(ChartDataLabels)
  const data = {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [
      {
        type: 'bar',
        label: '1st Cycle',
        data: [65, 59, 80, 81, 56, 55, 40],
        datalabels: {
          align: 'end',
          anchor: 'end',
        },
        borderColor: ['rgb(54, 162, 235)'],
        backgroundColor: ['rgba(54, 162, 235, 0.2)'],
        borderWidth: 1,
      },
      {
        type: 'bar',
        label: '2nd Cycle',
        data: [45, -29, 40, 51, -36, 75, -60],
        borderColor: ['rgb(255, 159, 64)'],
        backgroundColor: ['rgba(255, 159, 64, 0.2)'],
        borderWidth: 1,
      },
      {
        type: 'line',
        label: 'Target',
        data: [100, 100, 100, 100, 100, 100, 100],
        datalabels: {
          align: 'start',
          anchor: 'start',
        },
        borderColor: 'rgb(75, 192, 192)',
      },
    ],
  }

  const config = {
    type: 'scatter',
    data: data,
    options: {
      scales: {
        y: {
          beginAtZero: true,
          title: {
            display: true,
            text: 'Percentage',
          },
        },
      },
      plugins: {
        title: {
          display: true,
          text: 'Locality Coverage For IRS Jan-Jun, Year xxxx (Target 100%)',
        },
        legend: {
          position: 'bottom',
        },
        datalabels: {
          display: true,
          color: 'black',
          anchor: 'end',
          align: 'top',
          formatter: (value, context) => {
            return value + '%'
          },
        },
      },
    },
  }

  new Chart(ctx, config)
  const image = canvas.toBuffer()
  res.set('Content-Type', 'image/png')
  res.send(image)
})

const port = process.env.PORT ?? 5000
app.listen(port, () =>
  console.log('Server is running on http://localhost:' + port)
)

Any ideas why?

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