vue3中使用echarts

1牍帚、vite生成項目

# npm 6.x
npm init @vitejs/app my-vue-app --template vue

2晶衷、element-plus

開發(fā)項目嗽元,首先要挑選一個 UI 組件庫。目前市面上支持 Vue 3 的組件庫并不多掠兄,Element UI 不負眾望已經完成支持了像云。Element Plus 是餓了么 Element UI 團隊推出的適配了 Vue 3 的全新版本,新增了很多實用組件蚂夕,體驗非常好迅诬。

(1)、安裝

npm install element-plus -S

(2)双抽、引入

import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/lib/theme-chalk/index.css'
createApp(App).use(ElementPlus).mount('#app')

(3)百框、使用小技巧
修改卡片內邊距(elment-card)

<el-card shadow="hover" :body-style="{padding:'20px'}">
       <total-sales />
</el-card>

3、配置vue3的路由

(1)牍汹、安裝

npm install vue-router@next --save

(2)、配置路由
新建頁面

//view/welcom.vue
<template>
    welcom
</template>

新建index.js

//router/index.js
import {createRouter, createWebHashHistory} from 'vue-router';
// 1. 定義路由組件柬泽, 注意慎菲,這里一定要使用 文件的全名(包含文件后綴名)
import welcome from "../view/welcome.vue";

const routes = [
    { path: "/", redirect: '/welcome' },
    { path: "/welcome", component: welcome }
]

// Vue-router新版本中,需要使用createRouter來創(chuàng)建路由
export default  createRouter({
// 指定路由的模式,此處使用的是hash模式
history: createWebHashHistory(),
routes // short for `routes: routes`
})

main.js

import { createApp } from 'vue'
import router from './router/index'

import App from './App.vue'
createApp(App).use(router).mount('#app')

App.vue

<template>
<router-view></router-view>
</template>

<script setup>
import HelloWorld from './components/HelloWorld.vue'


</script>

<style>
html,body,#app {
  padding:0;
  margin:0;
  width:100%;
  height:100%;
  background-color: #eee;
}
</style>

4锨并、vite組件傳參

(1)露该、props(defineProps)
注冊屬性

<script setup>
import { defineProps } from 'vue'
let props = defineProps({
    title:String,
    value:String
})
</script>

使用

<div class="title">{{props.title}}</div>

5、插槽

新語法v-slot

<template v-slot>
       <div class="compare">6</div>
</template>
<template v-slot:footer>
       <span>昨日銷售額</span>
       <span class="money">¥ 30,000,000</span>
</template>

具名插槽就使用v-slot綁定第煮,默認插槽就空寫一個v-slot

6解幼、echarts安裝

(1)抑党、安裝

npm install --save echarts

(2)、使用

//引入
import * as echarts from 'echarts'
...
//初始化
var myChart = echarts.init(document.getElementById('main'))
//設置參數
myChart.setOption({
  xAxis:{},
  yAxis:{},
  series:[]
})
//...

7撵摆、組件封裝層級

為了更好的維護與開發(fā)底靠,展示一個組件封裝層級

組件封裝效果圖

組件層級

TopView為盛放這四個組件的容器組件:

<template>
    <div class="top_view" >
        <el-row :gutter="20">
            <el-col :span="6">
                <el-card shadow="hover" :body-style="{padding:'20px'}">
                   <total-sales />
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card shadow="hover">
                    <total-orders />
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card shadow="hover">
                    <total-users />
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card shadow="hover">
                    <today-users />
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>
<script setup>
import totalSales from '../TotalSales/index.vue'
import todayUsers from '../TodayUsers/index.vue'
import totalUsers from '../TotalUsers/index.vue'
import totalOrders from '../TotalOrders/index.vue'
</script>

totalSales,todayUsers,totalUsers,totalOrders為圖中的四個組件,但這四個組件有想類似的結構特铝,將結構抽離出來封裝為commandCard組件:

<template>
    <div class="common-card">
        <div class="title">{{props.title}}</div>
        <div class="value">{{props.value}}</div>
        <div class="chart">
            <slot></slot>
        </div>
        <div class="line"></div>
        <div class="total">
            <slot name="footer">

            </slot>
        </div>
    </div>
</template>
<script setup>
import { defineProps } from 'vue'
let props = defineProps({
    title:String,
    value:String
})
</script>
<style scope>
    .title{
        font-size:12px;
        color:#999;
    }
    .value{
        font-size:25px;
        color:#000;
        margin-top:5px;
        letter-spacing:1px;
    }
    .chart {
        height:50px;
    }
    .line{
        margin:10px 0;
        border-top:1px solid #eee;
    }
    .totle{
        color:#eee;
        font-size:12px;
    }
</style>

再以totalSales組件為例展示commandCard的用法

