vue柱状图x轴文字、垂直在柱子顶部 - echart图表

该博客展示了如何使用 ECharts 框架创建一个柱状图,其中 x 轴数据包含换行标签。通过 `mounted` 生命周期钩子和 `window.onresize` 事件监听窗口变化以实现图表的自适应重载。`vertical` 方法用于将连续字符转换为带有换行的字符串,使标签在图表中更易读。同时,配置了图表的样式,包括颜色渐变、边框、坐标轴细节等。此外,还定义了高亮状态下柱子的颜色变化。
摘要由CSDN通过智能技术生成
如图所示:在这里插入图片描述
代码:
<template>
  <div class="ml20 mr20 mb20 colorFOp">
    <div class="head tc mb20 ">测试案例</div>
      <div ref="test" class="testChart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      myTestChart:{},
    };
  },
  mounted() {
    this.testChart()
    window.onresize = ()=>{ //窗口变化自动重载
      this.myTestChart.resize()
    }
  
  },
  methods: {
    //添加\n换行符
    vertical(params){ 
      const txtArry = params.split('');
      let rs = "";
      txtArry.map(item => {
        rs += item + "\n"
      })
      return rs;
    },
    testChart(){
      this.myTestChart = echarts.init(this.$refs.test);
      let option = {
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          left: '10',
          right: '20',
          bottom: '30',
          top:'20',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            //实际使用:['1月','2月'].map(item => this.vertical(item))
            data: Array.from({length:12},(v,k)=>this.vertical(`${k+1}月 自诩人间第一流`)),
            axisTick:{
              show:false,
            },
            axisLine: {
              lineStyle: {
                type: 'solid',
                color: '#fff',//边线的颜色
                width:'1'//坐标线的宽度
              }
            },
            axisLabel: {
              show:true,
              inside: true,
              textStyle: {
                color: '#fff',//坐标值得具体的颜色
                fontSize:'10'
              },
            },
            position: "top",
            z: 10
          }
        ],
        yAxis: [
          {
            type: 'value',
            splitLine:{
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: 'solid',
                color: '#fff',//左边线的颜色
                width:'1'//坐标线的宽度
              }
            },
            axisLabel: {
              textStyle: {
                color: '#fff',//坐标值得具体的颜色
              }
            }
          }
        ],
        series: [
          {
            type: 'bar',
            showBackground: true,//展示背景
            itemStyle: { //柱子颜色
              color: new echarts.graphic.LinearGradient(
                0, 0, 0, 1,
                [
                  {offset: 0, color: '#00C4FF'},
                  {offset: 0.5, color: '#0F7AAE'},
                  {offset: 1, color: '#00023A'}
                ]
              ),      
              //normal: { //值垂直在柱子底部
				//	barBorderRadius: 5,
				//	label:{
				//		show:true,
				//		position: "insideBottom",
				//		textStyle: { //数值样式
				//			color: '#333333',
				//			fontSize: 11
				//		},
				//		formatter:(val) => {
				//			return `${this.vertical(val.value)}`
				//		},
				//	}
				//}              
            },
            emphasis: {//鼠标经过状态柱子颜色
              itemStyle: {
                color: new echarts.graphic.LinearGradient(
                  0, 0, 0, 1,
                  [
                    {offset: 0, color: '#00023A'},
                    {offset: 0.7, color: '#0F7AAE'},
                    {offset: 1, color: '#00C4FF'}
                  ]
                )
              }
            },
          
            barWidth : 25,//柱图宽度
            data: [711,322,733,411,222,444,666,555,333,222,444,888]
          },
        ]
      }

      // 使用刚指定的配置项和数据显示图表。
      this.myTestChart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
  .testChart{
    margin:100px auto;
    width:850px;
    height: 50vh;
  }
  .title{
    font-size: 16px;
    color: #00F8FD;
    line-height: 42px;
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘斩仙的笔记本

富贵险中求

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值