当前位置: 首页 > news >正文

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

1 前言

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

npm install echarts --save

2 图表组件

2.1 折线图组件

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

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

        组件代码:

<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() 方法,初始化图表。方法参数格式范例:

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

        组件代码:

<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() 方法,初始化图表。方法参数格式范例:

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

        组件代码:

<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: EChartsOptionoption = {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>

http://www.lryc.cn/news/509747.html

相关文章:

  • yolov3算法及其改进
  • Python + 深度学习从 0 到 1(02 / 99)
  • HTML+CSS+JS制作在线书城网站(内附源码,含5个页面)
  • 【FastAPI】中间件
  • 5个实用的设计相关的AI网站
  • STL 六大组件
  • Python选择题训练工具:高效学习、答题回顾与音频朗读一站式体验
  • Python实现机器学习驱动的智能医疗预测模型系统的示例代码框架
  • AWS Certified AI Practitioner 自学考试心得
  • JQ中的each()方法与$.each()函数的使用区别
  • 滚珠丝杆与直线导轨的区别
  • 【Ovis】Ovis1.6的本地部署及推理
  • C语言结构体位定义(位段)的实际作用深入分析
  • 儿童影楼管理系统:基于SSM的创新设计与功能实现
  • 青蛇人工智能学家
  • uniapp+vue 前端防多次点击表单,防误触多次请求方法。
  • 【ES6复习笔记】rest参数(7)
  • Hive SQL 窗口函数 `ROW_NUMBER() ` 案例分析
  • 前端mock数据 —— 使用Apifox mock页面所需数据
  • 车载U盘制作教程:轻松享受个性化音乐
  • springboot 3 websocket react 系统提示,选手实时数据更新监控
  • 现代图形API综合比较:Vulkan DirectX Metal WebGPU
  • 【Hot100刷题计划】Day04 栈专题 1~3天回顾(持续更新)
  • 用VBA将word文档处理成支持弹出式注释的epub文档可用的html内容
  • 舵机原理介绍 简洁讲解面向实战 非阻塞式驱动代码, arduino
  • Oracle Database 23ai 中的DBMS_HCHECK
  • 如何利用AWS监听存储桶并上传到tg bot
  • STM32 SPI读取SD卡
  • TANGO与LabVIEW控制系统集成
  • eth_type_trans 函数