-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathview.html
229 lines (187 loc) · 7.7 KB
/
view.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<!--
FusionCharts Chart Generator HTML Application (prototype)
This application parses XML to retrieve Applicaiton configurations,
chart configurations and chart data. It communicates with
PhantomJS Application (control.js) and renders charts
Using FusionCharts Suite XT (<http://www.fusioncharts.com/>)
-->
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="FusionCharts.js"></script>
<style type="text/css">
/* Remove default padding and margin of page */
body {
margin: 0;
padding: 0;
}
/* As a fix to PhantomJS behavior that adds some space below chart
* the styles below are applied to display the chart containers
* using absolute positioning.
*/
#wrapper {
position: relative;
}
.fc-container {
position: absolute;
left: 0;
}
</style>
<script type="text/javascript">
/**
* JavaScript code relate to rendering of charts
*/
// Event listener to check when FusionCharts and page is ready
// to render charts.
FusionCharts.addEventListener("ready", function(evt, arg) {
var xmlString,
xmlDoc,
$xml,
appConfig,
vizCollection,
vizDef,
chartXML,
chartConfig = {},
chartData,
chartType,
chartId,
chartContainer,
chartWidth,
chartHeight,
chartTop = 0,
yGutter = 0,
xGutter = 0,
chartContainerCount = 0,
renderCompleteCount = 0,
renderAt,
totalCharts;
// Check if PhantomJS interface is available
if (typeof window.callPhantom === 'function') {
// Notify PhantomJS Application (control.js) that
// the page is ready to render charts
// through the `ready` property.
//
// PhantomJS returns back the Application XML (as string)
// that contains Application configurations,
// chart configurations and chart data
xmlString = window.callPhantom({ ready: true });
// Convert XML string to XML Document
xmlDoc = jQuery.parseXML(xmlString);
// Convert XML Document to jQuery object for easy parsing
$xml = jQuery(xmlDoc);
// Retrieves all attributes (Application configurations)
// of `app` node
appConfig = $xml.find('app').getAttributes();
// Pass Application configurations to PhantomJS Application
window.callPhantom({ 'appConfig': true, 'config': appConfig });
// Retrieve all `visualization` nodes from XML
// Each `visualization` node contains chart configurations
// and chart XML data (as per FusionCharts XML format) in `chart' node
vizCollection = $xml.find('visualization');
// Count the total number of charts
totalCharts = vizCollection.length;
// Listen to `rendered` event of each chart
// This event listener notifies PhantomJS Application
// that a chart has been rendered
FusionCharts.addEventListener('rendered', function (evt, arg) {
// Chart configurations
var chart = evt.sender,
id = chart.id, // chart id
chartOffset = jQuery("#"+id).offset(), // chart coordinates
top = chartOffset.top, // chart top position
left = chartOffset.left, // chart left position
width = chart.width, // chart width
height = chart.height; // chart height
// One more chart has finished rendering
renderCompleteCount++;
// Notify PhantomJS Application that the chart has been rendered.
// It can now capture the image of the chart.
window.callPhantom({
'rendered': true,
'chart': chart,
// The output filename of the image
'saveAs': chartConfig[id].output,
// Chart's rectangular coordinates with respect to page
// TO be used by PhantomJS Application to capture image
'clipRect': {
top: top,
left: left,
width: width,
height: height
},
// `allRendered` is set to true when all charts has finished rendering.
// This is a flag that asks PhantomJS Application to exit
// as no more action is required.
'allRendered': renderCompleteCount == totalCharts
});
});
// Iterate through each chart
vizCollection.each(function() {
// One more chart
chartContainerCount++;
// convert each `visualization` Node to
// jQuery object for easy access
vizDef = $(this);
/* Retrieve chart configurations */
// Chart type
chartType = vizDef.attr('type');
// Chart width
chartWidth = vizDef.attr('width');
// Chart height
chartHeight = vizDef.attr('height');
// Chart XML Data
chartXML = vizDef.find('chart, map, graph');
// Disable animation
jQuery(chartXML).attr('animation', 0);
// Chart XML data as string from `chart' node
chartData = $('<div style="display: none;">').append(chartXML).html();
// Define the id for chart and chart's container
renderAt = "chart-container-" + (chartContainerCount);
chartId = "chart-id-" + (chartContainerCount);
// Chart output filename where the captured image will be stored
chartConfig[chartId] = chartConfig[chartId] || {};
chartConfig[chartId].output = vizDef.attr('outputFilename');
// Inject dynamic chart containter (DIV element) in HTML
// The DIV is set with class name `fc-container` to apply common style
// The DIV id is set
// The DIV is set with a calculated `top` style
jQuery("#wrapper").append(
"<div class='fc-container' id='" +
renderAt +
"' style='top: " + chartTop + "px;' />"
);
// Calculate abolute position of the next container
chartTop += parseInt(chartHeight) + yGutter;
// Render chart
FusionCharts.render({
type: chartType,
width: chartWidth,
height: chartHeight,
dataSource: chartData,
dataFormat: 'xml',
id: chartId,
renderAt: renderAt
});
});
}
});
// Extend jQuery to convert XML Attributes into Object
(function($) {
$.fn.getAttributes = function() {
var attributes = {};
if (this.length) {
$.each(this[0].attributes, function(index, attr) {
attributes[attr.name] = attr.value;
});
}
return attributes;
};
})(jQuery);
</script>
</head>
<body>
<div id="wrapper">
</div>
</body>
</html>