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

echarts柱状图实现左右横向对比

实现效果如上图

 其实是两组数据,其中一组数据改为负数,然后 在展示的时候,在将负数取反

第一处修改坐标轴

   xAxis: [{type: 'value',axisLabel: {formatter: function (value) {if (value < 0) {return -value;}else{return value;}}}}],

第二处修改数据,转负为正

 {name: '左',type: 'bar',color: '#21B8FF',stack: '总量',label: {show: true,position: 'left',formatter: function (value) {if (value.data < 0) {return -value.data;}},},emphasis: {focus: 'series'},data: [-12, -12, -10, -13, -19, -23, -21, -34, -22, -12]}

这里数据如果初始为正,先便利一遍加负号

我的全部代码

<template><div id="echarts11"></div>
</template><script setup>
import * as echarts from "echarts";
const emit = defineEmits();const props = defineProps({echartList: {default: [],},dolDate: {defalut: false}
});
const initChart = () => {let xdata = [];let ydata1 = [];let ydata2 = [];let xdataid = [];props.echartList.forEach((item) => {xdata.push(item.cityName);// xdataid.push(item.cityId);ydata1.push(item.parkArea);ydata2 = [-15, -25, -35, -45, -55]// ydata2.push(item.water);});const machart = echarts.init(document.getElementById("echarts11"));var option = {tooltip: {trigger: 'axis',axisPointer: {            // 坐标轴指示器,坐标轴触发有效type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'}},legend: {data: ['左', '右']},grid: {left: '3%',right: '4%',bottom: '3%',containLabel: true},xAxis: [{type: 'value',axisLabel: {formatter: function (value) {if (value < 0) {return -value;}else{return value;}}}}],yAxis: [{type: 'category',axisTick: {show: false},data:xdata}],series: [{name: '右',type: 'bar',color: '#74D9A9',stack: '总量',label: {show: true,position: 'right'},emphasis: {focus: 'series'},data: [2, 3, 9, 7, 139, 66, 42, 16, 9, 10]},{name: '左',type: 'bar',color: '#21B8FF',stack: '总量',label: {show: true,position: 'left',formatter: function (value) {if (value.data < 0) {return -value.data;}},},emphasis: {focus: 'series'},data: [-12, -12, -10, -13, -19, -23, -21, -34, -22, -12]}]};machart.setOption(option);setTimeout(() => {window.addEventListener("resize", resizeFn);}, 100);const resizeFn = () => {return machart.resize();};machart.on("click", (params) => {if (params.componentType === "series") {const dataIndex = params.dataIndex;const yValue = xdata[dataIndex]; // 获取对应柱子的y值// const cidtyid = xdataid[dataIndex]; // 获取对应柱子的y值emit("changedi", yValue);// 在这里你可以对获取到的y值进行其他操作,比如弹窗显示等}});
};
setTimeout(() => {initChart();
}, 800);
defineExpose({initChart,
});
onBeforeUnmount(() => {// 离开页面必须进行移除,否则会造成内存泄漏,导致卡顿window.removeEventListener("resize", initChart);
});
watch(() => props.echartList,(newVal, oldVal) => {if (newVal !== oldVal) {initChart();}}
);
</script><style scoped>
#echarts11 {width: 100%;height: 90%;/* height:    calc(30vh- 10px); */
}
</style>

http://www.lryc.cn/news/341615.html

相关文章:

  • 脸爱云一脸通智慧管理平台 SystemMng 管理用户信息泄露漏洞(XVE-2024-9382)
  • spring笔记2
  • 【挑战30天首通《谷粒商城》】-【第一天】02、简介-项目整体效果展示
  • Kafka 生产者应用解析
  • GEE错误——image.reduceRegion is not a function
  • rk356x 关于yocto编译linux及bitbake实用方法
  • Chrome您的连接不是私密连接 |输入“thisisunsafe”命令绕过警告or添加启动参数
  • 牛客面试前端1
  • Linux的软件包管理器-yum
  • 选择排序(Selection Sort)
  • 网络面试题目
  • Web,Sip,Rtsp,Rtmp,WebRtc,专业MCU融屏视频混流会议直播方案分析
  • Unreal 编辑器工具 批量重命名资源
  • Voice Conversion、DreamScene、X-SLAM、Panoptic-SLAM、DiffMap、TinySeg
  • 短信群发平台分析短信群发的未来发展趋势
  • supervisord 使用指南
  • AngularJS 的生命周期和基础语法
  • docker-compose 网络
  • 农药生产厂污废水如何处理达标
  • 根据相同的key 取出数组中最后一个值
  • Github Action Bot 开发教程
  • 使用docker创建rocketMQ主从结构,使用
  • 一次完整的 http 请求是怎样的?
  • 并行执行的概念—— 《OceanBase 并行执行》系列 一
  • 使用 ipdb 调试回调函数
  • 介绍一下mybatis的基本配置(mybatis-config.xml)
  • 【MySQL】第一次作业
  • 10个免费视频素材网站,剪辑师们赶紧收藏!
  • 【毕业设计】基于SSM的运动用品商城的设计与实现
  • 【Web】CTFSHOW 中期测评刷题记录(1)