You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I experienced a really weird behavior with my labels and don't know why. Im using this package in combination with chartjs-node-canvas.
Description
The provided example code has three different for loop heads which work/don't work.
// run only for top=60 => perfect resultfor(lettop=60;top<=60;top+=5){// run a few values for top more => only a few badfor(lettop=40;top<70;top+=5){// run from 0 to 100 in 5er steps => everything badfor(lettop=0;top<100;top+=5){
The weird part is, that it works if the for loop only runs one iteration. If it runs only a few iterations, it's sometimes correct and if its runs 20 iterations nothing is correct.
Screenshots
Correct:
Wrong:
Look at the placement of the labels. In the correct one, the labels are placed ontop of the bars, in the wrong one they are wrapped somehow.
Steps to reproduce
Create a folder and add the example code provided below
Create a folder next to the index.ts file called a.
Run the file with npx ts-node index.ts and try all 3 for heads. (Remember to remove all files in a/ first to have no conflicts)
import{ChartJSNodeCanvas}from"chartjs-node-canvas";import{ChartConfiguration}from"chart.js";import"chartjs-plugin-datalabels";// to fix typesimport{writeFileSync}from"fs";constabc="ABCDEFGHIJKLMNOPQRSTUVWXYZ";constsrcData=newArray(100).fill(0).map((_,i)=>({x: abc[i%(abc.length-1)],y: i}));(async()=>{// run only for top=60 => perfect result// for (let top = 60; top <= 60; top += 5) {// run a few values for top more => only a few badfor(lettop=40;top<70;top+=5){// run from 0 to 100 in 5er steps => everything bad// for (let top = 0; top < 100; top += 5) {constsliced=srcData.slice(0,top);// import via require as it doesnt work otherwise// eslint-disable-next-line @typescript-eslint/no-var-requiresconstChartDataLabels=require("chartjs-plugin-datalabels");constscale=2;constchartRenderer=newChartJSNodeCanvas({width: scale*Math.max(400,40*sliced.length),height: scale*400,chartCallback: async(ChartJS)=>{ChartJS.register(ChartDataLabels);},});constchart: ChartConfiguration={type: "bar",data: {labels: sliced.map((x)=>x.x),datasets: [{label: "ABC",data: sliced.map((x)=>x.y*2),backgroundColor: "#f43543",datalabels: {labels: {relative: {align: "end",anchor: "end",color: "#ffffff",offset: scale*12,font: {size: scale*10,weight: "bold",},formatter: (_value,ctx)=>`${~~(sliced[ctx.dataIndex].y*10**3)/10}%`,},},},},],},};constpictureBuffer=awaitchartRenderer.renderToBuffer(chart);writeFileSync(`./a/chart${top}.png`,pictureBuffer);}})();
The text was updated successfully, but these errors were encountered:
This must be related to this library, because doing the require in the for loop with some tweeks to fresh require it, everything is working fine.
// https://github.com/hughsk/fresh-requireexportconstfreshRequire: /*NodeJS.Require*/(id: string)=>any=(file)=>{constresolvedFile=require.resolve(file);consttemp=require.cache[resolvedFile];deleterequire.cache[resolvedFile];// eslint-disable-next-line @typescript-eslint/no-var-requiresconstmodified=require(resolvedFile);require.cache[resolvedFile]=temp;returnmodified;};// run only for top=60 => perfect result//for (let top = 45; top <= 45; top += 5) {// run a few values for top more => only a few badfor(lettop=40;top<70;top+=5){// run from 0 to 100 in 5er steps => everything bad// for (let top = 0; top < 100; top += 5) {// import via require as it doesnt work otherwise// eslint-disable-next-line @typescript-eslint/no-var-requiresconstChartDataLabels=freshRequire('chartjs-plugin-datalabels');[...]
Interesting. I've tried wrapping a response in a setTimeout() and calling update Chart again, but no luck...
It'd be great to get some input from the plugin author, as I have a similar issue: #422
Hello,
I experienced a really weird behavior with my labels and don't know why. Im using this package in combination with
chartjs-node-canvas
.Description
The provided example code has three different for loop heads which work/don't work.
The weird part is, that it works if the for loop only runs one iteration. If it runs only a few iterations, it's sometimes correct and if its runs 20 iterations nothing is correct.
Screenshots
Correct:
![working](https://user-images.githubusercontent.com/76838159/215261689-f29d7e4b-14e0-45f1-afb3-072b88087a02.png)
Wrong:
![not-working](https://user-images.githubusercontent.com/76838159/215261696-9c80c6a2-6b21-45d3-bda4-bb65b6f6a98d.png)
Look at the placement of the labels. In the correct one, the labels are placed ontop of the bars, in the wrong one they are wrapped somehow.
Steps to reproduce
index.ts
file calleda
.npx ts-node index.ts
and try all 3 for heads. (Remember to remove all files ina/
first to have no conflicts)Example code
package.json
:The text was updated successfully, but these errors were encountered: