【Echarts系列】水平柱状图

news/2024/7/3 0:13:20

【Echarts系列】水平柱状图

    • 示例
    • 数据格式
    • 代码

为了节省后续开发学习成本,这个系列将记录我工作所用到的一些echarts图表。

示例

水平柱状图如图所示:
在这里插入图片描述

数据格式

data = [
    {
      'name': '于洪区',
      'value': 2736
    },
    {
      'name': '新民市',
      'value': 2844
    },
    {
      'name': '皇姑区',
      'value': 2889
    },
    {
      'name': '沈河区',
      'value': 3143
    }
  ]

代码

Vue版本以及脚本语言的选择各有不同,核心内容主要是option,重点关注该部分内容即可。

<template>
  <div class="chart" ref="horizontalBarRef"></div>
</template>

<script lang="ts">
import { Component, Prop, Ref, Vue, Watch } from 'vue-property-decorator'
import echarts from 'echarts'

@Component({
  name: 'HorizontalBar',
  components: {}
})
export default class HorizontalBar extends Vue {
  @Prop() data!: any
  @Prop({ default: () => ['rgba(72, 133, 201, 0.6)', 'rgba(72, 133, 201, 1)']}) colors?: any[]
  @Ref() horizontalBarRef!: any

  //此处监听是为了在有状态切换时,例如时间年份或其他条件改变时,能够让Echarts图表重新渲染
  @Watch('data')
  onDataChange() {
    this.createHorizontalBarChart()
  }

  private chart: any = {}

  createHorizontalBarChart() {
    this.chart = echarts.init(this.horizontalBarRef)
    const data = this.data
    let names = []
    let values = []
    data.forEach(item => {
      names.push(item.name)
      values.push(item.value)
    })
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        },
        confine: true,         //让提示框恒定在网格容器内,【针对显示不全或者被遮盖问题】
      },
      grid: {
        left: 20,
        right: 40,
        top: 0,
        bottom: 20,
        containLabel: true     //网格边距包含坐标标签
      },
      xAxis: {
        axisTick: {
          show: false          //是否显示X轴坐标刻度
        },
        axisLabel: {
          show: false		   //是否显示X轴坐标标签
        },
        axisLine: {
          show: false		   //是否显示X轴轴线
        },
        splitLine: {
          lineStyle: {
            type: 'dashed'	   //以虚线形式展示X轴在网格中的分隔线
          }
        }
      },
      yAxis: {
        type: 'category',
        data: names,
        axisTick: {
          show: false,
          alignWithLabel: true    //刻度与坐标标签对齐
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: '#757790',     //设置Y轴轴线样式
            width: 2
          }
        },
        axisLabel: {
          textStyle: {
            color: '#757790',	 //设置Y轴坐标标签样式
            fontSize: 14
          }
        },
        splitLine: {
          show: false			//是否展示Y轴方向上的分隔线
        }
      },
      series: [{
        type: 'bar',
        barWidth: 10,
        itemStyle: {
          color: {
            type: 'linear',			//设置柱状条的渐变颜色
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [
              { offset: 0, color: this.colors[0] }, // 0% 处的颜色
              { offset: 1, color: this.colors[1] } // 100% 处的颜色
            ],
            global: false // 缺省为 false
          }
        },
        label: {
          show: true,				//展示柱状条数值信息
          position: 'right',
          color: '#12121D',
          fontSize: 14
        },
        data: values
      }]
    }
    this.chart.setOption(option)
  }

  mounted() {
    this.createHorizontalBarChart()
    window.addEventListener('resize', this.chartResize)
  }

  beforeDestroy() {
    if (this.chart) {
      window.removeEventListener('resize', this.chartResize)
      this.chart.dispose()
    }
  }

  chartResize() {
    if (this.chart) {
      this.chart.resize()
    }
  }
}
</script>
<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 300px;
}
</style>



http://lihuaxi.xjx100.cn/news/2215571.html

相关文章

通俗范畴论2 有向图与准范畴

退一步海阔天空&#xff0c;在正式进入范畴论之前&#xff0c;我们可以重新审视一下我们是如何认识世界的&#xff0c;有了这个对人类认识世界过程的底层理解&#xff0c;可以帮助我们更好地理解范畴论。 对于人类认识世界&#xff0c;最神奇的一点就是这个世界居然是可以认识…

20.1 JSON-JSON接口以及在Go语言中使用JSON

1. 简介 JSON即JavaScript对象表示法(JavaScript Object Notation)&#xff0c;是一种用于存储和交换数据的格式&#xff0c;是一种可供人类阅读和理解的纯文本格式。 JSON既可以键值对的形式&#xff0c;也可以数组的形式&#xff0c;表示数据。 JSON最初是JavaScript的一个…

【数据结构】初识集合深入剖析顺序表(Arraylist)

【数据结构】初识集合&深入剖析顺序表&#xff08;Arraylist&#xff09; 集合体系结构集合的遍历迭代器增强for遍历lambda表达式 List接口中的增删查改List的5种遍历ArrayList详解ArrayList的创建ArrayList的增删查改ArrayList的遍历ArrayList的底层原理 &#x1f680;所属…

在Python中实现排序算法:以冒泡排序和快速排序为例

在Python中实现排序算法&#xff0c;如冒泡排序和快速排序&#xff0c;是算法和数据结构学习中的基础内容。下面我将从技术难点、面试官关注点、回答吸引力以及代码举例四个方面进行详细描述。 一、技术难点 冒泡排序&#xff1a; 双重循环&#xff1a;冒泡排序需要两层循环&…

算法分析与设计(持续更新……)

&#x1f4da;不进&#x1f9e0; 一、入门 &#x1f4d9; 时间复杂度 ✨ 排序的时间复杂度 &#x1f4d9; 算法伪码 &#x1f4d9; 函数的渐近界 ✨ 渐近上界大O &#x1f468;‍&#x1f3eb; 笛霸格 教程视频 ✨ 渐近下界 Ω &#x1f468;‍&#x1f3eb; 笛霸格 教…

C++ 45 之 赋值运算符的重载

#include <iostream> #include <string> #include <cstring> using namespace std;class Students05{ public:int m_age;char* m_name;Students05(){}Students05(const char* name,int age){// 申请堆空间保存m_name;this->m_name new char[strlen(name)…

18.EventLoopGroup分工细化

分工细化一 服务端可以定义两个EventLoopGroup 第一个是boss,第二个是worker的。将accept事件和read,write事件分开处理。 package com.xkj.learn;import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerConte…

黑马头条Minio报错non-xml response from server错误的解决方法

今天在写项目的时候&#xff0c;想测试minio上传文件功能是否正常&#xff0c; 但是每次都出现non-xml response from server的错误。 自己也在网上找了很多解决方法&#xff0c;大部分是说用户名和密码的配置问题&#xff0c;但是检查后发现并没有错误。 最后发现是自己的dock…