|
| 1 | +<script setup lang="ts"> |
| 2 | +/* eslint-disable no-console */ |
| 3 | +import { |
| 4 | + type SpreadSheet, |
| 5 | + type ThemeCfg, |
| 6 | + type ThemeName, |
| 7 | + type S2Options, |
| 8 | +} from '@antv/s2'; |
| 9 | +import { Button, Space, Popover } from 'ant-design-vue'; |
| 10 | +import { ref, shallowRef, computed } from 'vue'; |
| 11 | +import { |
| 12 | + SheetComponent, |
| 13 | + ThemePanel, |
| 14 | + TextAlignPanel, |
| 15 | + FrozenPanel, |
| 16 | + Switcher, |
| 17 | + AdvancedSort, |
| 18 | + DrillDown, |
| 19 | + StrategyExport, |
| 20 | +} from '../../src'; |
| 21 | +import type { |
| 22 | + ThemePanelOptions, |
| 23 | + TextAlignPanelOptions, |
| 24 | + FrozenPanelOptions, |
| 25 | +} from '../../src'; |
| 26 | +import { pivotSheetDataCfg, defaultOptions } from '../config'; |
| 27 | +
|
| 28 | +const s2Ref = shallowRef<SpreadSheet>(); |
| 29 | +const themeCfg = ref<ThemeCfg>({ |
| 30 | + name: 'default', |
| 31 | +}); |
| 32 | +const options = ref<S2Options>({ |
| 33 | + ...defaultOptions, |
| 34 | + width: 800, |
| 35 | + height: 600, |
| 36 | +}); |
| 37 | +
|
| 38 | +const onSheetMounted = (instance: SpreadSheet) => { |
| 39 | + s2Ref.value = instance; |
| 40 | + console.log('onMounted:', instance); |
| 41 | +}; |
| 42 | +
|
| 43 | +const onThemeChange = (panelOptions: ThemePanelOptions, theme: any) => { |
| 44 | + themeCfg.value = { |
| 45 | + name: panelOptions.themeType as ThemeName, |
| 46 | + theme, |
| 47 | + }; |
| 48 | + if (s2Ref.value) { |
| 49 | + s2Ref.value.setOptions({ |
| 50 | + hierarchyType: panelOptions.hierarchyType, |
| 51 | + }); |
| 52 | + s2Ref.value.render(false); |
| 53 | + } |
| 54 | +
|
| 55 | + console.log('ThemePanel onChange:', panelOptions, theme); |
| 56 | +}; |
| 57 | +
|
| 58 | +const onThemeReset = ( |
| 59 | + panelOptions: ThemePanelOptions, |
| 60 | + prevOptions: ThemePanelOptions, |
| 61 | + theme: any, |
| 62 | +) => { |
| 63 | + console.log('ThemePanel onReset:', panelOptions, prevOptions, theme); |
| 64 | +}; |
| 65 | +
|
| 66 | +const onTextAlignChange = (panelOptions: TextAlignPanelOptions, theme: any) => { |
| 67 | + themeCfg.value = { |
| 68 | + ...themeCfg.value, |
| 69 | + theme, |
| 70 | + }; |
| 71 | + if (s2Ref.value) { |
| 72 | + s2Ref.value.render(false); |
| 73 | + } |
| 74 | +
|
| 75 | + console.log('TextAlignPanel onChange:', panelOptions, theme); |
| 76 | +}; |
| 77 | +
|
| 78 | +const onTextAlignReset = ( |
| 79 | + panelOptions: TextAlignPanelOptions, |
| 80 | + prevOptions: TextAlignPanelOptions, |
| 81 | + theme: any, |
| 82 | +) => { |
| 83 | + console.log('TextAlignPanel onReset:', panelOptions, prevOptions, theme); |
| 84 | +}; |
| 85 | +
|
| 86 | +const onFrozenChange = (panelOptions: FrozenPanelOptions) => { |
| 87 | + const [rowCount = 0, trailingRowCount = 0] = panelOptions.frozenRow; |
| 88 | + const [colCount = 0, trailingColCount = 0] = panelOptions.frozenCol; |
| 89 | +
|
| 90 | + if (s2Ref.value) { |
| 91 | + s2Ref.value.setOptions({ |
| 92 | + frozen: { |
| 93 | + rowHeader: panelOptions.frozenRowHeader, |
| 94 | + rowCount, |
| 95 | + colCount, |
| 96 | + trailingRowCount, |
| 97 | + trailingColCount, |
| 98 | + }, |
| 99 | + }); |
| 100 | + s2Ref.value.render(false); |
| 101 | + } |
| 102 | +
|
| 103 | + console.log('FrozenPanel onChange:', panelOptions); |
| 104 | +}; |
| 105 | +
|
| 106 | +const onFrozenReset = ( |
| 107 | + panelOptions: FrozenPanelOptions, |
| 108 | + prevOptions: FrozenPanelOptions, |
| 109 | +) => { |
| 110 | + console.log('FrozenPanel onReset:', panelOptions, prevOptions); |
| 111 | +}; |
| 112 | +
|
| 113 | +const toSwitcherItems = (fields: any[] = []) => { |
| 114 | + return fields.map((f) => { |
| 115 | + return typeof f === 'string' ? { id: f } : { id: f.field, ...f }; |
| 116 | + }); |
| 117 | +}; |
| 118 | +
|
| 119 | +// Switcher Logic |
| 120 | +const switcherFields = ref({ |
| 121 | + rows: pivotSheetDataCfg.fields.rows, |
| 122 | + columns: pivotSheetDataCfg.fields.columns, |
| 123 | + values: pivotSheetDataCfg.fields.values, |
| 124 | +}); |
| 125 | +
|
| 126 | +const dataCfg = computed(() => { |
| 127 | + return { |
| 128 | + ...pivotSheetDataCfg, |
| 129 | + fields: { |
| 130 | + ...pivotSheetDataCfg.fields, |
| 131 | + ...switcherFields.value, |
| 132 | + }, |
| 133 | + }; |
| 134 | +}); |
| 135 | +
|
| 136 | +const onSwitcherSubmit = ({ rows, columns, values }: any) => { |
| 137 | + console.log('Switcher onSubmit:', rows, columns, values); |
| 138 | + switcherFields.value = { |
| 139 | + rows: rows.items.map((i: any) => i.id), |
| 140 | + columns: columns.items.map((i: any) => i.id), |
| 141 | + values: values.items.map((i: any) => i.id), |
| 142 | + }; |
| 143 | + if (s2Ref.value) { |
| 144 | + s2Ref.value.render(false); |
| 145 | + } |
| 146 | +}; |
| 147 | +
|
| 148 | +// Advanced Sort Logic |
| 149 | +const onSortConfirm = (ruleValues: any[], sortParams: any[]) => { |
| 150 | + console.log('AdvancedSort onConfirm:', ruleValues, sortParams); |
| 151 | + if (s2Ref.value) { |
| 152 | + s2Ref.value.setDataCfg({ |
| 153 | + ...s2Ref.value.dataCfg, |
| 154 | + sortParams, |
| 155 | + }); |
| 156 | + s2Ref.value.render(false); |
| 157 | + } |
| 158 | +}; |
| 159 | +
|
| 160 | +// Drill Down Logic |
| 161 | +const drillFields = ref<string[]>([]); |
| 162 | +</script> |
| 163 | + |
| 164 | +<template> |
| 165 | + <div class="components-playground"> |
| 166 | + <Space direction="horizontal" class="config-panels"> |
| 167 | + <ThemePanel |
| 168 | + title="主题配置" |
| 169 | + :disableCustomPrimaryColorPicker="false" |
| 170 | + :defaultCollapsed="false" |
| 171 | + @change="onThemeChange" |
| 172 | + @reset="onThemeReset" |
| 173 | + /> |
| 174 | + <TextAlignPanel |
| 175 | + title="文字对齐" |
| 176 | + :defaultCollapsed="false" |
| 177 | + @change="onTextAlignChange" |
| 178 | + @reset="onTextAlignReset" |
| 179 | + /> |
| 180 | + <FrozenPanel |
| 181 | + title="冻结行列头" |
| 182 | + :defaultCollapsed="false" |
| 183 | + :inputNumberProps="{ |
| 184 | + size: 'small', |
| 185 | + step: 1, |
| 186 | + }" |
| 187 | + :defaultOptions="{ |
| 188 | + frozenRow: [1, 2], |
| 189 | + }" |
| 190 | + @change="onFrozenChange" |
| 191 | + @reset="onFrozenReset" |
| 192 | + /> |
| 193 | + <Switcher |
| 194 | + title="行列切换" |
| 195 | + :rows="{ items: toSwitcherItems(dataCfg.fields.rows) }" |
| 196 | + :columns="{ items: toSwitcherItems(dataCfg.fields.columns) }" |
| 197 | + :values="{ items: toSwitcherItems(dataCfg.fields.values) }" |
| 198 | + @submit="onSwitcherSubmit" |
| 199 | + /> |
| 200 | + <AdvancedSort |
| 201 | + v-if="s2Ref" |
| 202 | + :sheetInstance="s2Ref" |
| 203 | + @sort-confirm="onSortConfirm" |
| 204 | + /> |
| 205 | + <Popover trigger="click" placement="bottomLeft"> |
| 206 | + <template #content> |
| 207 | + <DrillDown |
| 208 | + :dataSet="[ |
| 209 | + { name: '地区', value: 'area', type: 'location' }, |
| 210 | + { name: '城市', value: 'city', type: 'location' }, |
| 211 | + { name: '日期', value: 'date', type: 'date' }, |
| 212 | + ]" |
| 213 | + v-model:drillFields="drillFields" |
| 214 | + /> |
| 215 | + </template> |
| 216 | + <Button>下钻</Button> |
| 217 | + </Popover> |
| 218 | + <StrategyExport v-if="s2Ref" :sheetInstance="s2Ref" /> |
| 219 | + </Space> |
| 220 | + <SheetComponent |
| 221 | + :dataCfg="dataCfg" |
| 222 | + :options="options" |
| 223 | + :themeCfg="themeCfg" |
| 224 | + sheetType="pivot" |
| 225 | + @mounted="onSheetMounted" |
| 226 | + adaptive |
| 227 | + /> |
| 228 | + </div> |
| 229 | +</template> |
| 230 | + |
| 231 | +<style lang="less" scoped> |
| 232 | +.components-playground { |
| 233 | + display: flex; |
| 234 | + flex-direction: column; |
| 235 | + gap: 20px; |
| 236 | +
|
| 237 | + .config-panels { |
| 238 | + display: flex; |
| 239 | + flex-wrap: wrap; |
| 240 | + align-items: flex-start; |
| 241 | + } |
| 242 | +} |
| 243 | +</style> |
0 commit comments