博主
258
258
258
258
专辑

第九节 Vue集成echarts

亮子 2022-02-28 08:44:43 6251 0 0 0

1、安装插件

npm install echarts --save

2、创建图表容器

<template>
    <div>
        <div>
            <div id="main1" style="width: 400px;height: 300px;"></div>
        </div>
    </div>
</template>

3、集成echarts

    import * as echarts from 'echarts';

	// 基于准备好的dom,初始化echarts实例
	let myChart = echarts.init(document.getElementById('main1'));
	this.echart1 = myChart
	// 准备数据
	let option = {
	  title: {
		text: 'ECharts 入门示例'
	  },
	  tooltip: {},
	  xAxis: {
		data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
	  },
	  yAxis: {},
	  series: [
		{
		  name: '销量',
		  type: 'bar',
		  data: [5, 20, 36, 10, 10, 20]
		}
	  ]
	}
	// 绘制图表
	myChart.setOption(option);

4、显示效果

图片alt

5、完整代码

<template>
    <div>
        <div>
            <div id="main1" style="width: 400px;height: 300px;"></div>
        </div>
    </div>
</template>

<script>
    import * as echarts from 'echarts';
    export default {
        name: 'Echart1',
        data() {
            return {
                echart1: null,
            }
        },
        mounted() {
            // 基于准备好的dom,初始化echarts实例
            let myChart = echarts.init(document.getElementById('main1'));
            this.echart1 = myChart
            // 准备数据
            let option = {
              title: {
                text: 'ECharts 入门示例'
              },
              tooltip: {},
              xAxis: {
                data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
              },
              yAxis: {},
              series: [
                {
                  name: '销量',
                  type: 'bar',
                  data: [5, 20, 36, 10, 10, 20]
                }
              ]
            }
            // 绘制图表
            myChart.setOption(option);
        },
        methods: {

        }
    }
</script>

<style>
</style>