User Tools

Site Tools


download-csv-js

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
download-csv-js [2022/05/10 11:40] emozolyakdownload-csv-js [2022/05/12 08:35] – [Button release script] emozolyak
Line 1: Line 1:
-====== Example of graph data download with JS for dashbaord ======+====== Example of graph data download with JS for dashboard ======
  
 The standard method of exporting data from graphs to XLS, HTML, CSV files has the resolution of the selected view on the respective graph, not all data - due to performance issues.  The standard method of exporting data from graphs to XLS, HTML, CSV files has the resolution of the selected view on the respective graph, not all data - due to performance issues. 
Line 6: Line 6:
 In the following example, dashboards' JS scripting option is presented.  In the following example, dashboards' JS scripting option is presented. 
  
 +===== Dashboard layout =====
 +There are 4 control elements on the dashboard you must provide: 
 +{{ ::api:js-csv-dash.png?direct |}}
 +  -Start time date picker 
 +  -End time date picker
 +  -Button for invoking the JS script
 +  -Dictionary element for selecting one of the many graphs you may have. E.g. if you have graphs with IDs of 1, 2, 3... you have to create a dictionary of 1, 2, 3.... and bind it with the register that will be used for the element on the dashboard. 
 +
 +===== Script =====
 +
 +Put the following script into the 'Script' tab of the dashboard properties (w/o comments)
 +
 +{{ :api:js-csv-dash-script-tab.png?direct&800 |}}
 +
 +==== Script text ====
  
 <code javascript> <code javascript>
-const DATA = { +const DATA = {  // constant names for accessing the dashboard selection 
-         TIME: {START: 7, END: 8}, +         TIME: {START: 7, END: 8},   // start and end time register ids 
-         DOWNLOAD_BTN: 9, +         DOWNLOAD_BTN: 9,            // register to detect if the button was pressed 
-         GRAPH_ID: 1, +         GRAPH_ID: 1,                // placeholder for the graph id that  
-         GRAPH_SELECTION_REG: 11+         GRAPH_SELECTION_REG: 11     // will be selected by selection register
 }; };
-const MAX_SLICES = 2999;+const MAX_SLICES = 2999;             // max number of the slices in the graph data api call
 const TIMEOUT = 5e3; const TIMEOUT = 5e3;
-const START_GRAPH_ID = 1;  +const START_GRAPH_ID = 1;            // start number of the range for graphs ids 
-const END_GRAPH_ID = 2; +const END_GRAPH_ID = 2;             // end number 
  
 let _i; let _i;
Line 129: Line 144:
 } }
  
 +</code>
 +
 +==== Button release script ====
 +
 +Because the dashboard expects the register change be greater than 0, we have to reset this register after each use. 
 +The following script can do that:
 +<code lua>
 +BUTTON_REG_ID = 9
 +
 +function main (userId)
 +    
 +    local cur_value = R(BUTTON_REG_ID)
 +    
 +    if (not resetDemand and cur_value > 0) then 
 +        resetDemand = true                        -- postpone reset for one scan for JS to detect change
 +        return 
 +    end 
 +    
 +    if (resetDemand) then 
 +        reset(BUTTON_REG_ID)                     -- now real reset 
 +        resetDemand = false 
 +    end 
 +    
 +end
 +
 +function reset(reg) 
 +    local cur_value = R(reg)
 +    if cur_value and cur_value ~= 0 then 
 +        W(reg, 0)
 +    end 
 +end  
 </code> </code>
  
download-csv-js.txt · Last modified: 2023/10/15 18:07 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki