Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

doughnutoffset included in chart-js #1216

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

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

Conversation

Copy link

@Ayush1234567434324 Ayush1234567434324 commented Jun 24, 2024

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

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

Ayush1234567434324 reacted with eyes emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
1 more reviewer

@Imsaksham0204 Imsaksham0204 Imsaksham0204 approved these changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /