yyb
8 小时以前 eae12ffeaa667f271a2c548b5e16557cac3686e3
能耗成本核算图表相关优化
已修改1个文件
100 ■■■■■ 文件已修改
src/views/costAccounting/energyCosts/index.vue 100 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/costAccounting/energyCosts/index.vue
@@ -474,14 +474,14 @@
                        <button
                          class="chart-tool"
                          type="button"
                          @click="downloadChart('consumption', '能耗用量对比')"
                          @click="downloadChart('unit', '能耗用量对比')"
                        >
                          下载
                        </button>
                        <button
                          class="chart-tool"
                          type="button"
                          @click="openBigChart('consumption', '能耗用量对比')"
                          @click="openBigChart('unit', '能耗用量对比')"
                        >
                          大图
                        </button>
@@ -489,12 +489,12 @@
                    </div>
                  </template>
                  <div
                    ref="consumptionChartWrap"
                    ref="unitChartWrap"
                    class="chart-wrap"
                    v-loading="tableLoading"
                  >
                    <div
                      ref="consumptionChart"
                      ref="unitChart"
                      class="chart-content"
                      v-show="hasTableData"
                    ></div>
@@ -605,10 +605,10 @@
        </el-table-column>
        <el-table-column prop="dosage" label="用量" align="right">
          <template #default="scope">
            <span class="consumption-value">{{
            <span class="unit-value">{{
              formatNumber(scope.row.dosage, 2)
            }}</span>
            <span class="consumption-unit">{{ scope.row.unit }}</span>
            <span class="unit-unit">{{ scope.row.unit }}</span>
          </template>
        </el-table-column>
        <el-table-column
@@ -875,13 +875,13 @@
  if (key === "cost") return costChartInstance;
  if (key === "type") return typeChartInstance;
  if (key === "purpose") return purposeChartInstance;
  if (key === "consumption") return consumptionChartInstance;
  if (key === "unit") return unitChartInstance;
  return null;
};
const ensurePanelForChart = (key) => {
  if (key === "cost" || key === "type") chartPanel.value = "core";
  if (key === "purpose" || key === "consumption") chartPanel.value = "advanced";
  if (key === "purpose" || key === "unit") chartPanel.value = "advanced";
};
const downloadChart = (key, title) => {
@@ -967,7 +967,7 @@
  const prop = sortState.prop;
  const direction = sortState.order === "ascending" ? 1 : -1;
  const numFields = new Set(["price", "cost", "consumption"]);
  const numFields = new Set(["price", "cost", "unit"]);
  return data.sort((a, b) => {
    const av = a?.[prop];
@@ -1011,12 +1011,12 @@
const costChart = ref(null);
const typeChart = ref(null);
const purposeChart = ref(null);
const consumptionChart = ref(null);
const unitChart = ref(null);
const costChartWrap = ref(null);
const typeChartWrap = ref(null);
const purposeChartWrap = ref(null);
const consumptionChartWrap = ref(null);
const unitChartWrap = ref(null);
const tableAnchor = ref(null);
@@ -1044,28 +1044,32 @@
let costChartInstance = null;
let typeChartInstance = null;
let purposeChartInstance = null;
let consumptionChartInstance = null;
let unitChartInstance = null;
// 图表区切换:core | advanced | none(点击当前选中可收起)
const chartPanel = ref("core");
const ensureChartsReady = (panel) => {
  if (panel === "core") {
    if (costChart.value && !costChartInstance)
    if (costChart.value && !costChartInstance) {
      costChartInstance = echarts.init(costChart.value);
    if (typeChart.value && !typeChartInstance)
      setTimeout(() => costChartInstance?.resize(), 50);
    }
    if (typeChart.value && !typeChartInstance) {
      typeChartInstance = echarts.init(typeChart.value);
    if (costChartInstance) updateCostChart();
    if (typeChartInstance) updateTypeChart();
      setTimeout(() => typeChartInstance?.resize(), 50);
    }
    return;
  }
  if (panel === "advanced") {
    if (purposeChart.value && !purposeChartInstance)
    if (purposeChart.value && !purposeChartInstance) {
      purposeChartInstance = echarts.init(purposeChart.value);
    if (consumptionChart.value && !consumptionChartInstance)
      consumptionChartInstance = echarts.init(consumptionChart.value);
    if (purposeChartInstance) updatePurposeChart();
    if (consumptionChartInstance) updateConsumptionChart();
      setTimeout(() => purposeChartInstance?.resize(), 50);
    }
    if (unitChart.value && !unitChartInstance) {
      unitChartInstance = echarts.init(unitChart.value);
      setTimeout(() => unitChartInstance?.resize(), 50);
    }
  }
};
@@ -1089,6 +1093,16 @@
watch(chartPanel, (val) => {
  if (val !== "none") resizeChartsAfterExpand();
});
// 监听表格数据变化,确保数据加载后图表正确渲染
watch(tableData, () => {
  nextTick(() => {
    updateCharts();
    nextTick(() => {
      handleResize();
    });
  });
}, { deep: true });
// 获取能耗类型标签类型
const getEnergyTypeType = (type) => {
@@ -1199,10 +1213,10 @@
  const typeCosts = {};
  data.forEach((item) => {
    if (!typeCosts[item.energyType]) {
      typeCosts[item.energyType] = 0;
    if (!typeCosts[item.energyTyep]) {
      typeCosts[item.energyTyep] = 0;
    }
    typeCosts[item.energyType] += parseFloat(item.cost);
    typeCosts[item.energyTyep] += parseFloat(item.cost);
  });
  const chartData = Object.entries(typeCosts).map(([name, value]) => ({
@@ -1334,28 +1348,28 @@
// 更新能耗用量对比图
const updateConsumptionChart = () => {
  const data = tableData.value;
  const consumptionData = {};
  const unitData = {};
  data.forEach((item) => {
    if (!consumptionData[item.energyType]) {
      consumptionData[item.energyType] = {
    if (!unitData[item.energyTyep]) {
      unitData[item.energyTyep] = {
        生产: 0,
        办公: 0,
      };
    }
    if (consumptionData[item.energyType].hasOwnProperty(item.type)) {
      consumptionData[item.energyType][item.type] = parseFloat(
        item.consumption
    if (unitData[item.energyTyep].hasOwnProperty(item.type)) {
      unitData[item.energyTyep][item.type] += parseFloat(
        item.dosage || 0
      );
    }
  });
  const energyTypes = Object.keys(consumptionData);
  const energyTypes = Object.keys(unitData);
  const productionConsumptions = energyTypes.map(
    (type) => consumptionData[type].生产
    (type) => unitData[type].生产
  );
  const officeConsumptions = energyTypes.map(
    (type) => consumptionData[type].办公
    (type) => unitData[type].办公
  );
  const option = {
@@ -1424,7 +1438,7 @@
      },
    ],
  };
  consumptionChartInstance.setOption(option);
  unitChartInstance.setOption(option);
};
// 统计维度切换
@@ -1453,6 +1467,7 @@
// 查询
const handleQuery = () => {
  queryPulse.value = true;
  window.setTimeout(() => {
    queryPulse.value = false;
@@ -1551,10 +1566,19 @@
// 更新所有图表
const updateCharts = () => {
  nextTick(() => {
    // 确保 core 面板的图表始终初始化(因为默认显示的是 core)
    ensureChartsReady("core");
    // 同时也初始化当前可见面板的图表
    if (chartPanel.value === "advanced") {
      ensureChartsReady("advanced");
    }
    // 更新所有已初始化的图表
    if (costChartInstance) updateCostChart();
    if (typeChartInstance) updateTypeChart();
    if (purposeChartInstance) updatePurposeChart();
    if (consumptionChartInstance) updateConsumptionChart();
    if (unitChartInstance) updateConsumptionChart();
  });
};
@@ -1606,7 +1630,7 @@
  costChartInstance && costChartInstance.resize();
  typeChartInstance && typeChartInstance.resize();
  purposeChartInstance && purposeChartInstance.resize();
  consumptionChartInstance && consumptionChartInstance.resize();
  unitChartInstance && unitChartInstance.resize();
};
onMounted(() => {
@@ -2613,12 +2637,12 @@
  width: 100%;
}
.consumption-value {
.unit-value {
  font-weight: bold;
  color: var(--lux-primary);
}
.consumption-unit {
.unit-unit {
  font-size: 12px;
  color: var(--lux-muted);
  margin-left: 2px;