feat(图表): 透视表行汇总小计可以根据维度灵活配置 #11336

This commit is contained in:
ulleo
2024-12-26 16:35:29 +08:00
committed by dataeaseShu
parent 98bf94b2e4
commit 6b3b1f5fc7
4 changed files with 104 additions and 5 deletions

View File

@@ -558,6 +558,10 @@ declare interface TotalConfig {
* 小计维度
*/
subTotalsDimensions: string[]
/**
* 兼容旧版的判断
*/
subTotalsDimensionsNew: boolean
/**
* 总计汇总设置
*/

View File

@@ -2,7 +2,7 @@
import { onMounted, PropType, reactive, watch, ref, inject, nextTick } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { DEFAULT_TABLE_TOTAL } from '@/views/chart/components/editor/util/chart'
import { cloneDeep, defaultsDeep } from 'lodash-es'
import { cloneDeep, defaultsDeep, find } from 'lodash-es'
import CustomAggrEdit from './CustomAggrEdit.vue'
const { t } = useI18n()
@@ -43,9 +43,61 @@ const state = reactive({
colTotalItem: {} as DeepPartial<CalcTotalCfg>,
totalCfg: [] as CalcTotalCfg[],
totalCfgAttr: '',
totalItem: {} as DeepPartial<CalcTotalCfg>
totalItem: {} as DeepPartial<CalcTotalCfg>,
selectedSubTotalDimensionName: '',
selectedSubTotalDimension: undefined as { name: string; checked: boolean },
subTotalDimensionList: []
})
function onSelectedSubTotalDimensionNameChange(name) {
state.selectedSubTotalDimension = find(state.subTotalDimensionList, d => d.name === name)
}
function changeRowSubTableTotal() {
const list = []
for (let i = 0; i < state.subTotalDimensionList.length; i++) {
if (state.subTotalDimensionList[i].checked) {
list.push(state.subTotalDimensionList[i].name)
}
}
state.tableTotalForm.row.subTotalsDimensions = list
changeTableTotal('row')
}
const initSubTotalDimensionList = () => {
const list = []
if (props.chart.xAxis.length > 2) {
for (let i = 0; i < props.chart.xAxis.length - 1; i++) {
//排除最后一个
const old = find(
state.tableTotalForm.row.subTotalsDimensions,
s => s === props.chart.xAxis[i].dataeaseName
)
list.push({
displayName: props.chart.xAxis[i].name,
name: props.chart.xAxis[i].dataeaseName,
checked: state.tableTotalForm.row.subTotalsDimensionsNew ? old != undefined : true
})
}
}
state.subTotalDimensionList = list
const existItem = find(
state.subTotalDimensionList,
s => s.name === state.selectedSubTotalDimensionName
)
if (existItem) {
state.selectedSubTotalDimension = existItem
} else {
state.selectedSubTotalDimensionName = list[0]?.name
state.selectedSubTotalDimension = list[0]
}
if (!state.tableTotalForm.row.subTotalsDimensionsNew) {
state.tableTotalForm.row.subTotalsDimensionsNew = true
changeRowSubTableTotal()
}
}
const emit = defineEmits(['onTableTotalChange'])
const changeTableTotal = prop => {
@@ -55,6 +107,9 @@ const changeTableTotal = prop => {
const init = () => {
const tableTotal = props.chart?.customAttr?.tableTotal
if (tableTotal) {
if (tableTotal.row) {
tableTotal.row.subTotalsDimensionsNew = !!tableTotal.row?.subTotalsDimensionsNew
}
state.tableTotalForm = defaultsDeep(cloneDeep(tableTotal), cloneDeep(DEFAULT_TABLE_TOTAL))
}
const yAxis = props.chart.yAxis
@@ -102,6 +157,7 @@ const init = () => {
total.originName = totalCfg[0].originName
}
})
initSubTotalDimensionList()
}
const showProperty = prop => props.propertyInner?.includes(prop)
const changeTotal = (totalItem, totals) => {
@@ -357,10 +413,46 @@ onMounted(() => {
v-model="state.tableTotalForm.row.showSubTotals"
:disabled="chart.xAxis.length < 2"
@change="changeTableTotal('row')"
>{{ t('chart.show') }}</el-checkbox
>
{{ t('chart.show') }}
</el-checkbox>
</el-form-item>
<div v-show="showProperty('row') && state.tableTotalForm.row.showSubTotals">
<div v-if="showProperty('row') && state.tableTotalForm.row.showSubTotals">
<div style="display: flex">
<div style="width: 22px; flex-direction: row"></div>
<div style="flex: 1">
<el-form-item class="form-item" :class="'form-item-' + themes">
<el-select
:effect="themes"
v-model="state.selectedSubTotalDimensionName"
:disabled="chart.xAxis.length < 2"
@change="onSelectedSubTotalDimensionNameChange"
>
<el-option
v-for="option in state.subTotalDimensionList"
:key="option.name"
:label="option.displayName"
:value="option.name"
/>
</el-select>
</el-form-item>
<el-form-item
v-if="state.selectedSubTotalDimension"
class="form-item"
:class="'form-item-' + themes"
>
<el-checkbox
:effect="themes"
v-model="state.selectedSubTotalDimension.checked"
:disabled="chart.xAxis.length < 2"
@change="changeRowSubTableTotal"
>
{{ t('chart.show') }}
</el-checkbox>
</el-form-item>
</div>
</div>
<el-form-item
:label="t('chart.total_position')"
class="form-item"

View File

@@ -389,6 +389,7 @@ export const DEFAULT_TABLE_TOTAL: ChartTableTotalAttr = {
label: t('chart.total_show'),
subLabel: t('chart.sub_total_show'),
subTotalsDimensions: [],
subTotalsDimensionsNew: true,
calcTotals: {
aggregation: 'SUM',
cfg: []

View File

@@ -175,7 +175,9 @@ export class TablePivot extends S2ChartView<PivotSheet> {
// total config
const { basicStyle, tooltip, tableTotal } = parseJson(chart.customAttr)
tableTotal.row.subTotalsDimensions = r
if (!tableTotal.row.subTotalsDimensionsNew || tableTotal.row.subTotalsDimensions == undefined) {
tableTotal.row.subTotalsDimensions = r
}
tableTotal.col.subTotalsDimensions = c
// 解析合计、小计排序