Get the FREE Ultimate OpenClaw Setup Guide →

chartjs-animations

npx machina-cli add skill sjnims/chartjs-expert/chartjs-animations --openclaw
Files (1)
SKILL.md
10.8 KB

Chart.js Animations (v4.5.1)

Complete guide to configuring and customizing animations in Chart.js.

Animation Basics

Chart.js animates charts automatically. Animations provide visual feedback during chart initialization, data updates, and user interactions.

Animation Configuration Structure

Three main configuration keys:

KeyPurposeExample Use Case
animationGlobal animation settingsDuration, easing, delay
animationsProperty-specific animationsAnimate only x-axis values
transitionsMode-specific animationsCustom hover behavior

Configuration Locations

// Global configuration (all charts)
Chart.defaults.animation.duration = 2000;

// Chart instance configuration
const chart = new Chart(ctx, {
  options: {
    animation: {
      duration: 1500,
      easing: 'easeInOutQuart'
    }
  }
});

// Dataset-specific configuration
datasets: [{
  label: 'Sales',
  data: [10, 20, 30],
  animation: {
    duration: 3000  // This dataset animates slower
  }
}]

Main Animation Options

Namespace: options.animation

OptionTypeDefaultDescription
durationnumber1000Animation length in milliseconds
easingstring'easeOutQuart'Easing function name
delaynumberundefinedDelay before animation starts (ms)
loopbooleanundefinedLoop animation endlessly

Basic Example

const chart = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
    animation: {
      duration: 2000,           // 2 seconds
      easing: 'easeInOutCubic', // Smooth start and end
      delay: 500                 // Wait 0.5s before starting
    }
  }
});

Looping Animation

options: {
  animation: {
    duration: 2000,
    loop: true,  // Repeat forever
    easing: 'linear'
  }
}

Property-Specific Animations

Configure animations for individual properties.

Namespace: options.animations[animationName]

OptionTypeDescription
propertiesstring[]Properties to animate (defaults to key name)
typestring'number', 'color', or 'boolean'
fromnumber|Color|booleanStart value
tonumber|Color|booleanEnd value
fnfunctionCustom interpolator

Default Property Animations

// Chart.js default animations
animations: {
  numbers: {
    type: 'number',
    properties: ['x', 'y', 'borderWidth', 'radius', 'tension']
  },
  colors: {
    type: 'color',
    properties: ['color', 'borderColor', 'backgroundColor']
  }
}

Custom Property Animation

options: {
  animations: {
    // Animate only Y values
    y: {
      duration: 2000,
      from: 0,  // Start from bottom
      easing: 'easeOutBounce'
    },
    // Animate only colors
    colors: {
      duration: 1000,
      from: 'transparent'
    },
    // Disable X animation
    x: false
  }
}

Tension Animation (Line Charts)

options: {
  animations: {
    tension: {
      duration: 1000,
      easing: 'linear',
      from: 1,    // Very curvy
      to: 0,      // Straight lines
      loop: true  // Continuously animate
    }
  }
}

Transitions

Transitions define animations for specific interaction modes.

Built-in Transition Modes

ModeWhen TriggeredDefault Duration
activeHover/focus400ms
resizeChart resize0ms (instant)
showDataset shown via legend1000ms
hideDataset hidden via legend1000ms
resetChart reset1000ms

Customizing Hover Animation

options: {
  transitions: {
    active: {
      animation: {
        duration: 600,  // Slower hover animation
        easing: 'easeOutElastic'
      }
    }
  }
}

Show/Hide Animations

options: {
  transitions: {
    show: {
      animations: {
        x: { from: 0 },
        y: { from: 0 }
      }
    },
    hide: {
      animations: {
        x: { to: 0 },
        y: { to: 0 }
      }
    }
  }
}

Custom Transition Mode

// Trigger custom transition
chart.update('myCustomMode');

// Define custom transition
options: {
  transitions: {
    myCustomMode: {
      animation: {
        duration: 500,
        easing: 'linear'
      }
    }
  }
}

Easing Functions

Chart.js provides 36 easing functions.

Easing Categories

CategoryFunctionsVisual Effect
LinearlinearConstant speed
QuadeaseInQuad, easeOutQuad, easeInOutQuadGradual acceleration/deceleration
CubiceaseInCubic, easeOutCubic, easeInOutCubicMore pronounced curves
QuarteaseInQuart, easeOutQuart, easeInOutQuartStrong acceleration
QuinteaseInQuint, easeOutQuint, easeInOutQuintVery strong acceleration
SineeaseInSine, easeOutSine, easeInOutSineSmooth, gentle curves
ExpoeaseInExpo, easeOutExpo, easeInOutExpoExponential growth
CirceaseInCirc, easeOutCirc, easeInOutCircCircular motion
ElasticeaseInElastic, easeOutElastic, easeInOutElasticSpring/bounce back
BackeaseInBack, easeOutBack, easeInOutBackOvershoot then settle
BounceeaseInBounce, easeOutBounce, easeInOutBounceBouncing ball effect

Easing Pattern Meanings

  • easeIn: Slow start, fast end
  • easeOut: Fast start, slow end (most natural)
  • easeInOut: Slow start, fast middle, slow end

Common Easing Choices

// Default (smooth and natural)
easing: 'easeOutQuart'

// Bouncy entrance
easing: 'easeOutBounce'

// Elastic snap
easing: 'easeOutElastic'

// Smooth both ends
easing: 'easeInOutCubic'

// Constant speed
easing: 'linear'

Animation Callbacks

Execute custom code during animations.

Namespace: options.animation

CallbackWhen CalledParameters
onProgressEach animation frameAnimation state object
onCompleteAnimation finishesAnimation state object

Callback Parameters

