2024-03-07 13:55:32 +08:00

233 lines
5.0 KiB
Vue

<template>
<el-card class="!border-none mb-4" shadow="never">
<el-form class="mb-[-16px]" inline>
<el-form-item label="图表类型">
<el-select v-model="chartType" placeholder="请选择图表类型" class="flex-1" @change="changeChartType">
<el-option v-for="item in chartTypeList" :key="item.value" :label="item.name" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="图标宽度" prop="num">
<el-input v-model="chartWitdth" clearable type="number" @blur="chartResize" />
</el-form-item>
<el-form-item label="图标高度" prop="project">
<el-input v-model="chartHeight" clearable type="number" @blur="chartResize" />
</el-form-item>
</el-form>
</el-card>
<el-card style="min-height: 75vh;">
<div class="tit">项目部门分布</div>
<div id="main" style="margin: 0 auto;" :style="{ width: chartWitdth + 'px', height: chartHeight + 'px' }"></div>
</el-card>
</template>
<script lang="ts" setup name="">
import * as echarts from 'echarts';
import { apistatisticsprojectInitiation } from '@/api/statistics'
import { ref, reactive, onMounted } from "vue"
var chartDom: any;
var option: any;
var myChart: any
const showChart = ref(true)
const chartWitdth = ref(500)
const chartHeight = ref(800)
const chartType = ref(3)
const chartTypeList = reactive([
{
name: '柱状图',
value: 1
},
{
name: '折线图',
value: 2
},
{
name: '饼状图',
value: 3
},
{
name: '漏斗图',
value: 4
},
])
// 柱状图
let option1 = {
tooltip: {
trigger: 'item',
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar',
}
],
};
// 漏斗图
let option4 = {
tooltip: {
trigger: 'item',
},
legend: {
data: ['Show', 'Click', 'Visit', 'Inquiry', 'Order']
},
series: [
{
name: 'Funnel',
type: 'funnel',
sort: 'descending',
gap: 2,
label: {
show: true,
position: 'inside'
},
labelLine: {
length: 10,
lineStyle: {
width: 1,
type: 'solid'
}
},
itemStyle: {
borderColor: '#fff',
borderWidth: 1
},
emphasis: {
label: {
fontSize: 20
}
},
data: [
{ value: 60, name: 'Visit' },
{ value: 40, name: 'Inquiry' },
{ value: 20, name: 'Order' },
{ value: 80, name: 'Click' },
{ value: 100, name: 'Show' }
],
}
]
};
// 折线图
let option2 = {
tooltip: {
trigger: 'item'
},
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',
}
]
};
// 饼图
let option3 = {
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left',
},
series: [
{
name: 'Access From',
type: 'pie',
radius: '50%',
data: [
{ value: 1048, name: 'Search Engine' },
{ value: 735, name: 'Direct' },
{ value: 580, name: 'Email' },
{ value: 484, name: 'Union Ads' },
{ value: 300, name: 'Video Ads' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
},
}
]
};
option = option3
const getData = async () => {
let res = await apistatisticsprojectInitiation()
// // option = option1
// // option = option2
// option = option3
initChart()
}
const initChart = () => {
myChart = echarts.init(chartDom);
myChart.setOption(option, true);
}
const changeChartType = (e: any) => {
switch (e) {
case 1:
option = option1
break;
case 2:
option = option2
break;
case 3:
option = option3
break;
case 4:
option = option4
break;
}
initChart()
}
const chartResize = () => {
myChart.resize()
}
onMounted(() => {
chartDom = document.getElementById('main');
getData()
})
</script>
<style>
.tit {
margin: 10px 0;
text-align: center;
font-size: 20px;
}
</style>