-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVFChartingExample.vfp
More file actions
371 lines (313 loc) · 17.4 KB
/
VFChartingExample.vfp
File metadata and controls
371 lines (313 loc) · 17.4 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<apex:page standardController="Report" extensions="VFChartingHelper" sidebar="false" >
<apex:includeScript value="{!$Resource.HelperFunctionsJS}"/>
<apex:includeScript value="{!$Resource.ReportFiltersJS}"/>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<apex:stylesheet value="{!$Resource.ReportStyle}"/>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart", "table"]});
google.setOnLoadCallback(function() {
init();
drawChart(null)
});
function init() {
// Uses the Reporting API to obtain information on the report and builds available filters based on this data.
var reportData = getReportInfo('{!$Api.Session_ID}', '{!$CurrentPage.parameters.id}');
// Some shortcut variables here
var aggregateInfo = reportData.reportExtendedMetadata.aggregateColumnInfo;
var aggregates = reportData.reportMetadata.aggregates;
createFilters(reportData);
createValueSelect(aggregateInfo, aggregates);
// Add some event listeners below
$('#submit').click(function(e) {
// Place holder for the text that will display active filters
var activeFiltersHtml = '';
// Reset the array
filtersArray.length = 0;
$.each($('div.filter'), function(k, v) {
var selectedValues = $(this).find('.value').val();
if(selectedValues && selectedValues != '' && selectedValues != 'CUSTOM') {
var fieldName = $(this).find('.value').attr('id');
var oper = $(this).find('.operator').val();
// Concatenate multiselect values
if (typeof(selectedValues) != "string") {
selectedValues = selectedValues.join(",");
}
// Add the filter label and values for display
activeFiltersHtml += $('label[for=\'' + fieldName + '\']').text().slice(0, -2) + ' ' + oper + ' ' + selectedValues + '<br />';
// Add the filters to the JSON array for submitting
filtersArray.push({'column':fieldName.substring(14),'operator':oper,'value':selectedValues});
}
});
// Update the page with the active filters list
$('#activeFilters').html(activeFiltersHtml);
var aggregateField = Number($('#reportDataField').val());
drawChart(filtersArray, aggregateField);
});
}
// Primary function
function drawChart(filters, aggregateField = 0) {
// Set filters here if any filters are passed in as parameters
if(filters != null) {
var metadata = {
reportMetadata : {
reportFilters : filters
}
};
}
// Fetch the JSON data representing the report
var jsonData = JSON.parse($.ajax({
type:'POST',
url: '/services/data/v29.0/analytics/reports/{!$CurrentPage.parameters.id}',
data: JSON.stringify(metadata),
beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', 'Bearer {!$Api.Session_ID}');},
dataType:'json',
contentType: 'application/json; charset=utf-8',
async: false
}).responseText);
console.log(jsonData);
// Some shortcut variables here
var aggregateInfo = jsonData.reportExtendedMetadata.aggregateColumnInfo;
var groupingInfo = jsonData.reportExtendedMetadata.groupingColumnInfo;
var groupingsDown = jsonData.reportMetadata.groupingsDown;
var groupingsAcross = jsonData.reportMetadata.groupingsAcross;
var aggregates = jsonData.reportMetadata.aggregates;
// create the array using the JSON data and report
/* var dataArray = [[
groupingInfo[groupingsDown[0].name].label,
aggregateInfo[aggregates[0]].label,
aggregateInfo[aggregates[1]].label
]];
// Iterate through summary data
$.each(jsonData.groupingsDown.groupings, function(indexLevel1, groupingLevel1) {
dataArray.push([
groupingLevel1.label,
jsonData.factMap[indexLevel1.toString()+ "!T"].aggregates[0].value,
jsonData.factMap[indexLevel1.toString()+ "!T"].aggregates[1].value,
]);
}); */
// create the array using the JSON data and report
var dataArray = [[
groupingInfo[groupingsDown[0].name].label,
groupingInfo[groupingsAcross[0].name].label,
aggregateInfo[aggregates[0]].label,
aggregateInfo[aggregates[1]].label
]];
dataArray.length = 0;
var newArray = [];
newArray.push(groupingInfo[groupingsDown[0].name].label);
$.each(jsonData.groupingsAcross.groupings, function(indexLevel2, groupingLevel2) {
newArray.push(groupingLevel2.label);
});
dataArray.push(newArray);
// Iterate through summary data
$.each(jsonData.groupingsDown.groupings, function(indexLevel1, groupingLevel1) {
var tempArray = [];
tempArray.push(groupingLevel1.label);
$.each(jsonData.groupingsAcross.groupings, function(indexLevel2, groupingLevel2) {
tempArray.push(jsonData.factMap[indexLevel1.toString()+"!"+indexLevel2.toString()].aggregates[0].value);
});
dataArray.push(tempArray);
});
// Iterate through summary data
/*$.each(jsonData.groupingsDown.groupings, function(indexLevel1, groupingLevel1) {
$.each(jsonData.groupingsAcross.groupings, function(indexLevel2, groupingLevel2) {
dataArray.push([
groupingLevel1.label,
groupingLevel2.label,
jsonData.factMap[indexLevel1.toString()+"!"+indexLevel2.toString()].aggregates[0].value,
jsonData.factMap[indexLevel1.toString()+"!"+indexLevel2.toString()].aggregates[1].value,
]);
});
}); */
console.log(dataArray);
// build the dataTable and set Chart options
var data = google.visualization.arrayToDataTable(dataArray);
var data1 = google.visualization.arrayToDataTable([
['Series', '2013', '2014'],
['800D2', 1000, 2222],
['800D2', 1170, 2222],
['A600', 660, 2333],
['A600', 1030, 2322]
]);
console.log(data);
console.log(data1);
// Set number format as needed
var hAxisFormat = '';
if (aggregateInfo[aggregates[aggregateField]].dataType == 'currency') {
hAxisFormat = '¤###,###,###;(¤###,###,###)';
}
var options = {
title: jsonData.attributes.reportName,
isStacked: true,
//vAxis: {title: groupingInfo[groupingsDown[0].name].label},
//hAxis: {
// title: aggregateInfo[aggregates[aggregateField]].label,
// format: hAxisFormat
// }
};
// Check and format any Currency fields as needed
//data = formatCurrency(jsonData, data);
// Set a dataView with a subset of the data to be used in the Chart
var view = new google.visualization.DataView(data);
//view.setColumns([0, aggregateField + 1]);
//view.setColumns([1, 2, 3]);
// Create and draw the chart
var chart = new google.visualization.BarChart(document.getElementById('chart'));
chart.draw(data, options);
}
function init_old() {
/*
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.VFChartingHelper.getRecordTypeIds}',
function(result,event) {
console.log(result);
}); */
//getRecordTypeIds();
//return;
// Uses the Tooling API to query for the custom field IDs, this is needed for custom URL filters if running the report this way
getCustomFieldIds('{!$Api.Session_ID}');
// Uses the Reporting API to obtain information on the report and builds available filters based on this data.
var reportData = getReportInfo('{!$Api.Session_ID}', '{!$CurrentPage.parameters.id}');
createFilters(reportData);
// Add some event listeners below
$('#submit').click(function(e) {
// Place holder for the text that will display active filters
var activeFiltersHtml = '';
// Reset the array
filtersArray.length = 0;
$.each($('div.filter'), function(k, v) {
var selectedValues = $(this).find('.value').val();
if(selectedValues && selectedValues != '' && selectedValues != 'CUSTOM') {
var fieldName = $(this).find('.value').attr('id');
var oper = $(this).find('.operator').val();
// Concatenate multiselect values
if (typeof(selectedValues) != "string") {
selectedValues = selectedValues.join(",");
}
// Add the filter label and values for display
activeFiltersHtml += $('label[for=\'' + fieldName + '\']').text().slice(0, -2) + ' ' + oper + ' ' + selectedValues + '<br />';
// Add the filters to the JSON array for submitting
filtersArray.push({'column':fieldName.substring(14),'operator':oper,'value':selectedValues});
}
});
// Update the page with the active filters list
$('#activeFilters').html(activeFiltersHtml);
drawChart(filtersArray);
});
$('#runreport').click(function(e) {
// This holds the URL parameters
var filtersList = '?';
$.each($(filtersArray), function(k, v) {
// If there is a period in the fieldname it is a custom field and need to lookup its ID to use in the URL
if ( v.column.indexOf('.') !== -1 ) {
// Lookup the ID and create the URL
var customField = getObjects(fieldIds, 'FullName', v.column);
filtersList += 'pc'+k+'='+customField[0].Id.substring(0, 15)+'&pn'+k+'='+'eq'+'&pv'+k+'='+v.value+'&';
} else if ( v.column.indexOf('RECORDTYPE') !== -1 ) {
//getRecordTypeIds();
} else {
filtersList += 'pc'+k+'='+v.column+'&pn'+k+'='+'eq'+'&pv'+k+'='+v.value+'&';
}
});
url = '/{!$CurrentPage.parameters.id}' + filtersList;
window.open(url,'_blank');
});
}
// Primary function
function drawChart_old(filters) {
// Set filters here if any filters are passed in as parameters
if(filters != null) {
var metadata = {
reportMetadata : {
reportFilters : filters
}
};
}
// Fetch the JSON data representing the report
var jsonData = JSON.parse($.ajax({
type:'POST',
url: '/services/data/v29.0/analytics/reports/{!$CurrentPage.parameters.id}',
data: JSON.stringify(metadata),
beforeSend: function(xhr) {xhr.setRequestHeader('Authorization', 'Bearer {!$Api.Session_ID}');},
dataType:'json',
contentType: 'application/json; charset=utf-8',
async: false
}).responseText);
console.log(jsonData);
// create the array using the JSON data and report
var dataArray = [[
jsonData.reportExtendedMetadata.groupingColumnInfo[jsonData.reportMetadata.groupingsDown[0].name].label,
jsonData.reportExtendedMetadata.aggregateColumnInfo[jsonData.reportMetadata.aggregates[0]].label,
jsonData.reportExtendedMetadata.aggregateColumnInfo[jsonData.reportMetadata.aggregates[1]].label
]];
// Iterate through summary data
$.each(jsonData.groupingsDown.groupings, function(indexLevel1, groupingLevel1) {
dataArray.push([
groupingLevel1.label,
jsonData.factMap[indexLevel1.toString()+ "!T"].aggregates[0].value,
jsonData.factMap[indexLevel1.toString()+ "!T"].aggregates[1].value,
]);
});
// build the dataTable and set Chart options
var data = google.visualization.arrayToDataTable(dataArray);
var options = {
title: jsonData.attributes.reportName,
vAxis: {title: jsonData.reportExtendedMetadata.groupingColumnInfo[jsonData.reportMetadata.groupingsDown[0].name].label},
hAxis: {
title: jsonData.reportExtendedMetadata.aggregateColumnInfo[jsonData.reportMetadata.aggregates[1]].label,
format: '¤###,###,###;(¤###,###,###)'
}
};
// Check and format any Currency fields as needed
data = formatCurrency(jsonData, data);
// Set a dataView with a subset of the data to be used in the Chart
var view = new google.visualization.DataView(data);
view.setColumns([0, 2]);
// Create and draw the chart
var chart = new google.visualization.BarChart(document.getElementById('chart'));
chart.draw(view, options);
// Calculate totals for the Table total row
footer = [['Total', 1],[getSum(data, 1), 1],[getSum(data, 2),1]];
//data.addRow(['Total', getSum(data, 1), getSum(data, 2)]);
//formatter.format(data, 2);
var tableView = new google.visualization.DataView(data);
var table = new google.visualization.Table(document.getElementById('table'));
// Add event listeners to the Table to add the footer (total) row.
google.visualization.events.addListener(table, 'ready', addFooter);
google.visualization.events.addListener(table, 'sort', addFooter);
google.visualization.events.addListener(table, 'page', addFooter);
table.draw(tableView, {sortColumn: 1, allowHtml: true});
};
function getRecordTypeIds() {
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.VFChartingHelper.getRecordTypeIds}',
function(result,event) {
console.log(result);
});
}
/*function getFieldData(field) {
$('#output').html('<div class="loading">Loading...</div>');
//var weeks = new Array();
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.VFChartingHelper.getFieldMetaData}',
field,
function(result,event) {
result = result.replace(/&(lt|gt|quot);/g, '"');
var parsedResult = jQuery.parseJSON(result);
console.log(parsedResult);
//weeks = result;
});
} */
</script>
<div id="filtersList"></div>
<div id="filters"></div>
<div id="values"></div>
<button id="submit">Submit</button>
<button id="runreport" style="display:none">Run Report</button>
<div id="activeFilters"></div>
<div id="chart" style="width: 900px; height: 500px;"></div>
<div id="table"></div>
</apex:page>