vue3 ts 引入echarts

1. 在main.ts中引入

import * as echarts from 'echarts'

 const app = createApp(App);

app.config.globalProperties.$echarts = echarts

2.vue界面代码

<template>
    <main>
        <Back />
        <el-row justify="center" style="background:none;">
            <el-col>
                <h3>echarts测试</h3>
            </el-col>
        </el-row>
        <div id='showEcharts' style='width:400px; height:400px'></div>
    </main>
</template>
   
<script lang="ts">
import { ref, onMounted, defineComponent } from "vue";
import * as echarts from 'echarts';
onMounted(
    () => {
        init()
    }
)
export default defineComponent({
    title: "echarts测试",
    //   name:
    path: '/doctor/echarts',
    data() {
        return {
            name: ['一', '二', '三', '四', '五', '六', '七'],
            val: [150, 230, 224, 218, 135, 147, 260],
        }
    },
    methods: {
        showorders() {
            // 基于准备好的dom,初始化echarts实例
            var chartDom = document.getElementById('showEcharts');
            var myChart = echarts.init(chartDom);
            myChart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                //   # x轴
                xAxis: {
                    type: 'category',
                    data: this.name
                },
                axisTick: {
                    alignWithLabel: true
                },
                //   # y 轴
                yAxis: {
                    type: 'value'
                },
                //   # 类型
                series: [
                    {
                        name: 'Direct',
                        type: 'bar',
                        barWidth: '60%',
                        data: this.val
                    }
                ]

            })
            // 指定图表的配置项和数据
            //   var option;

            //   option = {
            //     //   # x轴
            //       xAxis: {
            //           type: 'category',
            //           data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            //       },
            //     //   # y 轴
            //       yAxis: {
            //           type: 'value'
            //       },
            //     //   # 类型
            //       series: [
            //           {
            //           data:[150, 230, 224, 218, 135, 147, 260],
            //           type: 'bar'
            //           }
            //       ]
            //   };
            //   option && myChart.setOption(option);
        },

    },
    mounted() {
        this.showorders()
    },

})
</script>
   
<style>

</style>

这是个简单的柱状图。 defineComponent可以删除,并不影响功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值