使用chart.js绘画折线图、柱状图等图表

官网:https://github.com/chartjs/Chart.js

用到的是里面的chart.js-3.5.0\package\dist\chart.js文件

编写测试例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
	<script src="chart.js"></script>
</head>
<body>
<!--柱状图。用div容器包装,可修改图表大小、背景颜色-->
<div style="width:400px;height:300px;background-color:#0a073a;display:inline-block;margin-right:50px;">
	<canvas id="myChart" width="400" height="300" ></canvas>
</div>
<!--折线图-->
<div style="width:400px;height:300px;background-color:#0a073a;display:inline-block;">
	<canvas id="myChart2" width="400" height="300" ></canvas>
</div>

<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
      
      
    type: 'bar',
    data: {
      
      
        labels: ["颜值","身材","高冷","贴心","主见","可靠","聪明"],
        datasets: [{
      
      
            label: '特点值',
            data: [6.7,6.7,8.0,8.5,6.7,8.6,8.2],
            backgroundColor: [
                'rgba(255, 99, 132, 0.8)',
                'rgba(54, 162, 235, 0.8)',
                'rgba(255, 206, 86, 0.8)',
                'rgba(75, 192, 192, 0.8)',
                'rgba(153, 102, 255, 0.8)',
                'rgba(255, 159, 64, 0.8)',
				'rgba(208 ,100, 82,0.8)'
            ],
            borderColor: [
                'rgba(255,99,132,0.8)',
                'rgba(54, 162, 235, 0.8)',
                'rgba(255, 206, 86, 0.8)',
                'rgba(75, 192, 192, 0.8)',
                'rgba(153, 102, 255, 0.8)',
                'rgba(255, 159, 64,0.8)',
				'rgba(208 ,100, 82,0.8)'
            ],
            borderWidth: 1
        }]
    },
    options: {
      
      
		//从零开始
        scales: {
      
      
            yAxes: [{
      
      
                ticks: {
      
      
                    beginAtZero:true
                }
            }]
        }
    }
});

var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
      
      
    type: "line",
    data: {
      
      
        labels: ["2020.09", "2020.10", "2020.11","2020.12", "2021.01", "2021.02", "2021.03", "2021.04", "2021.05", "2021.06",  "2021.07", "2021.08"],
        datasets: [{
      
      
            label: '心动值',
            data: [5.1,5.5,6.5,7.3,8.1,8.4,8.7,9.1,9.2,9.3,9.2,9.4],
            backgroundColor: [
                'rgba(255, 99, 132, 0.8)',
                'rgba(54, 162, 235, 0.8)',
                'rgba(255, 206, 86, 0.8)',
                'rgba(75, 192, 192, 0.8)',
                'rgba(153, 102, 255, 0.8)',
                'rgba(255, 159, 64, 0.8)',
                'rgba(40,177,164,0.8)',
                'rgba(206, 51 ,27,0.8)',
                'rgba(42 ,187, 60,0.8)',
                'rgba(213, 34 ,199,0.8)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)',
				'rgba(40 ,177, 164,1)',
                'rgba(206, 51 ,27,1)',
                'rgba(42 ,187, 60,1)',
                'rgba(213, 34 ,199,1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
      
      
        scales: {
      
      
            yAxes: [{
      
      
                ticks: {
      
      
                    beginAtZero:true
                }
            }]
        }
    }
});
</script>

</body>
</html>

图表效果如下:
在这里插入图片描述
在学习绘画图表时发现的插件,但其实用起来还有点问题,比如图表大小的设置。

猜你喜欢

转载自blog.csdn.net/qq_38118138/article/details/119781720