Skip to content

Instantly share code, notes, and snippets.

@beauwest
Created October 30, 2019 13:03
Show Gist options
  • Save beauwest/a71814e0890157a0ade5e661fe4c486b to your computer and use it in GitHub Desktop.
Save beauwest/a71814e0890157a0ade5e661fe4c486b to your computer and use it in GitHub Desktop.
FluentReportsGenerator web components example
<head>
<meta charset="utf-8">
<title>fluentReports</title>
<meta name="description" content="Node and Browser based Reporting engine">
<meta name="keywords" content="fluentReports pdf report development data driven javascript engine node node.js io.js browser chrome firefox safari">
<meta name="author" content="Nathanael Anderson">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./fluentReportsGenerator.js"></script>
<style type="text/css" media="screen">
body {
background-color: #C9C9C9;
}
report-editor {
display: block;
border: solid red 1px;
height: 300px;
width: 1136px;
}
</style>
</head>
<body>
<div>Report Generator</div>
<report-editor></report-editor>
<pre id="results"></pre>
</body>
<script>
const data = [
{
id: 1,
name: "John Doe",
emphours: [{week: 20, day: "Monday", hours: 4}, {week: 20, day: "Tuesday", hours: 8}, {
week: 20,
day: "Wednesday",
hours: 8
}, {week: 21, day: "Thursday", hours: -2}, {week: 21, day: "Friday", hours: 8}, {
week: 22,
day: "Monday",
hours: 4
}, {week: 22, day: "Tuesday", hours: 8}, {week: 22, day: "Wednesday", hours: 8}, {
week: 23,
day: "Thursday",
hours: 2
}, {week: 23, day: "Friday", hours: 8}, {week: 25, day: "Monday", hours: 4}, {
week: 25,
day: "Tuesday",
hours: 8
}, {week: 25, day: "Wednesday", hours: 8}, {week: 26, day: "Thursday", hours: 2}, {
week: 26,
day: "Friday",
hours: 8
}]
},
{id: 3, name: "Sarah Williams", emphours: [{week: 20, day: "Monday", hours: 8}]},
{
id: 5,
name: "Jane Doe",
emphours: [{week: 20, day: "Monday", hours: 5}, {week: 20, day: "Tuesday", hours: 8}, {
week: 21,
day: "Wednesday",
hours: 7
}, {week: 21, day: "Thursday", hours: 8}, {week: 21, day: "Friday", hours: 8}, {
week: 22,
day: "Monday",
hours: 5
}, {week: 22, day: "Tuesday", hours: 8}, {week: 23, day: "Wednesday", hours: 7}, {
week: 23,
day: "Thursday",
hours: 8
}, {week: 23, day: "Friday", hours: 8}, {week: 25, day: "Monday", hours: 5}, {
week: 25,
day: "Tuesday",
hours: 8
}, {week: 26, day: "Wednesday", hours: 7}, {week: 26, day: "Thursday", hours: 8}, {
week: 26,
day: "Friday",
hours: 8
}]
}];
const reportStructure = {
type: 'report',
name: 'demo20.pdf',
autoPrint: false,
fontSize: 8,
variables: {counter: 0},
finalSummary: {type: 'raw', values: ["Total Hours:", "hours", 3]},
pageHeader: {type: 'raw', values: ["Employee Hours"]},
groupBy: [{
type: 'group',
groupOn: 'name',
header: [{
type: "print",
field: 'name',
settings: {fontBold: true, fill: '#6F6F6F', textColor: '#FFFFFF', link: 'http://www.fluentReports.com/'}
}],
detail: [],
footer: [{
type: 'calculation',
op: "concat",
name: 'totals',
fields: [{text: "Totals for "}, {field: "name"}]
}, {
type: "band",
fields: [{
function: {
type: 'function',
name: 'Totals for data.name',
function: "return `Totals for ${data.name}`",
async: false
}, width: 180
}, {total: "hours", width: 100, align: 3}]
}, {type: 'newLine'}]
}],
subReport: {
type: 'report',
dataType: 'parent',
data: 'emphours',
calcs: {sum: ['hours']},
groupBy: [{
type: "group",
groupOn: "week",
header: [{
skip: true,
type: 'function',
function: "vars.counter=0;",
async: false,
name: 'counter reset'
}, {
type: 'print',
function: {type: 'function', function: 'return `Week Number: ${data.week}`', name: 'Week number: data.week'},
settings: {x: 100, addY: 2}
}],
detail: [],
footer: [{type: 'newLine'}]
}],
detail: [{type: 'function', function: "vars.counter++;", name: 'increase counter'}, {
type: 'band',
fields: [{text: '', width: 80}, {field: 'day', width: 100}, {
field: 'hours',
width: 100,
align: 3,
textColor: {type: 'function', function: "return data.hours < 0 ? '#FF0000' : '#000000';", name: 'textColor'}
}],
settings: {
border: 0,
width: 0,
wrap: true,
textColor: '#0000FF',
fill: {type: 'function', function: "return (vars.counter % 2 === 0 ? '#f0f0f0' : '#e0e0e0');", name: 'fill'}
}
}]
}
};
customElements.define('report-editor',
class extends HTMLElement {
constructor() {
super();
const container = document.createElement('div');
container.setAttribute('id', 'fluentReportsEditor');
this.attachShadow({mode: 'open'}).appendChild(container);
const frg = new FluentReportsGenerator({
id: 'fluentReportsEditor', // WebComponents would not use this
element: container, // We want to pass an element reference to attach the fRG editor to.
data: data,
report: reportStructure,
save: (value, done) => {
console.log("Saving");
const results = document.getElementById("results");
results.innerText = JSON.stringify(value, null, 4);
console.dir(value);
done();
}
});
}
}
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment