vue 之 echarts饼形图 如何显示负数份额

博客围绕Vue中ECharts饼形图如何显示负数份额展开,涉及饼形图组件、深拷贝对象以及传入的数据等内容,还提及了barChartData。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue 之 echarts饼形图 如何显示负数份额

在这里插入图片描述

饼形图组件

<template>
  <div class="financial_expenses_bar">
    <div class="chart" ref="pie_chart"></div>
  </div>
</template>

<script>
export default {
  name: "FinancialExpensesBar",
  components: {},
  props: {
    barChartData: {
      type: Array,
      default() {
        return [];
      },
    },
    barChartColor: {
      type: Array,
      default() {
        return [];
      },
    },
    barChartRadius: {
      type: Array,
      default() {
        return ["50%", "50%"];
      },
    },
  },
  mounted() {
    this.getPieChatr();
  },
  // 监听传递进来的数据 为了更新 eachrts上的数据 因为在mounted需要除此下次
  watch: {
    barChartData() {
      this.getPieChatr();
    },
    barChartColor() {
      this.getPieChatr();
    },
  },
  methods: {
    getPieChatr() {
      let pieChartSimple = this.$echarts.init(this.$refs.pie_chart);

      let changeData = this.barChartData.map((item) => {
        let changeItem = {};
        this.$deepCopy(changeItem, item);
        return changeItem;
      });
      changeData.map((item) => {
        for (let key in item) {
          if (item[key] < 0) {
            item[key] = JSON.stringify(Math.abs(item[key]));
          }
        }
      });
      let that = this;

      //02:配置配置项和数据
      let option = {
        color: this.barChartColor, // 饼形图的颜色数据
        series: [
          {
            name: "余额",
            type: "pie",
            center: ["50%", "45%"],
            radius: this.barChartRadius,
            avoidLabelOverlap: false,
            label: {
              color: "#000",
              show: true,
              position: "outer",
              width: 10,
              height: 0,
              lineHeight: 0,
              labelLine: {
                length: 2,
                length2: 10,
              },
              formatter: function (p) {
                let value =
                  that.barChartData[p.dataIndex].value > 0
                    ? p.value * 1
                    : p.value * -1;
								let name = that.barChartData[p.dataIndex].name;
								let str = "{top|" + name  + "}\n {bottom|" + value + "}"
									return str;
              },

              align: "center",
              rich: {
                top: {
                  color: "#333",
                  fontSize: 12,
                  verticalAlign: "bottom",
                  padding: [3, 3, 0, 3],
                  align: "center",
                },
                bottom: {
                  color: "#333",
                  fontSize: 12,
                  padding: [0, 3, 3, 3],
                  verticalAlign: "top",
                  align: "center",
                },
              },
            },
            labelLine: {
              show: false,
            },
            // 饼形图数据
            data: changeData,
          },
        ],
      };

      //03:使用指定的配置项给实例对象,用于数据显示图表。
      pieChartSimple.setOption(option);
      //4:让图表跟随屏幕自适应
      window.addEventListener("resize", function () {
        pieChartSimple.resize();
      });
    },
  },
};
</script>
<style  lang="scss" scoped>
.financial_expenses_bar {
  height: 290px;
  width: 100%;
  background: #fff;

  .chart {
    width: 100%;
    height: 100%;
  }
}
</style>

深拷贝对象

const deepCopy = (newobj, oldobj) => {
	for (let k in oldobj) {
		let item = oldobj[k];
		if (item instanceof Array) {
			newobj[k] = [];
			deepCopy(newobj[k], item);
		} else if (item instanceof Object) {
			newobj[k] = {};
			deepCopy(newobj[k], item);
		} else {
			newobj[k] = item;
		}
	}
}

传入的数据

  • barChartData
 [
    {
      "name": "T",
      "value": "-1.15%"
    },
    {
      "name": "新",
      "value": "1.98%"
    }
  ]
  • 。。。
以下是在Vue中使用echarts饼形图的步骤: 1.首先,在Vue项目中安装echarts: ```shell npm install echarts --save ``` 2.在需要使用饼形图的组件中引入echarts: ```javascript import echarts from 'echarts' ``` 3.在组件中定义一个方法来初始化echarts表: ```javascript methods: { initChart() { let myChart = echarts.init(this.$refs.chart) myChart.setOption({ // 饼形图的配置项 // ... }) } } ``` 4.在组件的mounted钩子函数中调用initChart方法: ```javascript mounted() { this.initChart() } ``` 5.在组件的模板中添加一个div元素来渲染echarts表: ```html <template> <div ref="chart" style="width: 100%; height: 400px;"></div> </template> ``` 6.根据需要配置饼形图的数据和样式,例如: ```javascript myChart.setOption({ title: { text: '饼形图示例', left: 'center' }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 10, data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'] }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ {value: 335, name: '直接访问'}, {value: 310, name: '邮件营销'}, {value: 234, name: '联盟广告'}, {value: 135, name: '视频广告'}, {value: 1548, name: '搜索引擎'} ] } ] }) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值