Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
How to create a react-chartjs plugin

I have created a plugin for a pie chat. The idea of the plugin is to center the data in the middle of the Doughnut.

const [data1, setData1] = useState()
const [plugins, setPlugins] = useState([])
const dashboardInfo = useSelector((state) => state.dashboardInfo.data);
useEffect(() => {
    const xxxNE = dashboardInfo.division ? Object.values(dashboardInfo.division[0]) : [''];
    const sikPercentage = xxxNE[1] ? xxxNE[1][0].percentage : ''
    const trPercentage = xxxNE[1] ? xxxNE[1][1].percentage : ''
   
    setData1({
        labels: [ // legend labels
            "SIK: " + sikPercentage + '%',
            "TR: " + trPercentage + '%'
        ],
        datasets: [{
            label: '# of Votes',
            data: [2, 3],
            backgroundColor: [
                '#fb6340',
                'rgba(54, 162, 235, 0.5)',
            ],
            borderColor: ['#fff', '#fff'],
            borderWidth: 2,
            cutout: '90%',

        }]
    })

    setPlugins([{
        beforeDraw: function (chart) {
            var width = chart.width,
                height = chart.height,
                ctx = chart.ctx;
            ctx.restore();
            var fontSize = (height / 180).toFixed(2);
            ctx.font = fontSize + "em Montserrat";
            ctx.textBaseline = "top";
            ctx.textAlign = 'center';
            ctx.fillText(40 + ' / ' + sikPercentage  + '%', width / 2, height / 2);
            ctx.save();
        }
    }])

}, [dashboardInfoData, setData1, setPlugins])


return (
          <Doughnut
            data={data1}
            plugins={plugins}
            options={{
                legend: false,
                legendCallback: function (circleData) {
                    var ul = document.createElement('ul');
                    var borderColor = circleData.datasets[0].borderColor;
                    var dataValue = circleData.datasets[0].data;
                    circleData.data.labels.forEach(function (label, index) {
                        ul.innerHTML += `
               <li>
                 <span style="background-color: ${borderColor[index]}
                   ";></span> ${label} ${dataValue[index]}
               </li>
             `;
                    });
                    return ul.outerHTML;
                },
            }
            }
        />

The issue is that the plugin is not showing the data. Any idea what I am missing ?

Thank you

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!