diff --git a/google_chart_tools.js b/google_chart_tools.js index 76f3d14..51ff311 100644 --- a/google_chart_tools.js +++ b/google_chart_tools.js @@ -6,68 +6,72 @@ // Load the Visualization API and the chart package. google.load("visualization", "1", {packages:["corechart", "gauge", "orgchart", "geochart"]}); -(function($) { - Drupal.behaviors.googleChart = { - attach: function(context, settings) { - google.setOnLoadCallback(drawChart); - // Callback that creates and populates a data table, - // instantiates the chart, passes in the data and - // draws it. - function drawChart() { - // Loop on the charts in the settings. - for (var chartId in settings.chart) { - // Create the data table. - var data = new google.visualization.DataTable(); - // OrgChart charts need a different format data table. - if (settings.chart[chartId].chartType == "OrgChart") { - data.addColumn('string', 'Title'); - data.addColumn('string', 'Parent'); - data.addColumn('string', 'ToolTip'); - for ( var i in settings.chart[chartId].rows ) { - var row = new Array(); - row = [{v:settings.chart[chartId].rows[i][0], f:settings.chart[chartId].rows[i][1]}, settings.chart[chartId].rows[i][2], settings.chart[chartId].rows[i][3]]; - data.addRows([row]); - i = parseInt(i); - data.setRowProperty(i, 'style', settings.chart[chartId].rows[i][4]); - data.setRowProperty(i, 'selectedStyle', settings.chart[chartId].rows[i][5]); - } - } - else { - data.addColumn('string', 'Label'); +GoogleChartTools = { + // Callback that creates and populates a data table, + // instantiates the chart, passes in the data and + // draws it. + drawChart: function(settings) { + // Loop on the charts in the settings. + for (var chartId in settings.chart) { + // Create the data table. + var data = new google.visualization.DataTable(); + // OrgChart charts need a different format data table. + if (settings.chart[chartId].chartType == "OrgChart") { + data.addColumn('string', 'Title'); + data.addColumn('string', 'Parent'); + data.addColumn('string', 'ToolTip'); + for ( var i in settings.chart[chartId].rows ) { + var row = new Array(); + row = [{v:settings.chart[chartId].rows[i][0], f:settings.chart[chartId].rows[i][1]}, settings.chart[chartId].rows[i][2], settings.chart[chartId].rows[i][3]]; + data.addRows([row]); + i = parseInt(i); + data.setRowProperty(i, 'style', settings.chart[chartId].rows[i][4]); + data.setRowProperty(i, 'selectedStyle', settings.chart[chartId].rows[i][5]); + } + } + else { + data.addColumn('string', 'Label'); - // Adding the colomns. - // These are graphs titles. - for (var col in settings.chart[chartId].columns) { - data.addColumn('number', settings.chart[chartId].columns[col]); - } + // Adding the colomns. + // These are graphs titles. + for (var col in settings.chart[chartId].columns) { + data.addColumn('number', settings.chart[chartId].columns[col]); + } - // Adding the headers. - // The rows titles. - for (var i in settings.chart[chartId].header) { - var row = new Array(); - // Adding the rows. - // The points of the column for each row. - for (var j in settings.chart[chartId].rows) { - row[j] = parseFloat(settings.chart[chartId].rows[j][i]); - } - row.unshift(settings.chart[chartId].header[i]); - data.addRows([row]) - }; + // Adding the headers. + // The rows titles. + for (var i in settings.chart[chartId].header) { + var row = new Array(); + // Adding the rows. + // The points of the column for each row. + for (var j in settings.chart[chartId].rows) { + row[j] = parseFloat(settings.chart[chartId].rows[j][i]); } + row.unshift(settings.chart[chartId].header[i]); + data.addRows([row]) + }; + } - // Set chart options - var options = settings.chart[chartId].options; + // Set chart options + var options = settings.chart[chartId].options; - // Instantiate and draw our chart, passing in some options. - var chart = new Object; - var element = document.getElementById(settings.chart[chartId].containerId); - if (element) { - chart[settings.chart[chartId]] = new google.visualization[settings.chart[chartId].chartType](element); - chart[settings.chart[chartId]].draw(data, options); - } - } - } + // Instantiate and draw our chart, passing in some options. + var chart = new Object; + var element = document.getElementById(settings.chart[chartId].containerId); + if (element) { + chart[settings.chart[chartId]] = new google.visualization[settings.chart[chartId].chartType](element); + chart[settings.chart[chartId]].draw(data, options); + } + } + } +}; + +(function($) { + Drupal.behaviors.googleChart = { + attach: function(context, settings) { + google.setOnLoadCallback(function() { + GoogleChartTools.drawChart(settings); + }); } }; })(jQuery); - diff --git a/google_chart_tools.module b/google_chart_tools.module index e1ca599..5322589 100644 --- a/google_chart_tools.module +++ b/google_chart_tools.module @@ -10,6 +10,14 @@ function google_chart_tools_init() { } /** + * Initialise page so that can use charts. + */ +function google_charts_tools_add_library() { + drupal_add_css(drupal_get_path('module', 'google_chart_tools') . '/google_chart_tools.css'); + drupal_add_js(drupal_get_path('module', 'google_chart_tools') . '/google_chart_tools.js'); +} + +/** * Draw the chart */ function draw_chart($settings) { @@ -19,8 +27,7 @@ function draw_chart($settings) { $settings['chart'][$id]['containerId'] = drupal_strtolower(str_replace(" ", "-", $id)); } } - drupal_add_css(drupal_get_path('module', 'google_chart_tools') . '/google_chart_tools.css'); - drupal_add_js(drupal_get_path('module', 'google_chart_tools') . '/google_chart_tools.js'); + google_charts_tools_add_library(); drupal_add_js($settings, array('type' => 'setting')); $ret = array( 'title' => $chart['options']['title'], @@ -48,4 +55,4 @@ function google_chart_tools_load_types() { // allow other projects to alter chart types drupal_alter('gct_types', $types); return $types; -} \ No newline at end of file +}