Skip to content

Commit

Permalink
#197 Add CSV export to line chart
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Nov 21, 2017
1 parent 10fb008 commit 7627bb4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
1 change: 0 additions & 1 deletion ui/app/scripts/directives/dashboard/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
scope.colors = {};

scope.data = data;
console.log('Bar data:', scope.data);

var chart = {
data: {
Expand Down
59 changes: 48 additions & 11 deletions ui/app/scripts/directives/dashboard/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,45 @@
var labels = _.keys(response.data).map(function(d) {
return moment(d * 1).format('YYYY-MM-DD');
});
var len = labels.length;
var len = labels.length,
data = {_date: (new Array(len)).fill(0)},
rawData = {};

var columns = [];
var values = _.pluck(_.values(response.data), scope.options.field);
_.each(response.data, function(value, key) {
rawData[key] = value[scope.options.field]
});

_.each(rawData, function(value) {
_.each(_.keys(value), function(key){
data[key] = (new Array(len)).fill(0);
});
});

var i = 0;
var orderedDates = _.sortBy(_.keys(rawData));

_.each(orderedDates, function(key) {
var value = rawData[key];
data._date[i] = moment(key * 1).format('YYYY-MM-DD');

_.each(_.keys(value), function(item) {
data[item][i] = value[item];
});

i++;
});

scope.types = {};
scope.names = {};
scope.axes = {};
scope.colors = {};

_.each(scope.options.series, function(serie, index) {
var key = serie.field,
agg = serie.agg,
dataKey = agg === 'count' ? 'count' : (agg + '_' + key),
columnKey = 'agg_' + (index + 1);

columns.push([columnKey].concat(_.pluck(values, columnKey)));

scope.types[columnKey] = serie.type || 'line';
scope.names[columnKey] = serie.label || (agg === 'count' ? 'count' : (agg + ' of ' + key));
scope.axes[columnKey] = (scope.types[columnKey] === 'bar') ? 'y2' : 'y';
Expand All @@ -90,16 +112,14 @@
});
scope.groups = scope.options.stacked === true ? _.values(groups) : {};

scope.data = [
['date'].concat(labels)
].concat(columns);
scope.data = data;

console.log('Line data:', scope.data);

var chart = {
data: {
x: 'date',
columns: scope.data,
x: '_date',
json: scope.data,
names: scope.names || {},
type: scope.type || 'bar',
types: scope.types || {},
Expand Down Expand Up @@ -138,7 +158,24 @@
};

scope.getCsv = function() {

var dates = scope.data._date;
var keys = _.keys(scope.data);

// TODO update headers
var csv = [{data: _.map(keys, function(key){
return scope.names[key] || key;
}).join(';')}];

var row = [];
for(var i=0; i<dates.length; i++) {
row = _.map(keys, function(key) {
return scope.data[key][i];
});

csv.push({data: row.join(';')});
}

return csv;
};

if (scope.autoload === true) {
Expand Down

0 comments on commit 7627bb4

Please sign in to comment.