wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<script lang="tsx" setup>
import { useI18n } from '@/hooks/web/useI18n'
import { computed, reactive, toRefs, watch } from 'vue'
import {
  compareYearList,
  compareMonthList,
  compareDayList
} from '@/views/chart/components/editor/drag-item/components/compare'
 
const { t } = useI18n()
 
const props = defineProps({
  compareItem: {
    type: Object,
    required: true
  },
  chart: {
    type: Object,
    required: true
  },
  dimensionData: {
    type: Array,
    required: false
  },
  quotaData: {
    type: Array,
    required: false
  }
})
 
const { compareItem, chart } = toRefs(props)
 
const state = reactive({
  fieldList: [],
  compareList: [],
  dateFormatter: 'y_M_d'
})
 
const dateFormatterList = [
  { name: t('chart.y'), value: 'y' },
  { name: t('chart.y_M'), value: 'y_M' },
  { name: t('chart.y_M_d'), value: 'y_M_d' }
]
 
const changeDateFormatter = () => {
  const checkedField = state.fieldList.filter(ele => ele.id === compareItem.value.compareCalc.field)
  if (checkedField && checkedField.length > 0) {
    checkedField[0].dateStyle = state.dateFormatter
    if (!compareItem.value.compareCalc.custom) {
      compareItem.value.compareCalc.custom = { timeType: 'y_M_d' }
    }
    compareItem.value.compareCalc.custom.timeType = state.dateFormatter
  }
  initCompareType()
}
 
const initDateFormatter = () => {
  const timeType = compareItem.value.compareCalc.custom?.timeType
  if (isIndicator.value && timeType) {
    state.dateFormatter = timeType
    changeDateFormatter()
  }
}
 
const hintStr = computed<string>(() => {
  let dataFormatter = state.dateFormatter
  const checkedField = state.fieldList.filter(ele => ele.id === compareItem.value.compareCalc.field)
  if (checkedField && checkedField.length > 0) {
    dataFormatter = checkedField[0].dateStyle
  }
  switch (dataFormatter) {
    case 'y_M_d':
      switch (compareItem.value.compareCalc.type) {
        case 'day_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_day_pre')
            case 'sub':
              return t('chart.compare_calc_day_sub')
            case 'percent':
              return t('chart.compare_calc_day_percent')
          }
          break
        case 'month_yoy':
        case 'month_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_month_pre')
            case 'sub':
              return t('chart.compare_calc_month_sub')
            case 'percent':
              return t('chart.compare_calc_month_percent')
          }
          break
        case 'year_yoy':
        case 'year_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_year_pre')
            case 'sub':
              return t('chart.compare_calc_year_sub')
            case 'percent':
              return t('chart.compare_calc_year_percent')
          }
          break
      }
      break
    case 'y_M':
      switch (compareItem.value.compareCalc.type) {
        case 'month_yoy':
        case 'month_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_month_pre_m')
            case 'sub':
              return t('chart.compare_calc_month_sub_m')
            case 'percent':
              return t('chart.compare_calc_month_percent_m')
          }
          break
        case 'year_yoy':
        case 'year_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_year_pre_m')
            case 'sub':
              return t('chart.compare_calc_year_sub_m')
            case 'percent':
              return t('chart.compare_calc_year_percent_m')
          }
          break
      }
      break
    case 'y':
      switch (compareItem.value.compareCalc.type) {
        case 'year_yoy':
        case 'year_mom':
          switch (compareItem.value.compareCalc.resultData) {
            case 'pre':
              return t('chart.compare_calc_year_pre_y')
            case 'sub':
              return t('chart.compare_calc_year_sub_y')
            case 'percent':
              return t('chart.compare_calc_year_percent_y')
          }
          break
      }
      break
  }
  return ''
})
 
watch(
  () => props.chart,
  () => {
    initFieldList()
    initCompareType()
    initDateFormatter()
  },
  { deep: true }
)
 
const isIndicator = computed(() => {
  return chart.value.type === 'indicator'
})
 