{
  chart: Chart,           // Chart instance
  currentStep: number,    // Current animation step
  initial: boolean,       // True for initial chart animation
  numSteps: number        // Total animation steps
}

Progress Bar Example

const progressBar = document.getElementById('progress');

const chart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: {
    animation: {
      duration: 2000,
      onProgress: (animation) => {
        const progress = (animation.currentStep / animation.numSteps) * 100;
        progressBar.style.width = progress + '%';
      },
      onComplete: (animation) => {
        console.log('Animation finished!');
        progressBar.style.width = '100%';
      }
    }
  }
});

Trigger Action After Animation

options: {
  animation: {
    onComplete: (animation) => {
      if (animation.initial) {
        // Only run after initial chart render
        showDataTable();
      }
    }
  }
}

Disabling Animations

Disable All Animations

// Instance-level
options: {
  animation: false
}

// Global default
Chart.defaults.animation = false;

Disable Specific Animations

options: {
  animations: {
    colors: false,  // Disable color animations
    x: false        // Disable x-axis animations
  }
}

Disable Transition Modes

options: {
  transitions: {
    active: {
      animation: {
        duration: 0  // No hover animation
      }
    }
  }
}

Advanced Patterns

Progressive Line Reveal

options: {
  animation: {
    duration: 2000,
    onProgress: function(animation) {
      const chartInstance = animation.chart;
      const ctx = chartInstance.ctx;
      const meta = chartInstance.getDatasetMeta(0);

      // Draw line progressively
      const progress = animation.currentStep / animation.numSteps;
      // Custom rendering logic here
    }
  }
}

Staggered Animation

options: {
  animation: {
    delay: (context) => {
      let delay = 0;
      if (context.type === 'data' && context.mode === 'default') {
        delay = context.dataIndex * 50;  // 50ms delay per data point
      }
      return delay;
    }
  }
}

Drop Animation

options: {
  animations: {
    y: {
      duration: 2000,
      from: (ctx) => {
        // Drop from top of chart
        return ctx.chart.scales.y.getPixelForValue(
          ctx.chart.scales.y.max
        );
      },
      easing: 'easeOutBounce'
    }
  }
}

Color Fade Transition

options: {
  animations: {
    backgroundColor: {
      type: 'color',
      duration: 1000,
      from: 'transparent',
      to: 'rgba(255, 99, 132, 0.5)',
      easing: 'easeInOutQuad'
    }
  }
}

Performance Considerations

Reduce Animation Duration for Large Datasets

const dataPoints = data.datasets[0].data.length;

options: {
  animation: {
    duration: dataPoints > 100 ? 500 : 1000  // Faster for large datasets
  }
}

Skip Animations on Updates

// No animation for this update
chart.update('none');

// Or specific mode
chart.update('active');  // Use 'active' transition

Responsive Animation Handling

options: {
  transitions: {
    resize: {
      animation: {
        duration: 0  // Instant resize (default)
      }
    }
  }
}

Framework Integration

React (react-chartjs-2)

import { Line } from 'react-chartjs-2';

function AnimatedChart() {
  const options = {
    animation: {
      duration: 2000,
      easing: 'easeOutQuart',
      onComplete: () => {
        console.log('Animation done!');
      }
    }
  };

  return <Line data={data} options={options} />;
}

Vue (vue-chartjs)

<script setup>
import { Line } from 'vue-chartjs';

const chartOptions = {
  animation: {
    duration: 1500,
    easing: 'easeInOutCubic'
  }
};
</script>

<template>
  <Line :data="chartData" :options="chartOptions" />
</template>

Additional Resources

  • See references/easing-functions.md for visual easing function guide
  • See references/animation-patterns.md for advanced animation patterns
  • See examples/progressive-animation.html for progressive line reveal example
  • See examples/staggered-bars.html for staggered bar animation example

Source

git clone https://github.com/sjnims/chartjs-expert/blob/main/skills/chartjs-animations/SKILL.mdView on GitHub

Overview

This skill explains how to configure and customize Chart.js v4.5.1 animations. It covers global, property-specific, and transition animations, including duration, easing, delay, looping, and onComplete/onProgress callbacks. It helps you tailor animation behavior for initial renders and data updates.

How This Skill Works

Animations are organized into three configuration keys: animation (global defaults), animations (property-specific), and transitions (mode-specific). You can set global defaults via Chart.defaults.animation or options.animation, target individual properties with options.animations, and customize interaction behavior with transitions. The guide also shows per-dataset animation settings to vary timing across datasets.

When to Use It

  • When you need global control over chart animation duration and easing
  • When you want to animate only specific properties or datasets
  • When configuring hover or focus behaviors with transitions
  • When you need animation delays, looping, or progressive line animation
  • When you require onProgress or onComplete callbacks to trigger actions after animation

Quick Start

  1. Step 1: Update chart options to set animation: { duration: 2000, easing: 'easeInOutQuart', delay: 500 }
  2. Step 2: Add per-property animations under options.animations or dataset.animation to target specific properties
  3. Step 3: Include onProgress/onComplete callbacks in the animation config and/or use transitions for hover behavior

Best Practices

  • Prefer consistent global animation defaults for uniform charts
  • Use per-property animations selectively to avoid performance issues
  • Test with large datasets and adjust duration or easing for smooth rendering
  • Utilize transitions to enhance interactivity without heavy code
  • Leverage onProgress/onComplete callbacks to synchronize UI actions

Example Use Cases

  • Initial chart render with duration 2000ms and easeInOutCubic
  • Animate only Y values with a 1500ms duration
  • Disable animation for real-time data feeds to reduce churn
  • Implement hover transition mode to smooth highlight effects
  • Use onComplete to fetch new data after the animation finishes

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers