echarts 进行二次封装基于VUE(2.0)

1.首先安装 npm install echarts --save

2. 在main.js引入

// 引入echarts
import echarts from 'echarts'
// 一般都要加个$加到vue的原型链上,方便引用
Vue.prototype.$echarts = echarts

3. 封装代码在scr/components中新建ECharts文件夹与ECharts.vue文件

4. ECharts.vue 封装文件代码如下

<template>
  <div ref="echart"></div>
  <!-- 使用方法=>需要引入当前组件 -->
</template>

<script>
import echarts from "echarts";

export default {
  props: {
    isAxisChart: {
      //注意饼图和y轴排放 需要传递 :isAxisChart="饼图是2  柱状y轴排放是1"
      type: Number,
      default: 0,
    },
    chartData: {
      //折线、柱状图
      type: Object,
      default() {
        return {
          xData: [],
          series: [],
        };
      },
    },
  },
  watch: {
    chartData: {
      handler: function () {
        this.initChart();
      },
      deep: true,
    },
  },
  methods: {
    initChart() {
      this.initChartData();
      if (this.echart) {  //判断渲染
        this.echart.setOption(this.options);
      } else {
        this.echart = echarts.init(this.$refs.echart);
        this.echart.setOption(this.options);
      }
    },
    initChartData() {
      if (this.isAxisChart == 0) {
        //拆线图或者柱状图
        this.axisOption.xAxis.data = this.chartData.xData;
        this.axisOption.series = this.chartData.series;
      } else if (this.isAxisChart == 1) {
        // y轴排放 拆线图或者柱状图
        this.yaxisOption.yAxis.data = this.chartData.xData;
        this.yaxisOption.series = this.chartData.series;
      } else {
        //饼图
        // console.log(555, this.chartData.series)
        this.normalOption.series = this.chartData.series;
      }
    },
  },
  data() {
    return {
      //折线、柱状图
      axisOption: {
        legend: {
          data: ["", ""],
        },
        grid: {
          x: 50, //控制x轴文字与底部的距离
          y2: 100, // y2可以控制倾斜的文字狱最右边的距离,放置倾斜的文字超过显示区域
        },
        // 提示框
        tooltip: {
          trigger: "axis",
        },
        toolbox: {
          show: true,
          feature: {
            // dataView: { show: true, readOnly: false }, 文本形式
            magicType: { show: true, type: ["line", "bar"] }, //切换形态
            restore: { show: true }, //还原形态
            saveAsImage: { show: true }, //下载功能
          },
        },
        calculable: true,
        xAxis: {
          type: "category", // x类目轴
          data: [],
            axisLine: {
              lineStyle: {
                color: "#333333", //x类目轴颜色
              },
            },
          axisLabel: {
            interval: 0, //强制文字产生间隔
            rotate: 45, //文字逆时针旋转45°
            textStyle: {
              //文字样式
              fontSize: 13,
              fontFamily: "Microsoft YaHei",
            },
          },
        },
        yAxis: [
          {
            type: "value",
            axisLine: {
              lineStyle: {
                color: "#333333", //y类目轴颜色
              },
            },
          },
        ],
        // color: [
        //   "#2ec7c9",
        //   "#b6a2de",
        //   "#5ab1ef",
        //   "#ffb980",
        //   "#d87a80",
        //   "#8d98b3",
        // ],
        series: [],
      },

      //y轴排放折线、柱状图
      yaxisOption: {
        title: {
          text: "World Population",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {},
        grid: { // 控制图的大小,调整下面这些值就可以,
	        	x: 120,//控制x轴文字与底部的距离
            // y2: 100 // y2可以控制倾斜的文字狱最右边的距离,放置倾斜的文字超过显示区域
	    	},
        xAxis: {
          type: "value",
          boundaryGap: [0, 0.01],
        },
        yAxis: {
          type: "category",
          inverse: true, //设置倒序排序
          axisLabel:{
               textStyle: {    //文字样式
					        fontSize: 14,
					        fontFamily: 'Microsoft YaHei'
					    }
          },
          data: [],
        },
        series: [],
      },

      //饼图
      normalOption: {
        {
          top: 'bottom'
        },
        tooltip: {
          trigger: "item",
        },
        color: [
          "#0f78f4",
          "#dd536b",
          "#9462e5",
          "#a6a6a6",
          "#e1bb22",
          "#39c362",
          "#3ed1cf",
        ],
        series: [],
      },
      // 给个初始值null不然会报错
      echart: null,
    };
  },
  computed: {
    options() {
      if (this.isAxisChart == 0) {
        return this.axisOption;
      } else if (this.isAxisChart == 1) {
        return this.yaxisOption;
      } else {
        return this.normalOption;
      }
    },
  },
};
</script>


5.使用流程

<template>
  <div style="background:white">
        
                <div style="display:flex">
                   <div>
                       <echart
                        style="height: 300px; width:500px"
                        :chartData="echartData.user1"
                      />
                   </div>
                   <div>
                      <echart
                        style="height: 300px; width:500px"
                        :chartData="echartData.order"
                      />
                   </div>
                </div>
                

                <div style="display:flex">
                   <div>
                      <echart
                        style="height: 500px; width:500px"
                        :chartData="echartData.video"
                        :isAxisChart="2"
                      />
                   </div>
                   <div>
                      <echart
                        style="height: 500px; width:600px"
                        :chartData="echartData.user"
                        :isAxisChart="1"
                      />
                   </div>
                </div>
               
        

  </div>
</template>
<script>
import {
  selectbatchnumfordeptyc,
  selectbatchnumfordeptqs,
} from "@/api/statistics";
import { selectAll } from "@/api/settings";
import Echart from "../../../components/ECharts/ECharts.vue";
export default {
  components: { Echart },
  data() {
    return {
      echartData: {
        order: { //代表折线
          xData: [],
          series: [],
        },
        user: { //user代表柱状
          xData: [],
          series: [],
        },
        user1: { //user代表柱状
          xData: [],
          series: [],
        },
        video: {  //代表饼图
          series: [],
        },
      },
    };
  },
  created() {},
  mounted() {
     this.initData()
  },
  methods: {
     initData(){
        //柱状
        this.echartData.user1.xData =  ["1","2","3","4","5"]
        this.echartData.user1.series =  [
          {
            name: "外资",
            barWidth: 25,//柱状大小
            color: "#ee6666",
            type: "bar",
            data: [10,20,30,40,50],
            itemStyle:{
							normal: {
								label: {
									show: true, //开启显示
									position: 'top', //在上方显示
									textStyle: { //数值样式
										color: '#ee6666',
										fontSize: 14,
									}
								}
							}
						},
          },
        ]


        //拆线
        this.echartData.order.xData =  ["1","2","3","4","5"]
        this.echartData.order.series =  [
          {
            name: "外资",
            barWidth: 25,//柱状大小
            color: "#ee6666",
            type: "line",
            data: [10,20,5,10,35],
            itemStyle:{
							normal: {
								label: {
									show: true, //开启显示
									position: 'top', //在上方显示
									textStyle: { //数值样式
										color: '#ee6666',
										fontSize: 14,
									}
								}
							}
						},
          },
        ]

        //饼图
        this.echartData.video.series =  [
             {
               name: 'Nightingale Chart',
               type: 'pie',
               radius: [50, 150],
               center: ['50%', '50%'],
               roseType: 'area',
               itemStyle: {
                 borderRadius: 8
               },
               data: [
                 { value: 40, name: 'rose 1' },
                 { value: 38, name: 'rose 2' },
                 { value: 32, name: 'rose 3' },
                 { value: 30, name: 'rose 4' },
                 { value: 28, name: 'rose 5' },
                 { value: 26, name: 'rose 6' },
               ]
             }
       ]
       //柱状y轴
        this.echartData.user.xData =  ["1","2","3","4","5"]
        this.echartData.user.series =  [
          {
            name: "外资",
            barWidth: 25,//柱状大小
            color: "#ee6666",
            type: "bar",
            data: [10,20,30,40,50],
            itemStyle:{
							normal: {
								label: {
									show: true, //开启显示
									position: 'top', //在上方显示
									textStyle: { //数值样式
										color: '#ee6666',
										fontSize: 14,
									}
								}
							}
						},
          },
           {
            name: "内资",
            barWidth: 25,//柱状大小
            color: "#ddac1d",
            type: "bar",
            data: [10,20,30,40,50],
            itemStyle:{
							normal: {
								label: {
									show: true, //开启显示
									position: 'top', //在上方显示
									textStyle: { //数值样式
										color: '#ee6666',
										fontSize: 14,
									}
								}
							}
						},
          },
        ]
     }
  },
};
</script>
<style>

</style>

 效果图如下:

//仅供参考,如有雷同,纯属虚构

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很高兴能解答您的问题。关于您提到的问题,我可以向您提供一些基础信息和建议,但是需要提醒的是,我并不是一个完全精通于技术领域的AI,因此我的回答仅供参考。 针对您的问题,我们首先需要了解什么是vue2.0和axios。Vue.js是一个前端框架,而axios则是一个用于发送http请求的JavaScript库。在Vue.js中使用axios可以方便地向服务器发送请求,获取数据等。 对于二次封装axios,常见的做法是在Vue.js中创建一个Api.js文件,将需要用到的接口全部封装成方法,以便在Vue组件中调用。这些方法使用axios发送请求,并返回Promise对象。 下面是一个简单的使用axios发送GET请求的代码示例: ``` import axios from 'axios' const baseUrl = 'https://api.example.com/' export function getUser(userId) { return axios.get(`${baseUrl}/users/${userId}`) } ``` 使用时,可以在Vue组件中调用`getUser`方法,并使用Promise的语法进行后续处理: ``` import { getUser } from './api.js' getUser(123) .then(response => console.log(response.data)) .catch(error => console.error(error)) ``` 值得注意的是,由于axios发送的http请求是异步的,因此在处理返回结果时需要使用Promise的语法。同时,在进行二次封装时,应注意不要将敏感信息暴露到前端代码中,以保护用户隐私安全。 希望我的回答能够对您有所帮助,如有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太空游走的鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值