Element中使用ECharts的項目實踐
更新時間:2022年07月29日 15:46:20 作者:·~簡單就好
本文主要介紹了Element中使用ECharts的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
一、引入ECharts
1、直接引入echarts (安裝echarts項目依賴)
npm install echarts --save
2、全局引入 (我們安裝完成之后,可以在main.js中全局引入 echarts)
import echarts from "echarts"; Vue.prototype.$echarts = echarts;
3、我們可以將Echar封裝成組件的形式,方便調用
封裝在組件中:封裝成 Echarts.vue 文件放在ElementUI前端框架中
<template>
<div ref="chartDom" ></div>
</template>
<script>
import * as echarts from 'echarts';
import debounce from "lodash/debounce";
import { addListener, removeListener} from "resize-detector";
export default {
props: {
option: {
type: Object,
default: ()=> {}
}
},
watch: {
// option(val) {
// this.chart.setOption(val);
// },
option: {
handler(val) {
this.chart.setOption(val);
},
deep: true
}
},
created() {
this.resize = debounce(this.resize, 300);
},
mounted() {
this.renderChart();
addListener(this.$refs.chartDom, this.resize);
},
beforeDestroy() {
removeListener(this.$refs.chartDom, this.resize);
this.chart.dispose();
this.chart = null;
},
methods:{
resize(){
this.chart.resize();
},
renderChart() {
this.chart = echarts.init(this.$refs.chartDom);
this.chart.setOption(this.option);
}
},
}
</script>
<style>
</style>
4、此時我們可以通過Echart官網引入我們需要的圖
例如:以所選的折柱混合圖為例

引入(需要引入Echarts剛剛封裝好的組件)
從Echart官網獲取對應圖的代碼

引入代碼:
<template>
<div>
<el-row>
<Echarts :option="option" style="height: 400px;width: 630px" />
</el-row>
</div>
</template>
<script>
//引入Echart的包
import Echarts from "../../components/charts/Echarts";
export default {
components:{
Echarts,
},
data(){
return{
option:{
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
}
},
toolbox: {
feature: {
dataView: { show: true, readOnly: false },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
saveAsImage: { show: true }
}
},
legend: {
data: ['Evaporation', 'Precipitation', 'Temperature']
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisPointer: {
type: 'shadow'
}
}
],
yAxis: [
{
type: 'value',
name: 'Precipitation',
min: 0,
max: 250,
interval: 50,
axisLabel: {
formatter: '{value} ml'
}
},
{
type: 'value',
name: 'Temperature',
min: 0,
max: 25,
interval: 5,
axisLabel: {
formatter: '{value} °C'
}
}
],
series: [
{
name: 'Evaporation',
type: 'bar',
tooltip: {
valueFormatter: function (value) {
return value + ' ml';
}
},
data: [
2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
]
},
{
name: 'Precipitation',
type: 'bar',
tooltip: {
valueFormatter: function (value) {
return value + ' ml';
}
},
data: [
2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
]
},
{
name: 'Temperature',
type: 'line',
yAxisIndex: 1,
tooltip: {
valueFormatter: function (value) {
return value + ' °C';
}
},
data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
}
]
};
},
created: function () {
},
methods:{
}
}
</script>
<style scoped>
</style>
二、效果展示

到此這篇關于Element中使用ECharts的項目實踐的文章就介紹到這了,更多相關Element使用ECharts內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Vue和ECharts創(chuàng)建交互式圖表的代碼示例
在現(xiàn)代 Web 應用中,數(shù)據(jù)可視化是一個重要的組成部分,它不僅能夠幫助用戶更好地理解復雜的數(shù)據(jù),還能提升用戶體驗,本文給大家使用Vue和ECharts創(chuàng)建交互式圖表的示例,需要的朋友可以參考下2024-11-11
Create?vite理解Vite項目創(chuàng)建流程及代碼實現(xiàn)
這篇文章主要為大家介紹了Create?vite理解Vite項目創(chuàng)建流程及代碼實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
vue基礎之事件v-onclick="函數(shù)"用法示例
這篇文章主要介紹了vue基礎之事件v-onclick="函數(shù)"用法,結合實例形式分析了vue.js事件v-on:click="函數(shù)"的data數(shù)據(jù)添加、點擊響應、以及留言本功能相關操作技巧,需要的朋友可以參考下2019-03-03

