demo D171
Click a chart to filter in Vue
Click a slice and the grid narrows to it
chart.on('click', …)
This lets you click a slice or bar in a chart and narrows the grid to it. It turns a chart into a filter control, so a reader drills from the picture into the exact rows behind it with one click.
This is the Vue 3 version. A createLatticeGrid component takes the configuration through v-bind, and reactive data flows straight into the grid, so it updates the way the rest of your Vue app does.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<style>
#chart { height: 260px; }
#grid { margin-top: 14px; }
#grid > div { height: 340px; }
</style>
<div id="app"></div>
<script type="module">
import * as Vue from 'https://esm.sh/vue@3/dist/vue.esm-browser.prod.js';
import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/vue.esm.min.js';
import { createChart } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/charts.esm.min.js';
const LatticeGrid = createLatticeGrid({ vue: Vue, createGrid });
Vue.createApp({
components: { LatticeGrid },
data() {
return {
config: {
rowKey: 'id',
toolPanel: { side: 'left', panels: ['filters', 'columns'], exportName: 'circuits' },
columns: [
{ field: 'circuit', title: 'Circuit', layout: { width: 180 } },
{ field: 'region', title: 'Region', filter: { type: 'set' } },
{ field: 'status', title: 'Status', filter: { type: 'set' } },
{ field: 'bandwidth', title: 'Bandwidth', type: 'number' },
{ field: 'charge', title: 'Monthly charge', type: 'number',
format: { style: 'currency', currency: 'GBP' }, total: 'sum' },
],
rows,
},
active: null,
};
},
mounted() {
// The component exposes the live grid via a template ref, which the chart draws from.
const grid = this.$refs.grid.grid();
const chart = createChart({ grid, container: '#chart', type: 'pie', x: 'status', y: 'charge',
title: 'Charge by status (click to filter)' });
// Click a slice to filter the grid to it; click the same slice again to clear.
chart.on('click', ({ category }) => {
if (category == null) return;
if (this.active === category) {
grid.filters.clear();
this.active = null;
} else {
grid.filters.set({ col: 'status', op: 'eq', value: category });
this.active = category;
}
});
},
template: `
<div id="chart"></div>
<div id="grid"><lattice-grid ref="grid" v-bind="config" /></div>
`,
}).mount('#app');
</script>