目录1、安装2、Vue2中使用Echarts在main.js文件中给定一个容器3、vue3中使用Echarts在根组件里引入echart,一般是App.vue在需要使用的页面,定义d
npm install echarts --save
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
echarts
初始化应在钩子函数mounted()
中,这个钩子函数是在el 被新创建的 vm.$el
替换,并挂载到实例上去之后调用
// 引入基本模板
let echarts = require('echarts/lib/echarts')
// 引入柱状图组件
require('echarts/lib/chart/bar')
// 引入提示框和title组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
name: 'hello',
data() {
return {
msg: 'Welcome to Your vue.js App'
}
},
mounted() {
this.drawLine();
},
methods: {
drawLine() {
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.getElementById('myChart'))
// 绘制图表
title: {
text: '折线图堆叠'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'cateGory',
boundaryGap: false,
data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
},
yAxis: {
type: 'value'
},
series: [
{
name: '邮件营销',
type: 'line',
stack: '总量',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: '联盟广告',
type: 'line',
stack: '总量',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: '视频广告',
type: 'line',
stack: '总量',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: '直接访问',
type: 'line',
stack: '总量',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: '搜索引擎',
type: 'line',
stack: '总量',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
}
}
}
因为setup
中没有this
,而且这时候还没有渲染,所以在setup
中 ,可以使用provide/inject
来把echart
引入进来
import * as echarts from 'echarts'
import { provide } from 'vue'
export default {
name: 'App',
setup(){
provide('echarts',echarts) //provide
},
components: {
}
}
这里需要注意的是import * as echarts from 'echarts'
, 不能 import echarts from 'echarts'
,这样会报错,因为5,0版本的echarts的接口已经变成了下面这样:
export {
EChartsFullOption as EChartsOption,
connect,
disConnect,
dispose,
getInstanceByDom,
getInstanceById,
getMap,
init,
reGISterLocale,
registerMap,
registerTheme
};
<div id="home-page-traffic_chart" style="width: 600px; height: 280px">
export default {
name: 'data_page',
setup () {
const trafficData = ref({})
const echarts = inject('echarts')
onMounted(() => {
const myChart = echarts.init(document.getElementById('home-page-traffic_chart'))
// 绘制图表
myChart.setOption({
title: {
text: '今日话务统计'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisTick: {
alignWithLabel: true
}
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: '直接访问',
type: 'bar',
barWidth: '60%',
data: [10, 52, 200, 334, 390, 330, 220]
}
]
})
window.onresize = function () {
myChart.resize()
}
})
return {
}
}
}
效果图:
--结束END--
本文标题: vue2 vue3中使用Echarts详细
本文链接: https://www.lsjlt.com/news/137279.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0