Adds ideal progress to progress graph
This commit is contained in:
Juan Salvador Pérez García
2018-07-10 10:16:54 +02:00
parent 6e75f5b494
commit 762c4faef6
11 changed files with 660 additions and 6 deletions

View File

@@ -7,20 +7,27 @@
var ProposalGraph = function(url) {
this.url = url;
this.successfulProposalDataUrl = null;
this.proposalAchievementsUrl = null;
this.targetId = null;
this.groupBy = null;
this.proposalSuccess = null;
this.progressLabel = 'Progress';
this.supportsLabel = 'Supports';
this.successLabel = 'Success';
this.chart = null;
this.goals = null;
this.achievements = null;
this.xColumnValues = null;
this.succesfulColumnValues = null;
this.progressColumnValues = null;
};
ProposalGraph.prototype.refresh = function() {
this.refreshGoals()
.then(this.refreshData.bind(this))
.then(this.refreshSuccessfulData.bind(this))
.then(this.refreshAchievements.bind(this))
.done(this.draw.bind(this));
};
@@ -62,7 +69,7 @@
ProposalGraph.prototype.parseData = function(data) {
var key;
this.xColumnValues = [ 'x' ];
this.xColumnValues = [ ];
this.progressColumnValues = [ this.progressLabel ];
for (key in data) {
@@ -73,14 +80,78 @@
}
};
ProposalGraph.prototype.refreshSuccessfulData = function() {
return $.ajax({
url: this.successfulProposalDataUrl,
cache: false,
success: function (data) {
this.parseSuccessfulProposalData(data);
}.bind(this),
data: {
group_by: this.groupBy
}
});
};
ProposalGraph.prototype.parseSuccessfulProposalData = function(data) {
var key;
this.successfulColumnValues = [ this.successLabel ];
for (key in data) {
if (data.hasOwnProperty(key)) {
this.addXColumnValue(key);
this.successfulColumnValues.push(data[key]);
}
}
};
ProposalGraph.prototype.refreshAchievements = function() {
return $.ajax({
url: this.proposalAchievementsUrl,
cache: false,
success: function (data) {
this.parseAchievements(data);
}.bind(this),
data: {
group_by: this.groupBy
}
});
};
ProposalGraph.prototype.parseAchievements = function(data) {
var group;
this.achievements = [];
for (group in data) {
if (data.hasOwnProperty(group)) {
this.addXColumnValue(group);
this.achievements.push({
value: group,
text: data[group][data[group].length - 1].title
});
}
}
};
ProposalGraph.prototype.addXColumnValue = function (value) {
if (this.xColumnValues.indexOf(value) === -1) {
this.xColumnValues.push(value);
}
}
ProposalGraph.prototype.draw = function(data) {
this.xColumnValues = this.xColumnValues.sort();
this.xColumnValues.unshift('x');
this.chart = c3.generate({
bindto: '#' + this.targetId,
data: {
x: 'x',
columns: [
this.xColumnValues,
this.progressColumnValues
this.progressColumnValues,
this.successfulColumnValues
]
},
axis: {
@@ -93,13 +164,25 @@
}
},
x: {
type: 'category'
type: 'category',
tick: {
fit: true,
culling: {
max: 15
}
}
}
},
grid: {
y: {
lines: this.goals
},
x: {
lines: this.achievements
}
},
legend: {
position: 'right'
}
});
};
@@ -107,10 +190,13 @@
$(document).ready(function () {
$('[data-proposal-graph-url]').each(function () {
var graph = new ProposalGraph($(this).data('proposal-graph-url'));
graph.successfulProposalDataUrl = $(this).data('successful-proposal-graph-url');
graph.proposalAchievementsUrl = $(this).data('proposal-achievements-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.successLabel = $(this).data('proposal-graph-success-label');
graph.proposalSuccess = parseInt($(this).data('proposal-success'), 10);
graph.refresh();