jquery.gvChart-1.0.1.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * jQuery gvChart plugin
  3. * This plugin was created to simplify things when using Google Visualisation Charts.
  4. * It still needs to be used with google script import tag, however now you can
  5. * crate chart from your table.
  6. * All examples you will find on http://www.ivellios.toron.pl/technikalia/demos/gvChart/
  7. * @name jquery.gvChart-1.0.1.js
  8. * @author Janusz Kamieński - http://www.ivellios.toron.pl/technikalia
  9. * @version 1.0.1
  10. * @date December 04, 2010
  11. * @category jQuery plugin google charts
  12. * @copyright (c) 2010 Janusz Kamieński (www.ivellios.toron.pl)
  13. * @license CC Attribution Works 3.0 Poland - http://creativecommons.org/licenses/by/3.0/pl/deed.en_US
  14. * @example Visit http://www.ivellios.toron.pl/technikalia/demos/gvChart/ for more informations about this jQuery plugin
  15. */
  16. (function (jQuery){
  17. jQuery.fn.gvChart = function(settings){
  18. defaults={
  19. hideTable: true,
  20. chartType: 'AreaChart',
  21. chartDivID: 'gvChartDiv',
  22. gvSettings: null
  23. };
  24. var el = document.createElement('div');
  25. jQuery(el).insertBefore(this);
  26. gvChartCount++;
  27. gvChartID = defaults.chartDivID+gvChartCount;
  28. jQuery(el).attr('id',gvChartID);
  29. jQuery(el).addClass('gvChart');
  30. if(settings){
  31. jQuery.extend(defaults,settings);
  32. }
  33. if(defaults.hideTable)
  34. $(this).hide();
  35. var data = new google.visualization.DataTable();
  36. // add X label
  37. data.addColumn('string','X labels');
  38. var a = new Array();
  39. var headers = $(this).find('thead').find('th');
  40. var rows = $(this).find('tbody').find('tr');
  41. rows.each(function(index){
  42. data.addColumn('number',$(this).find('th').text());
  43. });
  44. data.addRows(headers.length-1);
  45. headers.each(function(index){
  46. if(index){
  47. data.setCell(index-1, 0, $(this).text());
  48. }
  49. });
  50. rows.each(function(index){
  51. $(this).find('td').each(function(index2){
  52. data.setCell(index2, index+1, parseFloat($(this).text()));
  53. });
  54. });
  55. chartSettings = {
  56. title : $(this).find('caption').text()
  57. };
  58. if(defaults.gvSettings){
  59. jQuery.extend(chartSettings,defaults.gvSettings);
  60. }
  61. eval("var chart = new google.visualization."+defaults.chartType+"(document.getElementById('"+gvChartID+"'))");
  62. chart.draw(data, chartSettings);
  63. }
  64. })(jQuery);
  65. function gvChartInit(){
  66. gvChartCount = 0;
  67. google.load('visualization', '1', {packages: ['corechart']});
  68. }