Files
nairobi/app/assets/javascripts/dashboard_graphs.js
Juan Salvador Pérez García 5236fba47e Fixed locales
Fixed locale related issues
2018-07-17 17:53:18 +02:00

76 lines
1.6 KiB
JavaScript

//= require d3
//= require c3
//= require c3ext
(function(){
'use strict';
var ProposalGraph = function(url) {
this.url = url;
this.targetId = null;
this.groupBy = null;
this.progressLabel = 'Progress';
this.supportsLabel = 'Supports';
};
ProposalGraph.prototype.refresh = function() {
$.ajax({
url: this.url,
cache: false,
success: function (data) {
this.draw(data);
}.bind(this),
data: {
group_by: this.groupBy
}
});
};
ProposalGraph.prototype.draw = function(data) {
var xColumnValues = [ 'x' ],
progressColumnValues = [ this.progressLabel ],
key;
for (key in data) {
if (data.hasOwnProperty(key)) {
xColumnValues.push(key);
progressColumnValues.push(data[key]);
}
}
c3.generate({
bindto: '#' + this.targetId,
data: {
x: 'x',
columns: [
xColumnValues,
progressColumnValues
]
},
axis: {
y: {
label: {
text: this.supportsLabel,
position: 'outer-middle'
}
},
x: {
type: 'category'
}
}
});
};
$(document).ready(function () {
$('[data-proposal-graph-url]').each(function () {
var graph = new ProposalGraph($(this).data('proposal-graph-url'));
graph.targetId = $(this).attr('id');
graph.groupBy = $(this).data('proposal-graph-group-by');
graph.progressLabel = $(this).data('proposal-graph-progress-label');
graph.supportsLabel = $(this).data('proposal-graph-supports-label');
graph.refresh();
});
});
})();