Skip to content

Querying Data

ChartFactor can be used as well to query data without rendering any visualization. This data could be either raw or aggregated data . This is useful when the data needs to be processed and shown in any other component that is not part of ChartFactor, ie: drop downs selectors, custom tables, menus, etc. The way to use the query tool is pretty much the same way that is used to render a visual. Actually, every time a visual is rendered, the data injected to that visual is available in the same way.  So it means that we can have our visual and also the data to be used anywhere else. The differences between using a visual or not (getting only the data) are the following: - The graph() function (or any chart shortcut) must be used. - The element() function is required, but the element passed as parameter may exist or not in the DOM, preferably must not exist to avoid any conflicts, and it will be only used to identify the query. - After the execute() method, which returns a JavaScript promise, the .then() function must be added in order to get the queried data.

1
2
3
4
5
6
7
8
9
cf.provider('Elasticsearch')
    .source('ticket_sales')
    .groupby(...groups)
    .metrics(...metrics)
    .element('dummy')
    .execute()
    .then(function(result){
        console.log(result.data)
    })

The result object obtained in the callback passed to .then() will have the following format:  

1
2
3
4
5
result = {
    'definition': {...},
    'data': [...],
    'visualization': undefined
}

In the result object, data is an array with the queried information in the format used by ChartFactor.  The other two attributes are useful only when rendering a visualization.