vue-cli3+echarts實(shí)現(xiàn)漸變色儀表盤組件封裝
本文實(shí)例為大家分享了vue-cli3+echarts實(shí)現(xiàn)封裝一個(gè)漸變色儀表盤組件,供大家參考,具體內(nèi)容如下
效果預(yù)覽

思路
1、使用兩個(gè)儀表盤疊加,起始角度一樣,底部?jī)x表盤結(jié)束角度固定不變
2、通過props傳入數(shù)據(jù)
3、計(jì)算在上層的儀表盤的結(jié)束角度并賦值
代碼
<template>
? <div class="gauge">
? ? <div class="gauge__bottom" ref="bottomGauge"></div>
? ? <div class="gauge__top" ref="topGauge"></div>
? ? <div class="gauge__label">數(shù)據(jù)占比</div>
? ? <div class="gauge__title">{{ this.gaugeData.gaugeTitle }}</div>
? </div>
</template>
<script>
import echarts from "echarts";
export default {
? name: "gauge",
? props: ["gaugeData"],//傳入的數(shù)據(jù)
? data() {
? ? return {
? ? ? bottomOption: {
? ? ? ? series: [
? ? ? ? ? {
? ? ? ? ? ? name: "",
? ? ? ? ? ? type: "gauge",
? ? ? ? ? ? startAngle: "225",
? ? ? ? ? ? endAngle: "-45",
? ? ? ? ? ? data: [{ value: 100, name: "" }],
? ? ? ? ? ? splitNumber: 10,
? ? ? ? ? ? detail: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? splitLine: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? pointer: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? axisTick: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? axisLabel: { show: false },
? ? ? ? ? ? axisLine: {
? ? ? ? ? ? ? lineStyle: {
? ? ? ? ? ? ? ? width: 10,
? ? ? ? ? ? ? ? color: [
? ? ? ? ? ? ? ? ? [
? ? ? ? ? ? ? ? ? ? 1,
? ? ? ? ? ? ? ? ? ? new echarts.graphic.LinearGradient(0, 0, 1, 0, [
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? offset: 0,
? ? ? ? ? ? ? ? ? ? ? ? // 起始顏色
? ? ? ? ? ? ? ? ? ? ? ? color: "#707089",
? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? offset: 1,
? ? ? ? ? ? ? ? ? ? ? ? // 結(jié)束顏色
? ? ? ? ? ? ? ? ? ? ? ? color: "#707089",
? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? ? ]),
? ? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? ? ],
? ? ? ? ? ? ? },
? ? ? ? ? ? },
? ? ? ? ? },
? ? ? ? ],
? ? ? },
? ? ? topOption: {
? ? ? ? series: [
? ? ? ? ? {
? ? ? ? ? ? name: "業(yè)務(wù)指標(biāo)",
? ? ? ? ? ? type: "gauge",
? ? ? ? ? ? startAngle: "225",
? ? ? ? ? ? endAngle: "",
? ? ? ? ? ? detail: {
? ? ? ? ? ? ? formatter: "{value}%",
? ? ? ? ? ? ? color: "#01F9FF",
? ? ? ? ? ? ? fontSize: 18,
? ? ? ? ? ? ? fontFamily: "ZhenyanGB-Regular",
? ? ? ? ? ? ? offsetCenter: [0, 0],
? ? ? ? ? ? },
? ? ? ? ? ? data: [{ value: "", name: "" }],
? ? ? ? ? ? splitNumber: 10,
? ? ? ? ? ? splitLine: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? pointer: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? axisTick: {
? ? ? ? ? ? ? show: false,
? ? ? ? ? ? },
? ? ? ? ? ? axisLabel: { show: false },
? ? ? ? ? ? axisLine: {
? ? ? ? ? ? ? lineStyle: {
? ? ? ? ? ? ? ? width: 10,
? ? ? ? ? ? ? ? color: "",
? ? ? ? ? ? ? },
? ? ? ? ? ? },
? ? ? ? ? },
? ? ? ? ],
? ? ? },
? ? };
? },
? mounted() {
? ? this.getTopGauge();
? ? this.getBottomGauge();
? },
? methods: {
? ? getTopGauge() {
? ? ? const chart = this.$refs.topGauge;
? ? ? if (chart) {
? ? ? ? const myChart = this.$echarts.init(chart, null, { renderer: "svg" });
? ? ? ? this.$once("hook:beforeDestroy", function () {
? ? ? ? ? echarts.dispose(myChart);
? ? ? ? });
? ? ? ? this.topOption.series[0].data[0].value = this.gaugeData.gaugePercent;
? ? ? ? this.topOption.series[0].axisLine.lineStyle.color = this.gaugeData.guageColor;
? ? ? ? let tmp = 225 - 270 * (this.gaugeData.gaugePercent / 100);
? ? ? ? this.topOption.series[0].endAngle = tmp;
? ? ? ? const option = this.topOption;
? ? ? ? myChart.setOption(option);
? ? ? }
? ? },
? ? getBottomGauge() {
? ? ? const chart = this.$refs.bottomGauge;
? ? ? if (chart) {
? ? ? ? const myChart = this.$echarts.init(chart, null, { renderer: "svg" });
? ? ? ? this.$once("hook:beforeDestroy", function () {
? ? ? ? ? echarts.dispose(myChart);
? ? ? ? });
? ? ? ? const option = this.bottomOption;
? ? ? ? myChart.setOption(option);
? ? ? }
? ? },
? },
};
</script>
<style lang="less">
.gauge {
? width: 150px;
? height: 165px;
? position: relative;
? &__top {
? ? position: absolute;
? ? top: 0;
? ? left: 0;
? ? width: 100%;
? ? height: 150px;
? }
? &__bottom {
? ? position: absolute;
? ? top: 0;
? ? left: 0;
? ? width: 100%;
? ? height: 150px;
? }
? &__label {
? ? position: absolute;
? ? height: 21px;
? ? width: 64px;
? ? background: #0547c9;
? ? border: 1px solid #1e92ff;
? ? border-radius: 11.5px;
? ? border-radius: 11.5px;
? ? bottom: 35px;
? ? left: 50%;
? ? transform: translate(-50%, 0);
? ? font-family: PingFangSC-Regular;
? ? font-size: 8px;
? ? color: #ffffff;
? ? text-align: center;
? ? line-height: 21px;
? }
? &__title {
? ? font-family: PingFangSC-Medium;
? ? font-size: 14px;
? ? color: #52f9cb;
? ? text-align: center;
? ? position: absolute;
? ? bottom: 5px;
? ? left: 50%;
? ? transform: translate(-50%, 0);
? }
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
element中一個(gè)單選框radio時(shí)的選中和取消代碼詳解
這篇文章主要給大家介紹了關(guān)于element中一個(gè)單選框radio時(shí)的選中和取消的相關(guān)資料,文中通過圖文以及代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友可以參考下2023-09-09
Vue this.$router.push(參數(shù))實(shí)現(xiàn)頁面跳轉(zhuǎn)操作
這篇文章主要介紹了Vue this.$router.push(參數(shù))實(shí)現(xiàn)頁面跳轉(zhuǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟
本次小編給大家?guī)淼氖顷P(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-02-02
使用provide/inject實(shí)現(xiàn)跨組件通信的方法
在 Vue 應(yīng)用中,組件間通信是構(gòu)建復(fù)雜應(yīng)用時(shí)的一個(gè)常見需求,Vue3.x 提供了provide和inject API,讓跨組件通信變得更加簡(jiǎn)潔和高效,本文將深入探討如何使用provide和inject在Vue3.x中實(shí)現(xiàn)跨組件通信,并通過示例代碼一步步進(jìn)行說明,需要的朋友可以參考下2024-03-03
Vue computed 計(jì)算屬性代碼實(shí)例
在本篇文章里小編給大家分享的是關(guān)于Vue computed 計(jì)算屬性代碼實(shí)例,需要的朋友們可以參考下。2020-04-04
vue中的事件修飾符once,prevent,stop,capture,self,passive
這篇文章主要介紹了vue中的事件修飾符once,prevent,stop,capture,self,passive,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

