Echarts实现柱状图不同时段不同颜色

本文介绍了一种在ECharts中实现柱状图的方法,通过在不同时间段显示不同颜色的柱子来直观展示数据变化。具体实现是在series.itemStyle.normal中设置颜色函数,根据数据名称(即时间)返回预设的颜色列表中的颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现柱状图在不同时间端显示不同颜色,在series.itemStyle.normal配置中写判断条件,返回不同颜色即可

<template>
      <div id="energy_hour"></div>
</template>


<script>
// 按需引入ECharts 主模块
var echarts = require("echarts/lib/echarts");
// 引入提示框、标题、legend组件
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");
require("echarts/lib/component/legend")
//引入柱状图
require("echarts/lib/chart/bar");

export default {
	methods: {
	    init_charts_energy_hour() {
	      this.chart_energy_hour = echarts.init(document.getElementById("energy_hour")); ////初始化echarts实例
          this.chart_energy_hour.setOption({
		        tooltip: {
		          trigger: "axis",
		          axisPointer: {
		            // 坐标轴指示器,坐标轴触发有效
		            type: "shadow" // 默认为直线,可选为:'line' | 'shadow'
		          }
		        },
		        grid: {
		          left: "3%",
		          right: "4%",
		          bottom: "3%",
		          containLabel: true
		        },
		        xAxis: [
		          {
		            type: "category",
		            data: ["00:00","01:00","02:00","03:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00"],
		            axisLabel: {
		              show: true,
		              textStyle: {
		                color: "#B8BBC2"
		              }
		            },
		            axisLine: {
		              lineStyle: {
		                color: "#71747D",
		                width: 1
		              }
		            },
		            axisTick: {
		              alignWithLabel: true
		            }
		          }
		        ],
		        yAxis: [
		          {
		            name: "每小时耗能(Kwh)",
		            type: "value",
		            axisLabel: {
		              show: true,
		              textStyle: {
		                color: "#B8BBC2"
		              }
		            },
		            axisLine: {
		              lineStyle: {
		                color: "#71747D",
		                width: 1
		              }
		            }
		          }
		        ],
		        series: [
		          {
		            name: "耗能",
		            type: "bar",
		            barWidth: "60%",
		            data: [150, 160, 200, 334, 390, 330, 220, 150, 240, 320, 110, 255],
		            itemStyle:{
		              normal: {
		                //不同时段柱子设置不同颜色
		                color: function(params) {
		                    let colorList = [
		                        "#57C181",
		                        "#2CABE2",
		                    ];
		                    if(Number(params.name.slice(0,2)) >= 0 && Number(params.name.slice(0,2)) < 7){
		                      return colorList[0];
		                    }else{
		                      return colorList[1]
		                    }
		                }
		              }
		            }
		          }
		        ]
      		});
	    },
	}
	mounted() {
      this.init_charts_energy_hour(); //在mounted的生命周期执行echrts初始化,绘制图表
    }
}
</script>

绘制出图形如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值