Skip to content

Instantly share code, notes, and snippets.

@jenishshingala
Last active November 19, 2015 15:47
Show Gist options
  • Save jenishshingala/a9f920cf5cdfb825d30c to your computer and use it in GitHub Desktop.
Save jenishshingala/a9f920cf5cdfb825d30c to your computer and use it in GitHub Desktop.
StackAreaChart_Opportunity_Close_Count
<!--
* Author: Jenish Shingala
* Description: Stack Area Chart Page.
This page Shows Last 30 days Record Count of Closed Won/Lost Opportunities.
-->
<apex:page controller="opportunityStackAreaChart" sidebar="false">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--D3 Libraries-->
<apex:includeScript value="{!$Resource.d3}"/>
<apex:includeScript value="{!$Resource.nv_js}"/>
<apex:stylesheet value="{!$Resource.nv}"/>
<!--hight and width of Chart.-->
<style>
#chart svg {
height: 450px;
display: block;
width:90%;
}
</style>
<apex:form >
<!--Chart SVG-->
<div id="chart">
<svg></svg>
</div>
<h4 style="margin-left:444px;color: #166F9C;font-size:17px;font-family: initial">Last 30 Days Closed Won/Lost Opportunity Count.</h4>
</apex:form>
<script type="text/javascript">
$(document).ready(function() {
//Get JSON data from Other Visualforce page.
d3.json('/apex/opportunitystackareadatapage', function(data){
nv.addGraph(function() {
var chart = nv.models.stackedAreaChart()
.y(function(d) { return d.recordcount; })
.x(function(d) { return d.modifieddate; })
.clipEdge(false)
.useInteractiveGuideline(true)
;
chart.yAxis
.showMaxMin(false)
.tickFormat(function(d) { return d; });
chart.xAxis
.showMaxMin(false)
.tickFormat(function(d) { return d3.time.format('%x')(new Date(d)); });
d3.select('#chart svg')
.datum(data)
.transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
});
} );
</script>
</apex:page>
/*
* Author: Jenish Shingala
* Description:Stack Area Chart Controller/
* Note: Ignore my Namespace Prifix While using this Code.
*/
public class opportunityStackAreaChart {
public string jsondata{get;set;}
public List<chartdata> lstOfChartdata{get;set;}
public opportunityStackAreaChart(){
lstOfChartdata = new List<chartdata>();
//Query to get Last 30 day's Count of Closed Won Opportunity.
List<AggregateResult> lstOfWonOpportunity = [select count(aurojenish__Closed_Modified_Date__c) recordcount,
DAY_ONLY(aurojenish__Closed_Modified_Date__c) mdate from opportunity
where aurojenish__Closed_Modified_Date__c>=LAST_N_DAYS:30
AND stagename='Closed Won' group by day_only(aurojenish__Closed_Modified_Date__c)];
//Query to get Last 30 day's Count of Closed Lost Opportunity.
List<AggregateResult> lstOfLostOpportunity = [select count(aurojenish__Closed_Modified_Date__c) recordcount,
DAY_ONLY(aurojenish__Closed_Modified_Date__c) mdate from opportunity
where aurojenish__Closed_Modified_Date__c>=LAST_N_DAYS:30
AND stagename='Closed Lost' group by day_only(aurojenish__Closed_Modified_Date__c)];
AddChartData(lstOfWonOpportunity,'Closed Won');
AddChartData(lstOfLostOpportunity,'Closed Lost');
jsondata = JSON.serialize(lstOfChartdata);
}
//Method to Prepare Json data(D3 accept only JSON format).
public void AddChartData(List<AggregateResult> queryResult,String key){
Date todaysdate = Date.today();
//Map of Date and Integer to get Record Count of Each day.
Map<date,integer> mapOfDateInteger = new Map<date,Integer>();
List<OpportunityWrapper> lstOfOpportunityWrapper = new List<OpportunityWrapper>();
List<GraphDataWrapper> lstOfGraphDataWrapper = new List<GraphDataWrapper>();
//Put last 30 day dates & zero.
for(integer i=-30;i<=0;i++){
mapOfDateInteger.put(todaysdate.addDays(i),0);
}
//Fill data into Wrapper Class list.
if(queryResult!=null && queryResult.size()>0){
for(AggregateResult objResult:queryResult){
lstOfOpportunityWrapper.add(new OpportunityWrapper(objResult.get('mdate'),objResult.get('recordcount')));
}
}
//Iteration through wrapper list & if date is found in map than put its record count inplace of zero.
if(lstOfOpportunityWrapper!=null && lstOfOpportunityWrapper.size()>0){
for(OpportunityWrapper objOpportunityWrapper:lstOfOpportunityWrapper){
if(mapOfDateInteger.containsKey(objOpportunityWrapper.modifiedcloseddate)){
mapOfDateInteger.put(objOpportunityWrapper.modifiedcloseddate,objOpportunityWrapper.recordCount);
}
}
}
for(Date objDate:mapOfDateInteger.keyset()){
Datetime dt = datetime.newInstance(objDate.year(), objDate.month(),objDate.day());
lstOfGraphDataWrapper.add(new GraphDataWrapper(dt.getTime(),mapOfDateInteger.get(objDate)));
}
chartdata objChartdata = new chartdata(key,lstOfGraphDataWrapper);
lstOfChartdata.add(objChartdata);
}
//Wrapper Class to Store modifieddate & recordcount.
public class OpportunityWrapper{
public Date modifiedcloseddate{get;set;}
public Integer recordCount{get;set;}
public OpportunityWrapper(object modifiedcloseddate,object recordcount){
this.modifiedcloseddate = (date) modifiedcloseddate;
this.recordCount = (integer) recordcount;
}
}
//Wrapper Class to arrange data into D3 stack area format.
public class chartdata{
public string key{get;set;}
public List<GraphDataWrapper> values{get;set;}
public chartdata(string key,List<GraphDataWrapper> values){
this.key = key;
this.values = values;
}
}
//Wrapper to prepare Last list of Data.
public class GraphDataWrapper{
public integer recordcount{get;set;}
public long modifieddate{get;set;}
public GraphDataWrapper(object modifieddate,object recordcount){
this.recordcount = (integer) recordcount;
this.modifieddate = (long) modifieddate;
}
}
}
<!--
* Author: Jenish Shingala
* Description: Visualforce page to store JSON data & pass it to D3 JS.
-->
<apex:page controller="opportunityStackAreaChart" showHeader="false" applyBodyTag="false" applyHtmlTag="false" contentType="text/css">
{!jsondata}
</apex:page>
/*
* Author: Jenish Shingala
* Description:Trigger to update Date field when opportunity is in Close won/Lost stage.
*
*/
trigger updateclosedatetrigger on opportunity (before update) {
for(Opportunity objOpportunity:Trigger.new){
if(objOpportunity.StageName=='Closed Won' || objOpportunity.StageName=='Closed Lost'){
objOpportunity.aurojenish__Closed_Modified_Date__c=system.now();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment