start integrating some basic charts in form answers

pull/1/head
ansuz 3 years ago
parent 2d405b5a32
commit 73acfd9b42

@ -165,6 +165,12 @@
map[key] = (map[key] || 0) + (typeof(val) === 'number'? val: 1); map[key] = (map[key] || 0) + (typeof(val) === 'number'? val: 1);
}; };
Util.values = function (obj) {
return Object.keys(obj).map(function (k) {
return obj[k];
});
};
Util.find = function (map, path) { Util.find = function (map, path) {
var l = path.length; var l = path.length;
for (var i = 0; i < l; i++) { for (var i = 0; i < l; i++) {

@ -24,11 +24,23 @@ define([
]); ]);
}; };
Charts.row = function (text, count) {
return h('tr', h('td', {
style: '--size: ' + count,
}, text));
};
// table.charts-css.bar.reverse // table.charts-css.bar.reverse
// Charts.bars /*
Charts.bars = function (rows) {
return Charts.table([
], [
]);
};
*/
return Charts; return Charts;
}); });

@ -38,6 +38,7 @@ define([
'css!/bower_components/codemirror/addon/dialog/dialog.css', 'css!/bower_components/codemirror/addon/dialog/dialog.css',
'css!/bower_components/codemirror/addon/fold/foldgutter.css', 'css!/bower_components/codemirror/addon/fold/foldgutter.css',
'css!/lib/datepicker/flatpickr.min.css', 'css!/lib/datepicker/flatpickr.min.css',
'css!/lib/chart/charts.min.css',
'css!/bower_components/components-font-awesome/css/font-awesome.min.css', 'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
'less!/form/app-form.less', 'less!/form/app-form.less',
], function ( ], function (
@ -991,17 +992,52 @@ define([
printResults: function (answers, uid) { // XXX hackathon each question format has a 'printResults' method printResults: function (answers, uid) { // XXX hackathon each question format has a 'printResults' method
var results = []; var results = [];
var empty = 0; var empty = 0;
// XXX hackathon charts for text inputs are only useful if we tally duplicated answers var tally = {};
// XXX hackathon use horizontal bar charts https://chartscss.org/charts/bar/
Object.keys(answers).forEach(function (author) { // TODO deduplicate these? Object.keys(answers).forEach(function (author) {
var obj = answers[author]; var obj = answers[author];
var answer = obj.msg[uid]; var answer = obj.msg[uid];
if (!answer || !answer.trim()) { return empty++; } if (!answer || !answer.trim()) { return empty++; }
results.push(h('div.cp-form-results-type-text-data', answer)); Util.inc(tally, answer);
}); });
results.push(getEmpty(empty)); var counts = Util.values(tally);
var max = Math.max.apply(null, counts);
if (max < 2) { // there are no duplicates, so just return text
Object.keys(answers).forEach(function (author) {
var obj = answers[author];
var answer = obj.msg[uid];
if (!answer || !answer.trim()) { return empty++; }
results.push(h('div.cp-form-results-type-text-data', answer));
});
results.push(getEmpty(empty));
return h('div.cp-form-results-type-text', results);
}
return h('div.cp-form-results-type-text', results); // increase the scale of the bar chart if there are more empty answers than anything else
max = Math.max(max, empty);
// there are duplicates, so return a bar chart
var rows = []; // XXX
Object.keys(tally).forEach(function (answer) {
console.error('answer: %s, count: %s', answer, tally[answer]);
rows.push(Charts.row(answer, tally[answer] / max));
});
if (empty) { // XXX
rows.push(row('XXX empty', empty));
}
var table = Charts.table([
//h('caption', ''), // XXX
h('tbody', rows)
], [
'charts-css',
'bar',
'show-heading',
]);
return h('div.cp-form-results-type-text', table);
}, },
icon: h('i.cptools.cptools-form-text') icon: h('i.cptools.cptools-form-text')
}, },
@ -1139,15 +1175,37 @@ define([
if (!answer || !answer.trim()) { return empty++; } if (!answer || !answer.trim()) { return empty++; }
Util.inc(count, answer); Util.inc(count, answer);
}); });
var counts = Util.values(count);
var max = Math.max.apply(null, counts);
var rows = [];
Object.keys(count).forEach(function (text) {
rows.push(Charts.row(text, count[text] / max));
});
var table = Charts.table([
h('tbody', rows)
], [
'charts-css',
'bar',
//'show-heading',
]);
return h('div.cp-form-results-type-radio', {
style: 'width: 100%',
}, table);
/*
Object.keys(count).forEach(function (value) { Object.keys(count).forEach(function (value) {
results.push(h('div.cp-form-results-type-radio-data', [ results.push(h('div.cp-form-results-type-radio-data', [
h('span.cp-value', value), h('span.cp-value', value),
h('span.cp-count', count[value]) h('span.cp-count', count[value])
])); ]));
}); });
results.push(getEmpty(empty)); results.push(getEmpty(empty)); // XXX show number of empty answers
return h('div.cp-form-results-type-radio', results); return h('div.cp-form-results-type-radio', results);
*/
}, },
icon: h('i.cptools.cptools-form-list-radio') icon: h('i.cptools.cptools-form-list-radio')
}, },

@ -1,8 +1,7 @@
define([ define([
'/common/hyperscript.js', '/common/hyperscript.js',
'/common/inner/charts.js', '/common/inner/charts.js',
'/common/common-util.js', ], function (h, Charts) {
], function (h, Charts, Util) {
var wrap = function (content) { var wrap = function (content) {
return h('div', { return h('div', {
style: 'height: 500px; width: 500px; padding: 15px; border: 1px solid #222; margin: 15px;' style: 'height: 500px; width: 500px; padding: 15px; border: 1px solid #222; margin: 15px;'

Loading…
Cancel
Save