ピボットテーブルの項目スライサー

SpreadJSのピボットテーブルは、テーブルスライサーと同じように使用できる項目スライサーをサポートします。

以下のデモでスライサーをクリックすると、利用可能なすべてのプロパティを見ることができます。

ピボットテーブルの項目スライサーは、テーブルスライサーと同じように、SlicerCollection(WorkSheet.slicers)によって管理されます。 項目スライサーは、任意のフィールド(Calcフィールドを除く)に追加できます。 項目スライサーで行われた変更は、手動フィルターを使用した場合と同じで、ラベルフィルターの「textItems」を意味します。 スライサーの追加 項目スライサーを追加する場合は、まず例として「pt」という名前のピボットテーブルを作成します。 (initPivotTable の具体的な実装については、この記事の最後にあります)。 次に、「name」フィールドに項目スライサーを追加します。 項目の状態 項目は2つの状態を持つように定義しました。 selected:項目がフィルターによって選択されているかどうか。 noData:他のフィルターによって選択されている場合、つまり、選択してもしなくても効果がない場合は、noDataと定義します。 これら2つの状態によって、項目の状態が構成されます。 たとえば、"selected && noData"や"unselected && hasData"のようになります。 スライサーの使用 次に、slicer_nameを制御することができます。 たとえば、2つの列に項目を表示したい場合です。 項目の高さをより高くしたい場合 noDataの状態の項目を表示したくない場合 ピボットテーブルを作成するサンプル 項目スライサーのスタイルのカスタマイズ SpreadJS は、ピボットテーブルの項目スライサーのスタイルのカスタマイズをサポートしています。
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> </gc-spread-sheets> <div class="options-container"> <div class="block slicer-infos"> <div>現在選択されているスライサーの情報:</div> <br /> <div class="slicer-info"> <label>列の数:</label> <input class="info-input" type="number" id="columnCount" v-model="columnCountRef" @change="changeProperty('columnCount')" /> </div> <div class="slicer-info"> <input type="checkbox" id="showHeader" v-model="showHeaderRef" @change="changeProperty('showHeader')" /> <label for="showHeader">ヘッダーを表示する</label> </div> <div class="slicer-info"> <input type="checkbox" id="hideNoDataItems" v-model="hideNoDataItemsRef" @change="changeProperty('hideNoDataItems')" /> <label for="hideNoDataItems">データのない項目を非表示にする</label> </div> <div class="slicer-info"> <input type="checkbox" id="visuallyNoDataItems" v-model="visuallyNoDataItemsRef" @change="changeProperty('visuallyNoDataItems')" /> <label for="visuallyNoDataItems">データのない項目を視覚的に表示する</label> </div> <div class="slicer-info"> <input type="checkbox" id="showNoDataItemsInLast" v-model="showNoDataItemsInLastRef" @change="changeProperty('showNoDataItemsInLast')" /> <label for="showNoDataItemsInLast">データのない項目を最後に表示する</label> </div> <div class="slicer-info"> <p>並べ替え</p> <input type="radio" id="ascending" name="sortState" value="1" v-model="sortStateRef" @change="changeProperty('sortState', 1)" /> <label for="ascending">昇順 (AからZ)</label><br /> <input type="radio" id="descending" name="sortState" value="2" v-model="sortStateRef" @change="changeProperty('sortState', 2)" /> <label for="descending">降順 (ZからA)</label><br /> </div> </div> <div class="block"> <div>スライサーを追加する</div> <p> 行と列のフィールドにスライサーを追加することのみをサポートします。 </p> <select class="select-list" name="slicerList" id="slicerList" v-model="selectedFieldRef"> <option value="region">region</option> <option value="country">country</option> <option value="city">city</option> <option value="四半期 (date)">四半期 (date)</option> <option value="amount">amount</option> <option value="id">id</option> </select> <button class="select-button" id="addSlicerBtn" @click="addSlicer">追加</button> </div> <div class="block"> <div>現在のスライサーの種類を変更する</div> <br /> <div class="slicerStyle"> <select class="select-list" name="slicerStyle" id="slicerStyle" v-model="styleNameRef"> <option value="light1">light1</option> <option value="light2">light2</option> <option value="light3">light3</option> <option value="light4">light4</option> <option value="light5">light5</option> <option value="light6">light6</option> <option value="dark1">dark1</option> <option value="dark2">dark2</option> <option value="dark3">dark3</option> <option value="dark4">dark4</option> <option value="dark5">dark5</option> <option value="dark6">dark6</option> <option value="other1">other1</option> <option value="other2">other2</option> <option value="custom1">custom1</option> <option value="custom2">custom2</option> </select> <button class="select-button" id="changeStyle" @click="setStyle">変更</button> </div> </div> </div> </div> </template> <script setup> import GC from "@mescius/spread-sheets"; import "@mescius/spread-sheets-shapes"; import "@mescius/spread-sheets-slicers"; import "@mescius/spread-sheets-vue"; import { shallowRef } from "vue"; import "@mescius/spread-sheets-pivot-addon"; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); let slicerProp = {}; let columnCountRef = shallowRef(0); let showHeaderRef = shallowRef(true); let hideNoDataItemsRef = shallowRef(false); let visuallyNoDataItemsRef = shallowRef(true); let showNoDataItemsInLastRef = shallowRef(true); let sortStateRef = shallowRef(1); let selectedFieldRef = shallowRef("region"); let styleNameRef = shallowRef(""); let slicerCountRef = shallowRef(0); let activeSlicerRef = shallowRef(null); let spreadRef = shallowRef(null); let ptNameRef = shallowRef(null); function initSpread(spread) { spreadRef.value = spread; initCustomThemes(spread); spread.setSheetCount(2); initSheets(spread); let pivotLayoutSheet = spread.getSheet(0); initPivotTable(pivotLayoutSheet); bindEvents(pivotLayoutSheet); initSlicers(pivotLayoutSheet); slicerProp["columnCount"] = columnCountRef; slicerProp["showHeader"] = showHeaderRef; slicerProp["hideNoDataItems"] = hideNoDataItemsRef; slicerProp["visuallyNoDataItems"] = visuallyNoDataItemsRef; slicerProp["showNoDataItemsInLast"] = showNoDataItemsInLastRef; } function initCustomThemes(spread) { const theme1 = new GC.Spread.Sheets.Slicers.SlicerStyle(); theme1.fromJSON(GC.Spread.Sheets.Slicers.SlicerStyles.light1().toJSON()); theme1.name('custom1'); theme1.wholeSlicerStyle(new GC.Spread.Sheets.Slicers.SlicerStyleInfo('rgb(225, 245, 254)')); const theme2 = new GC.Spread.Sheets.Slicers.SlicerStyle(); theme2.fromJSON(GC.Spread.Sheets.Slicers.SlicerStyles.other2().toJSON()); theme2.name('custom2'); const wholeSlicerStyle = new GC.Spread.Sheets.Slicers.SlicerStyleInfo(); wholeSlicerStyle.backColor('#e3f2fd'); theme2.wholeSlicerStyle(wholeSlicerStyle); spread.customSlicerThemes.add(theme1); spread.customSlicerThemes.add(theme2); } function initSheets(spread) { spread.suspendPaint(); spread.setSheetCount(2); let sheet = spread.getSheet(1); sheet.name("DataSource"); sheet.setRowCount(650); sheet.setColumnWidth(5, 120); sheet.getCell(-1, 5).formatter("YYYY-mm-DD"); sheet.getRange(-1, 4, 0, 1).formatter("$ #,##0"); sheet.setArray(0, 0, pivotSales); let table = sheet.tables.add('tableSales', 0, 0, 637, 6); table.style(GC.Spread.Sheets.Tables.TableThemes["none"]); let sheet0 = spread.getSheet(0); sheet0.name("PivotLayout"); spread.resumePaint(); } function initPivotTable(sheet) { sheet.setRowCount(1000); let option = { showRowHeader: true, showColumnHeader: true, bandRows: true, bandColumns: true }; let pivotTable = sheet.pivotTables.add("pivotTable", "tableSales", 1, 0, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium8.name(), option); pivotTable.suspendLayout(); pivotTable.add("region", "region", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("country", "countrys", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("city", "city", GC.Spread.Pivot.PivotTableFieldType.rowField); let groupInfo = { originFieldName: "date", dateGroups: [ { by: GC.Pivot.DateGroupType.quarters } ] }; pivotTable.group(groupInfo); pivotTable.add("amount", "amount", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); pivotTable.options.subtotalsPosition = GC.Spread.Pivot.SubtotalsPosition.none; pivotTable.resumeLayout(); pivotTable.autoFitColumn(); ptNameRef.value = pivotTable.name(); } function initSlicers(sheet) { let slicer_region = sheet.slicers.add("slicer_region", ptNameRef.value, "region", GC.Spread.Sheets.Slicers.SlicerStyles.dark2().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable); slicer_region.position(new GC.Spread.Sheets.Point(491, 20)); slicer_region.height(210); let slicer_country = sheet.slicers.add("slicer_country", ptNameRef.value, "country", GC.Spread.Sheets.Slicers.SlicerStyles.light1().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable) slicer_country.position(new GC.Spread.Sheets.Point(691, 20)); slicer_country.height(460); slicer_country.showNoDataItems(false); let slicer_city = sheet.slicers.add("slicer_city", ptNameRef.value, "city", GC.Spread.Sheets.Slicers.SlicerStyles.other2().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable); slicer_city.position(new GC.Spread.Sheets.Point(891, 20)); slicer_city.height(320); slicer_city.columnCount(2); } function bindEvents(sheet) { sheet.bind(GC.Spread.Sheets.Events.SlicerChanged, function () { let slicers = sheet.slicers.all(); for (let i = 0; i < slicers.length; i++) { if (slicers[i].isSelected()) { activeSlicerRef.value = slicers[i]; updateSlicerInfo(); break; } } }); } function updateSlicerInfo() { if (!activeSlicerRef.value) { return; } let slicer = activeSlicerRef.value; columnCountRef.value = slicer.columnCount(); showHeaderRef.value = slicer.showHeader(); hideNoDataItemsRef.value = !slicer.showNoDataItems(); visuallyNoDataItemsRef.value = slicer.visuallyNoDataItems(); showNoDataItemsInLastRef.value = slicer.showNoDataItemsInLast(); sortStateRef.value = slicer.sortState(); const slicerStyleName = slicer.style().name().toLowerCase(); styleNameRef.value = slicerStyleName.includes('custom') ? slicerStyleName : slicerStyleName.substr(11); } function changeProperty(prop, v) { if (!activeSlicerRef.value) { return; } v = v || slicerProp[prop].value; if (v !== null && v !== undefined) { if (prop === "hideNoDataItems") { activeSlicerRef.value.showNoDataItems(!hideNoDataItemsRef.value); } else { activeSlicerRef.value[prop](v); } } } function setStyle() { if (!activeSlicerRef.value) { return; } activeSlicerRef.value.style(styleNameRef.value); } function addSlicer() { let sheet = spreadRef.value.getActiveSheet(); sheet.slicers.add( selectedFieldRef.value + "_" + slicerCountRef.value, ptNameRef.value, selectedFieldRef.value, GC.Spread.Sheets.Slicers.SlicerStyles.light1().name(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable ); slicerCountRef.value += 1; } </script> <style scoped> .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 330px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 330px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; font-size: 14px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .slicer-info { margin-top: 5px; margin-bottom: 5px; } .block { border: 1px solid gray; padding-left: 5px; padding-top: 10px; padding-bottom: 10px; margin-bottom: 1px; } .select-list { width: 120px; } .select-button { width: 80px; } #app { height: 100%; } </style>
<!DOCTYPE html> <html lang="en" style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/pivotSales.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/ja/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, packageConfigPaths: [ '../node_modules/*/package.json', "../node_modules/@mescius/*/package.json", "../node_modules/@babel/*/package.json", "../node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', "systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js", '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js', '@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js', '@mescius/spread-sheets-slicers': 'npm:@mescius/spread-sheets-slicers/index.js', '@mescius/spread-sheets-pivot-addon': 'npm:@mescius/spread-sheets-pivot-addon/index.js', }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);