首页 > 其他分享 >echart 柱状图x轴悬浮显示文字

echart 柱状图x轴悬浮显示文字

时间:2023-12-26 15:11:28浏览次数:26  
标签:textStyle echart 悬浮 selectEchartData value color 柱状图 true axisTip

两种方法: 1.自带属性,自带样式 ; 2. 自定义样式

   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>

  

标签:textStyle,echart,悬浮,selectEchartData,value,color,柱状图,true,axisTip
From: https://www.cnblogs.com/everseventh/p/17928180.html

相关文章

  • wpf + LiveCharts.wpf 做个漂亮的图表
    十年河东,十年河西,莫欺少年穷学无止境,精益求精参考:WPFLiveChart图表详解接着上一篇博客: wpf+MaterialDesign+Prism8实现导航功能 1、项目引入图表包 2、定义用户控件IndexView的IndexViewModel,如下usingLiveCharts;usingPrism.Mvvm;usingSystem;usingSystem.Col......
  • echarts在vue中不显示中国地图
    遇到的情况是在开发中,中国地图是正常显示的但是打包之后,放到服务器,地图就不显示了,经过搜索,说是因为低版本的echarts需要手动添加map首先找到这个文件然后手动写上这个最后重新打包试试吧......
  • echart 宽度自适应
    created(){this.getEchartData()},getEchartData(){constparam={}this.$axios.get(url,param).then(res=>{if(res.data&&res.data.data){this.echartData=res.data.datathis......
  • eCharts记录一柱形图案例
     option={color:'#8AE6C7',grid:{left:'50',right:'50',bottom:'50',containLabel:true},textStyle:{color:'rgba(0,0,0,.58)'},xAxis:{type:'catego......
  • [LeetCode Hot 100] LeetCode84. 柱状图中最大的矩形
    题目描述思路:枚举+优化(单调栈)先固定矩阵的高。然后向左向右找到第一个比当前元素值小的元素,确定好左右边界。对于元素2来说:向左找到第一个比当前元素值小的元素:1的右边界向右找到第一个比当前元素值小的元素:3的右边界枚举每个元素的上边界,确定往左数最远到达哪个边界......
  • 如何解决appium 自动化无法获取悬浮窗问题
    如何解决appium自动化无法获取悬浮窗问题-CSDN博客......
  • wpf + LiveCharts.wpf 做个漂亮的图表
    十年河东,十年河西,莫欺少年穷学无止境,精益求精参考:WPFLiveChart图表详解接着上一篇博客: wpf+MaterialDesign+Prism8实现导航功能 1、项目引入图表包 2、定义用户控件IndexView的IndexViewModel,如下usingLiveCharts;usingPrism.Mvvm;usingSystem;using......
  • echarts画桑基图,并根据选择的分析因子画图
    最近要在系统中增加一个桑基图,要求桑基图可以根据选择的分析因子重新绘图。仔细看了下echarts的示例,桑基图本身很简单,困难的是如何根据数据资料整理成桑基图的数据格式,并且实现选择分析因子重新绘图。研究了几天的数据资料,特将方法写个demo记录下。<scriptsrc="./plugins/ech......
  • echarts柱形图给X轴坐标类目添加点击事件
    在项目中遇到这么个需求要在柱形图上的x轴添加点击事件,当点击对应x轴文字的时候要弹出模态框展示子图表根据echarts的Api给图表实例绑定点击事件myChartInstance?.on('click','xAxis.category',(params)=>{if(params.value==='其他变动成本'){set......
  • Highcharts 用SVGRenderer方法使柱状图连接列边​
    需求在Highcharts中,可以使用SVGRenderer方法来创建路径连接柱状图的列边,并根据具体的数据和需求进行适当的调整和扩展。分析要使用Highcharts的SVGRenderer方法来绘制柱状图并连接列边,可以按照以下步骤进行操作:创建柱状图:使用Highcharts的 chart 方法创建一个柱状图,并......