让前端飞WEB前端程序开发

用 Chart.js 画扇形图并显示标签

2017-12-24  本文已影响418人  高正杰

Chart.js 的最新版本可以在 https://github.com/chartjs/Chart.js/releases 下载,也可以使用 CDN https://cdnjs.com/libraries/Chart.js

<!DOCTYPE html>
<html>
<head>
    <title>chart.js 扇形图</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
</head>
<body>
    <div  style="width:40%">
    <canvas id="myChart" width="200" height="200"></canvas>
    </div>
    <script>
    var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'pie',
        data: {
            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
            datasets: [{
                label: '# of Votes',
                data: [12, 19, 3, 5, 2, 3],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
            }]
        },
        options: {
            responsive: true
        }
    });
    </script>
</body>
</html>

运行结果如下所示,当你的鼠标移动到上面的时候会显示对应的标签和值。



该文章于2017年11月30日于CSDN上首次发表,2017年12月24日搬家至此!

上一篇下一篇

猜你喜欢

热点阅读