-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Download feature request/issue #51
Comments
Try some of this code... function getTime() {
const time = window
.grafanaRuntime
.getDashboardTimeRange();
const fromStr = new Date(time.from).toLocaleDateString("en-GB");
const toStr = new Date(time.to).toLocaleDateString("en-GB");
return [fromStr, toStr];
}
function downloadCSV(data, name, time) {
// Create Blob from CSV data and download
const csv = convertToCSV(data);
const blob = new Blob([csv], {type: "text/csv;charset=utf-8;"});
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
const [fromStr, toStr] = time;
const filename = stuff.csv`;
a.download = filename;
a.click();
// Revoke the Object URL
URL.revokeObjectURL(a.href);
}
const time = getTime();
const data = window
.grafanaRuntime
.getPanelData();
const fields = data[selectedId]
.series[0]
.fields;
downloadCSV(fields, selectedName, time); |
Nice this is exactly what Im looking for! I tried various ways to let the user download the data via a button but I did not manage to get it working to my liking. Reason for having a download button for data from the script is that we can run complex calculations in JS which we cannot with grafana transformations. However our users often need to download the data as csv to include those values in deliverables other than screenshot/png export from grafana. Ideally we would have a download button in the "displayModeBar" but I did not manage to add a custom button. So I tried to create a download button in the layout of the panel via "updatemenus" instead but executing a script from that button seems not allowed (and or I dont know how to get it working). I ended up with 2 working methods (that are not ideal). The first method that worked is via the "the On-event Handler" menu. The downloadCSV script runs sucesfully if you click on a point of the trace but not on a button or someting like that. The second option is a bit sketchy and If you ask me also outside this grafana panel plugin but i got it working via a button in the "document.body" where scripts can be executed. See below scripts. ===============================Via "the On-event Handler"================================ SCRIPT // Dummy data to mimic fields from the query // Define your traces based on the data // Return data and layout for Plotly to render in Grafana ON EVENT-HANDLER function downloadCSV(fields, name) { const filename = (name + URL.revokeObjectURL(a.href); // Dummy data to mimic fields from the query try { // Event handling for the Plotly panel switch (eventType) { switch (eventType) { ===============================Via the "document.body"================================ SCRIPT """ function downloadCSV(fields, name) { const filename = (name + URL.revokeObjectURL(a.href); // Get the data from the Grafana panel // Dummy data to mimic fields from the query // Define your traces based on the data // Add a custom download button with a longer delay
} // Return traces and layout for Grafana to render =======================Failed Via the Layout "updatesmenu"=========================== SCRIPT """ function downloadCSV(fields, name) { const filename = (name + URL.revokeObjectURL(a.href); // Get the data from the Grafana panel // Dummy data to mimic fields from the query // Define your traces based on the data // Define the layout with a custom button for downloading CSV // Return the data and layout for Plotly to render |
===============================Via "the On-event Handler"================================
THE ON-EVENT HANDLER
|
=======================Failed Via the Layout "updatesmenu"=========================== SCRIPT
|
===============================Via the "document.body"================================ SCRIPT
|
Hi,
I was wondering if it is possible to add a download button to a plotly panel in Grafana. I was able to add the button , but it doesn't allow me to actually download the data.
Am I doing something wrong or is there an other way to add a download button?
Example script (provided by GPT):
// Access the fields
let time = [
1696003200000, // 2023-09-30 00:00:00
1696006800000, // 2023-09-30 01:00:00
1696010400000, // 2023-09-30 02:00:00
1696014000000, // 2023-09-30 03:00:00
1696017600000, // 2023-09-30 04:00:00
1696021200000 // 2023-09-30 05:00:00
];
// Hardcoded test data (some random values)
let test = [5.1, 6.3, 4.8, 7.2, 8.0, 6.7];
// Prepare CSV data
function generateCSV() {
let csvRows = ["Time,test"];
for (let i = 0; i < time.length; i++) {
const row = [
new Date(time[i]).toISOString(), // Format timestamp as ISO string
pitch[i]?.toFixed(2)
];
csvRows.push(row.join(","));
}
return csvRows.join("\n");
}
// Download CSV function
function downloadCSV() {
const csvData = generateCSV();
const blob = new Blob([csvData], { type: "text/csv" });
const url = URL.createObjectURL(blob);
}
// Set up the trace
let tracetest = {
x: time.map(t => new Date(t)), // Convert time to Date objects for proper formatting
y: test,
mode: 'lines',
name:
test
};
// Define updatemenus with a pseudo-download button that triggers the downloadCSV function
var updatemenus = [{
type: 'buttons',
buttons: [
{
label: 'Download Data as CSV',
method: 'relayout', // Dummy method
args: [{}, {downloadCSV: true}] // Pass a custom argument
}
],
direction: 'right',
pad: {'r': 10, 't': 10},
showactive: false,
x: 0.15,
xanchor: 'left',
y: 1.2,
yanchor: 'top'
}];
// Define layout
let layout = {
title: 'test Data Over Time',
xaxis: {
autorange: true
},
yaxis: {
title: 'test',
autorange: true
},
updatemenus: updatemenus
};
// Add a listener to call downloadCSV if the button is clicked
document.addEventListener('plotly_relayout', (event) => {
if (event.detail && event.detail.downloadCSV) {
downloadCSV();
}
});
// Return the data and layout for the plot
return { data: [tracetest], layout };
The text was updated successfully, but these errors were encountered: