Commit 92a5d294 authored by xinzhedeai's avatar xinzhedeai

ai预警

parent feae79dd
...@@ -46,6 +46,15 @@ export const getEnvironmentData = async (): Promise<any> => await axios.get('/ho ...@@ -46,6 +46,15 @@ export const getEnvironmentData = async (): Promise<any> => await axios.get('/ho
// 获取在线数据接口 // 获取在线数据接口
export const getOnlineData = async (): Promise<any> => await axios.get('/home/online/data'); export const getOnlineData = async (): Promise<any> => await axios.get('/home/online/data');
// 获取车辆日志列表接口
export const getCarLogList = async (): Promise<any> => await axios.get('home/car/log');
// 修改报警状态
export const setAlarmStatusApi = async (command: string): Promise<any> => await axios.put(`/home/sound/${command}`, data);
// 获取报警状态
export const getAlarmStatusApi = async (): Promise<any> => await axios.get('/home/sound/status');
// export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list'); // export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list');
// export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list'); // export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list');
// export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list'); // export const getOnlineAlarmList = async (): Promise<any> => await axios.get('/home/online/alarm/list');
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
></canvas> ></canvas>
<div class="legend-container"> <div class="legend-container">
<div v-for="(item, index) in chartData" :key="index" class="legend-item"> <div v-for="(item, index) in chartData" :key="index" class="legend-item">
<div class="color-block" :style="{ backgroundColor: item.color }"></div> <div class="color-block" :style="{ backgroundColor: item.color || 'fff' }"></div>
<div class="legend-text"> <div class="legend-text">
<span class="legend-name">{{ item.name }}</span> <span class="legend-name">{{ item.name }}</span>
<span class="legend-value" :style="`color: ${item.color}`">{{ item.value }}</span> <span class="legend-value" :style="`color: ${item.color}`">{{ item.value }}</span>
...@@ -23,7 +23,11 @@ ...@@ -23,7 +23,11 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, watch } from 'vue'; import { ref, onMounted, onBeforeUnmount, nextTick, reactive } from 'vue'
import { getCarLogList } from '@/api/index-dp';
import { useMessage } from 'naive-ui';
const message = useMessage();
// 1. 定义 TS 类型接口 // 1. 定义 TS 类型接口
interface ChartDataItem { interface ChartDataItem {
...@@ -38,13 +42,8 @@ interface RingLayer { ...@@ -38,13 +42,8 @@ interface RingLayer {
} }
// 2. 核心配置与数据(5个类别) // 2. 核心配置与数据(5个类别)
const chartData: ChartDataItem[] = [ // 使用 ref 创建响应式数据
{ name: '疲劳驾驶', value: 10, color: '#409EFF' }, const chartData = ref<ChartDataItem[]>([]);
{ name: '违规打电话', value: 15, color: '#52C41A' },
{ name: '违规抽烟', value: 0, color: '#FAAD14' },
{ name: '左顾右盼', value: 2, color: '#FA8C16' },
{ name: '人脸丢失', value: 0, color: '#F5222D' },
];
// 调整环形图层尺寸以适应新的 canvas 尺寸,增大环间距离 // 调整环形图层尺寸以适应新的 canvas 尺寸,增大环间距离
const ringLayers: RingLayer[] = [ const ringLayers: RingLayer[] = [
...@@ -94,7 +93,7 @@ const drawChart = () => { ...@@ -94,7 +93,7 @@ const drawChart = () => {
// 步骤2:按比例绘制数值扇区 // 步骤2:按比例绘制数值扇区
const drawSectors = () => { const drawSectors = () => {
const validData = chartData.filter((item) => item.value > 0); const validData = chartData.value.filter((item) => item.value > 0);
const total = validData.reduce((sum, item) => sum + item.value, 0); const total = validData.reduce((sum, item) => sum + item.value, 0);
if (total === 0) { if (total === 0) {
console.warn('No valid data to draw'); console.warn('No valid data to draw');
...@@ -106,7 +105,7 @@ const drawChart = () => { ...@@ -106,7 +105,7 @@ const drawChart = () => {
// 先绘制背景 // 先绘制背景
drawBackground(); drawBackground();
chartData.forEach((item, index) => { chartData.value.forEach((item, index) => {
if (item.value <= 0) return; if (item.value <= 0) return;
const layer = ringLayers[index]; const layer = ringLayers[index];
if (!layer) return; if (!layer) return;
...@@ -137,22 +136,36 @@ const drawChart = () => { ...@@ -137,22 +136,36 @@ const drawChart = () => {
// 5. 挂载后初始化图表(DOM 渲染完成后执行) // 5. 挂载后初始化图表(DOM 渲染完成后执行)
onMounted(() => { onMounted(() => {
console.log('Component mounted, attempting to draw chart'); getCarLogList().then((response) => {
// 使用 nextTick 确保 DOM 已完全渲染 if (response.data.code == 200) {
setTimeout(() => { // 使用 ref 的 value 属性更新数据,确保是响应式的
drawChart(); chartData.value = response.data.data.map((item: any) => ({
}, 0); name: item.name,
value: item.count * 1,
color: item.color
}));
console.log('*****************************', chartData.value);
console.log('Component mounted, attempting to draw chart');
// 使用 nextTick 确保 DOM 已完全渲染
nextTick(() => {
drawChart();
});
} else {
message.error(response.data.msg);
}
}).catch((error) => {
console.error('获取数据失败:', error);
message.error('获取数据失败');
});
}); });
// 可选:监听数据变化重新绘制(如需动态更新数据可启用) // // 可选:监听数据变化重新绘制(如需动态更新数据可启用)
watch([() => chartData], () => { // import { watch } from 'vue';
drawChart(); // watch(chartData, () => {
}, { deep: true }); // drawChart();
// }, { deep: true });
// 导出绘制函数以便外部调用
defineExpose({
drawChart
});
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
...@@ -160,7 +173,7 @@ defineExpose({ ...@@ -160,7 +173,7 @@ defineExpose({
width: 100%; width: 100%;
overflow: auto; overflow: auto;
background-image: url("@/assets/jinrun/module-bg.png"); background-image: url("@/assets/jinrun/module-bg.png");
background-size: cover; background-size: cover;
background-repeat: no-repeat; background-repeat: no-repeat;
} }
...@@ -189,10 +202,10 @@ background-size: cover; ...@@ -189,10 +202,10 @@ background-size: cover;
align-items: center; align-items: center;
margin-bottom: 15px; margin-bottom: 15px;
width: 2.3rem; width: 2.3rem;
height: 0.28rem; height: 0.28rem;
background: #144f8025; background: #144f8025;
border-radius: 0.14rem; border-radius: 0.14rem;
padding-left: 0.3rem; padding-left: 0.3rem;
} }
.color-block { .color-block {
...@@ -203,25 +216,21 @@ padding-left: 0.3rem; ...@@ -203,25 +216,21 @@ padding-left: 0.3rem;
} }
.legend-text { .legend-text {
display: flex; display: flex;
gap: 0.1rem; gap: 0.1rem;
justify-content: space-between; justify-content: space-between;
width: 1.3rem; width: 1.3rem;
padding-left: .12rem; padding-left: .12rem;
} }
// .legend-text:first-child{
// display: inline-block;
// text-align: left;
// }
.legend-name { .legend-name {
font-size: 12px; font-size: .16rem;
color: #fff; color: #fff;
margin-bottom: 2px; margin-bottom: 2px;
} }
.legend-value { .legend-value {
font-size: 12px; font-size: .16rem;
color: #666; color: #666;
} }
...@@ -243,4 +252,4 @@ padding-left: 0.3rem; ...@@ -243,4 +252,4 @@ padding-left: 0.3rem;
text-shadow: 0rem 0rem 0rem rgba(5, 38, 68, 0.5); text-shadow: 0rem 0rem 0rem rgba(5, 38, 68, 0.5);
} }
} }
</style> </style>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment