css实现渐变电量效果柱状图

我们通常的做法就是用echarts来实现

比如

echarts象形柱图实现电量效果柱状图

接着我们实现进阶版,增加渐变效果

echarts分割柱形图实现渐变电量效果柱状图

接着是又在渐变的基础上,增加了背景色块的填充

echarts实现渐变电量效果柱状图

其实思路是一致的,就是效果层的叠加

以上,就是在项目中实际会用到的效果

如果你只是想做一个静态demo,不需要数据,不需要后台,这里教你一个简单快捷的方法,用css实现!

实现效果如下↓

相关代码:

复制代码
<template>
    <!--css实现电量效果-->
    <div class="panel">
        <div class="content">
            <div class="legend">
                <div class="legend-item">
                    <span class="legend-icon" style="background:linear-gradient(0deg,rgba(247,240,42,1),rgba(255,84,84,1));"></span>
                    <span class="legend-label">报修</span>
                </div>
                <div class="legend-item">
                    <span class="legend-icon" style="background:linear-gradient(0deg,rgba(22,233,16,1),rgba(22,230,247,1));"></span>
                    <span class="legend-label">维护</span>
                </div>
            </div>
            <div class="chart">
                <div class="chart-item" v-for="(item,index) in datas">
                    <div class="chart-bg">
                        <span class="char-bg-item" v-for="n in 12" v-bind:style="char_bg_item_style1(item,n)"></span>
                    </div>
                    <div class="chart-bg">
                        <span class="char-bg-item" v-for="n in 12" v-bind:style="char_bg_item_style2(item,n)"></span>
                    </div>
                    <div class="chart-label">{{item.label}}</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import gradient from "@/lib/GradientColor.js";

export default {
    name: "BatteryCss",
    data() {
        return {
            maxValue: 30,
            datas: [
                { label: "2023/01", fix: 10, mainten: 12 },
                { label: "2023/02", fix: 8, mainten: 6 },
                { label: "2023/03", fix: 4, mainten: 8 },
                { label: "2023/04", fix: 7, mainten: 5 },
                { label: "2023/05", fix: 4, mainten: 6 },
                { label: "2023/06", fix: 2, mainten: 3 },
            ],
        };
    },
    methods: {
        char_bg_item_style1(item, n) {
            if (n <= 12 - item.fix) {
                return {
                    backgroundColor: "#D9D9D9",
                };
            } else {
                return {
                    //渐变色
                    backgroundColor: gradient("#F0E82C", "#EF7E5D", 12, n),
                };
            }
        },
        char_bg_item_style2(item, n) {
            if (n <= 12 - item.mainten) {
                return {
                    backgroundColor: "#D9D9D9",
                };
            } else {
                return {
                    //渐变色
                    backgroundColor: gradient("#25E2F2", "#19E419", 12, n),
                };
            }
        },
    },
};
</script>
<style scoped>
.panel {
    width: 500px;
    background: #1c242b;
}
.legend {
    height: 32px;
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    line-height: 32px;
}

.legend-item {
    width: 80px;
}

.legend-icon {
    width: 18px;
    height: 12px;
    border-radius: 2px;
    display: inline-block;
}

.legend-label {
    font-size: 12px;
    color: #aaa;
    margin-left: 10px;
}

.chart {
    width: 100%;
    height: calc(100% - 32px);
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
}

.chart-item {
    width: 72px;
    height: 124px;
}

.chart-bg {
    position: relative;
    top: 14px;
    left: 14px;
    width: 18px;
    height: 81px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    float: left;
    margin-left: 2px;
}

.char-bg-item {
    height: 4px;
    width: 18px;
    display: inline-block;
    background: #d9d9d9;
}

.chart-label {
    height: calc(124px - 81px);
    font-size: 14px;
    color: #a5a6a6;
    position: relative;
    top: 20px;
    text-align: center;
}
</style>

版本信息↓

思路很简单,就是循环画一个一个小块块

相关推荐
Mr Xu_17 分钟前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠23 分钟前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript
lang2015092835 分钟前
JSR-340 :高性能Web开发新标准
java·前端·servlet
好家伙VCC1 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
未来之窗软件服务2 小时前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
嘿起屁儿整2 小时前
面试点(网络层面)
前端·网络
VT.馒头2 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
phltxy3 小时前
Vue 核心特性实战指南:指令、样式绑定、计算属性与侦听器
前端·javascript·vue.js
Byron07074 小时前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js
css趣多多5 小时前
地图快速上手
前端