Created
May 24, 2011 21:38
-
-
Save fractastical/989774 to your computer and use it in GitHub Desktop.
@fractastical Google Chart URL Gen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The step is the distance between each label on the y axis | |
public Integer calculateStepSize(Integer highestVal) | |
{ | |
//calculates the appropriate step to display on the y axis | |
List<Integer> possibleSteps = New Integer[]{1,2,5,10,20,50}; | |
for(Integer i= (possibleSteps.size() - 1);i >= 0; i--) { | |
System.debug('HIGH DIV 6:' + (highestVAl / 6) + ' POSS STEP:' + possibleSteps[i]); | |
if ((highestVal / 6) > possibleSteps[i]) | |
return possibleSteps[i]; | |
} | |
return 1; | |
} | |
public Integer roundUpToClosestStep(Integer highestVal, Integer step) | |
{ | |
return highestVal + (step - (math.mod(highestVal,step))); | |
} | |
//The idea is that any parameter could be overwritten with a Map specifying a different value pair (hash) | |
//Data integrity should also be insured (e.g. that the length of both the data and labels are the same) | |
public String generateChartURL(String title, List<Integer> incomingData, List<String> incomingLabels, Map <String,String> valuePairs) | |
{ | |
if (incomingData.size() == 0) | |
return ''; | |
String dataString = ''; | |
Integer highest = 0; | |
for (Integer d : incomingData) { | |
if (d > highest) | |
highest = d; | |
dataString += d + ','; | |
} | |
dataString = dataString.substring(0, dataString.length() - 1); | |
Integer step = calculateStepSize(highest); | |
highest = roundUpToClosestStep(highest, step); | |
//System.debug('STEP:' + step); | |
String chartType = 'cht=bvg'; //vertical bar chart | |
String colors = 'chco=4D89F9,C6D9FD'; | |
String spacing = 'chbh=20,4,20'; | |
String chartSize = 'chs=600x200'; | |
//y axis, start value, highest value, step size | |
String YAxisRange = 'chxr=1,0,' + Highest + ',' + step; | |
String scaling = 'chds=0,' + Highest; | |
String data = 'chd=t:' + dataString; | |
String chartLabels = 'chxt=x,y&chxl=0:|'; | |
//System.debug('LABEL SIZE:' + incomingLabels.size()); | |
for (String l: incomingLabels) | |
chartLabels += l + '|'; | |
chartLabels = chartLabels.substring(0, chartLabels.length() - 1); | |
//System.debug('CHART LABEL:'+chartlabels); | |
String chartTitle = 'chtt=' + title.replaceAll(' ','+'); | |
String chartPath = 'http://chart.apis.google.com/chart?'; | |
return chartPath + chartType + '&' + ChartTitle + '&' + YAxisRange + '&' + colors + '&' + Scaling + '&' + chartLabels + '&' + spacing + '&' + Data + '&' + chartSize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment