//1、安装脚手架
npm install -g @vue/cli
npm install vue-chartjs chart.js
//2、新建Chart.vue组件
<template>
<div class="chart">
<canvas ref="chart"></canvas>
</div>
</template>
<script>
import { Line } from 'vue-chartjs'
export default {
extends: Line,
props: ['data', 'options'],
mounted () {
this.renderChart(this.data, this.options)
}
}
</script>
<style scoped>
.chart {
position: relative;
width: 100%;
height: 400px;
}
</style>
//3、页面引入
<template>
<div class="statistics">
<chart :data="chartData" :options="chartOptions"></chart>
</div>
</template>
<script>
import Chart from '@/components/Chart'
export default {
components: {
Chart
},
data () {
return {
chartData: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'Sales',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: 'Profit',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
data: [28, 48, 40, 19, 86, 27, 90]
}
]
},
chartOptions: {
responsive: true,
maintainAspectRatio: false
}
}
}
}
</script>
<style scoped>
.statistics {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>