jqxGrid AI

Ask your data anything

aiAsk sends the model your column aggregates plus a row sample and returns a concise answer - and a chart suggestion when it helps. Running with an offline mock.

Filter Command bar Ask your data Classify Summarize
Try: what is the average unit price? what is the total quantity? how many rows are there?

Note: the mock computes from the visible rows. When the answer suggests a chart, the returned spec (type / categoryField / valueField / aggregate) is rendered live below with jqxChart — swap in a real model and it works the same.

jQuery Grid: Ask Data

jqxGrid AI - ask your data questions in plain language.

A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.

jqxGrid API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxdraw.js, jqxchart.core.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxlistbox.js, jqxdropdownlist.js, jqxgrid.js, jqxgrid.sort.js, jqxgrid.selection.js, jqxgrid.aggregates.js, jqxgrid.pager.js, jqxgrid.ai.js, jqxgrid.celltemplates.js.

It also loads helper scripts of this demo site, which are not part of the library: ai-mock.js, generatedata.js (generatedata(), random sample rows), demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {
    var data = generatedata(120);
    var adapter = new $.jqx.dataAdapter({
        localdata: data, datatype: 'array', datafields: [
            { name: 'firstname', type: 'string' }, { name: 'productname', type: 'string' },
            { name: 'quantity', type: 'number' }, { name: 'price', type: 'number' }
        ]
    });
    $("#grid").jqxGrid({
        theme: 'tailwind', width: '100%', height: 340, source: adapter, sortable: true, pageable: true,
        aiSendRequest: jqxGridAIMock, rowsheight: 48, columnsheight: 38,
        columns: [
            { text: 'Customer', datafield: 'firstname', width: 200, cellTemplate: { type: 'avatar' } },
            { text: 'Product', datafield: 'productname', width: 230 },
            { text: 'Quantity', datafield: 'quantity', width: 120, cellsalign: 'right' },
            { text: 'Unit Price', datafield: 'price', cellsalign: 'right', cellTemplate: '{{value | c2}}' }
        ]
    });

    function ask() {
        var q = document.getElementById('q').value;
        if (!q.trim()) { return; }
        $('#result').html('<div class="ai-avatar">' + ICON.spark + '</div><div class="ai-result-body">' +
            '<div class="ai-result-title">Analyzing</div><div class="ai-dots"><span></span><span></span><span></span></div></div>').show();
        $("#grid").jqxGrid('aiAsk', q, function (err, res) {
            if (err) { render('<span class="ai-hint">' + err.message + '</span>'); return; }
            var html = '<div class="ai-answer-text">' + res.answer.replace(/\n/g, '<br/>') + '</div>';
            if (res.chart) {
                html += '<div class="ai-chart-cap">' + ICON.chart + '<span>' + res.chart.aggregate + '(' + res.chart.valueField + ') by ' + res.chart.categoryField + '</span></div>' +
                    '<div id="ai-chart" class="ai-chart-box"></div>';
            }
            render(html);
            if (res.chart) { buildAiChart(res.chart); }
        });
    }
    function render(body) {
        $('#result').html('<div class="ai-avatar">' + ICON.spark + '</div><div class="ai-result-body">' +
            '<div class="ai-result-title">Answer</div>' + body + '</div>').show();
    }
    // aggregate the visible grid rows per the model's chart spec
    function chartData(spec) {
        var rows = $('#grid').jqxGrid('getrows'), g = {};
        rows.forEach(function (r) {
            var k = r[spec.categoryField]; if (k == null) { k = '—'; }
            var v = parseFloat(r[spec.valueField]); if (isNaN(v)) { v = 0; }
            if (!g[k]) { g[k] = { sum: 0, cnt: 0, min: Infinity, max: -Infinity }; }
            var o = g[k]; o.sum += v; o.cnt++; o.min = Math.min(o.min, v); o.max = Math.max(o.max, v);
        });
        var agg = spec.aggregate || 'sum';
        return Object.keys(g).map(function (k) {
            var o = g[k], val = agg === 'avg' ? o.sum / o.cnt : agg === 'count' ? o.cnt : agg === 'min' ? o.min : agg === 'max' ? o.max : o.sum;
            return { cat: k, val: Math.round(val * 100) / 100 };
        }).sort(function (a, b) { return b.val - a.val; }).slice(0, 8);
    }
    function buildAiChart(spec) {
        var data = chartData(spec); if (!data.length) { return; }
        var DARK = document.documentElement.getAttribute('data-theme') === 'dark';
        var PAL = DARK ? ['#a5b4fc', '#818cf8', '#6366f1', '#4f46e5', '#4338ca', '#3730a3', '#3730a3', '#312e81']
            : ['#4338ca', '#4f46e5', '#6366f1', '#818cf8', '#a5b4fc', '#c7d2fe', '#c7d2fe', '#e0e7ff'];
        var trunc = function (v) { v = String(v); return v.length > 12 ? v.slice(0, 11) + '…' : v; };
        if (spec.type === 'pie') {
            $('#ai-chart').jqxChart({
                theme: 'tailwind', source: data, backgroundColor: 'transparent', showLegend: true, title: '', description: '',
                padding: { left: 5, top: 5, right: 5, bottom: 5}, colorScheme: 'scheme05',
                seriesGroups: [{ type: 'pie', showLabels: false, series: [{ dataField: 'val', displayText: 'cat', radius: 82, innerRadius: 42, centerOffset: 0 }] }]
            });
            return;
        }
        $('#ai-chart').jqxChart({
            theme: 'tailwind', source: data, backgroundColor: 'transparent', showLegend: false, title: '', description: '', enableAnimations: true,
            padding: { left: 5, top: 10, right: 10, bottom: 5 },
            categoryAxis: { dataField: 'cat', showGridLines: false, formatFunction: trunc },
            seriesGroups: [{
                type: spec.type === 'line' ? 'line' : 'column', columnsGapPercent: 55,
                valueAxis: { visible: true, gridLinesColor: DARK ? '#2E3440' : '#eef0f4', axisSize: 0 },
                series: [{ dataField: 'val', color: DARK ? '#818cf8' : '#4f46e5', lineWidth: 3, symbolType: 'circle', colorFunction: function (v, i) { return PAL[Math.min(i, PAL.length - 1)]; } }]
            }]
        });
    }
    $('#ask').jqxButton({ theme: 'tailwind', width: 96, height: 46 }).addClass('jqx-primary');
    $('#ask').on('click', ask);
    $('#q').on('keydown', function (e) { if (e.keyCode === 13) { ask(); } });
    $('.ai-chip').on('click', function () { document.getElementById('q').value = $(this).text(); ask(); });
});

