import 'bootstrap.css';
import '@mescius/wijmo.styles/wijmo.css';
import './styles.css';
import * as wijmo from '@mescius/wijmo';
import * as grid from '@mescius/wijmo.grid';
import * as chart from '@mescius/wijmo.chart';
import '@mescius/wijmo.chart.render';
import * as pdf from '@mescius/wijmo.pdf';
import * as gridPdf from '@mescius/wijmo.grid.pdf';
import { getData, getExpenses } from './data';
//
document.readyState === 'complete' ? init() : window.onload = init;
const docName = 'FlexGrid.pdf';
//
function init() {
const exportSettings = {
embeddedFonts: [{ source: 'https://demo.mescius.jp/wijmo/sample/fonts/ipaexg.ttf', name: 'ipaexg' }],
styles: {
cellStyle: {
font: { family: 'ipaexg' },
backgroundColor: '#ffffff',
borderColor: '#c6c6c6'
},
altCellStyle: {
backgroundColor: '#f9f9f9'
},
groupCellStyle: {
backgroundColor: '#dddddd'
},
headerCellStyle: {
backgroundColor: '#eaeaea'
}
}
};
const data = getData(1500);
//
initExportGridSample(exportSettings, data);
initExportSample(exportSettings, data);
initApiSample(exportSettings, data);
}
//
//
function initExportGridSample(exportSettings, data) {
let flexGrid = new grid.FlexGrid('#flexGrid1', {
autoGenerateColumns: false,
selectionMode: grid.SelectionMode.ListBox,
headersVisibility: grid.HeadersVisibility.All,
columns: [
{ header: 'ID', binding: 'id' },
{ header: '開始', binding: 'start', format: 'd' },
{ header: '終了', binding: 'end', format: 'd' },
{ header: '国', binding: 'country' },
{ header: '商品', binding: 'product' },
{ header: '売上', binding: 'amount', format: 'c' },
{ header: '色', binding: 'color' },
{ header: '費用', binding: 'amount2', format: 'c2' },
{ header: '値引', binding: 'discount', format: 'p1' },
{ header: '有効', binding: 'active' }
],
itemsSource: data
});
//
let ctx = {
exporting: false,
progress: 0,
preparing: false,
worker: null,
grid: flexGrid,
button: document.querySelector('#btnExport1'),
progressEl: document.querySelector('#txtProgress1')
};
//
ctx.button.addEventListener('click', () => {
if (!ctx.exporting) {
start(ctx, './export-grid', () => {
gridPdf.PdfWebWorkerClient.exportGrid(ctx.worker, flexGrid, docName, exportSettings, null, (prgVal) => {
progress(ctx, prgVal);
});
});
}
else {
cancel(ctx);
}
});
//
window.addEventListener('unload', () => {
if (ctx.worker) {
ctx.worker.terminate();
ctx.worker = null;
}
});
}
//
//
function initExportSample(exportSettings, data) {
let flexPie = new chart.FlexPie('#flexPie', {
itemsSource: ((totals) => [
{ name: '宿泊費', value: totals.hotel },
{ name: '交通費', value: totals.transport },
{ name: '食事代', value: totals.meal },
{ name: '燃料費', value: totals.fuel },
{ name: 'その他', value: totals.Misc }
])(getExpenses().totals),
binding: 'value',
bindingName: 'name',
innerRadius: 0.75,
dataLabel: {
content: '{value:c1}',
position: chart.PieLabelPosition.Inside
}
});
//
let flexGrid = new grid.FlexGrid('#flexGrid2', {
autoGenerateColumns: false,
selectionMode: grid.SelectionMode.ListBox,
headersVisibility: grid.HeadersVisibility.All,
columns: [
{ header: 'ID', binding: 'id' },
{ header: '開始', binding: 'start', format: 'd' },
{ header: '終了', binding: 'end', format: 'd' },
{ header: '国', binding: 'country' },
{ header: '商品', binding: 'product' },
{ header: '売上', binding: 'amount', format: 'c' },
{ header: '色', binding: 'color' },
{ header: '費用', binding: 'amount2', format: 'c2' },
{ header: '値引', binding: 'discount', format: 'p1' },
{ header: '有効', binding: 'active' }
],
itemsSource: data
});
//
let ctx = {
exporting: false,
progress: 0,
preparing: false,
worker: null,
grid: flexGrid,
button: document.querySelector('#btnExport2'),
progressEl: document.querySelector('#txtProgress2')
};
//
ctx.button.addEventListener('click', () => {
if (!ctx.exporting) {
start(ctx, './export', () => {
gridPdf.PdfWebWorkerClient.addString(ctx.worker, 'タイトル', 'title');
gridPdf.PdfWebWorkerClient.addImage(ctx.worker, 'resources/canada.png', 'flag', null);
flexPie.saveImageToDataUrl(chart.ImageFormat.Svg, (url) => {
gridPdf.PdfWebWorkerClient.addImage(ctx.worker, url, 'chart', null);
});
gridPdf.PdfWebWorkerClient.addGrid(ctx.worker, flexGrid, 'grid', exportSettings);
//
gridPdf.PdfWebWorkerClient.export(ctx.worker, null, (data) => pdf.saveBlob(data.blob, docName), (prgVal) => progress(ctx, prgVal));
});
}
else {
cancel(ctx);
}
});
//
window.addEventListener('unload', () => {
if (ctx.worker) {
ctx.worker.terminate();
ctx.worker = null;
}
});
}
//
//
function initApiSample(exportSettings, data) {
let flexGrid = new grid.FlexGrid('#flexGrid3', {
autoGenerateColumns: false,
selectionMode: grid.SelectionMode.ListBox,
headersVisibility: grid.HeadersVisibility.All,
columns: [
{ header: 'ID', binding: 'id' },
{ header: '開始', binding: 'start', format: 'd' },
{ header: '終了', binding: 'end', format: 'd' },
{ header: '国', binding: 'country' },
{ header: '商品', binding: 'product' },
{ header: '売上', binding: 'amount', format: 'c' },
{ header: '色', binding: 'color' },
{ header: '費用', binding: 'amount2', format: 'c2' },
{ header: '値引', binding: 'discount', format: 'p1' },
{ header: '有効', binding: 'active' }
],
itemsSource: data
});
//
let ctx = {
exporting: false,
progress: 0,
preparing: false,
worker: null,
grid: flexGrid,
button: document.querySelector('#btnExport3'),
progressEl: document.querySelector('#txtProgress3')
};
//
ctx.button.addEventListener('click', () => {
if (!ctx.exporting) {
start(ctx, './api', () => {
ctx.worker.addEventListener('message', (e) => {
if (e.data.type === 'progress') {
progress(ctx, e.data.value);
}
else {
pdf.saveBlob(new Blob([new Uint8Array(e.data.data)], { type: 'application/pdf' }), docName);
}
});
//
var gridData = gridPdf.PdfWebWorkerClient.serializeGrid(flexGrid, exportSettings);
// Send gridData as a tranferable object
ctx.worker.postMessage({ grid: gridData, settings: JSON.stringify(exportSettings) }, [gridData]);
});
}
else {
cancel(ctx);
}
});
//
window.addEventListener('unload', () => {
if (ctx.worker) {
ctx.worker.terminate();
ctx.worker = null;
}
});
}
//
function start(ctx, url, ready) {
ctx.preparing = true;
ctx.progress = 0;
updateUI(ctx);
//
ctx.worker = loadWorker(url, () => ready());
}
//
function progress(ctx, progress) {
if (progress === 0 || progress === 1) {
ctx.preparing = false;
ctx.exporting = progress === 0;
}
ctx.progress = progress;
//
updateUI(ctx);
}
//
function cancel(ctx) {
ctx.worker.terminate();
ctx.worker = null;
ctx.exporting = false;
ctx.progress = 0;
//
updateUI(ctx);
}
//
/**
* Creates web worker that executes the module from the specified URL.
* @param url The module URL, relative to the src/workers directory.
* @param ready
*/
function loadWorker(url, ready) {
let worker = new Worker('src/workers/worker-loader.js');
//
worker.addEventListener('message', (e) => {
if (e.data === '#ready#') {
ready();
}
});
//
worker.postMessage({ url: url });
//
return worker;
}
//
function updateUI(ctx) {
ctx.grid.isDisabled = ctx.preparing;
wijmo.enable(ctx.button, !ctx.preparing);
ctx.button.textContent = ctx.exporting ? 'キャンセル' : 'エクスポート';
ctx.progressEl.textContent = (ctx.progress * 100).toFixed(1) + '%';
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Web Workers</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- SystemJS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.5/system.src.js" integrity="sha512-skZbMyvYdNoZfLmiGn5ii6KmklM82rYX2uWctBhzaXPxJgiv4XBwJnFGr5k8s+6tE1pcR1nuTKghozJHyzMcoA==" crossorigin="anonymous"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('./src/app');
</script>
</head>
<body>
<div class="container-fluid">
<p>
このサンプルは、「シンプルな単一グリッドのエクスポート」のシナリオを解説し、バックグラウンドスレッドでFlexGridをエクスポートする方法を示しています。
</p>
<button class="btn btn-default" id="btnExport1">エクスポート</button>
<span id="txtProgress1">0%</span>
<div id="flexGrid1" class="grid"></div>
<hr />
<p>
このサンプルは、「カスタムドキュメントの生成」のシナリオを解説し、FlexPieのSVG画像、FlexGrid、およびPNG画像をバックグラウンドスレッドでPDFにエクスポートする方法を示しています。
</p>
<p>
ここでは、<a href="https://www.npmjs.com/package/xmldom">xmldom</a>ライブラリを使用して、SVGを描画できるようにDOMサポートをサーバーに提供しています。
</p>
<button class="btn btn-default" id="btnExport2">エクスポート</button>
<span id="txtProgress2">0%</span>
<p></p>
<div class="row">
<div class="col-md-6">
<div id="flexPie"></div>
</div>
<div class="col-md-6">
<div id="flexGrid2" class="grid"></div>
</div>
</div>
<hr />
<p>
このサンプルは、「Web WorkerAPIの使用」のシナリオを解説し、Web Workers APIを使用してバックグラウンドスレッドでFlexGridをPDFにエクスポートする方法を示しています。
</p>
<button class="btn btn-default" id="btnExport3">エクスポート</button>
<span id="txtProgress3">0%</span>
<div id="flexGrid3" class="grid"></div>
</div>
</body>
</html>
export function getData(count) {
// data used to generate random items
const countries = ['アメリカ', 'ドイツ', 'イギリス', '日本', 'イタリア', 'ギリシャ'];
const products = ['ウィジェット', 'ガジェット', 'ツール'];
const colors = ['オレンジ', '白', '赤', '緑', '青'];
//
let data = [];
let dt = new Date();
//
// add count items
for (let i = 0; i < count; i++) {
// constants used to create data items
let date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length);
//
// create the item
let item = {
id: i,
start: date,
end: date,
country: countries[countryId],
product: products[productId],
color: colors[colorId],
amount: Math.random() * 10000 - 5000,
amount2: Math.random() * 10000 - 5000,
discount: Math.random() / 4,
active: i % 4 == 0,
};
//
// add the item to the list
data.push(item);
}
return data;
}
//
export function getExpenses() {
// [5; 10]
let count = 5 + Math.round(Math.random() * 5), ret = {
items: [],
totals: { hotel: 0, transport: 0, fuel: 0, meal: 0, Misc: 0, total: 0 }
}, msPerDay = 1000 * 24 * 60 * 60, curDate = Date.now() - 60 * msPerDay;
//
for (let i = 0; i < count; i++) {
let item = {
date: new Date(curDate),
description: '顧客訪問',
hotel: 30 + Math.random() * 200,
transport: 10 + Math.random() * 150,
fuel: Math.random() * 50,
meal: 30 + Math.random() * 170,
Misc: Math.random() * 220,
total: 0
};
//
item.total = item.hotel + item.transport + item.fuel + item.meal + item.Misc;
//
ret.totals.fuel += item.fuel;
ret.totals.hotel += item.hotel;
ret.totals.meal += item.meal;
ret.totals.Misc += item.Misc;
ret.totals.total += item.total;
ret.totals.transport += item.transport;
//
ret.items.push(item);
//
curDate += msPerDay * Math.round(Math.random() * 4);
}
//
return ret;
}
body {
margin-bottom: 24px;
}
.grid {
margin-top: 20px;
height: 200px;
}
.wj-flexpie {
height: 250px;
}
import { PdfWebWorker } from '@mescius/wijmo.grid.pdf';
//
PdfWebWorker.initExportGrid();
import xmldom from 'xmldom';
import * as pdf from '@mescius/wijmo.pdf';
import * as gridPdf from '@mescius/wijmo.grid.pdf';
//
self.DOMParser = xmldom.DOMParser;
//
gridPdf.PdfWebWorker.initExport((doc, clientData) => {
gridPdf.PdfWebWorker.sendExportProgress(0);
//
doc.registerFont({ source: 'https://demo.mescius.jp/wijmo/sample/fonts/ipaexg.ttf', name: 'ipaexg' });
doc.setFont(new pdf.PdfFont('ipaexg'));
doc.drawText(clientData['title'].content);
doc.drawImage(clientData['flag'].content);
doc.drawSvg(clientData['chart'].content);
//
// Suppose that text and images rendering takes 5% of the time.
gridPdf.PdfWebWorker.sendExportProgress(0.05);
//
let gridData = clientData['grid'];
gridData.settings.progress = (value) => {
// Change progress from 6% to 90%. Left the final 10% for the document finalization, which will start after the doc.end() method is called.
gridPdf.PdfWebWorker.sendExportProgress(map(value, 0, 1, 0.06, 0.9));
};
//
doc.ended.addHandler(() => {
// Document has been created, set progress to 100%.
gridPdf.PdfWebWorker.sendExportProgress(1);
});
//
gridPdf.FlexGridPdfConverter.draw(gridData.content, doc, null, null, gridData.settings);
//
doc.end();
});
//
function map(value, inMin, inMax, outMin, outMax) {
let k = (outMax - outMin) / (inMax - inMin);
return (value - inMin) * k + outMin;
}
//
import * as pdf from '@mescius/wijmo.pdf';
import * as gridPdf from '@mescius/wijmo.grid.pdf';
//
addEventListener('message', (e) => {
let json = e.data.grid, settings = JSON.parse(e.data.settings);
//
settings.documentOptions = settings.documentOptions || {};
//
settings.documentOptions.ended = (sender, args) => {
postProgress(1); // 100%
//
// Convert Blob to a transferable object...
let fileReader = new FileReader();
fileReader.onload = (e) => {
let buffer = e.target.result;
// ...and send it to client.
postMessage({ data: buffer }, [buffer]);
};
fileReader.readAsArrayBuffer(args.blob);
};
//
settings.progress = (value) => {
postProgress(value * 0.9); // 0% to 90%
};
//
let doc = new pdf.PdfDocument(settings.documentOptions), grid = gridPdf.PdfWebWorker.deserializeGrid(json, settings);
//
gridPdf.FlexGridPdfConverter.draw(grid, doc, null, null, settings);
//
doc.end();
});
//
function postProgress(value) {
postMessage({ type: 'progress', value: value });
}
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'jszip': 'npm:jszip/dist/jszip.js',
'@mescius/wijmo': 'npm:@mescius/wijmo/index.js',
'@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js',
'@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles',
'@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures',
'@mescius/wijmo.chart': 'npm:@mescius/wijmo.chart/index.js',
'@mescius/wijmo.chart.analytics': 'npm:@mescius/wijmo.chart.analytics/index.js',
'@mescius/wijmo.chart.animation': 'npm:@mescius/wijmo.chart.animation/index.js',
'@mescius/wijmo.chart.annotation': 'npm:@mescius/wijmo.chart.annotation/index.js',
'@mescius/wijmo.chart.finance': 'npm:@mescius/wijmo.chart.finance/index.js',
'@mescius/wijmo.chart.finance.analytics': 'npm:@mescius/wijmo.chart.finance.analytics/index.js',
'@mescius/wijmo.chart.hierarchical': 'npm:@mescius/wijmo.chart.hierarchical/index.js',
'@mescius/wijmo.chart.interaction': 'npm:@mescius/wijmo.chart.interaction/index.js',
'@mescius/wijmo.chart.radar': 'npm:@mescius/wijmo.chart.radar/index.js',
'@mescius/wijmo.chart.render': 'npm:@mescius/wijmo.chart.render/index.js',
'@mescius/wijmo.chart.webgl': 'npm:@mescius/wijmo.chart.webgl/index.js',
'@mescius/wijmo.chart.map': 'npm:@mescius/wijmo.chart.map/index.js',
'@mescius/wijmo.gauge': 'npm:@mescius/wijmo.gauge/index.js',
'@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js',
'@mescius/wijmo.grid.detail': 'npm:@mescius/wijmo.grid.detail/index.js',
'@mescius/wijmo.grid.filter': 'npm:@mescius/wijmo.grid.filter/index.js',
'@mescius/wijmo.grid.search': 'npm:@mescius/wijmo.grid.search/index.js',
'@mescius/wijmo.grid.grouppanel': 'npm:@mescius/wijmo.grid.grouppanel/index.js',
'@mescius/wijmo.grid.multirow': 'npm:@mescius/wijmo.grid.multirow/index.js',
'@mescius/wijmo.grid.transposed': 'npm:@mescius/wijmo.grid.transposed/index.js',
'@mescius/wijmo.grid.transposedmultirow': 'npm:@mescius/wijmo.grid.transposedmultirow/index.js',
'@mescius/wijmo.grid.pdf': 'npm:@mescius/wijmo.grid.pdf/index.js',
'@mescius/wijmo.grid.sheet': 'npm:@mescius/wijmo.grid.sheet/index.js',
'@mescius/wijmo.grid.xlsx': 'npm:@mescius/wijmo.grid.xlsx/index.js',
'@mescius/wijmo.grid.selector': 'npm:@mescius/wijmo.grid.selector/index.js',
'@mescius/wijmo.grid.cellmaker': 'npm:@mescius/wijmo.grid.cellmaker/index.js',
'@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js',
'@mescius/wijmo.odata': 'npm:@mescius/wijmo.odata/index.js',
'@mescius/wijmo.olap': 'npm:@mescius/wijmo.olap/index.js',
'@mescius/wijmo.rest': 'npm:@mescius/wijmo.rest/index.js',
'@mescius/wijmo.pdf': 'npm:@mescius/wijmo.pdf/index.js',
'@mescius/wijmo.pdf.security': 'npm:@mescius/wijmo.pdf.security/index.js',
'@mescius/wijmo.viewer': 'npm:@mescius/wijmo.viewer/index.js',
'@mescius/wijmo.xlsx': 'npm:@mescius/wijmo.xlsx/index.js',
'@mescius/wijmo.undo': 'npm:@mescius/wijmo.undo/index.js',
'@mescius/wijmo.interop.grid': 'npm:@mescius/wijmo.interop.grid/index.js',
'@mescius/wijmo.touch': 'npm:@mescius/wijmo.touch/index.js',
'@mescius/wijmo.cloud': 'npm:@mescius/wijmo.cloud/index.js',
'@mescius/wijmo.barcode': 'npm:@mescius/wijmo.barcode/index.js',
'@mescius/wijmo.barcode.common': 'npm:@mescius/wijmo.barcode.common/index.js',
'@mescius/wijmo.barcode.composite': 'npm:@mescius/wijmo.barcode.composite/index.js',
'@mescius/wijmo.barcode.specialized': 'npm:@mescius/wijmo.barcode.specialized/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);