vue echarts实现横向柱状图

2022-04-15 0 691

本文实例为大家分享了vue echarts实现横向柱状图的具体代码,供大家参考,具体内容如下

实现效果:

vue echarts实现横向柱状图

代码:

<template>
  <div class="OverYearsPompany">
    <div id="OverYearsPompanyChart" style="flex: 1; height: 368px; margin-top: -42px"></div>
  </div>
</template>
<script>
import { getProposedInvestments } from '@/api/government';
const colors = [
  ['rgba(240, 7, 100, 1)', 'rgba(0, 215, 229, 1)'],
  ['rgba(240, 7, 100, 1)', 'rgba(0, 215, 229, 1)'],
  ['rgba(240, 7, 100, 1)', 'rgba(0, 215, 229, 1)'],
  ['rgba(240, 7, 100, 1)', 'rgba(0, 215, 229, 1)'],
];
export default {
  data() {
    return {
      investmentsWayData: [],
      investmentsWayDataCount: [],
      investmentsWayDataCounts: [],
    };
  },
  mounted() {
    this.getProposedInvestments();
  },
  methods: {
    initMap() {
      var myChart = this.$echarts.init(document.getElementById('OverYearsPompanyChart'));
      const option = {
        tooltip: {
          show: true,
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        xAxis: {
          type: 'value',
          axisLabel: {
            show: true,
            color: '#02CFFCFF',
            fontFamily: 'TencentSans',
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#02CFFCFF',
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(71, 126, 171, 1)',
            },
          },
        },
        yAxis: [
          {
            type: 'category',
            inverse: true, //倒叙
            axisLabel: {
              color: '#02CFFCFF',
              fontFamily: 'TencentSans',
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#02CFFCFF',
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dotted',
                color: 'rgba(71, 126, 171, 1)',
              },
            },
            data: this.investmentsWayData,
          },
        ],
        series: [
          {
            type: 'bar',
            barWidth: 15,
            label: {
              position: ['98%', -20],
              show: true,
              color: '#fff',
              fontFamily: 'TencentSans',
            },
            data: this.investmentsWayDataCount,
            itemStyle: {
              color(params) {
                const { dataIndex } = params;
                let color = {
                  type: 'linear',
                  x: 1,
                  y: 0,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: colors[dataIndex] ? colors[dataIndex][0] : 'red',
                    },
                    {
                      offset: 1,
                      color: colors[dataIndex] ? colors[dataIndex][1] : 'red',
                    },
                  ],
                };
                return color;
              },
            },
          },
        ],
      };
      myChart.setOption(option);
    },
    getProposedInvestments() {
      getProposedInvestments().then((res) => {
        const { status, data } = res;
        const { proposedInvestmentsWayDis } = JSON.parse(data);
        if (status === 200) {
          // this.investmentsWayData=[{0: "合资", 1: "合作", 2: "独资", 3: "其他"}]
          this.investmentsWayData = proposedInvestmentsWayDis.map((item) => {
            return item.wayDis;
          });
          // this.investmentsWayDataCount=[{0: "496", 1: "372", 2: "248", 3: "124"}]
          this.investmentsWayDataCount = proposedInvestmentsWayDis.map((item) => {
            return item.count;
          });
          // this.investmentsWayDataCounts=[{itemStyle:
    // color:{
    // 0: "rgba(240, 7, 100, 1)"
    // 1: "rgba(0, 215, 229, 1)"}
    // value: "496"}]
          this.investmentsWayDataCounts = proposedInvestmentsWayDis.map((item, index) => {
            return {
              value: item.count,
              itemStyle: {
                color: colors[index],
              },
            };
          });
          this.initMap();
        }
      });
    },
  },
};
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript vue echarts实现横向柱状图 https://www.niceym.com/23846.html