当前位置: 首页 > news >正文

echarts自定义图表

普通柱状图
在这里插入图片描述
自定义柱状图
在这里插入图片描述

实现思路
4个图层

  • 背景 边框实体 内部透明
  • 地步透明间隙
  • 红色柱形+数据数据使用自定义字体+倾斜
  • 柱形上方扁平矩形矩形颜色透明 label字体颜色和背景色相同实现矩形块 上下靠padding实现 向下 paddingBottom使用负数

完整代码

<!DOCTYPE html>
<html style="height: 100%; background: #000"><head><meta charset="utf-8" /><title>发光柱状图</title><script src="https://cdn.jsdelivr.net/npm/echarts@5"></script><style>/* 在 CSS 文件中定义自定义字体 */@font-face {font-family: "MyCustomFont"; /* 自定义字体名称 */src: url("public/只含数字.ttf");font-weight: normal;font-style: normal;}</style></head><body style="background: #fff; height: 100vh; overflow: hidden; padding: 200px"><divid="main"style="height: 200px; width: 300px"></div><script>const chart = echarts.init(document.getElementById("main"));const data = [39, 47, 40, 84, 35, 90, 71];const categories = ["A", "B", "C", "D", "E", "F", "G"];const redBarOffset = 5;// 最大值为100时 如下 增加 下方空隙 + 上方labelconst maxBarValue = 100 + redBarOffset + 10;const fontSize = 12;const option = {backgroundColor: "#000",xAxis: {type: "category",data: categories,axisLine: { lineStyle: { color: "transparent" } },axisLabel: {color: "#666",fontSize: fontSize,},},yAxis: {show: false,max: maxBarValue + redBarOffset + 10,},grid: {left: "5%",right: "5%",bottom: "12%",top: "5%",},series: [// 金色背景柱{type: "bar",data: Array(data.length).fill(maxBarValue),barWidth: "80%",itemStyle: {color: "#211316fa",borderColor: "#302727",borderWidth: 3,},z: 1,},// 占位透明柱(用于悬空红柱){type: "bar",data: Array(data.length).fill(redBarOffset),stack: "data",barWidth: "40%",itemStyle: {color: "transparent",},z: 2,},// 红色柱子(真实数据){type: "bar",animationDuration: 1500,animationEasing: "elasticOut",data: data.map(v => v),stack: "data",barGap: "-75%",itemStyle: {color: "#7A192A",// color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [//     { offset: 0, color: "#9D1727" },//     { offset: 1, color: "#8A2F34" },// ]),shadowColor: "#9D1727",shadowBlur: 10,},label: {show: true,position: "top",color: "#eebb43",fontWeight: "bold",fontSize: fontSize,formatter: "{c}",fontFamily: "MyCustomFont",fontStyle: "italic",textShadowColor: "#eebb43",textShadowBlur: 3,distance: fontSize,},z: 3,},// 顶部矩形块 用label替代{type: "bar",stack: "data",data: data.map(v => 0),color: "transparent",label: {show: true, // 显示标签position: "top", // 标签位置(如 top/inside/outside)color: "#9f2C2D", // 文字颜色(白色更醒目)backgroundColor: "#9f2C2D", // 背景颜色borderRadius: fontSize / 7, // 边框圆角设为 0 实现矩形// padding: [fontSize / 6, fontSize / 1.5], // 内边距调整矩形大小padding: [0, fontSize / 1.5, -fontSize / 6, fontSize / 1.5], // 内边距调整矩形大小formatter: "", // 显示数据值({c} 代表数据值)shadowColor: "#9f2C2D",shadowBlur: fontSize / 2,align: "center",},z: 4,},],};chart.setOption(option);</script></body>
</html>

相关文章:

  • AI与智能农业:如何通过精准农业提升作物产量与资源使用效率?
  • Linux进程学习【环境变量】进程优先级
  • AUTOSAR_Feature_Model_Analysis
  • c++流对象
  • 智慧水库与AI深度融合的实现方案及典型应用场景
  • MySQL快速入门篇---增删改查(下)
  • LeetCode 24 两两交换链表中的节点
  • 【深度好文】4、Milvus 存储设计深度解析
  • 【Nginx】负载均衡配置详解
  • 【2025最新Java面试八股】如何在Spring启动过程中做缓存预热?
  • kafka 中消费者 groupId 是什么
  • [python] 基于WatchDog库实现文件系统监控
  • Seaborn模块练习题
  • GCC 内建函数汇编展开详解
  • 【数据挖掘】时间序列预测-时间序列预测策略
  • 脏读、幻读、可重复读
  • 反序列化漏洞2
  • 数据结构(七)---链式栈
  • 力扣HOT100之链表:23. 合并 K 个升序链表
  • ComfyUI for Windwos与 Stable Diffusion WebUI 模型共享修复
  • 当隐身13年的北小京决定公开身份 ,专业戏剧评论依然稀缺
  • 因高颜值走红的女通缉犯出狱后当主播自称“改邪归正”,账号已被封
  • 湖南小伙“朱雀玄武敕令”提交申请改名为“朱咸宁”
  • 应勇:以法治力量服务黄河流域生态保护和高质量发展
  • 强政神鸟——故宫里的乌鸦
  • 经济日报:上海车展展现独特魅力