// 过滤xaxis,extStack所有日期字段
const initFieldList = () => {
  let xAxis = null
  if (Object.prototype.toString.call(chart.value.xAxis) === '[object Array]') {
    xAxis = JSON.parse(JSON.stringify(chart.value.xAxis))
  } else {
    xAxis = JSON.parse(chart.value.xAxis)
  }
  const t1 = xAxis.filter(ele => {
    return ele.deType === 1
  })
 
  if (chart.value.type === 'table-pivot') {
    let xAxisExt = null
    if (Object.prototype.toString.call(chart.value.xAxisExt) === '[object Array]') {
      xAxisExt = JSON.parse(JSON.stringify(chart.value.xAxisExt))
    } else {
      xAxisExt = JSON.parse(chart.value.xAxisExt)
    }
    const t2 = xAxisExt.filter(ele => {
      return ele.deType === 1
    })
 
    t1.push(...t2)
  }
  if (isIndicator.value) {
    t1.length = 0
    t1.push(...props.dimensionData.filter(ele => ele.deType === 1))
    t1.push(...props.quotaData.filter(ele => ele.deType === 1))
  }
 
  state.fieldList = t1
  // 如果没有选中字段,则默认选中第一个
  if (
    (!compareItem.value.compareCalc.field || compareItem.value.compareCalc.field === '') &&
    state.fieldList.length > 0
  ) {
    compareItem.value.compareCalc.field = state.fieldList[0].id
  }
}
// 同环比计算类型
const yoyLabel = ['day_mom', 'month_yoy', 'year_yoy', 'month_mom', 'year_mom']
// 获得不同字段格式对应能计算的同环比列表
const initCompareType = () => {
  const checkedField = state.fieldList.filter(ele => ele.id === compareItem.value.compareCalc.field)
  if (checkedField && checkedField.length > 0) {
    switch (checkedField[0].dateStyle) {
      case 'y':
        state.compareList = compareYearList
        break
      case 'y_M':
        state.compareList = compareMonthList
        break
      case 'y_M_d':
        state.compareList = compareDayList
        break
      default:
        break
    }
  } else {
    state.compareList = []
  }
  // 如果没有选中一个同环比类型,则默认选中第一个
  if (!yoyLabel.includes(compareItem.value.compareCalc.type) && state.compareList.length > 0) {
    compareItem.value.compareCalc.type = state.compareList[0].value
  }
}
 
const fieldFormatter = field => {
  if (isIndicator.value) {
    return field.name
  } else {
    return field.name + '(' + t('chart.' + field.dateStyle) + ')'
  }
}
initFieldList()
initCompareType()
initDateFormatter()
</script>
 
<template>
  <div>
    <el-form ref="form" :model="compareItem.compareCalc" label-width="80px" class="compare-form">
      <el-form-item :label="t('chart.compare_date')">
        <el-select
          v-model="compareItem.compareCalc.field"
          :placeholder="t('chart.pls_select_field')"
          @change="initCompareType"
        >
          <el-option
            v-for="field in state.fieldList"
            :key="field.id"
            :label="fieldFormatter(field)"
            :value="field.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="isIndicator" :label="t('chart.datePattern')">
        <el-select
          v-model="state.dateFormatter"
          :placeholder="t('chart.date_format')"
          @change="changeDateFormatter"
        >
          <el-option
            v-for="field in dateFormatterList"
            :key="field.value"
            :label="field.name"
            :value="field.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('chart.compare_type')">
        <el-radio-group v-model="compareItem.compareCalc.type">
          <el-radio v-for="radio in state.compareList" :key="radio.value" :value="radio.value"
            >{{ t('chart.' + radio.value) }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
 
      <el-form-item :label="t('chart.compare_data')">
        <el-radio-group v-model="compareItem.compareCalc.resultData">
          <el-radio value="pre">{{ t('chart.data_pre') }}</el-radio>
          <el-radio value="sub">{{ t('chart.data_sub') }}</el-radio>
          <el-radio value="percent">{{ t('chart.data_percent') }}</el-radio>
        </el-radio-group>
      </el-form-item>
 
      <el-form-item :label="t('chart.compare_calc_expression')">
        <span class="exp-style">
          {{ hintStr }}
        </span>
      </el-form-item>
 
      <el-form-item :label="t('chart.tip')">
        <span class="exp-style" style="padding-top: 2px">
          {{ t('chart.compare_calc_tip') }}
        </span>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<style lang="less" scoped>
.ed-form-item {
  margin-bottom: 10px !important;
}
 
.compare-form :deep(.ed-form-item__label) {
  font-size: 12px !important;
  font-weight: 400 !important;
  padding-top: 8px !important;
}
 
.compare-form :deep(.ed-radio__label) {
  font-size: 12px !important;
  font-weight: 400 !important;
}
 
.el-select-dropdown__item :deep(span) {
  font-size: 12px !important;
}
 
.exp-style {
  color: #c0c4cc;
  font-size: 12px;
}
</style>