美文网首页
Echart柱状图-横版模式+方框数字显示

Echart柱状图-横版模式+方框数字显示

作者: 衬fzy | 来源:发表于2022-03-26 15:41 被阅读0次
image.png

"echarts": "^5.1.2",

<template>
  <div id="leftEcharts2" class="echarts"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      Time1: null,
      dataArr: [
        { name: "名称47", value: 797 },
        { name: "名称1", value: 751 },
        { name: "名称32", value: 666 },
        { name: "名称22", value: 631 },
        { name: "名称05", value: 333 }
      ]
    };
  },
  beforeDestroy() {
    clearInterval(this.Time1);
  },
  mounted() {
    this.echartsFun();
    this.Time1 = setInterval(() => {
      this.echartsFun();
    }, 60000);
  },
  methods: {
    async echartsFun() {
      var colorList = ["#EA3C3C", "#FF7D26", "#CA9F29", "#5BFF83", "#00FBFF"];
      const myChart = echarts.init(document.getElementById("leftEcharts2"));
      const option = {
        grid: {
          // 这个一定要写,不写的话会有默认比列导致图标显示不完整
          top: "0%",
          left: "0%",
          right: "0%",
          bottom: "0%",
          containLabel: true // 显示范围包含坐标刻度
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        xAxis: {
          nameGap: 1,
          max: this.dataArr[0].value,
          show: false,
          type: "value"
        },
        yAxis: [
          {
            nameGap: 1,
            type: "category",
            inverse: true,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisPointer: {
              label: {
                show: true,
                margin: 30
              }
            },
            data: this.dataArr.map(item => item.value),
            axisLabel: {
              // 图形前面方形样式
              margin: 25,
              fontSize: 12,
              align: "left",
              color: "#fff",
              rich: {
                a0: {
                  borderWidth: 1,
                  padding: [2, 1, 0, 1], // 让数字居中
                  borderColor: colorList[0],
                  width: 14,
                  height: 13,
                  color: colorList[0],
                  align: "center"
                },
                a1: {
                  borderWidth: 1,
                  padding: [2, 1, 0, 1],
                  borderColor: colorList[1],
                  width: 14,
                  height: 13,
                  color: colorList[1],
                  align: "center"
                },
                a2: {
                  borderWidth: 1,
                  padding: [2, 1, 0, 1],
                  borderColor: colorList[2],
                  width: 14,
                  height: 13,
                  color: colorList[2],
                  align: "center"
                },
                a3: {
                  borderWidth: 1,
                  padding: [2, 1, 0, 1],
                  borderColor: colorList[3],
                  width: 14,
                  height: 13,
                  color: colorList[3],
                  align: "center"
                },
                a4: {
                  borderWidth: 1,
                  padding: [2, 1, 0, 1],
                  borderColor: colorList[4],
                  width: 14,
                  height: 13,
                  color: colorList[4],
                  align: "center"
                }
              },
             formatter: (params, k) => {
                return ["{a" + k + "|" + (k + 1) + "}"].join("\n");
              }
            }
          },
          {
            nameGap: 1,
            type: "category",
            inverse: true,
            axisTick: "none",
            axisLine: "none",
            show: true,
            data: this.dataArr.map(item => item.value),
            axisLabel: {
              show: true,
              fontSize: 12,
              color: "#fff",
              formatter: (val, k) => {
                console.log(val);
                return `  ${this.dataArr[k].name}`;
              }
            }
          }
        ],
        series: [
          {
            name: "value",
            type: "bar", // bar柱状图
            showBackground: true, // 设置柱状图背景色
            backgroundStyle: { color: "rgba(225, 225, 225, 0.55)" }, // 背景色
            barWidth: 10,
            barCategoryGap: "1%", // 柱子之间间距
            data: this.dataArr.map((item, i) => {
              let itemStyle = {
                color: colorList[i]
              };
              return {
                value: item.value,
                itemStyle: itemStyle
              };
            }),
            label: {
              show: false,
              position: "right",
              color: "#333333",
              fontSize: 14,
              offset: [10, 0],
              normal: {
                // 柱状图末端内部显示数字
                show: true,
                position: "insideBottomRight",
                // formatter: "{c} h", // 拼接单位
                distance: 0,
                offset: [14, 10],
                color: "#fff",
                fontSize: 12,
                padding: [5, 15, 10, 15]
              }
            }
          }
          // {
          //   name: "背景",
          //   type: "bar",
          //   barWidth: 10,
          //   barGap: "-100%",
          //   itemStyle: {
          //     normal: {
          //       color: "rgba(225, 225, 225, 0.55)"
          //     }
          //   },
          //   data: maxArr
          // }
        ]
      };
      myChart.setOption(option, true); // true无数据时更新试图
    }
  }
};
</script>
<style lang="scss" scoped>
.echarts {
  width: 100%;
  height: 100%;
}
</style>

感谢大家点赞!

相关文章

  • Echart柱状图-横版模式+方框数字显示

    "echarts": "^5.1.2", 感谢大家点赞!

  • Echart不显示地图问题

    Echart不显示地图的问题 使用百度Echart的时候遇到不会显示地图的问题,是现在Echart不直接提供地图需...

  • echarts柱状图时间轴样式修改

    echart柱状图中,给x轴添加时间轴,需要调整样式,各项内容如下~

  • 魔兽世界常用命令

    显示伤害数字 显示治疗数字 伤害字体大小 伤害数字弹出设置方法 其中输入0为旧版弹出模式,1为新版弹出模式

  • echarts使用感受

    一.Echart基本图表显示与地图显示将各种图形拆分为交互组件,各组件接收特定参数实现效果 echart实例生成每...

  • Echart 柱状图模板

    好久不写Echart图,配置基本上会忘记,这里记录一个柱状图的模板留着参考。这是个组件。效果: 代码:

  • Android 简单的柱状图 自定义绘制

    直接步入正题(下方附带demo)。 显示需求: 1、柱状图实现上方文字显示; 2、柱状图外部边框...

  • 图表优化

    1.进入页面默认显示柱状图。选择柱状图type后,更改日期默认显示之前type的柱状图。 在js中设置全局变量va...

  • echart柱状图或折线图顶端显示数值

    思路 直接在series中添加itemStyle或label 代码 网站导航 网站导航

  • echarts处理区域点击

    echart默认的点击事件只能通过点击图形触发,比如柱状图,只有点击柱子才会触发,如果数据相差较大,有些柱子非常小...

网友评论

      本文标题:Echart柱状图-横版模式+方框数字显示

      本文链接:https://www.haomeiwen.com/subject/yyfdjrtx.html