From 6d66bb59b7c9bd25e8cdd83d89b925aa607b8ec4 Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Wed, 26 Aug 2015 12:03:37 +0200 Subject: [PATCH] Reupload the lost changes in own last cherry pick --- pandora_console/include/graphs/fgraph.php | 2 +- .../include/graphs/flot/pandora.flot.js | 187 +++++++++++++++++- 2 files changed, 187 insertions(+), 2 deletions(-) diff --git a/pandora_console/include/graphs/fgraph.php b/pandora_console/include/graphs/fgraph.php index b0ead0116a..76ed029a6c 100644 --- a/pandora_console/include/graphs/fgraph.php +++ b/pandora_console/include/graphs/fgraph.php @@ -266,7 +266,7 @@ function area_graph($flash_chart, $chart_data, $width, $height, $color, $graph['font_size'] = $font_size; $graph['backgroundColor'] = $backgroundColor; $graph['unit'] = $unit; - + $id_graph = serialize_in_temp($graph, null, $ttl); // Warning: This string is used in the function "api_get_module_graph" from 'functions_api.php' with the regec patern "//" diff --git a/pandora_console/include/graphs/flot/pandora.flot.js b/pandora_console/include/graphs/flot/pandora.flot.js index a4f0aad1d5..83b6134f61 100644 --- a/pandora_console/include/graphs/flot/pandora.flot.js +++ b/pandora_console/include/graphs/flot/pandora.flot.js @@ -569,7 +569,7 @@ function pandoraFlotArea(graph_id, values, labels, labels_long, legend, }); switch (type) { - case 'line_simple': + case 'line_simple': stacked = null; filled = false; break; @@ -1035,6 +1035,183 @@ function pandoraFlotArea(graph_id, values, labels, labels_long, legend, //return '
'+v+'
'; } + // Used to export the graph data to a file. + // Uses plot, labels and labels_long as scope variables. + function exportData (options) { + options = options || {}; + + // Options + var type = options.type || 'csv'; + type = type.toLowerCase().trim(); + + var graphData, + dataObject, + dataObjects = plot.getData(), + result = []; + + // Throw errors + var retrieveDataOject = function (dataObjects) { + var result; + + if (typeof dataObjects === 'undefined') + throw new Error('Empty parameter'); + + // Try to retrieve the avg set (not 100% reliable, I know) + if (dataObjects.length == 1) { + result = dataObjects.shift(); + } + if (dataObjects.length > 1) { + dataObjects.forEach(function (element) { + if (/^Avg.:/i.test(element.label)) + result = element; + }); + + // If the avg set is missing, retrieve the first set + if (typeof result === 'undefined') + result = dataObjects.shift(); + } + + if (typeof result === 'undefined') + throw new Error('Empty result'); + + return result; + } + + // Throw errors + var processDataObject = function (dataObject) { + var result; + + if (typeof dataObject === 'undefined') + throw new Error('Empty parameter'); + + if (typeof dataObject.data === 'undefined' + || !(dataObject.data instanceof Array)) + throw new Error('Object malformed'); + + /* { + * head: [,,...,], + * data: [ + * [,,...,], + * [,,...,], + * ..., + * [,,...,], + * ] + * } + */ + if (type === 'csv') { + result = { + head: ['date', 'value'], + data: [] + }; + + dataObject.data.forEach(function (item, index) { + var date = '', value = item[1]; + + // Long labels are preferred + if (typeof labels_long[index] !== 'undefined') + date = labels_long[index]; + else if (typeof labels[index] !== 'undefined') + date = labels[index]; + + result.data.push([date, value]); + }); + } + /* [ + * { + * 'date': , + * 'value': + * } + * ], + * [ + * { + * 'date': , + * 'value': + * } + * ], + * ..., + * [ + * { + * 'date': , + * 'value': + * } + * ] + */ + else if (type === 'json') { + result = []; + + dataObject.data.forEach(function (item, index) { + var date = '', value = item[1]; + + // Long labels are preferred + if (typeof labels_long[index] !== 'undefined') + date = labels_long[index]; + else if (typeof labels[index] !== 'undefined') + date = labels[index]; + + result.push({ + 'date': date, + 'value': value + }); + }); + } + + if (typeof result === 'undefined') + throw new Error('Empty result'); + + return result; + } + + try { + dataObject = retrieveDataOject(dataObjects); + graphData = processDataObject(dataObject); + + // Transform the object data into a string + // cause PHP has limitations in the number + // of POST params received. + var graphDataStr = JSON.stringify(graphData); + + // Build form + var $form = $('
'), + $dataInput = $(''), + $typeInput = $(''), + $separatorInput = $(''), + $excelInput = $(''); + + $dataInput + .prop('name', 'data') + .prop('type', 'text') + .prop('value', graphDataStr); + + $typeInput + .prop('name', 'type') + .prop('type', 'text') + .prop('value', type); + + $separatorInput + .prop('name', 'separator') + .prop('type', 'text') + .prop('value', ';'); + + $excelInput + .prop('name', 'excel_encoding') + .prop('type', 'text') + .prop('value', false); + + $form + .prop('method', 'POST') + .prop('action', homeurl + '/include/graphs/export_data.php') + .append($dataInput, $typeInput, $separatorInput, $excelInput) + .hide() + // Firefox made me write into the DOM for this :( + .appendTo('body') + .submit(); + } + catch (e) { + alert('There was an error exporting the data'); + console.log(e); + } + } + // Prepared to turn series with a checkbox //~ $('.check_serie_'+graph_id).click(function() { //~ // Format of the id is graph_3905jf93f03_serie_id @@ -1052,6 +1229,14 @@ function pandoraFlotArea(graph_id, values, labels, labels_long, legend, $('#overview_' + graph_id).css('visibility','hidden'); }); + $('#menu_export_csv_' + graph_id).click(function() { + exportData({ type: 'csv' }); + }); + + $('#menu_export_json_' + graph_id).click(function() { + exportData({ type: 'json' }); + }); + $('#menu_threshold_' + graph_id).click(function() { datas = new Array();