mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

[charts] Add animation to Gauge chart

Open Annastrin opened this issue 1 year ago • 1 comments

Summary

Can you please make the Gauge animated (like the Pie chart)?

Examples

No response

Motivation

No response

Search keywords: gauge animation

Annastrin avatar Oct 30 '24 05:10 Annastrin

Thanks for opening this @Annastrin ... I am pretty sure this is on the teams radar already, but in case it isn't I'll add this to the board! 👍🏼

michelengelen avatar Oct 30 '24 10:10 michelengelen

That sounds nice, but il will come with somewhat a performance impact, because the <path /> will need to be recomputed each time.

The other option is to use a cursor, and apply some CSS transition/animation to it

alexfauquette avatar Nov 04 '24 13:11 alexfauquette

If you do the animation outside of React there shouldn't really be much of a perf cost to it (assuming you use this lightweight animate function):

diff --git a/packages/x-charts/src/Gauge/GaugeValueArc.tsx b/packages/x-charts/src/Gauge/GaugeValueArc.tsx
index b05975a86..29b723b65 100644
--- a/packages/x-charts/src/Gauge/GaugeValueArc.tsx
+++ b/packages/x-charts/src/Gauge/GaugeValueArc.tsx
@@ -32,8 +32,29 @@ export function GaugeValueArc(props: React.ComponentProps<'path'>) {
   const valueAngle =
     startAngle + ((value - valueMin) / (valueMax - valueMin)) * (endAngle - startAngle);
 
+  const ref = React.useRef(null)
+  React.useEffect(() => {
+    if (!shouldAnimate) { return undefined }
+
+    return animate({
+      from: startAngle,
+      to: valueAngle,
+      onChange: (angle: any) => {
+        ref.current.setAttribute('d',
+          d3Arc().cornerRadius(cornerRadius)({
+            startAngle,
+            endAngle: angle,
+            innerRadius,
+            outerRadius,
+          })
+        )
+      }
+    }).cancel
+  })
+
   return (
     <StyledPath
+      ref={ref}
       transform={`translate(${cx}, ${cy})`}
       d={
         d3Arc().cornerRadius(cornerRadius)({

romgrk avatar Nov 04 '24 13:11 romgrk

Fixed by #17304 will be availabel in v8

alexfauquette avatar Apr 14 '25 14:04 alexfauquette

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

github-actions[bot] avatar Apr 14 '25 14:04 github-actions[bot]