echart封装 以及 配合用依赖data v 将echart自定义方法混入全局

694 阅读1分钟

datav.jiaminghi.com/guide/#%E7%…

npm install @jiaminghi/data-view

main js内全局引入

import dataV from '@jiaminghi/data-view'

Vue.use(dataV)
//npm install echarts --save 这个默认的不要
npm install echarts@4.9.0  用这个,因为5.0新版对vue2.9支持有问题

但是不在全局引入 全局引入的话

import echarts from 'echarts' Vue.prototype.$echarts = echarts

关于混入 使用和uniapp一样的混入方式

image.png

mixin js

//import store from '@/common/store'
//引入组件
import Line from '@/components/line'
import Pie from '@/components/pie'
import Bar from '@/components/bar'
import echarts from 'echarts';
export default Vue => {
    console.log(echarts);
	Vue.mixin({
		methods:{
			initChart(
                container,option,onclick
            ){
                const myChart = echarts.init(container);
                myChart.setOption(option);
                if(onclick){
                    myChart.on("click",onclick)
                }
            }
		}
	})
    //以下是全局 注册组件 类似uniapp的easy-com模式
    Vue.component('myline', Line)
    Vue.component('mypie', Pie)
    Vue.component('mybar', Bar)
}

index.JS

import initMixin from "./mixin"
export default {
	install(Vue) {
		initMixin(Vue)
	}
}

在main.JS加入

import initMixin from '@/core'
Vue.use(initMixin)

image.png

现在的目录结构

image.png

举例line组件

<template>
  <div ref="chart" :style="{width,height}" class="container"></div>
</template>

<script>
export default {
  props: {
    width: {
      type: String,
      default: "500px"
    },
    height: {
      type: String,
      default: "300px"
    },
    data: {
      type: Array,
      required: true
    },
    options: {
      required: true
    }
  },
  mounted() {
    let series = this.options.keys.map(r => {
      return {
        name: r.label,
        type: "line",
        data: this.data.map(rr => rr[r.val])
      };
    });

    let option1 = {
      title: {
        text: this.options.title,
        left: "center",
        subtext: this.options.subtext
      },
      legend: {
        bottom: 0
        // formatter: "2020{name}"
      },
      tooltip: {
        formatter: data => {
          return this.options.tooltipFmt(data);
        }
      },
      xAxis: {
        data: this.data.map(r => r[this.options.xAxisKey]),
        axisLabel: {
          rotate: 30,
          fontSize: 10
        }
      },
      yAxis: {
        type: "value"
      },
      series
    };

    this.initChart(this.$refs.chart, option1, data => {
      console.log(data);
    });
  }
};
</script>

<style>
.container {
  width: 500px;
  height: 300px;
}
</style>

在页面调用line组件

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <myline :data="zz" :options="options"/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  },
   data() {
    return {
      zz: [
        {
          date: "2020-01-01",
          shouru: 51000,
          zhichu: 13000,
          zhongjiang: 14000
        },
        {
          date: "2020-01-02",
          shouru: 16000,
          zhichu: 10500,
          zhongjiang: 12000
        },
        {
          date: "2020-01-03",
          shouru: 41000,
          zhichu: 70300,
          zhongjiang: 14400
        },
        {
          date: "2020-01-04",
          shouru: 10500,
          zhichu: 12600,
          zhongjiang: 24000
        },
        {
          date: "2020-01-05",
          shouru: 12200,
          zhichu: 14030,
          zhongjiang: 11000
        }
      ],
      options: {
        title: "我的图表",
        subtext: "2021",
        xAxisKey: "date",
        tooltipFmt(data) {
          return `${data.name}<br>${data.marker} ${data.seriesName}${data.value}元`;
        },
        keys: [
          {
            label: "收入",
            val: "shouru"
          },
          {
            label: "支出",
            val: "zhichu"
          },
          {
            label: "抽奖",
            val: "zhongjiang"
          }
        ]
      }
    };
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

效果

image.png