Hi @jinnyzor ,
Here is an example that you can run for testing purposes:
Some info in the comments of what I am trying to achieve:
How to display this ‘marker’ value as a horizontal line within the ag-grid range chart (y-axis)?
If there is no way: how to export the filtered data from ag-grid back to python/dash?
- I set up a demo callback that saves gridApi.getRenderedNodes().
- However this only works if the data fits within the grid screen.
- Then there is still the matter of parsing the ag-grid data back into a pandas dataframe to generate a dcc.Graph.
Desired result: (marker = 30000000)
test.py:
import os
from dash import Dash, dcc, html, Input, Output, State, callback, clientside_callback
import dash_ag_grid as dag
import plotly.express as px
app = Dash(__name__)
df = px.data.gapminder()
# How to display this 'marker' value as a horizontal line within the ag-grid range chart (y-axis)?
# If there is no way: how to export the filtered data from ag-grid back to python/dash?
# - I set up a demo callback that saves gridApi.getRenderedNodes().
# - However this only works if the data fits within the grid screen.
# - Then there is still the matter of parsing the ag-grid data back into a pandas dataframe to generate a dcc.Graph.
marker = 30000000
defaultColDef = {
# Row Group
"enableRowGroup": True, # Allows the column to be grouped in the UI
# Sort and Filter
"editable": False,
"sortable": True,
"filter": True,
# Auto-size and set minimum resizable column width
"resizable": True,
"minWidth": 50,
# Sorting infer data type?
"cellDataType": True,
# Pivot
"enablePivot": True,
"pivot": False,
}
columnDefs = [
{"field": 'year', "rowGroup": True},
{"field": 'continent', "rowGroup": True},
{"field": 'country'},
{"field": 'pop', "aggFunc": 'avg'},
]
dashGridOptions = {
"animateRows": True,
"enableCharts": True,
"enableRangeSelection": True,
"sideBar": True,
"rowGroupPanelShow": 'always',
"pivotMode": False, # True, False
"pivotPanelShow": 'always', # (Initial--cannot be updated) 'never', 'always', 'onlyWhenPivoting'
# "suppressExpandablePivotGroups ": False,
}
app.layout = html.Div([
# buttons
html.Button('Generate Chart', id="btn-make-chart"),
# grid div
html.Div([
dcc.Store(id="grid-api", data='empty'),
dag.AgGrid(
id="grid-id",
enableEnterpriseModules=True,
# licenseKey = os.environ['AGGRID_ENTERPRISE'],
columnDefs=columnDefs,
style={"height": "100%", "width": "100%"},
defaultColDef=defaultColDef,
rowData=df.to_dict('records'),
columnSize="sizeToFit",
dashGridOptions=dashGridOptions,
),
],
style={'height': '95%'},
),
# debug log div
html.Div(
id='debug-log',
# style={'display': 'none'}, # # none | block
),
],
style={
'height': '95vh',
'margin': 0,
},
)
clientside_callback(
"""
function(gridId, n_clicks_timestamp, savedData) {
console.log('')
var debug_output = "btn-make-chart >> n_clicks_timestamp = " + n_clicks_timestamp
// console.log(debug_output)
const gridApi = dash_ag_grid.getApi(gridId);
console.log('saved data=', savedData)
console.log('new gridApi=', gridApi)
console.log('selectedNodes=', gridApi.getSelectedNodes())
console.log('renderedNodes=', gridApi.getRenderedNodes())
var newSavedData = gridApi.getRenderedNodes()
console.log('newSavedData=', 'renderedNodes')
// console.log('make chart')
gridApi.setGridOption('popupParent', document.body); // sneaky way of setting up the DOM
// number of nodes to plot
var numberOfNodes = -1
gridApi.forEachNode((node) => {
numberOfNodes = numberOfNodes + 1
});
// console.log(numberOfNodes)
// combo chart
var createRangeChartParams = {
chartType: 'groupedColumn',
cellRange: {
rowStartIndex: 0,
rowEndIndex: numberOfNodes,
columns: ['pop'],
},
aggFunc: 'avg',
chartContainer: document.querySelector('#myChart'),
// suppressChartRanges: true,
};
// make chart
gridApi.createRangeChart(createRangeChartParams);
return newSavedData
}
""",
Output('grid-api', 'data'),
Input('grid-id', 'id'),
Input("btn-make-chart", "n_clicks_timestamp"),
State("grid-api", "data"),
prevent_initial_call=True
)
if __name__ == "__main__":
app.run(debug=True)