I am trying to render chart with image using pointStyle. I implemented this chart on client side and its working fine but when i try to render chart using nodejs (SSR) its show only chart without an image.
Nodejs Code
var express = require("express");
const { Canvas, loadImage } = require("canvas");
const { CanvasRenderService } = require("chartjs-node-canvas");
var router = express.Router();
async function initChartData() {
const img = await loadImage("https://i.imgur.com/yDYW1I7.png");
const canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
console.log('<img src="' + canvas.toDataURL() + '" />');
const data = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
datasets: [
{
label: "Tokyo",
fill: false,
borderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointRadius: 5,
pointStyle: '<img src="' + canvas.toDataURL() + '" />', // or ['',img,'','','','']
data: [0, 7, 8, 14, 18, 22],
},
],
};
const configuration = {
type: "line",
data: data,
options: {
responsive: false,
animation: false,
maintainAspectRatio: false,
},
};
return configuration;
}
const mkChart = async (params) => {
const conf = await initChartData();
const canvasRenderService = new CanvasRenderService(400, 400);
return await canvasRenderService.renderToBuffer(conf);
};
router.get("/", async function (req, res, next) {
var image = await mkChart(req.query);
res.type("image/png");
res.send(image);
});
module.exports = router;
Package.json
"dependencies": {
"canvas": "^2.8.0",
"chart.js": "^3.5.1",
"chartjs-node-canvas": "^3.2.0",
"cookie-parser": "~1.4.4",
"express": "~4.16.1",
}
Client side demo : https://jsfiddle.net/3t1qguy7/1/ Repository https://github.com/ahmedosamaalam/node-chartjs/tree/main