Skip to content

Instantly share code, notes, and snippets.

@peterknolle
Created January 25, 2014 22:59
Show Gist options
  • Save peterknolle/8625020 to your computer and use it in GitHub Desktop.
Save peterknolle/8625020 to your computer and use it in GitHub Desktop.
Apex Analytics API and Report Chart Component
<apex:page controller="AnalyticsController">
<style>
label {
font-weight: bold;
}
#filters {
overflow: hidden;
width: 100%
}
#filterBox {
float: left;
align: center;
padding: 5px 5px 5px 0px;
}
</style>
<apex:form >
<apex:outputLabel value="Select Report"/>
<apex:selectList value="{!reportId}" multiselect="false" size="1">
<apex:selectOptions value="{!availableReports}"/>
</apex:selectList>
<apex:commandButton action="{!getReportInfo}" value="Get Report Filters" reRender="report"/><br/>
<apex:outputPanel id="report" layout="block">
<apex:outputPanel rendered="{!reportId != null}">
<div id="filters">
<apex:repeat value="{!availableColumnFilters}" var="colFilter">
<div id="filterBox">
<apex:outputLabel >{!colFilter.label}</apex:outputLabel><br/>
<apex:selectList value="{!colFilter.operator}" size="1" multiselect="false" style="width: 100px;">
<apex:selectOption itemLabel="--None--" itemValue=""/>
<apex:selectOptions value="{!availableDataTypeFilterOperators[colFilter.dataType]}"/>
</apex:selectList>
<apex:inputText value="{!colFilter.value}"/>
</div>
</apex:repeat>
</div>
<apex:commandButton value="Get Chart with Filters" reRender="chart"/><br/>
<apex:outputPanel layout="block" id="chart">
<analytics:reportChart reportId="{!reportId}" filter="{!chartFilter}"/>
</apex:outputPanel>
</apex:outputPanel>
</apex:outputPanel>
</apex:form>
</apex:page>
public with sharing class AnalyticsController{
public List<SelectOption> availableReports { get; set; }
public Id reportId { get; set; }
public Map<String, List<SelectOption>> availableDataTypeFilterOperators { get; set; }
public List<ColumnFilter> availableColumnFilters { get; set; }
public AnalyticsController() {
availableReports = retrieveAvailableReports();
availableDataTypeFilterOperators = retrieveAvailableDataTypeFilterOperators();
}
public List<SelectOption> retrieveAvailableReports() {
List<SelectOption> reptOpts = new List<SelectOption>();
for (Report r : [
Select Id, Name
From Report
Where Format In ('Summary','Matrix')
Order By Name
]) {
reptOpts.add(new SelectOption(r.Id, r.Name));
}
return reptOpts;
}
public Map<String, List<SelectOption>> retrieveAvailableDataTypeFilterOperators() {
Map<String, List<SelectOption>> dataTypeFilterOpts = new Map<String, List<SelectOption>>();
Map<String, List<Reports.FilterOperator>> filterOperatorMap = Reports.ReportManager.getDataTypeFilterOperatorMap();
for (String dataType : filterOperatorMap.keySet()) {
List<SelectOption> operators = new List<SelectOption>();
// Append _DATA to match ColumnDataType from ReportTypeColumn
dataTypeFilterOpts.put(dataType.toUpperCase() + '_DATA', operators);
for (Reports.FilterOperator fo : filterOperatorMap.get(dataType)) {
operators.add(new SelectOption(fo.getName(), fo.getLabel()));
}
}
return dataTypeFilterOpts;
}
public PageReference getReportInfo() {
Reports.ReportDescribeResult descRes = Reports.ReportManager.describeReport(reportId);
availableColumnFilters = new List<ColumnFilter>();
for (Reports.ReportTypeColumnCategory category : descRes.getReportTypeMetadata().getCategories()) {
for (Reports.ReportTypeColumn col : category.getColumns().values()) {
if (col.getFilterable()) {
ColumnFilter cf = new ColumnFilter(
col.getLabel(),
col.getName(),
col.getDataType().name()
);
availableColumnFilters.add(cf);
}
}
}
return null;
}
public String getChartFilter() {
return JSON.serialize(getSelectedFilters());
}
private List<ColumnFilter> getSelectedFilters() {
List<ColumnFilter> selectedFilters = new List<ColumnFilter>();
for (ColumnFilter cf : availableColumnFilters) {
if (String.isNotBlank(cf.operator)) {
selectedFilters.add(cf);
}
}
return selectedFilters;
}
public class ColumnFilter {
public ColumnFilter(String lab, String col, String dt) {
label = lab;
column = col;
dataType = dt;
}
// Values needed for apex:analytics component
public String column { get; set; }
public String operator { get; set; }
public String value { get; set; }
// Values need for display and operator select list
public String label { get; set; }
public String dataType { get; set; }
}
}
@peterknolle
Copy link
Author

This is code from my blog article Apex Analytics API and Report Chart Component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment