項目中經常會用到Report以及Dashboard來分析匯總數據,Dashboard可以指定view as user,如果針對不同的用戶需要顯示其允許查看的數據,比如 根據role hierarchy來顯示數據,需要指定run as login user.但是dashboards runas the logged-in user是有數量的限制的,針對此種情況,就需要使用自定義實現Dashboard功能。
使用自定義操作可以通過apex class獲取數據,在visualforce page上畫不同組的chart,點擊chart以后跳轉到相關詳情的report頁面,但是這種情況無法處理funnel chart的情況,因為visualforce的api沒有提供funnel chart樣式的元素。
這種情況下,比較偷懶的操作為在Report上使用Role Hierarchy進行限制來對數據進行獲取,然后在Report中配置chart,使用aynalytics:reportChart傳遞需要顯示的report ids進行展示,從而實現dashboard的效果。
功能:實現自定義Dashboard,Dashboard顯示兩個chart,分別為通過Type對Account進行分組以及通過State/Province對Account分組,每個用戶只能看到當前用戶以及下級的內容。
准備工作:
1.創建Report,此Report通過Type進行分組,developername為Account_Report_By_Type
2.創建Report,此Report通過State/Province進行分組,developername為Account_By_Billing_State_Province
3.創建Dashboard,包含上面的兩個Report,datasource也分別對應上面兩個report。
准備工作結束,現在需要通過程序來實現上面的Dashboard。
1.AnalyticsReportChartController:用來獲取上述兩個report id,並放在reportIds
1 public with sharing class AnalyticsReportChartController { 2 public List<Id> reportIds{get;set;} 3 public AnalyticsReportChartController() { 4 reportIds = new List<Id>(); 5 reportIds.add(accountByTypeReportId); 6 reportIds.add(accountByStateProvinceReportId); 7 } 8 public Id accountByTypeReportId{ 9 get { 10 if(accountByTypeReportId == null) { 11 Report rt = [select id from Report where DeveloperName = 'Account_Report_By_Type' limit 1]; 12 accountByTypeReportId = rt.Id; 13 } 14 return accountByTypeReportId; 15 }set; 16 } 17 18 public Id accountByStateProvinceReportId { 19 get { 20 if(accountByStateProvinceReportId == null) { 21 Report rt = [select id from Report where DeveloperName = 'Account_By_Billing_State_Province' limit 1]; 22 accountByStateProvinceReportId = rt.Id; 23 } 24 return accountByStateProvinceReportId; 25 }set; 26 27 } 28 }
2.AnalyticsReportChart.page:實現展示兩個report的chart,點擊后跳轉到相關的report中
1 <apex:page controller="AnalyticsReportChartController"> 2 <apex:panelGrid columns="2"> 3 <apex:outputPanel id="reportPanel"> 4 <apex:repeat value="{!reportIds}" var="report"> 5 <div style="display: inline-block;width: 400px;height: 400px;vertical-align: top;"> 6 <analytics:reportChart reportId="{!report}" showRefreshButton="false" size="small" cacheResults="false"></analytics:reportChart> 7 </div> 8 </apex:repeat> 9 </apex:outputPanel> 10 </apex:panelGrid> 11 </apex:page>
效果展示:
總結:使用analytics:reportChart可以很方便的實現DashBoard的展示效果,但是此種方式僅限於Dashboard中的一個Chart對應一個Report,而不是一個Chart對應多個Report,如果出現一個Chart對應多個Report,需要創建成一對一的關系才能實現。