Vue中使用ECharts

安装ECharts

npm install echarts --save

引入ECharts

main.js
旧版:
import echarts from 'echarts';

Vue.prototype.$echarts = echarts

新版:

import * as echarts from 'echarts'

Vue.prototype.$echarts = echarts

页面代码

<template>
	<div style="width: 100%;">

		<div style="display: flex;height: 50%;width: 100%;">
             <!-- 柱状图  -->
			<a-card style="width: 100%;height: 500px;" id="maychar">

			</a-card>
			
		</div>
		
		<div style="display: flex;">
            <!-- 折线图 -  -->
			<a-card style="width: 50%;height: 420px;" id="maychar3">
				
			</a-card>
             <!-- 饼图 -->
			<a-card style="width:50%;height: 420px;color: white;" id="maychar2">

			</a-card>

		</div>
		<div style="display: flex;">
			<!-- 柱状图  -->
			<a-card style="width: 50%;height: 420px;" id="maychar5">
				
			</a-card>

            <!-- 饼图 -->
			<a-card style="width: 50%;height: 420px;" id="maychar4">
				
			</a-card>
		</div>

	</div>
</template>

<script>
	import * as echarts from 'echarts'
	export default {
		data() {
			return {

			};
		},
		mounted() {
			this.getCharts();
			this.getCharts2();
			this.getCharts3();
			this.getCharts4();
			this.getCharts5()
		},
		methods: {
			getCharts() {
				let chartBox = this.$echarts.init(document.getElementById("maychar"));
				let option = {
					legend: {


					},
					toolbox: {
						show: true,
						feature: {
							mark: {
								show: true
							},
							saveAsImage: {
								show: true
							}
						}
					},
					grid: {
						// left: '15%',
					   top:'20%'
					   
					},
					tooltip: {},

					title: {
						text: 'abcde',
						left: 'center',
						top: 20,
						textStyle: {
							color: '#000000'
						}
					},
					dataset: {
						  dimensions: ['product', '状态', '工时', '金额'],
						source: [

							['1', 43.3, 85.8, 93.7],
							['2', 83.1, 73.4, 55.1],
							['3', 86.4, 65.2, 82.5],
							['4', 72.4, 53.9, 39.1],
							['5', 43.3, 85.8, 93.7],
							['6', 72.4, 53.9, 39.1],
							
						]
					},
					xAxis: {
						type: 'category'
					},
					yAxis: {},

					series: [
						
						{
						type: 'bar',
						color: 'rgba(204,204,204)',
					}, {
						type: 'bar',
						color: 'rgba(0,51,102)',
					}, {
						type: 'bar',
						color: 'rgba(153,204,255, 0.9)',
					}]
				};
				chartBox.setOption(option);
				// 根据页面大小自动响应图表大小
				window.addEventListener("resize", function() {
					chartBox.resize();
				});
			},

			getCharts2() {
				let chartBox = this.$echarts.init(document.getElementById("maychar2"));
				let option = {
					
					title: {
						text: 'abc',
						left: 'center',
						top: 20,
						textStyle: {
							color: '#000000'
						}
					},
					toolbox: {
						feature: {
							saveAsImage: {}
						}
					},
					tooltip: {
						trigger: 'item'
					},
					legend: {
						left: 'center',
						top: 'bottom',
						data: ['小明', '阿珍', '阿强', '打酱油', '嘿嘿']
					},
					visualMap: {
						show: false,
						min: 80,
						max: 600,
						inRange: {
							colorLightness: [0, 1]
						}
					},
					series: [{
						name: '',
						type: 'pie',
						radius: '55%',
						center: ['50%', '50%'],
						data: [{
								value: 335,
								name: '小明'
							},
							{
								value: 310,
								name: '阿珍'
							},
							{
								value: 274,
								name: '阿强'
							},
							{
								value: 235,
								name: '打酱油'
							},
							{
								value: 400,
								name: '嘿嘿'
							}
						].sort(function(a, b) {
							return a.value - b.value;
						}),
						roseType: 'radius',
						label: {
							color: 'rgba(255, 255, 255, 0.3)'
						},
						labelLine: {
							lineStyle: {
								color: 'rgba(255, 255, 255, 0.3)'
							},
							smooth: 0.2,
							length: 10,
							length2: 20
						},
						itemStyle: {
							color: '#1f668c',
							shadowBlur: 200,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						},
						animationType: 'scale',
						animationEasing: 'elasticOut',
						animationDelay: function(idx) {
							return Math.random() * 200;
						}
					}]
				};
				chartBox.setOption(option);
				// 根据页面大小自动响应图表大小
				window.addEventListener("resize", function() {
					chartBox.resize();
				});
			},

			getCharts3() {

				let chartBox = this.$echarts.init(document.getElementById("maychar3"));
				let option = {
					title: {
						text: 'ab',
						left: 'center',
						top: 20,
						textStyle: {
							color: '#000000'
						}
					},
					tooltip: {
						trigger: 'axis'
					},

					legend: {
						left: 'center',
						top: 'bottom',
						data: ['a', 'b']
					},
					grid: {

					},
					toolbox: {
						feature: {
							saveAsImage: {}
						}
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: ['1', '2', '3', '4', '5', '6', '7']
					},
					yAxis: {
						type: 'value'
					},
					series: [{
							name: 'a',
							type: 'line',
							stack: 'Total',
							data: [120, 132, 101, 134, 90, 230, 210],
							color:'rgb(255,204,51)',
						},
						{
							name: 'b',
							type: 'line',
							stack: 'Total',
							data: [220, 182, 191, 234, 290, 330, 310],
							color:'rgb(153,204,255)'
						},

					]
				};
				chartBox.setOption(option);
				// 根据页面大小自动响应图表大小
				window.addEventListener("resize", function() {
					chartBox.resize();
				});
			},
			getCharts4() {

				let chartBox = this.$echarts.init(document.getElementById("maychar4"));
				let option = {
					title: {
						text: 'abcd',
						left: 'center',
						top: 20,
						textStyle: {
							color: '#000000'
						}
					},
					tooltip: {
						trigger: 'item',
						formatter: '{a} <br/>{b} : {c} ({d}%)'
					},
					color: ['#FFCC99', '#ffe45d', '#99CCFF','#CCFFFF', '#FFCCCC', '#CCCCFF','#CCFFCC','#66CCFF'],
					legend: {
						left: 'center',
						top: 'bottom',
						data: [
							'a',
							'b',
							'c',
							'd',
							'e',
							'f',
							'g',
							'k',

						]
					},
					toolbox: {
						show: true,
						feature: {
							mark: {
								show: true
							},

							saveAsImage: {
								show: true
							}
						}
					},
					series: [{
						name: '',
						type: 'pie',
						radius: [20, 140],
						center: ['45%', '54%'],
						roseType: 'area',
						itemStyle: {
							borderRadius: 5
						},
						data: [{
								value: 30,
								name: 'a',
								
							},
							{
								value: 28,
								name: ' b',
								
							},
							{
								value: 26,
								name: 'c'
							},
							{
								value: 24,
								name: 'd'
							},
							{
								value: 22,
								name: 'e'
							},
							{
								value: 20,
								name: 'f'
							},
							{
								value: 18,
								name: 'g'
							},
							{
								value: 16,
								name: 'k'
							}
						]
					}]
				};
				chartBox.setOption(option);
				// 根据页面大小自动响应图表大小
				window.addEventListener("resize", function() {
					chartBox.resize();
				});
			},
			getCharts5() {

				let chartBox = this.$echarts.init(document.getElementById("maychar5"));
				let option = {
					title: {
						text: 'abc',
						left: 'center',
						top: 20,
						textStyle: {
							color: '#000000'
						}
					},
					toolbox: {
						feature: {
							saveAsImage: {}
						}
					},
					xAxis: {
						type: 'category',
						data: ['a', 'b', 'b', 'd', 'e', 'f', 'g']
					},
					yAxis: {
						type: 'value'
					},
					series: [{
						data: [
							20,
							// {
							// 	value: 200,
							// 	itemStyle: {
							// 		color: '#666699'
							// 	}
							// },
							20,
							50,
							80,
							70,
							10,
							30,
							
						],
						type: 'bar',
						color:'#93c4f5'
					}]
				};
				chartBox.setOption(option);
				// 根据页面大小自动响应图表大小
				window.addEventListener("resize", function() {
					chartBox.resize();
				});
			},


		}
	}
</script>

<style>
</style>

效果展示

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值