var ICON = {
    spark: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l1.7 4.6L18.3 8.3 13.7 10 12 14.6 10.3 10 5.7 8.3 10.3 6.6z"/><path d="M18.5 13.5l.9 2.1 2.1.9-2.1.9-.9 2.1-.9-2.1-2.1-.9 2.1-.9z"/></svg>',
    chat: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 15a2 2 0 0 1-2 2H8l-4 4V5a2 2 0 0 1 2-2h13a2 2 0 0 1 2 2z"/></svg>',
    chart: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M5 20V10M12 20V4M19 20v-7M3 20h18"/></svg>'
};
document.querySelector('.ai-badge').innerHTML = ICON.spark + ' jqxGrid AI';
document.querySelector('.ai-lead').innerHTML = ICON.chat;

body.html

<div class="ai-wrap">
    <div class="ai-hero">
        <span class="ai-badge">jqxGrid AI</span>
        <h1>Ask your data anything</h1>
        <p><code>aiAsk</code> sends the model your column aggregates plus a row sample and returns a concise answer - and a chart suggestion when it helps. Running with an offline mock.</p>
    </div>
    <div class="ai-nav">
        <a href="filtering-nlp.htm">Filter</a>
        <a href="ai-command-bar.htm">Command bar</a>
        <a href="ai-ask-data.htm" class="active">Ask your data</a>
        <a href="ai-classify-column.htm">Classify</a>
        <a href="ai-summarize.htm">Summarize</a>
    </div>

    <div class="ai-card">
        <div class="ai-row">
            <div class="ai-inputbar">
                <span class="ai-lead"></span>
                <input id="q" type="text" placeholder="e.g. 'what is the average unit price?'">
            </div>
            <input type="button" id="ask" value="Ask">
        </div>
        <div class="ai-chips">
            <span class="ai-chips-label">Try:</span>
            <span class="ai-chip">what is the average unit price?</span>
            <span class="ai-chip">what is the total quantity?</span>
            <span class="ai-chip">how many rows are there?</span>
        </div>
    </div>

    <div class="ai-card"><div id="grid"></div></div>

    <div id="result" class="ai-result" style="display:none;"></div>

    <p class="ai-foot"><strong>Note:</strong> the mock computes from the visible rows. When the answer suggests a chart, the returned spec (<code>type / categoryField / valueField / aggregate</code>) is rendered live below with <code>jqxChart</code> — swap in a real model and it works the same.</p>
</div>

Learn more

More jQuery Grid demos