Skip to content
Snippets Groups Projects
Commit 78eb4049 authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Google Charts redraw charts after resize event

parent 53ebba7a
No related branches found
No related tags found
No related merge requests found
...@@ -2,58 +2,128 @@ ...@@ -2,58 +2,128 @@
* @file * @file
* JavaScript integration between Google and Drupal. * JavaScript integration between Google and Drupal.
*/ */
(function ($) { (function ($) {
'use strict'; 'use strict';
Drupal.googleCharts = Drupal.googleCharts || {'charts': []};
/**
* Behavior to initialize Google Charts.
*
* @type {{attach: Drupal.behaviors.chartsGooglecharts.attach}}
*/
Drupal.behaviors.chartsGooglecharts = { Drupal.behaviors.chartsGooglecharts = {
attach: function (context, settings) { attach: function (context, settings) {
// Load Google Charts API.
google.charts.load('current', {packages: ['corechart']}); google.charts.load('current', {packages: ['corechart']});
$('.charts-google').each(function (param) { $(context).find('body').once('load-google-charts').each(function () {
var chartId = $(this).attr('id'); // Re-draw charts if viewport size has been changed.
$('#' + chartId).once().each(function () { $(window).on('resize', function () {
if ($(this).attr('data-chart')) { Drupal.googleCharts.waitForFinalEvent(function () {
var dataTable = $(this).attr('data-chart'); // Re-draw Google Charts.
var googleChartOptions = $(this).attr('google-options'); Drupal.googleCharts.drawCharts(true);
var googleChartType = $(this).attr('google-chart-type'); }, 200, 'reload-google-charts');
google.charts.setOnLoadCallback(draw(googleChartType, dataTable, googleChartOptions));
}
}); });
function draw(chartType, dataTable, googleChartOptions) { });
return function () { // Draw Google Charts.
var data = google.visualization.arrayToDataTable(JSON.parse(dataTable)); Drupal.googleCharts.drawCharts();
var googleChartTypeObject = JSON.parse(chartType); }
var googleChartTypeFormatted = googleChartTypeObject.type; };
var chart;
switch (googleChartTypeFormatted) { /**
case 'BarChart': * Helper function to draw Google Charts.
chart = new google.visualization.BarChart(document.getElementById(chartId)); */
break; Drupal.googleCharts.drawCharts = function (reload) {
case 'ColumnChart': $('.charts-google').each(function () {
chart = new google.visualization.ColumnChart(document.getElementById(chartId)); var chartId = $(this).attr('id');
break; var $charts;
case 'DonutChart':
chart = new google.visualization.PieChart(document.getElementById(chartId)); if (reload === true) {
break; $charts = $('#' + chartId);
case 'PieChart': }
chart = new google.visualization.PieChart(document.getElementById(chartId)); else {
break; $charts = $('#' + chartId).once('draw-google-charts');
case 'ScatterChart': }
chart = new google.visualization.ScatterChart(document.getElementById(chartId));
break; $charts.each(function () {
case 'AreaChart': var $chart = $(this);
chart = new google.visualization.AreaChart(document.getElementById(chartId));
break; if ($chart.attr('data-chart')) {
case 'LineChart': var data = $chart.attr('data-chart');
chart = new google.visualization.LineChart(document.getElementById(chartId)); var options = $chart.attr('google-options');
} var type = $chart.attr('google-chart-type');
chart.draw(data, JSON.parse(googleChartOptions));
}; google.charts.setOnLoadCallback(Drupal.googleCharts.drawChart(chartId, type, data, options));
} }
}); });
});
};
/**
* Helper function to draw a Google Chart.
*/
Drupal.googleCharts.drawChart = function (chartId, chartType, dataTable, googleChartOptions) {
return function () {
var data = google.visualization.arrayToDataTable(JSON.parse(dataTable));
var options = JSON.parse(googleChartOptions);
var googleChartTypeObject = JSON.parse(chartType);
var googleChartTypeFormatted = googleChartTypeObject.type;
var chart;
} switch (googleChartTypeFormatted) {
case 'BarChart':
chart = new google.visualization.BarChart(document.getElementById(chartId));
break;
case 'ColumnChart':
chart = new google.visualization.ColumnChart(document.getElementById(chartId));
break;
case 'DonutChart':
chart = new google.visualization.PieChart(document.getElementById(chartId));
break;
case 'PieChart':
chart = new google.visualization.PieChart(document.getElementById(chartId));
break;
case 'ScatterChart':
chart = new google.visualization.ScatterChart(document.getElementById(chartId));
break;
case 'AreaChart':
chart = new google.visualization.AreaChart(document.getElementById(chartId));
break;
case 'LineChart':
chart = new google.visualization.LineChart(document.getElementById(chartId));
}
chart.draw(data, options);
};
}; };
/**
* Helper function to run a callback function once when triggering an event
* multiple times.
*
* Example usage:
* @code
* $(window).resize(function () {
* Drupal.googleCharts.waitForFinalEvent(function(){
* alert('Resize...');
* }, 500, "some unique string");
* });
* @endcode
*/
Drupal.googleCharts.waitForFinalEvent = (function () {
var timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout(timers[uniqueId]);
}
timers[uniqueId] = setTimeout(callback, ms);
};
})();
}(jQuery)); }(jQuery));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment