Vue使用ECharts的步骤

1.npm安装

npm install echarts

2.在使用的页面引入ECharts

import * as echarts from 'echarts';

3.获取dom容器、初始化ECharts实例对象

// 获取dom节点
var chartDom = document.querySelector( dom + " .chart");
// 初始化echarts可视化图放在哪个dom节点
var myChart = echarts.init(chartDom);

4.指定配置项和数据(option)

var option = {
                color: ["#2f89cf"],
                // 设置图标的标题
                title: {
                },
                // 图标的提示组件
                tooltip: {
                },
                // 图例组件   但是柱状图没有显示
                legend: {

                },
                // 网格配置  可以控制折线图、柱状图 、饼图的大小
                grid: {

                },
                // 工具箱组件  可以另存为图片等功能
                toolbox: {

                },
                xAxis: {
                    
                },
                yAxis: {
                    
                },
                // 系列图表配置  它决定着显示那种图表类型
                series: [
                    
                ]
            };

5.将配置项设置给ECharts实例对象

option && myChart.setOption(option);

6.// 让图跟着屏幕自适应

// 让图标跟着屏幕自适应
window.addEventListener("resize", function() {
    myChart.resize();
})

使用:

<template>
	<div class="visual-box">
		<h3 class="title">{{title}}</h3>
		<div class="visual-data" :class="`visual-data${i}`"></div>
	</div>
</template>

<script>
	import * as echarts from 'echarts';
    export default {
		props: {
			title: {
				type: String,
				default: () => '这是一个标题'
			},
			i: {
				type: Number,
			},
			option: {
				type: Object,
				default: {
					title: "2022年成交数",
					option: {
						backgroundColor: '#fff',//背景色
						tooltip: {
							trigger: 'axis',
							axisPointer: {
								type: 'shadow'
							}
						},
						grid: {
							top: '5%',
							right: '5%',
							left: '8%',
							bottom: '20%'
						},
						xAxis: [{
							type: 'category',
							data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
							axisLine: {
								lineStyle: {
									color: '#D1D9EB'
								}
							},
							axisLabel: {
								margin: 10,
								color: '#A1A7B3',
								textStyle: {
									fontSize: 12
								},
							},
							axisTick: {
								show: false
							}
						}],
						yAxis: [
							{
								type: "value",
								name: '',
								nameTextStyle: {
									color: '#C1C6CF',
									fontSize: 12,
									align: "right",
									padding:5
								},
								axisLabel: {
									formatter:'{value}',
									color: '#A1A7B3',
									fontSize:12
								},
								axisTick: {
									show: false
								},
								axisLine: {
									show: false,
								},
								splitLine: {
									lineStyle: {
										color: '#D1D9EB',
										type:'dashed'
									}
								}
							},
						],
						series: [{
							type: 'bar',
							data: [80, 80, 97, 53, 95, 26, 72, 97, 53, 95, 26, 72],
							barWidth: '15px',
							itemStyle: {
								color:'rgb(38,80,180)',
								barBorderRadius: [2 ,2 ,0 ,0 ]
							},
						}]
					}
				},
			}
		},
        data: () => ({
            
        }),
        mounted() {
			this.visualBox1()
        },
        methods: {
			visualBox1() {
				// 获取dom节点
				var chartDom = document.querySelector(".visual-box" + ' ' + ".visual-data"+this.i)
				// 初始化echarts可视化图放在哪个dom节点
				var myChart = echarts.init(chartDom, null, {height: 180,});
				var option = this.option;
				option && myChart.setOption(option);
				// 让图标跟着屏幕自适应
				window.addEventListener("resize", function() {
				    myChart.resize();
				})
			},
			
		}
	}
</script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值