vue3.0 安装使用echarts

1、安装 , 控制台输入, 默认echarts 5版本

npm i echarts -S     或
npm install echarts --save

2、项目中使用echarts

        引入:
             import * as echarts from 'echarts'

        (1)初始化:
                var myChart = echarts.init(document.querySelector('#main'))   //  #main是放置图表的容器,可自定义,设置容器时一定要设置宽高,不设置会导致图表不显示走入误区。

        (2)定义option,根据自己需求定义,可在echarts官方网站
Apache EChartshttps://echarts.apache.org/zh/index.html        上查找自己需要的实例

                var option= {
                      xAxis: {

                                type: 'category',

                                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

                      },

                      yAxis: {

                                type: 'value'

                      },

                      series: [

                                {

                                      data: [150, 200, 210, 218, 135, 147, 245],

                                      type: 'line'

                                    }

                        ]

                 }

        (3)设置数据

                myChart.setOption(option)

        即可看到自己设置的图表

实例:

<template>

    <div id="main" style="width: 500px;height: 500px">

    </div>

</template>

<script>

    import {defineComponent, reactive, onMounted} from 'vue'

    import * as echarts from 'echarts'

    export default defineComponent({

        setup () {

            const state = reactive({

                option: {

                    xAxis: {

                        type: 'category',

                        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

                    },

                    yAxis: {

                        type: 'value'

                    },

                    series: [

                        {

                            data: [150, 230, 224, 218, 135, 147, 260],

                            type: 'line'

                        }

                    ]

                }

            })

            onMounted (() => {

                const myChart = echarts.init(document.querySelector('#main'))

                myChart.setOption(state.option)

            })

            return {

                state

            }

        }

    })

</script>

        

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3.0使用 ECharts,解决图例(legend)事件被外层 div 事件遮挡的问题,可以通过以下步骤实现: 1. 在 Vue3.0使用 ECharts,需要先安装 `echarts` 和 `vue-echarts` 两个依赖: ```bash npm install echarts vue-echarts ``` 2. 在 Vue3.0使用 ECharts,需要在组件中注册 ECharts 组件,并将图表配置项传递给 ECharts 组件: ```vue <template> <div class="chart-container"> <vue-echarts :options="chartOptions" @legend-selectchanged="handleLegendSelect"></vue-echarts> </div> </template> <script> import * as echarts from 'echarts'; import { use } from 'echarts/core'; import { CanvasRenderer } from 'echarts/renderers'; import { LineChart } from 'echarts/charts'; import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'; import VueECharts from 'vue-echarts'; // 注册必要的组件 use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent]); export default { components: { VueECharts, }, data() { return { // ECharts 图表配置项 chartOptions: { // ... }, }; }, methods: { // 监听图例的选择事件 handleLegendSelect(params) { console.log(params); }, }, }; </script> ``` 3. 在组件中,为外层 div 元素添加一个事件监听器,并通过判断事件的 target 是否为图例元素来决定是否处理该事件: ```vue <template> <div class="chart-container" @click="handleContainerClick"> <vue-echarts :options="chartOptions" @legend-selectchanged="handleLegendSelect"></vue-echarts> </div> </template> <script> import * as echarts from 'echarts'; import { use } from 'echarts/core'; import { CanvasRenderer } from 'echarts/renderers'; import { LineChart } from 'echarts/charts'; import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'; import VueECharts from 'vue-echarts'; // 注册必要的组件 use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent]); export default { components: { VueECharts, }, data() { return { // ECharts 图表配置项 chartOptions: { // ... }, }; }, methods: { // 监听图例的选择事件 handleLegendSelect(params) { console.log(params); }, // 监听外层 div 的点击事件 handleContainerClick(event) { if (event.target.tagName === 'SPAN' && event.target.className.indexOf('ec-legend-item') >= 0) { // 如果点击的是图例元素,则处理该事件 event.stopPropagation(); this.$refs.chart.dispatchAction({ type: 'legendToggleSelect', name: event.target.innerText, }); } }, }, }; </script> ``` 在上述代码中,我们为外层 div 元素添加了一个 `@click` 事件监听器,并在事件处理函数中判断事件的 target 是否为图例元素。如果是,则处理该事件,并通过 `$refs.chart` 获取到 ECharts 实例,调用 `dispatchAction` 方法来触发图例的展示或隐藏。同时,我们在处理事件时调用了 `event.stopPropagation` 方法来阻止事件冒泡到外层 div,从而避免了被遮挡的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值