1. 程式人生 > 其它 >vue3.0基礎版封裝

vue3.0基礎版封裝

./echarts/index.vue
<template>
<!-- echart封裝 -->
    <div  :style="{ width, height }" ref="myChart"></div>
</template>

<script lang="ts" scoped>
import {
  defineComponent,
  ref,
  PropType,
  watch
} from 'vue'
import * as echarts from 'echarts'
import { EChartsOption } from 'echarts'
export default defineComponent({
  props: {
    option: {} as PropType<EChartsOption>,
    width: { default: '100%' },
    height: { default: '100%' }
  },
  setup (prop) {
    const myChart = ref<HTMLElement>()
    function initChart () {
      if (prop.option) {
        const chart = echarts.init(myChart.value as HTMLElement)
        chart.setOption(prop.option as EChartsOption)
        window.addEventListener('resize', () => {
          if (chart) {
            chart.resize()
          }
        })
      }
    }
    watch(prop, () => {
      initChart()
    })
    return {
      myChart
    }
  }
})
</script>

./echarts/echartsData
import { EChartsOption } from 'echarts'

export function roadvibrationOption (): EChartsOption {
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow' // 預設為直線,可選為:'line' | 'shadow'
      }
    },
    textStyle: {
      color: '#333'
    },
    color: ['#7BA9FA', '#4690FA'],
    grid: {
      containLabel: true,
      left: '10%',
      top: '20%',
      bottom: '10%',
      right: '10%'
    },
    xAxis: {
      type: 'category',
      data: ['浩星', '妍仔', '哆啦a夢', '李強', '王穎', '老王'],
      axisLine: {
        lineStyle: {
          color: '#333'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        margin: 20, // 刻度標籤與軸線之間的距離。
        textStyle: {
          color: '#000'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: true,
        lineStyle: {
          color: '#B5B5B5'
        }
      },
      splitLine: {
        lineStyle: {
          // 使用深淺的間隔色
          color: ['#B5B5B5'],
          type: 'dashed',
          opacity: 0.5
        }
      },
      axisLabel: {}
    },
    series: [{
      data: [4, 22, 1, 11, 23, 11],
      stack: 'zs',
      type: 'bar',
      barMaxWidth: 'auto',
      barWidth: 60,
      itemStyle: {
        color: {
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          type: 'linear',
          global: false,
          colorStops: [{
            offset: 0,
            color: '#5EA1FF'
          },
          {
            offset: 1,
            color: '#90BEFF'
          }
          ]
        }
      }
    },

    // 下面的立體,控制顏色是color第一個
    {
      data: [1, 1, 1, 1, 1, 1],
      type: 'pictorialBar',
      barMaxWidth: '20',
      symbol: 'diamond',
      symbolOffset: [0, '50%'],
      symbolSize: [60, 15],
      zlevel: 2
    },
    // 上面的立體,控制顏色是color第二個
    {
      data: [4, 22, 1, 11, 23, 11],
      type: 'pictorialBar',
      barMaxWidth: '20',
      symbolPosition: 'end',
      symbol: 'diamond',
      symbolOffset: [0, '-50%'],
      symbolSize: [60, 12],
      zlevel: 2
    }
    ]
  } as EChartsOption
}

使用
<template>
<div class="wrap">
  <echarts :option="displayData"></echarts>
</div>
</template>

<script lang="ts">
import echarts from './echarts/index.vue'
import i18n, { t } from '../locale/index'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { EChartsOption } from 'echarts'
import { roadvibrationOption } from './echarts/echartsData'
export default defineComponent({
  components: {
    echarts
  },
  setup () {
    const displayData = ref<EChartsOption>()
    
    onMounted(async () => {
      displayData.value = roadvibrationOption()
    
    })
    return { displayData }
  }
})
</script>

<style scoped lang="less">
.wrap{
  width: 160px;
  height: 100px;
}
</style>