Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
charts
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drupal.org
charts
Commits
78eb4049
Commit
78eb4049
authored
7 years ago
by
Daniel Cothran
Browse files
Options
Downloads
Patches
Plain Diff
Google Charts redraw charts after resize event
parent
53ebba7a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/charts_google/js/charts_google.js
+111
-41
111 additions, 41 deletions
modules/charts_google/js/charts_google.js
with
111 additions
and
41 deletions
modules/charts_google/js/charts_google.js
+
111
−
41
View file @
78eb4049
...
@@ -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
'
Bar
Chart
'
:
* Helper function to draw Google
Chart
s.
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
chart
Id
=
$
(
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
'
LineC
hart
'
:
var
data
=
$chart
.
attr
(
'
data-c
hart
'
);
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
));
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment