FEATURE: add graph support to admin reports

thanks to graph js, this runs latest beta cause we needed support for
smarter X axis legend
This commit is contained in:
Sam 2016-04-14 15:46:01 +10:00
parent 46487f095e
commit 4bc860652b
6 changed files with 86 additions and 18 deletions

View File

@ -0,0 +1,43 @@
import loadScript from 'discourse/lib/load-script';
export default Ember.Component.extend({
tagName: 'canvas',
refreshChart(){
const ctx = this.$()[0].getContext("2d");
const model = this.get("model");
const rawData = this.get("model.data");
var data = {
labels: rawData.map(r => r.x),
datasets: [{
data: rawData.map(r => r.y),
label: model.get('title'),
backgroundColor: "rgba(200,220,240,0.3)",
borderColor: "#08C"
}]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
scales: {
yAxes: [{
display: true,
ticks: {
suggestedMin: 0
}
}]
}
},
};
this._chart = new Chart(ctx, config);
},
didInsertElement(){
loadScript("/javascripts/Chart.min.js").then(() => this.refreshChart.apply(this));
}
})

View File

@ -4,9 +4,10 @@ import Report from 'admin/models/report';
import computed from 'ember-addons/ember-computed-decorators'; import computed from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
viewMode: 'table', queryParams: ["mode", "start-date", "end-date", "category-id", "group-id"],
viewMode: 'graph',
viewingTable: Em.computed.equal('viewMode', 'table'), viewingTable: Em.computed.equal('viewMode', 'table'),
viewingBarChart: Em.computed.equal('viewMode', 'barChart'), viewingGraph: Em.computed.equal('viewMode', 'graph'),
startDate: null, startDate: null,
endDate: null, endDate: null,
categoryId: null, categoryId: null,
@ -35,6 +36,16 @@ export default Ember.Controller.extend({
var q; var q;
this.set("refreshing", true); this.set("refreshing", true);
this.setProperties({
'start-date': this.get('startDate'),
'end-date': this.get('endDate'),
'category-id': this.get('categoryId'),
});
if (this.get('groupId')){
this.set('group-id', this.get('groupId'));
}
q = Report.find(this.get("model.type"), this.get("startDate"), this.get("endDate"), this.get("categoryId"), this.get("groupId")); q = Report.find(this.get("model.type"), this.get("startDate"), this.get("endDate"), this.get("categoryId"), this.get("groupId"));
q.then(m => this.set("model", m)).finally(() => this.set("refreshing", false)); q.then(m => this.set("model", m)).finally(() => this.set("refreshing", false));
}, },
@ -43,8 +54,8 @@ export default Ember.Controller.extend({
this.set('viewMode', 'table'); this.set('viewMode', 'table');
}, },
viewAsBarChart() { viewAsGraph() {
this.set('viewMode', 'barChart'); this.set('viewMode', 'graph');
}, },
exportCsv() { exportCsv() {

View File

@ -7,15 +7,18 @@
@module Discourse @module Discourse
**/ **/
export default Discourse.Route.extend({ export default Discourse.Route.extend({
queryParams: { mode: {}, "start-date": {}, "end-date": {}, "category-id": {}, "group-id": {}},
model: function(params) { model: function(params) {
const Report = require('admin/models/report').default; const Report = require('admin/models/report').default;
return Report.find(params.type); return Report.find(params.type, params['start-date'], params['end-date'], params['category-id'], params['group-id'])
}, },
setupController: function(controller, model) { setupController: function(controller, model) {
controller.setProperties({ controller.setProperties({
model: model, model: model,
categoryId: 'all', categoryId: (model.get('category_id') || 'all'),
groupId: model.get('group_id'),
startDate: moment(model.get('start_date')).format('YYYY-MM-DD'), startDate: moment(model.get('start_date')).format('YYYY-MM-DD'),
endDate: moment(model.get('end_date')).format('YYYY-MM-DD') endDate: moment(model.get('end_date')).format('YYYY-MM-DD')
}); });

View File

@ -18,14 +18,17 @@
<a href {{action "viewAsTable"}}>{{i18n 'admin.dashboard.reports.view_table'}}</a> <a href {{action "viewAsTable"}}>{{i18n 'admin.dashboard.reports.view_table'}}</a>
{{/if}} {{/if}}
| |
{{#if viewingBarChart}} {{#if viewingGraph}}
{{i18n 'admin.dashboard.reports.view_chart'}} {{i18n 'admin.dashboard.reports.view_graph'}}
{{else}} {{else}}
<a href {{action "viewAsBarChart"}}>{{i18n 'admin.dashboard.reports.view_chart'}}</a> <a href {{action "viewAsGraph"}}>{{i18n 'admin.dashboard.reports.view_graph'}}</a>
{{/if}} {{/if}}
</div> </div>
{{#conditional-loading-spinner condition=refreshing}} {{#conditional-loading-spinner condition=refreshing}}
{{#if viewingGraph}}
{{admin-graph model=model}}
{{else}}
<table class='table report'> <table class='table report'>
<tr> <tr>
<th>{{model.xaxis}}</th> <th>{{model.xaxis}}</th>
@ -36,16 +39,10 @@
<tr> <tr>
<td>{{row.x}}</td> <td>{{row.x}}</td>
<td> <td>
{{#if viewingTable}} {{row.y}}
{{row.y}}
{{/if}}
{{#if viewingBarChart}}
<div class='bar-container'>
<div class='bar' style="width: {{unbound row.percentage}}%">{{row.y}}</div>
</div>
{{/if}}
</td> </td>
</tr> </tr>
{{/each}} {{/each}}
</table> </table>
{{/if}}
{{/conditional-loading-spinner}} {{/conditional-loading-spinner}}

View File

@ -1986,7 +1986,7 @@ en:
30_days_ago: "30 Days Ago" 30_days_ago: "30 Days Ago"
all: "All" all: "All"
view_table: "table" view_table: "table"
view_chart: "bar chart" view_graph: "graph"
refresh_report: "Refresh Report" refresh_report: "Refresh Report"
start_date: "Start Date" start_date: "Start Date"
end_date: "End Date" end_date: "End Date"

14
public/javascripts/Chart.min.js vendored Normal file

File diff suppressed because one or more lines are too long