@fractastical Google Chart URL Gen


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // The step is the distance between each label on the y axis
  2. public Integer calculateStepSize(Integer highestVal)
  3. {
  4. //calculates the appropriate step to display on the y axis
  5. List<Integer> possibleSteps = New Integer[]{1,2,5,10,20,50};
  6. for(Integer i= (possibleSteps.size() - 1);i >= 0; i--) {
  7. System.debug('HIGH DIV 6:' + (highestVAl / 6) + ' POSS STEP:' + possibleSteps[i]);
  8. if ((highestVal / 6) > possibleSteps[i])
  9. return possibleSteps[i];
  10. }
  11. return 1;
  12. }
  13.  
  14. public Integer roundUpToClosestStep(Integer highestVal, Integer step)
  15. {
  16. return highestVal + (step - (math.mod(highestVal,step)));
  17. }
  18.  
  19. //The idea is that any parameter could be overwritten with a Map specifying a different value pair (hash)
  20. //Data integrity should also be insured (e.g. that the length of both the data and labels are the same)
  21. public String generateChartURL(String title, List<Integer> incomingData, List<String> incomingLabels, Map <String,String> valuePairs)
  22. {
  23. if (incomingData.size() == 0)
  24. return '';
  25.  
  26. String dataString = '';
  27. Integer highest = 0;
  28. for (Integer d : incomingData) {
  29. if (d > highest)
  30. highest = d;
  31. dataString += d + ',';
  32. }
  33. dataString = dataString.substring(0, dataString.length() - 1);
  34.  
  35. Integer step = calculateStepSize(highest);
  36. highest = roundUpToClosestStep(highest, step);
  37. //System.debug('STEP:' + step);
  38.  
  39. String chartType = 'cht=bvg'; //vertical bar chart
  40. String colors = 'chco=4D89F9,C6D9FD';
  41. String spacing = 'chbh=20,4,20';
  42. String chartSize = 'chs=600x200';
  43.  
  44. //y axis, start value, highest value, step size
  45. String YAxisRange = 'chxr=1,0,' + Highest + ',' + step;
  46. String scaling = 'chds=0,' + Highest;
  47. String data = 'chd=t:' + dataString;
  48.  
  49. String chartLabels = 'chxt=x,y&chxl=0:|';
  50.  
  51. //System.debug('LABEL SIZE:' + incomingLabels.size());
  52. for (String l: incomingLabels)
  53. chartLabels += l + '|';
  54. chartLabels = chartLabels.substring(0, chartLabels.length() - 1);
  55.  
  56. //System.debug('CHART LABEL:'+chartlabels);
  57.  
  58. String chartTitle = 'chtt=' + title.replaceAll(' ','+');
  59.  
  60.  
  61. String chartPath = 'http://chart.apis.google.com/chart?';
  62. return chartPath + chartType + '&' + ChartTitle + '&' + YAxisRange + '&' + colors + '&' + Scaling + '&' + chartLabels + '&' + spacing + '&' + Data + '&' + chartSize;
  63. }
  64.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.