firefly-iii/public/js/gcharts.js

102 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-05-24 11:22:41 -05:00
/* globals currencyCode, language, defaultPieChartOptions, defaultLineChartOptions, defaultColumnChartOptions, defaultBarChartOptions, defaultStackedColumnChartOptions, defaultComboChartOptions */
/* exported googleLineChart, googleBarChart, googleColumnChart, googleStackedColumnChart, googleComboChart, googlePieChart */
2015-02-06 00:23:26 -06:00
var google = google || {};
2015-05-14 06:41:21 -05:00
google.load('visualization', '1.1', {'packages': ['corechart', 'bar', 'line'],'language': language });
2015-02-06 00:23:26 -06:00
function googleChart(chartType, URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
if ($('#' + container).length === 1) {
$.getJSON(URL).success(function (data) {
/*
Get the data from the JSON
*/
2015-05-24 11:22:41 -05:00
var gdata = new google.visualization.DataTable(data);
2015-02-06 00:23:26 -06:00
/*
Format as money
*/
var money = new google.visualization.NumberFormat({
2015-03-16 12:02:52 -05:00
decimalSymbol: ',',
groupingSymbol: '.',
prefix: currencyCode + ' '
});
2015-02-06 00:23:26 -06:00
for (var i = 1; i < gdata.getNumberOfColumns(); i++) {
money.format(gdata, i);
}
/*
Create a new google charts object.
*/
2015-03-15 03:34:57 -05:00
var chart = false;
2015-02-06 00:23:26 -06:00
var options = false;
if (chartType === 'line') {
2015-03-16 12:02:52 -05:00
chart = new google.visualization.LineChart(document.getElementById(container));
2015-02-06 00:23:26 -06:00
options = options || defaultLineChartOptions;
}
if (chartType === 'column') {
2015-03-16 12:02:52 -05:00
chart = new google.visualization.ColumnChart(document.getElementById(container));
2015-02-06 00:23:26 -06:00
options = options || defaultColumnChartOptions;
}
if (chartType === 'pie') {
chart = new google.visualization.PieChart(document.getElementById(container));
options = options || defaultPieChartOptions;
}
if (chartType === 'bar') {
2015-03-16 12:02:52 -05:00
chart = new google.visualization.BarChart(document.getElementById(container));
2015-02-06 00:23:26 -06:00
options = options || defaultBarChartOptions;
}
if (chartType === 'stackedColumn') {
chart = new google.visualization.ColumnChart(document.getElementById(container));
options = options || defaultStackedColumnChartOptions;
}
if (chartType === 'combo') {
chart = new google.visualization.ComboChart(document.getElementById(container));
options = options || defaultComboChartOptions;
}
if (chart === false) {
alert('Cannot draw chart of type "' + chartType + '".');
} else {
2015-03-16 12:02:52 -05:00
chart.draw(gdata, options);
2015-02-06 00:23:26 -06:00
}
}).fail(function () {
$('#' + container).addClass('google-chart-error');
});
} else {
console.log('No container found called "' + container + '"');
}
}
function googleLineChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('line', URL, container, options);
}
2015-05-24 11:22:41 -05:00
2015-02-06 00:23:26 -06:00
function googleBarChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('bar', URL, container, options);
}
function googleColumnChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('column', URL, container, options);
}
function googleStackedColumnChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('stackedColumn', URL, container, options);
}
function googleComboChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('combo', URL, container, options);
}
function googlePieChart(URL, container, options) {
2015-05-24 11:22:41 -05:00
"use strict";
2015-02-06 00:23:26 -06:00
return googleChart('pie', URL, container, options);
}