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

doughnutoffset included in chart-js #1216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ayush1234567434324
Copy link

Doughnut Offset

Manipulated offset via on visiting canvas

Main changes 1

const chartRef = useRef(null);

Handle Chart Click function

const handleChartClick = (e) => {
    const chart = chartRef.current;
    const points = chart.getElementsAtEventForMode(e, 'nearest', { intersect: true }, true);

    if (points.length) {
      const firstPoint = points[0];

      // Apply the offset effect
      chart.data.datasets[firstPoint.datasetIndex].offset[firstPoint.index] = 60;
      chart.update();

      // Reset the offset after a brief delay
      setTimeout(() => {
        chart.data.datasets[firstPoint.datasetIndex].offset[firstPoint.index] = 0;
        chart.update();
      }, 1000); // Adjust the delay time as needed
    }
  };  

Doughnut Component


  <Doughnut
      data={data}
      options={{
        maintainAspectRatio: true,
        responsive: true,
        onClick: (e) => handleChartClick(e), // Call the function for chart click events
        plugins: {
          legend: {
            onClick:  handleLegendClick// Call the function for legend item click events
          },
        },
      }}
      ref={chartRef}
    /> 

Doughnut canvas simulation(visiting chart)

doughnutoffset canvas

Manipulated offset via on visiting labels

Main changes 2

Legend click handler

 const handleLegendClick = (e, legendItem,legend) => {
    const chart = legend.chart;
    const index = legendItem.index;

    // Apply the offset effect
    chart.data.datasets[0].offset[index] = 60;
    chart.tooltip.setActiveElements([{ datasetIndex: 0, index }]);
    chart.update();

    // Reset the offset after a brief delay
    setTimeout(() => {
      chart.data.datasets[0].offset[index] = 0;
      chart.update();
    }, 1000); // Adjust the delay time as needed
  };

Doughnut component

<Doughnut
      data={data}
      options={{
        maintainAspectRatio: true,
        responsive: true,
        onClick: (e) => handleChartClick(e), // Call the function for chart click events
        plugins: {
          legend: {
            onClick:  handleLegendClick// Call the function for legend item click events
          },
        },
      }}
      ref={chartRef}
    />


Doughnut canvas simulation(visiting label)

doughnutoffset labels

@Ayush1234567434324
Copy link
Author

👍

Copy link

@Imsaksham0204 Imsaksham0204 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Successfully merging this pull request may close these issues.

2 participants