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 = {
styles: {
cellStyle: {
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: 'Start Date', binding: 'start', format: 'd' },
{ header: 'End Date', binding: 'end', format: 'd' },
{ header: 'Country', binding: 'country' },
{ header: 'Product', binding: 'product' },
{ header: 'Amount', binding: 'amount', format: 'c' },
{ header: 'Color', binding: 'color' },
{ header: 'Pending', binding: 'amount2', format: 'c2' },
{ header: 'Discount', binding: 'discount', format: 'p1' },
{ header: 'Active', 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: 'Hotel', value: totals.hotel },
{ name: 'Transport', value: totals.transport },
{ name: 'Meal', value: totals.meal },
{ name: 'Fuel', value: totals.fuel },
{ name: 'Misc', 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: 'Start Date', binding: 'start', format: 'd' },
{ header: 'End Date', binding: 'end', format: 'd' },
{ header: 'Country', binding: 'country' },
{ header: 'Product', binding: 'product' },
{ header: 'Amount', binding: 'amount', format: 'c' },
{ header: 'Color', binding: 'color' },
{ header: 'Pending', binding: 'amount2', format: 'c2' },
{ header: 'Discount', binding: 'discount', format: 'p1' },
{ header: 'Active', 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', '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: 'Start Date', binding: 'start', format: 'd' },
{ header: 'End Date', binding: 'end', format: 'd' },
{ header: 'Country', binding: 'country' },
{ header: 'Product', binding: 'product' },
{ header: 'Amount', binding: 'amount', format: 'c' },
{ header: 'Color', binding: 'color' },
{ header: 'Pending', binding: 'amount2', format: 'c2' },
{ header: 'Discount', binding: 'discount', format: 'p1' },
{ header: 'Active', 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 ? 'Cancel' : 'Export';
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>
This sample demonstrates the "Simple single grid export" scenario and shows how to export FlexGrid in
a background thread.
</p>
<button class="btn btn-default" id="btnExport1">Export</button>
<span id="txtProgress1">0%</span>
<div id="flexGrid1" class="grid"></div>
<hr />
<p>
This sample demonstrates the "Custom document generation" scenario and shows how to export FlexPie
(as a SVG image), FlexGrid and PNG image to a PDF in a background thread.
</p>
<p>
The <a href="https://www.npmjs.com/package/xmldom">xmldom</a> library is used here to bring DOM
support to the server to be able to draw SVG.
</p>
<button class="btn btn-default" id="btnExport2">Export</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>
This sample demonstrates the "Low-level usage of Web Workers API" scenario and shows how to export
FlexGrid to a PDF in a background thread using Web Workers API.
</p>
<button class="btn btn-default" id="btnExport3">Export</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 = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'];
const products = ['Widget', 'Gadget', 'Doohickey'];
const colors = ['Orange', 'White', 'Red', 'Green', 'Blue'];
//
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: 'Customer visit',
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 gridPdf from '@mescius/wijmo.grid.pdf';
//
self.DOMParser = xmldom.DOMParser;
//
gridPdf.PdfWebWorker.initExport((doc, clientData) => {
gridPdf.PdfWebWorker.sendExportProgress(0);
//
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);