はじめに
SpreadJSのピボットテーブルは、項目スライサーとタイムラインスライサーの、2種類のスライサーをサポートします。
テーブルスライサーも同じように、ピボットスライサーもスライサーコレクションで管理されます。
SpreadJSでは、追加するスライサーのタイプを区別するために、SlicerTypeを定義しました。
項目スライサーは、任意のフィールド(計算フィールドを除く)に追加できます。
項目スライサーによる変更は、手動フィルターを使用した場合と同じで、ラベルフィルターの "textItems" を意味します。
タイムラインスライサーは、日付型フィールドにのみ追加できます。
タイムラインスライサーによる変更は、ラベル条件フィルターを使用した場合と同じで、ラベルフィルターの "condition" を意味します。
例えば、ピボットテーブルの項目スライサーを追加したい場合。
"pt" という名前のピボットテーブルを作成したと仮定します。
"name" フィールドに項目スライサーを追加したい場合。
また、ピボットテーブル "pt" に "birthday" という日付フィールドがあると仮定し、
ピボットテーブルのタイムラインスライサーを追加するとします。
次の疑問が出てくるかもしれません:
項目スライサーとタイムラインスライサーを、同時に日付フィールドに追加できることは明らかです。
これらのスライサーは同時に動作できるのか?
デフォルトでは、出来ません。
これらはlabelFilterを通して動作します。"textItems" フィルターを使用すると、"condition" フィルターが失われます。
この問題は、ピボットテーブルのallowMultipleFiltersPerFieldオプションで解決できます。
スライサー内の項目をクリックしてドラッグし、フィルタリングしてみてください。
クリックまたはドラッグするときに、ShiftキーとCtrlキー(Windowsの場合)/コマンドキー(Macの場合)を押してみることもできます。
シェイプに基づくスライサー
シェイプに基づくピボットスライサーにより、多くのシェイプAPIを使用してピボットスライサーを制御できます。
例として
ピボットテーブルへの接続
ピボットスライサーはピボットテーブルへの接続を管理して、接続の追加・切断を行います。
ピボットテーブルとの接続が切断すると、スライサーのフィルターアクションはピボットテーブルには影響しません。
また、ピボットテーブルのフィルターアクションもスライサーには影響しません。
例として、この機能を使うと、複数の同じソースのピボットテーブルを管理するためにスライサーを使用できます。
2つのピボットテーブル "pt1" と "pt2" があると仮定すると、どちらも "table1" という名前のテーブルから作成されているので、同じデータソース、同じフィールドを持っています。
これで、slicer_nameを使用して、2つのピボットテーブルのフィルターを制御できます。
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet> </gc-worksheet>
<gc-worksheet> </gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div class="block slicer-infos">
<div>現在選択しているスライサーの情報:</div><br>
<div class="slicer-info">
<label class="info-label">スライサー名:</label>
<input class="info-input" id="name" v-model="nameRef" @change="changeProperty('name')" />
</div>
<div class="slicer-info">
<label class="info-label">キャプション名:</label>
<input class="info-input" id="captionName" v-model="captionNameRef"
@change="changeProperty('captionName')" />
</div>
<div class="slicer-info">
<label class="info-label">X:</label>
<input type="number" class="info-input" name="x" id="x" min="0" max="1000" step="1" v-model="xRef"
@change="changeProperty('x')" />
</div>
<div class="slicer-info">
<label class="info-label">Y:</label>
<input type="number" class="info-input" name="y" id="y" min="0" max="1000" step="1" v-model="yRef"
@change="changeProperty('y')" />
</div>
<div class="slicer-info">
<label class="info-label">幅:</label>
<input type="number" class="info-input" name="width" id="width" min="0" max="1000" step="1"
v-model="widthRef" @change="changeProperty('width')" />
</div>
<div class="slicer-info">
<label class="info-label">高さ:</label>
<input type="number" class="info-input" name="height" id="height" min="0" max="1000" step="1"
v-model="heightRef" @change="changeProperty('height')" />
</div>
</div>
<div class="block">
<div>スライサー接続</div><br />
<div class="Connection">
<div class="slicer-info">
<input type="checkbox" id="pt1" v-model="connectPT1Ref" @change="changeConnection(1)" />
<label for="pt1">ピボットテーブル1に接続する</label>
</div>
<div class="slicer-info">
<input type="checkbox" id="pt2" v-model="connectPT2Ref" @change="changeConnection(2)" />
<label for="pt2">ピボットテーブル2に接続する</label>
</div>
</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 nameRef = shallowRef("");
let captionNameRef = shallowRef("");
let xRef = shallowRef(0);
let yRef = shallowRef(0);
let widthRef = shallowRef(0);
let heightRef = shallowRef(0);
let connectPT1Ref = shallowRef(false);
let connectPT2Ref = shallowRef(false);
let activeSlicerRef = shallowRef(null);
function initSpread(spread) {
initSheets(spread);
let pivotLayoutSheet = spread.getSheet(0);
initPivotTable(pivotLayoutSheet);
initSlicer(pivotLayoutSheet);
bindEvents(pivotLayoutSheet);
slicerProp["name"] = nameRef;
slicerProp["captionName"] = captionNameRef;
slicerProp["x"] = xRef;
slicerProp["y"] = yRef;
slicerProp["width"] = widthRef;
slicerProp["height"] = heightRef;
}
function initSheets(spread) {
spread.suspendPaint();
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.setValue(0, 0, "Pivot Table 1");
sheet0.setValue(0, 10, "Pivot Table 2");
sheet0.name("PivotLayout");
spread.resumePaint();
}
function initPivotTable(sheet) {
let groupInfo = { originFieldName: "date", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }, { by: GC.Pivot.DateGroupType.years }, { by: GC.Pivot.DateGroupType.months }] };
let pt1 = sheet.pivotTables.add("pt1", "tableSales", 1, 0, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.light8);
pt1.suspendLayout();
pt1.options.showRowHeader = true;
pt1.options.showColumnHeader = true;
pt1.options.subtotalsPosition = GC.Spread.Pivot.SubtotalsPosition.top;
pt1.layoutType(GC.Spread.Pivot.PivotTableLayoutType.compact);
pt1.add("region", "region", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt1.add("country", "country", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt1.add("city", "city", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt1.add("amount", "amounts", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
pt1.resumeLayout();
pt1.autoFitColumn();
let pt2 = sheet.pivotTables.add("pt2", "tableSales", 1, 10, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.medium23);
pt2.suspendLayout();
pt2.options.showRowHeader = true;
pt2.options.showColumnHeader = true;
pt2.options.subtotalsPosition = GC.Spread.Pivot.SubtotalsPosition.top;
pt2.layoutType(GC.Spread.Pivot.PivotTableLayoutType.compact);
pt2.group(groupInfo);
pt2.add("年 (date)", "年 (date)", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt2.add("四半期 (date)", "四半期 (date)", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt2.add("月 (date)", "月 (date)", GC.Spread.Pivot.PivotTableFieldType.rowField);
pt2.add("amount", "Amounts", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
pt2.resumeLayout();
pt2.autoFitColumn();
}
function initSlicer(sheet) {
sheet.suspendPaint();
let yearSlicer = sheet.slicers.add("Years", 'pt2', "date", GC.Spread.Sheets.Slicers.TimelineStyles.light5(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline);
let monthSlicer = sheet.slicers.add("Months", 'pt2', "date", GC.Spread.Sheets.Slicers.TimelineStyles.light1(), GC.Spread.Sheets.Slicers.SlicerType.pivotTimeline);
setTimelineProp(yearSlicer, 967, 20, 310, 160, 1, "年");
setTimelineProp(monthSlicer, 967, 160, 310, 160, 3, "月");
let regionSlicer = sheet.slicers.add("region", 'pt1', "region", GC.Spread.Sheets.Slicers.SlicerStyles.dark1(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable);
let countrySlicer = sheet.slicers.add("country", 'pt1', "country", GC.Spread.Sheets.Slicers.SlicerStyles.dark2(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable);
let citySlicer = sheet.slicers.add("city", 'pt1', "city", GC.Spread.Sheets.Slicers.SlicerStyles.dark3(), GC.Spread.Sheets.Slicers.SlicerType.pivotTable);
setSlicerProp(regionSlicer, 285, 20, 140, 200, 1, true, true);
setSlicerProp(countrySlicer, 425, 20, 140, 200, 1, true, true);
setSlicerProp(citySlicer, 565, 20, 140, 200, 1, true, true);
sheet.resumePaint();
}
function setSlicerProp(slicer, x, y, width, height, columnCount, showHeader, showNoDataItemsInLast) {
slicer.position({ x, y });
slicer.width(width);
slicer.height(height);
if (columnCount) {
slicer.columnCount(columnCount);
}
slicer.showHeader(!!showHeader);
slicer.showNoDataItemsInLast(!!showNoDataItemsInLast);
slicer.allowMove(false);
slicer.allowResize(false);
}
function setTimelineProp(timeline, x, y, width, height, level, caption) {
timeline.position({ x, y });
timeline.width(width);
timeline.height(height);
timeline.level(level);
timeline.captionName(caption);
timeline.allowMove(false);
timeline.allowResize(false);
}
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 changeProperty(prop, v) {
if (!activeSlicerRef.value) {
return;
}
v = v || slicerProp[prop].value;
if (v !== null && v !== undefined && v !== '') {
activeSlicerRef.value[prop](v);
}
}
function updateSlicerInfo() {
if (!activeSlicerRef.value) {
return;
}
let slicer = activeSlicerRef.value;
nameRef.value = slicer.name();
captionNameRef.value = slicer.captionName();
xRef.value = slicer.x();
yRef.value = slicer.y();
widthRef.value = slicer.width();
heightRef.value = slicer.height();
connectPT1Ref.value = slicer.isConnectedPivotTable('pt1');
connectPT2Ref.value = slicer.isConnectedPivotTable('pt2');
}
function changeConnection(id) {
let connect = id === 1 ? connectPT1Ref.value : connectPT2Ref.value;
let ptName = id === 1 ? 'pt1' : 'pt2';
let slicer = activeSlicerRef.value;
if (slicer) {
if (connect) {
slicer.connectPivotTable(ptName);
} else {
slicer.disconnectPivotTable(ptName);
}
}
}
</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;
}
.info-label {
width: 35%;
display: inline-block;
}
.info-input {
width: 58%;
display: inline-block;
}
#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);