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
+ } ) ;
0 commit comments