echarts 树形图 基于vue

效果如图
在这里插入图片描述
父组件

<template>
  <div>
    <tree-chart></tree-chart>
  </div>
</template>
<script>
import treeChart from "@/views/my_components/charts/treeChart";
export default {
  components: { treeChart },
  data() {
    return {
    };
  },
  created() {
  },
  methods: {
  },
};
</script>

子组件

<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
// import resize from './mixins/resize'

const animationDuration = 6000;

export default {
  //   mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "500px",
    },
    // chartData: {
    //   type: Object,
    //   required: true
    // }
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    setTimeout(() => {
      this.initChart();
    }, 400);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({
      seriesData,
      chartTitleText,
      chartTitleColor,
      chartTitleFontSize,
    } = {}) {
      this.chart.setOption({
        // title: {
        //   text: "手机品牌",
        //   subtext: "子标题",
        //   textStyle: {
        //     color: chartTitleColor || "#333", // 文字颜色
        //     fontSize: chartTitleFontSize || 14, // 文字大小
        //   },
        // },
        tooltip: {
          trigger: "item",
          triggerOn: "mousemove",
        },
        series: [
          {
            type: "tree",
            orient: "vertical",
            top: "10%",
            left: "8%",
            bottom: "22%",
            right: "20%",
            edgeShape: "polyline", //折线
            lineStyle: {
              width: 1,
              // color: "#000",
              // type: "solid", // 'curve'|'broken'|'solid'|'dotted'|'dashed'
            },
            label: {
              //每个节点的label
              backgroundColor: "#F0F2F5",
              position: "left",
              verticalAlign: "middle",
              align: "right",
            },
            data: [
              {
                name: "手机",
                value: 6,
                symbolSize: [70, 70],//图片大小
                symbol:'image://' + require('@/assets/logo/logo.png'),// 使用本地图片
                  // "image://https://iconfont.alicdn.com/t/1d7c8230-c6f9-4698-8224-3a575fe1fb43.png",
                // itemStyle: {
                //     normal: {
                //         label: {
                //             show: false
                //         }
                //     }
                // },
                children: [
                  {
                    name: "小米",
                    value: 4,
                    symbol:
                      "image://https://iconfont.alicdn.com/t/1d7c8230-c6f9-4698-8224-3a575fe1fb43.png",
                    symbolSize: [60, 60],
                    children: [
                      {
                        name: "小米1",
                        symbol: "circle",
                        symbolSize: 20,
                        value: 4,
                        itemStyle: {
                          normal: {
                            color: "#fa6900",
                            label: {
                              show: true,
                              position: "right",
                            },
                          },
                          emphasis: {
                            label: {
                              show: false,
                            },
                            borderWidth: 0,
                          },
                        },
                      },
                      {
                        name: "小米2",
                        value: 4,
                        symbol: "circle",
                        symbolSize: 20,
                        itemStyle: {
                          normal: {
                            label: {
                              show: true,
                              position: "right",
                              formatter: "{b}",
                            },
                            color: "#fa6900",
                            borderWidth: 2,
                            borderColor: "#cc66ff",
                          },
                          emphasis: {
                            borderWidth: 0,
                          },
                        },
                      },
                      {
                        name: "小米3",
                        value: 2,
                        symbol: "circle",
                        symbolSize: 20,
                        itemStyle: {
                          normal: {
                            label: {
                              position: "right",
                            },
                            color: "#fa6900",
                            brushType: "stroke",
                            borderWidth: 1,
                            borderColor: "#999966",
                          },
                          emphasis: {
                            borderWidth: 0,
                          },
                        },
                      },
                    ],
                  },
                  {
                    name: "苹果",
                    symbol:
                      "image://http://www.viastreaming.com/images/apple_logo2.png",
                    symbolSize: [60, 60],
                    value: 4,
                  },
                  {
                    name: "华为",
                    symbol:
                      "image://https://iconfont.alicdn.com/t/1d7c8230-c6f9-4698-8224-3a575fe1fb43.png",
                    symbolSize: [60, 60],
                    value: 2,
                  },
                  {
                    name: "联想",
                    symbol:
                      "image://https://iconfont.alicdn.com/t/1d7c8230-c6f9-4698-8224-3a575fe1fb43.png",
                    symbolSize: [60, 60],
                    value: 2,
                  },
                ],
              },
            ],
          },
        ],
      });
    },
  },
};
</script>

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在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、付费专栏及课程。

余额充值