Echarts 组件封装的 3种模板

一、安装echarts

复制代码
npm install echarts --save

二、在需要的页面引入

复制代码
import * as echarts from "echarts"

三、创建组件

1、模板1:vue2+javascript

复制代码
<template>
    <div>
        <div class="echart_size" :id="id"></div>
    </div>
</template>
<script>
import * as echarts from 'echarts'
export default {
    props: {
        // 接收的参数
        id: {
            type: String,
            default: ''
        },
        datas: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            // 变量
        }
    },
    created() {
        this.$nextTick(() => {
            this.barBtn()
        })
    },
    methods: {
        barBtn() {
            let myChart = echarts.init(document.getElementById(this.id))
            let option = {
                // 某图表
            }
            myChart.setOption(option)
            // 让图表跟随屏幕自动的去适应
            window.addEventListener('resize', function () {
                myChart.resize()
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.echart_size{
    width: 500px;
    height: 500px;
}
</style>

2、模板2:vue3+javascript

复制代码
<template>
    <div class="echart_size" :id="props.id"></div>
</template>

<script setup>
import { reactive, ref, nextTick, onMounted } from 'vue'
import * as echarts from 'echarts'
const props = defineProps({
    id: {
        type: String,
        required: true
    },
    datas:{
        type: Array,
        required: true
    }
})

let person=reactive({
    // 初始变量
})

onMounted(()=>{
    GetEchar()
})

const GetEchar = () => {
    const myCharts = ref()
    nextTick(() => {
        const chartDom = document.getElementById(props.id)
        myCharts.value = echarts.init(chartDom)
        let option = {
           // 某图表
        };
        myCharts.value.setOption(option)
        // 让图表跟随屏幕自动的去适应
        window.addEventListener('resize', function () {
            myCharts.value.resize()
        })
    })
}

</script>

<style lang="scss" scoped>
.echart_size {
    width: 100%;
    height: 100%;
}
</style>

3、模板3:vue3+typescript

复制代码
<template>
    <div class="echart_size" :id="props.id"></div>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick, onMounted } from 'vue'
import * as echarts from 'echarts'
let person: any = reactive({
    // 初始变量
})
type Props = {
    id: string
}
const props = withDefaults(defineProps<Props>(), {})

onMounted(()=>{
    GetEchar()
})
const GetEchar = () => {
    const myChart = ref<HTMLElement>()
    const myCharts = ref<any>()
    nextTick(() => {
        const chartDom = document.getElementById(props.id)!
        myCharts.value = echarts.init(chartDom)
        let option = {
           
        };
        myCharts.value.setOption(option)
        // 让图表跟随屏幕自动的去适应
        window.addEventListener('resize', function () {
            myCharts.value.resize()
        })
    })
}

</script>

<style lang="scss" scoped>
.echart_size {
    width: 500px;
    height: 500px;
}
</style>

四、页面调用

1、vue2

复制代码
<template>
  <div>
    <EchartModule :id="'myEchart'" :datas="data" />
  </div>
</template>

<script>
  import EchartModule from '@/components/echartModule'
  export default {
    components: {EchartModule},
    data(){
      return{
        data: [
            { value: 0, label: '测试1' },
            { value: 1, label: '测试2' }
        ]
      }
    }
  }
</script>

2、vue3+js

复制代码
<template>
   <EchartModule :id="'myEchart'" :datas="data" />
</template>

<script setup>
import { reactive } from 'vue'
import EchartModule from '@/components/echartModule'
let person=reactive({
  data:[
     { value: 0, label: '测试1' },
     { value: 1, label: '测试2' }
  ]
})
</script>

3、vue3+ts

复制代码
// vue3+ts
<template>
   <EchartModule :id="'myEchart'" :datas="data" />
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import EchartModule from '@/components/echartModule'
let person:any=reactive({
  data:[
     { value: 0, label: '测试1' },
     { value: 1, label: '测试2' }
  ]
})
</script>

希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

相关推荐
EndingCoder19 分钟前
React从基础入门到高级实战:React 高级主题 - React 微前端实践:构建可扩展的大型应用
前端·javascript·react.js·前端框架·状态模式
BigTopOne1 小时前
【ijkplayer】 android 初始化硬解码
前端
1024小神1 小时前
rust或tauri项目执行命令的时候,cmd窗口也会弹出显示解决方法
前端·javascript
橙某人1 小时前
🤝和Ollama贴贴!解锁本地大模型的「私人订制」狂欢🎉
前端·deepseek
贩卖纯净水.1 小时前
Webpack搭建本地服务器
前端·webpack·node.js
brzhang1 小时前
iOS 26 的备忘录,终于他娘的要支持 Markdown 了!
前端·后端·架构
云边散步2 小时前
《校园生活平台从 0 到 1 的搭建》第一篇:创建项目与构建目录结构
前端·javascript·vue.js
前端老爷更车2 小时前
Threejs 渲染阴影流程
前端
鸭蛋超人不会飞2 小时前
H5引入微信SDK
前端
陈_杨2 小时前
鸿蒙5开发宝藏案例分享---埋点开发实战指南
前端