Echarts系列——饼图

本文档展示了如何使用ECharts库创建一个基础的折线图,并详细解释了组件的初始化、数据加载、点击事件处理以及数据为空时的处理方法。通过`mounted`钩子初始化图表,`getData`方法负责从URL获取数据,`noDataMethod`显示无数据提示,`draw`方法用于设置图表配置并绘制。同时,定义了`goDetail`方法,用于根据点击的数据跳转到详细页面。
摘要由CSDN通过智能技术生成
<template>
  <div id="chartLine" class="chart"></div>
</template>

<script>
/*
基础折线图,需要dataset:source、dimensions
*/
export default {
  props: {
    config: {
      type: Object,
      default() {
        return {
          title: "标题",
          xField: "",
          yField: "",
          seriesField: "",
        };
      },
    },
  },
  data() {
    return {
      chartObj: null,
      dataset: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.chartObj = this.echarts.init(this.$el, "macarons");
      this.chartObj.on("click", "series.pie", (params) => {
        this.goDetail(params.data);
      });
    },
    goDetail(query) {
      console.log(query);
      this.$router.push({
        name: "product-detail",
        query: query,
      });
    },
    loadingMethod() {
      this.chartObj.showLoading("default", {
        text: "loading",
        color: "#2d8cf0",
        textColor: "#000",
        maskColor: "rgba(255, 255, 255, 0.8)",
        zlevel: 0,
      });
    },
    noDataMethod() {
      this.chartObj.clear();
      this.chartObj.showLoading("default", {
        text: "暂无数据",
        color: "#ffffff",
        textColor: "#8a8e91",
        maskColor: "rgba(255, 255, 255, 0.8)",
      });
    },
    getData(url, params) {
      this.loadingMethod();
      this.postJson(url, params).then((data) => {
        if (data.result) {
          this.chartObj.hideLoading();
          this.dataset = data.result;
          this.draw();
        } else {
          this.noDataMethod();
        }
      });
    },
    draw() {
      let option = {
        title: {
          text: this.config.title,
          textStyle: {
            fontSize: 16,
            fontWeight: "500",
            color: "#000000",
          },
        },
        tooltip: {
          trigger: "item",
        },
        dataset: {
          source: this.dataset,
        },
        legend: {
          orient: "vertical",
          left: "left",
          top: 30,
        },
        series: [
          {
            type: "pie",
            emphasis: { focus: "data" },
            radius: ["30%", "50%"],
            labelLine: {
              show: false,
            },
            label: {
              position: "center",
              formatter: `{@[0]}\n\r{@[2]}`,
            },
            encode: {
              itemName: 0,
              value: 1,
              tooltip: [0, 1],
            },
          },
        ],
      };
      this.chartObj.setOption(option);
    },
  },
};
</script>

<style lang='less' scope>
.chart {
  width: 100%;
  height: 400px;
}
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值