googleChart.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* global google, angular */
  2. angular.module("robware").directive('googleChart', function() {
  3. return {
  4. restrict: 'E',
  5. templateUrl: '/scripts/directives/templates/googleChart.html',
  6. scope: {
  7. data: '=',
  8. headings: '=?',
  9. ignore: '=?',
  10. height: '='
  11. },
  12. link: function(scope, element, attributes) {
  13. var chartLoaded = false;
  14. if (!scope.headings)
  15. scope.headings = [];
  16. if (!scope.ignore)
  17. scope.ignore = [];
  18. var chartData, chart;
  19. var chartOptions = {
  20. title: attributes.title,
  21. height: scope.height || 400,
  22. curveType: attributes.curveType,
  23. legend: {
  24. position: attributes.legendPosition || 'right'
  25. },
  26. vAxis: {
  27. title: attributes.vAxisTitle
  28. },
  29. backgroundColor: attributes.background || 'transparent'
  30. };
  31. function formatToolTip(input, args) {
  32. return input.replace(/{(.+?)(:.*?)?}/g, function(match, index, format) {
  33. if (format)
  34. format = format.slice(1);
  35. if (args[index]===undefined)
  36. return match;
  37. if (isNumber(args[index]))
  38. return args[index].toString(format, true);
  39. return args[index].toString(format);
  40. });
  41. }
  42. function shouldIgnore(value){
  43. return scope.ignore.indexOf(value)>-1;
  44. }
  45. function setupChart() {
  46. if (!chartLoaded)
  47. return;
  48. var firstRow = scope.data[0];
  49. var fields = {};
  50. for (var propertyName in firstRow) {
  51. if (shouldIgnore(propertyName))
  52. continue;
  53. var datum = firstRow[propertyName];
  54. var type = typeof (datum);
  55. if (type === "object")
  56. type = datum.constructor.name;
  57. fields[propertyName] = {
  58. title: scope.headings[propertyName] || propertyName,
  59. type: type.toLowerCase()
  60. };
  61. }
  62. chartData = new google.visualization.DataTable();
  63. angular.forEach(fields, function(value) {
  64. chartData.addColumn(value.type, value.title);
  65. if (attributes['toolTip' + value.title.upperCaseFirst()])
  66. chartData.addColumn({type: 'string', role: 'tooltip'});
  67. });
  68. angular.forEach(scope.data, function(value) {
  69. var row = [];
  70. angular.forEach(value, function(value2, index) {
  71. if (shouldIgnore(index))
  72. return;
  73. row.push(value2);
  74. var attrIndex = 'toolTip' + index.upperCaseFirst();
  75. if (attributes[attrIndex])
  76. row.push(formatToolTip(attributes[attrIndex], value));
  77. });
  78. chartData.addRow(row);
  79. });
  80. chart = new google.visualization.LineChart(element[0]);
  81. drawChart();
  82. }
  83. function drawChart() {
  84. chart.draw(chartData, chartOptions);
  85. }
  86. $.getScript("https://www.google.com/jsapi", function() {
  87. //google.load('visualization', '1.1', {packages: ['line'], callback:function(){
  88. google.load('visualization', '1', {
  89. packages: ['corechart'],
  90. callback: function() {
  91. chartLoaded = true;
  92. setupChart();
  93. }
  94. });
  95. });
  96. scope.$watch("data", function(v) {
  97. setupChart();
  98. });
  99. angular.element(window).bind('resize', function() {
  100. drawChart();
  101. });
  102. }
  103. };
  104. });