| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <template>
- <echartsBase :option="setOption"/>
- </template>
- <script setup>
- import echartsBase from './index.vue'
- import { nextTick, onMounted, ref, watch } from 'vue'
- const props = defineProps({
- title: {
- type: String,
- default: '管理费已支出与剩余预算曲线图'
- },
- color: {
- type: Array,
- default: () => (['#4095E5', '#C25750'])
- },
- unit: {
- type: String,
- default: '元'
- },
- datas: {
- type: Array,
- default: () => ([])
- }
- })
- //初始变量
- const setOption = ref({})
- const titles = ref(props.title)
- const colors = ref(props.color)
- const units = ref(props.unit)
- const datas = ref(props.datas)
- //创建月份
- const monthData = ref([])
- for (let i = 1; i < 13; i++) {
- monthData.value = [...monthData.value, i + '月']
- }
- watch(() => [
- props.title,
- props.color,
- props.unit,
- ], ([title, color, unit]) => {
- titles.value = title
- colors.value = color
- units.value = unit
- setOptions()
- })
- //深度监听数据变化
- watch(() => [
- props.datas,
- ], ([data]) => {
- datas.value = data
- setDataFormat()
- }, { deep: true })
- //处理数据
- const setDataFormat = () => {
- const data = datas.value
- let legend = [], series = [];
- for (let i = 0; i < data.length; i++) {
- if (data[i].name) {
- legend.push(data[i].name)
- series.push({
- name: data[i].name,
- type: 'line',
- stack: 'Total',
- areaStyle: {},
- data: data[i].value
- })
- }
- }
- setOptions(legend, series)
- }
- //设置图表
- const setOptions = (legend = [], series = []) => {
- setOption.value = {
- color: colors.value,
- title: {
- text: titles.value,
- left: 'center',
- bottom: '0',
- textStyle: {
- color: '#B3B3B3',
- fontSize: 14,
- }
- },
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'cross',
- }
- },
- legend: {
- data: legend
- },
- grid: {
- top: '40px',
- left: '0',
- right: '10px',
- bottom: titles.value ? '40px' : '0',
- containLabel: true
- },
- xAxis: [
- {
- type: 'category',
- boundaryGap: false,
- data: monthData.value
- }
- ],
- yAxis: [
- {
- type: 'value',
- axisLabel: {
- formatter: '{value}' + units.value
- },
- }
- ],
- series: series
- }
- }
- //渲染完成
- onMounted(() => {
- nextTick(() => {
- setDataFormat()
- })
- })
- </script>
|