javascriptag-gridvega-liteobservablehq

AG Data Grid table: generate a Vega-Lite chart that updates automatically


I would like to use Ag Data Grid filtered data to generate plots in Vega-Lite that update automatically whenever the filter(s) are changed. I would prefer to have the implementation in an observablehq notebook. Here is a section on how to access the table data in Ag Grid documentation. I created an observablehq notebook where the chart is updated using a button. There are two issues with this implementation:

  1. The chart is generated only after the button is clicked.
  2. I would prefer the chart update to be automatic without the need to click a button.

Solution

  • You can make the AG Grid table an Observable “view” so that you can both (1) see the table and (2) refer to its value in other cells, which will re-run when you filter the table. Once you do that, it'll work naturally with anything else you want to do in the notebook. Here's a working example with Observable Plot, and here's the same example with Vega-Lite.

    Assuming you've loaded AgGrid and have gridOptions in a different cell, you can wrap AgGrid as an Observable view like this:

    viewof table = {
      const node = htl.html`<div style="height:320px;" class="ag-theme-alpine"></div>`;
      
      new AgGrid.Grid(node, gridOptions);
      gridOptions.api.addEventListener("modelUpdated", update) // set value after filter
      gridOptions.api.sizeColumnsToFit();
      update(); // set initial value
    
      function update() { // for Observable dataflow
        let value = [];
        gridOptions.api.forEachNodeAfterFilter(d => value.push(d.data));
        node.value = value;
        node.dispatchEvent(new Event("input"));
      }
      
      return node;
    }
    

    A few notes on how this is different from your version (as of when I saw it):

    Thanks for the question; I’ve been meaning to check out AG Grid for a while!