<template>
    <common-card  title="累計銷售額" value="¥ 32,039,165">
        <template v-slot>
            <div class="compare-wrapper">
                <div class="compare">
                    <span>日同比</span><span class="emphasis">7.33%</span><span class="increase"></span>
                </div>
                <div class="compare">
                    <span>月同比</span><span class="emphasis">38.79%</span><span class="decrease"></span>
                </div>
            </div>
        </template>
        <template v-slot:footer>
            <span>昨日銷售額</span>
            <span class="emphasis">¥ 30,000,000</span>
        </template>
    </common-card>
</template>
<script setup>
import commonCard from '../CommonCard/index.vue'

</script>
<style scope>
    .compare-wrapper{
        height:100%;
        display:flex;
        flex-direction: column;
        align-items: center;
    }
    .compare{
        width:100%;
        font-size:12px;
        margin-top:3px;
        display:flex;
    }
    .increase{
        display:inline-block;
        width:0;
        height:0;
        border-width:3px;
        border-color:transparent transparent red transparent;
        border-style:solid;
        margin:3px 0 0 5px;
    }
    .decrease{
        display:inline-block;
        width:0;
        height:0;
        border-width:3px;
        border-color: green transparent transparent transparent;
        border-style:solid;
        margin:7px 0 0 5px;
    }
</style>
<style>
    .emphasis{
        margin-left:5px;
        color:#333;
        letter-spacing: 1px;
        font-weight: 700;
    }
    .increase{
        display:inline-block;
        width:0;
        height:0;
        border-width:3px;
        border-color:transparent transparent red transparent;
        border-style:solid;
        margin:3px 0 0 5px;
    }
    .decrease{
        display:inline-block;
        width:0;
        height:0;
        border-width:3px;
        border-color: green transparent transparent transparent;
        border-style:solid;
        margin:7px 0 0 5px;
    }
</style>

8暑中、餅圖指示線問題

版本前提

 "dependencies": {
    "echarts": "^5.1.2",
    "element-plus": "^1.0.2-beta.69",
    "v-charts": "^1.19.0",
    "vue": "^3.0.5",
    "vue-echarts": "^6.0.0",
    "vue-router": "^4.0.10"
  }

問題描述:
餅圖設置label后,指示線消失

指示線消失

代碼:

series:[{
            type:'pie',
            data:mockData,
            radius: [0, 100],
            center: ["50%", "50%"],
            itemStyle: {
              normal: {
                    labelLine: {
                        normal: {
                            show:true,
                            lineStyle: {
                                color: 'rgba(255, 255, 255, 0.3)'
                            },
                            smooth: 0.2,
                            length: 50,
                            length2: 100,
                        }
                    },
                    label:{
                        show:true,
                        position:'outter',
                        formatter:function(params){
                            return params.data.legendname
                        }
                    }
              }
            }
            
        }]

問題解決
將position字段去掉后鲫剿,指示線重新顯示

指示線復現

代碼:

series:[{
            type:'pie',
            data:mockData,
            radius: [0, 100],
            center: ["50%", "50%"],
            itemStyle: {
              normal: {
                    labelLine: {
                        normal: {
                            show:true,
                            lineStyle: {
                                color: 'rgba(255, 255, 255, 0.3)'
                            },
                            smooth: 0.2,
                            length: 50,
                            length2: 100,
                        }
                    },
                    label:{
                        show:true,
                        formatter:function(params){
                            return params.data.legendname
                        }
                    }
              }
            }
 }]
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末鳄逾,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子灵莲,更是在濱河造成了極大的恐慌雕凹,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件政冻,死亡現場離奇詭異枚抵,居然都是意外死亡,警方通過查閱死者的電腦和手機赠幕,發(fā)現死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門俄精,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人榕堰,你說我怎么就攤上這事竖慧。” “怎么了逆屡?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵圾旨,是天一觀的道長。 經常有香客問我魏蔗,道長砍的,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任莺治,我火速辦了婚禮廓鞠,結果婚禮上,老公的妹妹穿的比我還像新娘谣旁。我一直安慰自己床佳,他們只是感情好,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布榄审。 她就那樣靜靜地躺著砌们,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浪感,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天昔头,我揣著相機與錄音,去河邊找鬼影兽。 笑死揭斧,一個胖子當著我的面吹牛,可吹牛的內容都是我干的赢笨。 我是一名探鬼主播未蝌,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼茧妒!你這毒婦竟也來了萧吠?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤桐筏,失蹤者是張志新(化名)和其女友劉穎纸型,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體梅忌,經...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡狰腌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了牧氮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片琼腔。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖踱葛,靈堂內的尸體忽然破棺而出丹莲,到底是詐尸還是另有隱情,我是刑警寧澤尸诽,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布甥材,位于F島的核電站,受9級特大地震影響性含,放射性物質發(fā)生泄漏洲赵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一商蕴、第九天 我趴在偏房一處隱蔽的房頂上張望叠萍。 院中可真熱鬧,春花似錦绪商、人聲如沸俭令。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春理张,著一層夾襖步出監(jiān)牢的瞬間赫蛇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工雾叭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留悟耘,地道東北人。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓织狐,卻偏偏與公主長得像暂幼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子移迫,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內容