Element+Echarts:制作可视化卡片

来自CloudWiki
跳转至: 导航搜索

先备知识

实训步骤

效果图

Vue2022072401.png

代码

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>VUE第一个案例-helloWorld</title>
		<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>

		<!-- 引入样式 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
		<!-- 引入组件库 -->
		<script src="https://unpkg.com/element-ui/lib/index.js"></script>
		<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
		<style type="text/css">
			.title {
				font-size: 20px;
				line-height: 50px;
			}

			.text {
				font-size: 14px;
			}

			.charts_title {
				font-weight: bold;
				color: #66B1FF;
			}

			.item {
				margin-bottom: 18px;

			}

			.clearfix:before,
			.clearfix:after {
				display: table;
				content: "";
			}

			.clearfix:after {
				clear: both
			}

			.box-card {
				width: 100%;
				height: 300px;
			}
		</style>

	</head>

	<body>
		<div id="app">
			<el-row :gutter="12">
				<el-col :span="8">
					<el-card class="box-card" style="height: 300px">
						<div slot="header" class="clearfix">
							<span class="charts_title">Echarts柱形图</span>
							<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
						</div>
						<div id="chartColumn" style="width: 100%; height: 260px;">
						</div>
					</el-card>
				</el-col>
				<el-col :span="8">
					<el-card class="box-card" style="height: 300px">
						<div slot="header" class="clearfix">
							<span class="charts_title">Echarts折线图</span>
							<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
						</div>
						<div id="chartColumn2" style="width: 100%; height: 260px;">
						</div>
					</el-card>
				</el-col>
				<el-col :span="8">
					<el-card class="box-card" style="height: 300px">
						<div slot="header" class="clearfix">
							<span class="charts_title">Echarts饼图</span>
							<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
						</div>
						<div id="chartPie" style="width: 100%; height: 260px;">
						</div>
					</el-card>
				</el-col>
			</el-row>




		</div>




		<script>
			new Vue({

				el: '#app',
				data() {
					return {
						chartColumn: null,
						chartColumn2: null,
						chartPie: null,
					}
				},
				mounted() {
					this.drawLine();
					this.drawLine2();
					this.drawPie();
				},
				methods: {
					drawLine() {
						this.chartColumn = echarts.init(document.getElementById('chartColumn'));

						this.chartColumn.setOption({
							title: {
								text: ''
							},
							tooltip: {},
							xAxis: {
								data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
							},
							yAxis: {},
							series: [{
								name: '销量',
								type: 'bar',
								data: [5, 20, 36, 10, 10, 20]
							}]

						});
					},
					drawLine2() {
						this.chartColumn2 = echarts.init(document.getElementById('chartColumn2'));

						this.chartColumn2.setOption({
							title: {
								text: 'Column Chart'
							},
							tooltip: {
								trigger: "axis",
								axisPointer: {
									type: "cross",
									label: {
										backgroundColor: "#76baf1"
									}
								}
							},
							xAxis: {
								type: 'category',
								data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
							},
							yAxis: {
								type: 'value'
							},
							series: [{
								data: [820, 932, 901, 934, 1290, 1330, 1320],
								type: 'line'
							}]
						});
					},
					drawPie() {
						this.chartPie = echarts.init(document.getElementById('chartPie'));

						this.chartPie.setOption({
							series: [{
								name: '访问来源',
								type: 'pie', // 设置图表类型为饼图
								radius: '55%', // 饼图的半径,外半径为可视区尺寸(容器高宽中较小一项)的 55% 长度。
								data: [ // 数据数组,name 为数据项名称,value 为数据项值
									{
										value: 235,
										name: '视频广告'
									},
									{
										value: 274,
										name: '联盟广告'
									},
									{
										value: 310,
										name: '邮件营销'
									},
									{
										value: 335,
										name: '直接访问'
									},
									{
										value: 400,
										name: '搜索引擎'
									}
								]
							}]
						});
					},

				},

			})
		</script>
	</body>
</html>