Vue.js组件(6):echarts组件

1 前言

本章主要对常用的echars图表展示进行基本的组件封装。使用该组件前需要在项目中引入echarts。官网:Apache ECharts

复制代码
npm install echarts --save

2 图表组件

2.1 折线图组件

组件属性:chartId,指定图表挂载div的id,注意不要出现重复。当获取到数据后,在父组件中调用该组件的initChart() 方法,初始化图表。方法参数格式范例:

javascript 复制代码
{
    "xAxis":['项1', '项2', '项3'], 
    "yAxis":[12, 11, 122]    
}
  • xAxis:横坐标名称
  • yAxis:每项对应的数据量

组件代码:

javascript 复制代码
<script setup lang="ts">
import * as echarts from 'echarts'

const props = defineProps<{
  chartId: string
}>()

const initChart = (data: any) => {
  const chartDom = document.getElementById(props.chartId);
  var myChart = echarts.init(chartDom);
  var option;

  option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    xAxis: {
      type: 'category',
      data: data.xAxis,
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: data.yAxis,
        type: 'line'
      }
    ]
  };

  option && myChart.setOption(option);
}

defineExpose({ initChart })
</script>

<template>
  <div :id="chartId" style="width: 100%; height: 100%"></div>
</template>

<style scoped>

</style>

2.2 柱状图组件

组件属性:chartId,指定图表挂载div的id,注意不要出现重复。当获取到数据后,在父组件中调用该组件的initChart() 方法,初始化图表。方法参数格式范例:

javascript 复制代码
{
    "xAxis":['项1', '项2', '项3'], 
    "yAxis":[12, 11, 122]    
}
  • xAxis:横坐标名称
  • yAxis:每项对应的数据量

组件代码:

javascript 复制代码
<script setup lang="ts">
import * as echarts from 'echarts'

const props = defineProps<{
  chartId: string
}>()

const initChart = (data: any) => {
  const chartDom = document.getElementById(props.chartId);
  var myChart = echarts.init(chartDom);
  var option;

  option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    xAxis: {
      type: 'category',
      data: data.xAxis,
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: data.yAxis,
        type: 'bar'
      }
    ]
  };

  option && myChart.setOption(option);
}

defineExpose({ initChart })
</script>

<template>
  <div :id="chartId" style="width: 100%; height: 100%"></div>
</template>

<style scoped>

</style>

2.3 饼图组件

组件属性:chartId,指定图表挂载div的id,注意不要出现重复。当获取到数据后,在父组件中调用该组件的initChart() 方法,初始化图表。方法参数格式范例:

html 复制代码
[
        { value: 1048, name: '数据项1' },
        { value: 735, name: '数据项2' },
        { value: 580, name: '数据项3' },
        { value: 484, name: '数据项4' },
        { value: 300, name: '数据项5' }
      ]

组件代码:

html 复制代码
<script setup lang="ts">
import * as echarts from 'echarts'

const props = defineProps<{
  chartId: string
}>()

const initChart = (data: any) => {
  type EChartsOption = echarts.EChartsOption;

  const chartDom = document.getElementById(props.chartId)!
  const myChart = echarts.init(chartDom)
  let option: EChartsOption

  option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center'
    },
    series: [
      {
        name: '数据量',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: data
      }
    ]
  }
  option && myChart.setOption(option)
}

defineExpose({ initChart })
</script>

<template>
  <div :id="chartId" style="width: 100%; height: 100%"></div>
</template>

<style scoped>

</style>
相关推荐
NaZiMeKiY1 小时前
HTML5前端第二章节
前端·html·html5
天若有情6731 小时前
深入浅出:HTML 中 <a> 标签嵌入链接教程
前端·html
烂蜻蜓1 小时前
HTML 样式之 CSS 全面解析
前端·css·html
冬冬小圆帽1 小时前
Webpack 优化深度解析:从构建性能到输出优化的全面指南
前端·webpack·node.js
大龄大专大前端3 小时前
JavaScript闭包的认识/应用/原理
前端·javascript·ecmascript 6
字节源流3 小时前
【SpringMVC】常用注解:@SessionAttributes
java·服务器·前端
Json____3 小时前
SpringBoot 和vue前后端配合开发网页拼图10关游戏源码技术分享
vue.js·spring boot·游戏·html·游戏机·拼图游戏·拼图小游戏
肥肠可耐的西西公主3 小时前
前端(vue)学习笔记(CLASS 4):组件组成部分与通信
前端·vue.js·学习
烛阴3 小时前
JavaScript 函数绑定:从入门到精通,解锁你的代码超能力!
前端·javascript
花椒和蕊3 小时前
【vue+excel】导出excel(目前是可以导出两个sheet)
javascript·vue.js·excel