echart 柱状图x轴悬浮显示文字
两种方法: 1.自带属性,自带样式 ; 2. 自定义样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | renderBarChart () { // let that = this const barChart = echarts.init(document.getElementById( 'bar-chart' )) const barOptions = { title: { // text: this.selectEchartData.label.length > 32 ? this.selectEchartData.label.substr(0, 32) + '...' : this.selectEchartData.label, textStyle: { fontSize: 14 }, left: 'center' }, tooltip: { // 属性自带样式 trigger: 'axis' , confine: true , textStyle: { fontSize: 12 } }, grid: { left: '2%' , right: '4%' , bottom: '4%' , containLabel: true }, xAxis: { triggerEvent: true , // 开启坐标轴标签响应和触发鼠标事件,自定义x轴文字样式 // type: 'category', data: this .selectEchartData.questionResVOList.map(obj => obj.optionLabel), axisLabel: { formatter: function (value) { return value.length > 7 ? value.substring(0, 4) + '...' : value }, textStyle: { fontSize: 12, color: '#999999' , fontWeight: 400 }, interval: 0 // x轴文字 }, axisLine: { lineStyle: { color: '#E0E0E0' } }, axisTick: { inside: true , show: false } }, yAxis: [ { type: 'value' , scale: true , min: 0, // max: (value) => { // 当数据位数不固定时,最大值向上取整 // let num = 10 ** (value.max.toString().length - 2) // return Math.ceil(value.max / num) * num // }, minInterval: 5, splitLine: { show: true , lineStyle: { type: 'dashed' , color: '#ddd' } }, axisLine: { lineStyle: { color: '#E0E0E0' } }, axisLabel: { textStyle: { color: '#999999' } }, axisTick: { inside: true } } ], series: [{ label: { show: true , // 是否显示 position: 'top' , // 数值显示的位置 textStyle: { fontSize: 10, color: '#333333' , fontWeight: 600 } }, type: 'bar' , barWidth: '24px' , // barWidth: '60%', itemStyle: { color: function (params) { return '#FF783D' // return that.customColors[params.dataIndex % that.customColors.length]/ } }, data: this .selectEchartData.questionResVOList.map(obj => obj.voteCount) }] } barChart.setOption(barOptions) // 自定义悬浮x轴文字样式 barChart.on( 'mouseover' , 'xAxis' , function (e) { console.log(e,999); let axisTip = document.querySelector( '.axis-tip' ) axisTip.innerText = e.value axisTip.style.left = e.event.offsetX + 'px' axisTip.style.top = e.event.offsetY + 'px' axisTip.style.display = 'block' }) barChart.on( 'mouseout' , 'xAxis' , function (e) { let axisTip = document.querySelector( '.axis-tip' ) axisTip.innerText = '' axisTip.style.display = 'none' }) }, |
.axis-tip {
display: none;
position: absolute;
padding: 5px 5px;
font-size: 12px;
line-height: 18px;
max-width: 200px;
height: fit-content;
color: #575757;
background: #ffffff;
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.2);
border-radius: 4px;
bottom: 0;
left: 0;
}
<div id="bar-chart" ref="barChartRef" style="width:40%;height:250px;"></div>
<div class="axis-tip"></div>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构