vue3中使用echarts

1、vite生成项目

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

2、element-plus

开发项目,首先要挑选一个 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来创建路由
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、组件封装层级

为了更好的维护与开发,展示一个组件封装层级

组件封装效果图

组件层级

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
                        }
                    }
              }
            }
 }]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,099评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,473评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,229评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,570评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,427评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,335评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,737评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,392评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,693评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,730评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,512评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,349评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,750评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,017评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,290评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,706评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,904评论 2 335

推荐阅读更多精彩内容