npm install element-ui -S
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
es2015
也是不对的bable.config.js
文件npm install echarts -S
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
<template>
<el-row>
<el-col offset="3" type="flex" >
<div style="width:1200px; height: 800px; " ref="chart">
div>
el-col>
el-row>
template>
<script>
export default {
name: "SummaryChart",
// 使用 mounted 生命周期函数来调用这个函数
// 不懂得 mounted 和其他生命周期函数的同学可以去下方链接的视频
mounted(){
this.createEcharts();
},
methods: {
// 方法中定义图表创建函数
createEcharts(){
// 通过全局的方式时,一定要用 $ref 来定位容器 然后用 echarts 的方式初始化容器
var myChart = this.$echarts.init(this.$refs.chart)
// 最重要的配置:这里的数据决定了最终图表的显示
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
// Use axis to trigger tooltip
type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
}
},
legend: {},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series: [
{
name: 'Direct',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [320, 302, 301, 334, 390, 330, 320]
},
{
name: 'Mail Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Affiliate Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [150, 212, 201, 154, 190, 330, 410]
},
{
name: 'Search Engine',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [820, 832, 901, 934, 1290, 1330, 1320]
}
]
};
// 一些常规操作
myChart.setOption(option)
window.addEventListener("resize", () => {
myChart.resize();
});
}
},
}
</script>
ref
进行唯一标识,可以用 id
进行标识<template>
<el-row>
<el-col offset="3" type="flex" >
<div id="chart" style="width:1200px; height: 800px; " >
div>
el-col>
el-row>
template>
<script>
// 直接在组件代码中引入 echarts 库
import * as echarts from 'echarts'
export default {
name: "SummaryChart",
mounted(){
this.createEcharts();
},
methods: {
createEcharts(){
// 可以通过 echarts.init 的方法初始化容器
// 容器也可以通过 getElementById 的方式拿到
var myChart = echarts.init(document.getElementById('chart'))
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
// Use axis to trigger tooltip
type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
}
},
legend: {},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series: [
{
name: 'Direct',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [320, 302, 301, 334, 390, 330, 320]
},
{
name: 'Mail Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Affiliate Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Video Ad',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [150, 212, 201, 154, 190, 330, 410]
},
{
name: 'Search Engine',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: [820, 832, 901, 934, 1290, 1330, 1320]
}
]
};
myChart.setOption(option)
window.addEventListener("resize", () => {
myChart.resize();
});
}
},
}
</script>
<style>
</style>
Vue.use()
而 echarts 使用的是 Vue.prototype=
呢?答案结论:在 Vue 生态内的插件用 Vue.use,生态外的插件引用使用 prototype,但这两种方法的底层实现是一样的,都是 prototype