Home  >  Article  >  Web Front-end  >  How to use chart animation effects in Highcharts

How to use chart animation effects in Highcharts

PHPz
PHPzOriginal
2023-12-17 08:11:17931browse

How to use chart animation effects in Highcharts

Highcharts is a powerful JavaScript charting library that provides many chart types and customization options. It also has easy-to-use animation effects that make data visualization more vivid. In this article, we will learn how to use chart animation effects in Highcharts and demonstrate several specific code examples.

  1. Understand the animation API of Highcharts
    Highcharts provides many different animation APIs, including setting animation effects directly in options, using Highcharts built-in animations, custom animations, and setting animations using global options. . These APIs can be studied in depth through the Highcharts documentation. Below we will use a simple example to demonstrate how to use animation effects in Highcharts.
  2. Set chart animation effect
    In Highcharts, we can make the chart move by setting animation effects in the options. The following is the code for an example of a circular progress bar:
Highcharts.chart('container', {
  chart: {
    type: 'pie',
    animation: true, // 开启动画
  },
  title: {
    text: '圆形进度条',
  },
  plotOptions: {
    pie: {
      dataLabels: {
        enabled: false,
      },
      startAngle: -90,
      endAngle: 90,
      center: ['50%', '75%'],
      size: '150%',
      colors: ['#64E572', '#FFFF00', '#FD6666'],
    },
  },
});

In the above code, we set animation to true in the chart option, which means that the animation effect is turned on. Next, we will customize the data and bind them to the chart.

  1. Custom animation
    In addition to setting animation effects in the options, we can also use custom animations. Custom animations in Highcharts are controlled by the options.animation property. Here is an example of animation implemented by SVG paths:
// 创建SVG路径动画效果
var chart = Highcharts.chart('container', {
    chart: {
        type: 'spline',
        animation: {
            //动画时长
            duration: 2000,
            //定义一个缓动函数
            easing: 'easeOutBounce'
        }
    },
    title: {
        text: '数据展示'
    },
    xAxis: {
        categories: ['1月', '2月', '3月', '4月', '5月', '6月',
            '7月', '8月', '9月', '10月', '11月', '12月']
    },
    yAxis: {
        title: {
            text: '人数'
        }
    },
    series: [{
        name: '男',
        data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 24.2, 23.3, 18.3, 13.9, 9.6]
    }, {
        name: '女',
        data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6]
    }]
});

//实现SVG路径动画
var path = chart.series[0].graph.element.getAttribute('d');
var pathLength = chart.series[0].graph.element.getTotalLength();
//设置dashStyle为ShortDash可以让线条更明显的显示出来
chart.series[0].update({
    animation: {
        duration: 2000,
        easing: 'easeOutBounce'
    },
    dashStyle: 'ShortDash',
    lineWidth: 2,
    marker: {
        enabled: false,
        radius: 4,
        symbol: 'square'
    },
    dataLabels: {
        enabled: false
    }
});

In this example, we create an animation effect implemented by SVG paths. Through the options.animation.duration and options.animation.easing properties, you can set the animation duration and easing function respectively. Next, we bind data to the chart through the options.series property and implement SVG path animation.

Summary
Highcharts provides a variety of animation effects. We can set and customize animation effects through simple code to make charts more vivid. In actual data display, reasonable use of animation effects can help us better express the data and arouse the audience's interest.

The above is the detailed content of How to use chart animation effects in Highcharts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn