樞紐分析圖
使用透檢視之前,需要先註冊透視表和圖表依賴,然後在建立工作簿之前新增以下兩個外掛。UI 語言套件需要合併到 locales 配置中,並載入對應的樣式表。該功能需要 Pro 許可證。
pnpm add @univerjs-pro/sheets-pivot-chart @univerjs-pro/sheets-pivot-chart-uinpm install @univerjs-pro/sheets-pivot-chart @univerjs-pro/sheets-pivot-chart-uiyarn add @univerjs-pro/sheets-pivot-chart @univerjs-pro/sheets-pivot-chart-uibun add @univerjs-pro/sheets-pivot-chart @univerjs-pro/sheets-pivot-chart-uiimport { UniverSheetsPivotChartPlugin } from '@univerjs-pro/sheets-pivot-chart'import { UniverSheetsPivotChartUIPlugin } from '@univerjs-pro/sheets-pivot-chart-ui'import SheetsPivotChartLocale from '@univerjs-pro/sheets-pivot-chart-ui/locale/zh-TW'import { LocaleType } from '@univerjs/core'import '@univerjs-pro/sheets-pivot-chart/facade'import '@univerjs-pro/sheets-pivot-chart-ui/lib/index.css'univerAPI.loadLocales(LocaleType.ZH_TW, SheetsPivotChartLocale)univer.registerPlugin(UniverSheetsPivotChartPlugin)univer.registerPlugin(UniverSheetsPivotChartUIPlugin)Sheets 高階預設已包含這兩個外掛,可與核心預設、繪圖預設一起使用。本頁介紹的 API 還需顯式匯入 @univerjs-pro/sheets-pivot-chart/facade。
設定 Worker
預設設定
以下配置項屬於 UniverSheetsAdvancedPreset:
useWorkerboolean預設值:false在公式 Worker 中啟用進階功能。
使用 Worker 時,主執行緒傳入 UniverSheetsAdvancedPreset({ useWorker: true }),Worker 內部傳入 UniverSheetsAdvancedWorkerPreset()。外掛模式的細節見包參考。
獨立報表
在獨立報表模式下,報表由圖表自身持有。插入圖表後需要新增一個行欄位和一個值欄位,範例使用資料來源的前兩列。
const workbook = univerAPI.getActiveWorkbook()const sheet = workbook?.getActiveSheet()if (!sheet) throw new Error('No active worksheet')sheet.getRange('A1:B4').setValues([ ['Region', 'Sales'], ['East', 100], ['West', 200], ['East', 50],])const chart = await sheet.insertPivotChart( sheet.newPivotChart(univerAPI.Enum.ChartTypeString.Column) .setSource('A1:B4').setPosition('D2').setSize(640, 360).build(),)const fields = chart.getSourceFieldsInfo()await chart.addField(fields[0].id, univerAPI.Enum.PivotTableFiledAreaEnum.Row, 0)await chart.addField(fields[1].id, univerAPI.Enum.PivotTableFiledAreaEnum.Value, 0)連結樞紐分析表
欄位、篩選、排序和摺疊的修改會與所關聯的透視表以及該透視表上的其他關聯圖表同步,外觀和位置則保持獨立。刪除圖表不會刪除透視表;刪除透視表後圖表將不可用,直到恢復同一個透視表。
const pivotTable = workbook.getPivotTableById('pivot-table-1')if (!pivotTable) throw new Error('PivotTable not found')const linkedChart = await sheet.insertPivotChart( pivotTable.newChart(univerAPI.Enum.ChartTypeString.Line).setPosition('D22').build(),)linkedChart.setFieldButtonsVisible(true).setExpandCollapseButtonsVisible(true)支援的圖表類型
支援折線、柱形、條形、面積、餅圖、圓環和組合圖,以及適用的堆積型別。不支援散點、氣泡、雷達和瀑布圖。餅圖使用第一個值系列,圓環圖保留多個系列。圖表分類中不包含小計和總計。
欄位、篩選、樣式和生命週期 API 請參閱 FSheetPivotChart。
複製與刪除工作表
複製工作表時,其中的透檢視也會一併複製,並獲得新的 ID。如果獨立報表的資料來源位於被複制的工作表上,副本將使用新的工作表和獨立的快取。當透視表隨工作表一起復制時,關聯透檢視會指向複製後的透視表;對其他工作表的引用保持不變。刪除工作表會移除其中的透檢視及其相關資源,撤銷操作可以恢復它們的定義和源資料。
報表篩選
getOptions()、setOptions({ allowMultipleFilters: true }) 和 setDateFilter(fieldId, condition) 作用於圖表實際使用的報表。對於關聯圖表,這些方法會更新源透視表及其他關聯圖表;對於獨立圖表,這些方法只更新該圖表自身的報表。省略 condition 只清除日期條件。日期序列值和整日比較的說明見多重篩選與日期條件。
你覺得這篇文檔如何?