Skip to content

Commit 11b1479

Browse files
committed
added a SVG export example
1 parent c0b1af2 commit 11b1479

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,8 @@ usage:
501501
}
502502
}
503503
```
504+
505+
506+
## Further Links
507+
508+
- [Examples](examples/README.md)

examples/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# js-gantt (Examples)
2+
[![Bower version](https://badge.fury.io/bo/js-gantt.svg)](https://badge.fury.io/bo/js-gantt)
3+
[![npm version](https://badge.fury.io/js/js-gantt.svg)](https://badge.fury.io/js/js-gantt)
4+
[![Build Status](https://travis-ci.org/pmeisen/js-gantt.svg?branch=master)](https://travis-ci.org/pmeisen/js-gantt)
5+
6+
This folder contains examples created to illustrate some of the possibilities.
7+
8+
### Creating SVG from nodeJS
9+
10+
Yes it is possible to create Gantt-Charts, i.e.,
11+
SVG files (and also convert these to, e.g., PNG) within nodeJS. I personally
12+
only use the library within a browser, so the example just shows what may be
13+
possible. The examples utilizes *phantomJs* to render the SVG in the headless
14+
browser, retrieves the created SVG and stores it in a file. There are some
15+
things to be considered to make this example more usable:
16+
17+
1. It may be nicer to push the options, as well as the width and height
18+
through nodeJs into the JavaScript. Currently the *options* are also
19+
defined in the JavaScript part. The main reason is that the *js-gantt*
20+
library only supports *Date* instances out of the box. The options coming
21+
from nodeJs are stringified (using *JSON*), which does not support *Date*
22+
types.
23+
2. The rendering of the SVG may take some milliseconds (or seconds if large
24+
external sources are used). The *js-gantt* chart provides events to know
25+
when the rendering is finished. Nevertheless, in the example the script
26+
just waits a couple of seconds. In a nicer version, nodeJs would utilize
27+
the events provided by the library.
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./node_modules/**/*
2+
./output/**/*
+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
var phantom = require('phantom');
2+
var fs = require('fs');
3+
4+
// we keep the instance to close it at the end of the pipe
5+
var phInstance = null;
6+
var svg = null;
7+
8+
phantom.create()
9+
.then(instance => {
10+
phInstance = instance;
11+
return instance.createPage();
12+
})
13+
14+
// load an empty page
15+
.then(page => {
16+
return page.open('about:blank').then(status => {
17+
if (status === 'success') {
18+
return page;
19+
} else {
20+
throw new Error('Unable to load blank page.');
21+
}
22+
})
23+
})
24+
25+
// inject some scripts
26+
.then(page => {
27+
return page.includeJs('https://rawgit.com/pmeisen/js-gantt/master/dist/js-gantt.min.js').then(() => {
28+
return page;
29+
});
30+
})
31+
32+
// add the SVG with the specified options
33+
.then(page => {
34+
return page.evaluate(function () {
35+
var chartEl = document.createElement('div');
36+
chartEl.id = 'chart';
37+
document.body.appendChild(chartEl);
38+
39+
var chart = new GanttChart();
40+
chart.init(chartEl, {
41+
data: {
42+
url: 'https://rawgit.com/pmeisen/js-gantt/master/resources/sample-data-array.json',
43+
postProcessor: function (data) {
44+
var f = 'dd.MM.yyyy HH:mm:ss';
45+
for (var i = 0; i < data.length; i++) {
46+
var record = data[i];
47+
record[1] = GanttChart.DateUtil.parseString(record[1], f);
48+
record[2] = GanttChart.DateUtil.parseString(record[2], f);
49+
}
50+
51+
return {
52+
names: ['caller', 'start', 'end',
53+
'duration', 'recipient', 'origin',
54+
'destination', 'ratepermin', 'costs',
55+
'origincontinent', 'destinationcontinent',
56+
'callergender', 'recipientgender'],
57+
records: data
58+
};
59+
},
60+
mapper: {
61+
startname: 'start',
62+
endname: 'end',
63+
group: ['callergender', 'recipientgender'],
64+
label: ['callergender', 'start', 'caller'],
65+
tooltip: ['caller', 'recipient', 'start', 'end']
66+
},
67+
timeaxis: {
68+
start: GanttChart.DateUtil.createUTC(2014, 6, 11),
69+
end: GanttChart.DateUtil.createUTC(2014, 6, 11, 23, 59, 0),
70+
granularity: 'mi'
71+
}
72+
},
73+
illustrator: {
74+
config: {
75+
axis: {
76+
viewSize: 1440,
77+
tickInterval: 360
78+
},
79+
view: {
80+
showBorder: false,
81+
tooltip: '{1} called {2}\nbetween {3|date|HH:mm} - {4|date|HH:mm})',
82+
coloring: {
83+
groupMapping: {
84+
'["Female","Female"]': '#7cb5ec',
85+
'["Male","Female"]': '#434348',
86+
'["Female","Male"]': '#f7a35c',
87+
'["Male","Male"]': '#90ed7d'
88+
}
89+
},
90+
theme: {
91+
intervalColor: '#444444',
92+
intervalHeight: 10,
93+
intervalBorderSize: 0
94+
}
95+
},
96+
scrollbars: {
97+
vertical: {
98+
hideOnNoScroll: true
99+
},
100+
horizontal: {
101+
hideOnNoScroll: true
102+
}
103+
}
104+
}
105+
}
106+
});
107+
chart.resize(600, 300);
108+
}).then(() => {
109+
return page;
110+
});
111+
})
112+
113+
// wait a little depending on the options that should be changed
114+
.then(page => {
115+
return new Promise(f => setTimeout(f, 2000)).then(() => page);
116+
})
117+
118+
// read the page after the full content is loaded
119+
.then(page => {
120+
return page.evaluate(function () {
121+
return document.getElementsByClassName('ganttview')[0].innerHTML;
122+
}).then(chart => {
123+
svg = chart;
124+
return page;
125+
});
126+
})
127+
128+
// clean up when we are done
129+
.then(() => {
130+
phInstance.exit();
131+
})
132+
133+
// write the file
134+
.then(() => {
135+
try {
136+
fs.mkdirSync("./output");
137+
} catch (err) {
138+
if (err.code !== 'EEXIST') throw err
139+
}
140+
141+
fs.writeFileSync("./output/sample.svg", svg);
142+
})
143+
144+
// in any case of an error log it
145+
.catch(error => {
146+
console.log(error);
147+
phInstance.exit();
148+
});
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "js-gantt-export-svg-example",
3+
"version": "1.0.0",
4+
"description": "This is an example showing how to use nodeJs and js-gantt to export a Gantt-Chart to SVG.",
5+
"main": "example.js",
6+
"scripts": {
7+
"start": "node example.js"
8+
},
9+
"authors": [
10+
"Philipp Meisen <[email protected]>"
11+
],
12+
"private": false,
13+
"license": "MIT",
14+
"dependencies": {
15+
"phantom": "^3.2.1"
16+
}
17+
}

js-gantt.iml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
99
<excludeFolder url="file://$MODULE_DIR$/bower_components" />
1010
<excludeFolder url="file://$MODULE_DIR$/dist" />
11+
<excludeFolder url="file://$MODULE_DIR$/examples/exportSvgWithNodeJs/node_modules" />
12+
<excludeFolder url="file://$MODULE_DIR$/examples/exportSvgWithNodeJs/output" />
1113
<excludeFolder url="file://$MODULE_DIR$/node_modules" />
1214
<excludeFolder url="file://$MODULE_DIR$/server/lib" />
1315
<excludeFolder url="file://$MODULE_DIR$/server/testLib" />

0 commit comments

Comments
 (0)