您的位置:首页 > 娱乐 > 明星 > Vue3使用VueEcharts实现图表

Vue3使用VueEcharts实现图表

2024/12/23 10:04:24 来源:https://blog.csdn.net/weixin_72439307/article/details/141391065  浏览:    关键词:Vue3使用VueEcharts实现图表

这里以折线图为例,其他以此类推
编写折线图子组件
 

<script lang="ts" setup>
import { useDark } from '@vueuse/core'
import moment from 'moment'
import { computed } from 'vue'
import VueEcharts from 'vue-echarts'const props = defineProps<{data: {t: Datedata: number}[]
}>()const isDark = useDark()const chartTheme = computed(() => {return isDark.value ? 'dark' : 'light'
})const chartOption = computed(() => {
//处理数据获取x轴数据const dateList = props.data.map(function (item) {return moment(item.t).format('YYYY-MM-DD')})
//处理数据获取图表数据const valueList = props.data.map(function (item) {return item.data})return {visualMap: [{show: false,type: 'continuous',seriesIndex: 0,min: 0,max: 400}],
//信息窗体tooltip: {trigger: 'axis'},xAxis: [{data: dateList}],yAxis: [{}],
//图表配置series: [{type: 'line',showSymbol: false,data: valueList}]}
})
//notMerge: true保证配置更新时图表不重复
//theme表示图表的主题
//option图表的配置项
//autoresize图标大小自适应
</script>
<template><VueEcharts:update-options="{notMerge: true}":theme="chartTheme":option="chartOption"autoresize/></template>

父组件引入使用图表

<script lang="ts" setup>
import { onMounted, ref, watch } from 'vue'
import axios from '@/request/interceptors'
import ComplaintCountChart from './ComplaintCountChart.vue'
import { regulatoryBodiesIdStore } from '@/stores/insuiList'
import { ElMessage } from 'element-plus'const chartData = ref({complaintOverviewChartData: {names: [] as string[],links: [] as any[]},complaintSourceTypeChartData: {},complaintAcceptTypeChartData: {},complaintCountChartData: []
})onMounted(() => {loadComplaintOverviewData()
})
//获取数据
function loadComplaintOverviewData() {axios.get(`/api/public-opinion-tracking/complaint-overview/regulator/${regulatoryBodiesIdStore().regulatoryBodiesId}/complaint-overview-data`,{params: {complaintTimeStart: props.timeRange.range[0],complaintTimeEnd: props.timeRange.range[1]}}).then((data: any) => {if (data.errorcode === 0) {chartData.value = data.data} else {ElMessage.error(data.message)}}).catch((e) => {if (e.response?.data?.message) {ElMessage.error(e.response.data.message)}console.error(e)})
}</script>
<template><div ><ComplaintCountChart :data="chartData.complaintCountChartData"></ComplaintCountChart></div>
</template>
<style scoped></style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com