Vue引入echarts使用教程

栏目: JavaScript · 发布时间: 5年前

内容简介:本文将介绍如何在vue project中引入echarts,在引入echarts之前首先需要添加echarts的依赖包。在全局引入,需要在main.js文件中,引入echarts。局部引入echarts就是在单个vue文件中,引入echarts进行使用。

本文将介绍如何在vue project中引入echarts,在引入echarts之前首先需要添加echarts的依赖包。

  • vue2.0框架添加方法。
npm install echarts
复制代码
  • vue3.0框架添加方法。
npm add echarts
复制代码

全局引入

在全局引入,需要在main.js文件中,引入echarts。

  1. 全部引入
import echarts from 'echarts

Vue.prototype.$echarts = echarts
复制代码
  1. 按需引入
// echarts 按需引入
let echarts2 = require('echarts/lib/echarts')

// 引入折线图等组件
require('echarts/lib/chart/line')
require('echarts/lib/chart/bar')
require('echarts/lib/chart/radar')

// 引入提示框和title组件,图例
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')

Vue.prototype.$echarts2 = echarts2
复制代码

局部引入

局部引入echarts就是在单个vue文件中,引入echarts进行使用。

  1. 全部引入
import echarts from "echarts";
复制代码
  1. 按需引入
// echarts 按需引入
let echarts2 = require("echarts/lib/echarts");

// 引入折线图等组件
require("echarts/lib/chart/line");
require("echarts/lib/chart/bar");
require("echarts/lib/chart/radar");

// 引入提示框和title组件,图例
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");
require("echarts/lib/component/legend");

复制代码

代码示例

<template>
<div class="container">
<div id="main1" class="box"></div>
<div id="main2" class="box"></div>
<div id="main3" class="box"></div>
<div id="main4" class="box"></div>
</div>
</template>
<script>
import echarts from "echarts";
// echarts 按需引入
let echarts2 = require("echarts/lib/echarts");
// 引入折线图等组件
require("echarts/lib/chart/line");
require("echarts/lib/chart/bar");
require("echarts/lib/chart/radar");
// 引入提示框和title组件,图例
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");
require("echarts/lib/component/legend");
export default {
name: "",
components: {},
mounted() {
this.initChart();
},
data() {
return {};
},
methods: {
initChart() {
let myChart1 = this.$echarts.init(document.getElementById('main1'));
let myChart2 = this.$echarts2.init(document.getElementById('main2'));
let myChart3 = echarts.init(document.getElementById('main3'));
let myChart4 = echarts2.init(document.getElementById('main4'));
// 绘制图表
myChart1.setOption(this.setOption('全局全部引入'));
myChart2.setOption(this.setOption('全局按需引入'));
myChart3.setOption(this.setOption('局部全部引入'));
myChart4.setOption(this.setOption('局部按需引入'));
},
setOption(title) {
let option = {
title: { text: title },
tooltip: {},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
}
]
};
return option;
}
}
};
</script>
<style scoped>
.container {
position: relative;
margin: 0;
padding: 0;
display: flex;
justify-content: space-around;
}
.box {
width: 300px;
height: 300px;
border: 2px solid #000;
}
</style>
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Mastering Bitcoin

Mastering Bitcoin

Andreas M. Antonopoulos / O'Reilly Media / 2014-12-20 / USD 34.99

Mastering Bitcoin tells you everything you need to know about joining one of the most exciting revolutions since the invention of the web: digital money. Bitcoin is the first successful digital curren......一起来看看 《Mastering Bitcoin》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具