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]: Re-rendered multiple time while using afterDraw #1215

Open
1 task
LittleCuong opened this issue Jun 12, 2024 · 0 comments
Open
1 task

[Bug]: Re-rendered multiple time while using afterDraw #1215

LittleCuong opened this issue Jun 12, 2024 · 0 comments
Labels

Comments

@LittleCuong
Copy link

Would you like to work on a fix?

  • Check this if you would like to implement a PR, we are more than happy to help you go through the process.

Current and expected behavior

I am making a stacked bar chart with label text is replaced with image but my handleDrawImage runs multiple times in the first-time component render and hovering to display tooltip that causing performance issue

   const handleDrawImage = (chart: ChartJS) => {
    const { ctx } = chart;
    const dataLength = plans ? plans.length : 0;
    const chartHeight = chart.chartArea.height;
    const step = (chartHeight - barSize * dataLength) / dataLength;
    const yOffset = 30;
    ctx.save();
    plans?.forEach((item, i) => {
        let newImage = new Image();
        newImage.src = "Image source here";
        const imageY = i * (barSize + step) + yOffset;
        ctx.drawImage(newImage, 0, imageY, 30, 30);
    });
    ctx.restore();
 };
 
 return (
    <div className="col-span-9">
        <div className="w-full flex items-center justify-center">
            <Bar
                data={data}
                height={400}
                options={chartOptions}
                redraw={true}
                plugins={[
                    {
                        id: 'sectorBackground',
                        afterDraw: (chart) => handleDrawImage(chart),
                    },
                    ChartDataLabels,
                ]}
            />
        </div>
    </div>
 );

Reproduction

N/A

chart.js version

"^4.4.3"

react-chartjs-2 version

"^5.2.0"

Possible solution

No response

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

No branches or pull requests

1 participant