Skip to content

Querying Data

ChartFactor can be used 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 rendered outside ChartFactor components, ie: drop-down selectors, custom tables, menus, etc. Querying data is done in pretty much the same way as rendering visualizations. Actually, every time a visual is rendered, the data injected to that visual is available in the same way.  This means we can have our visual and also the data to be used anywhere else.

To query data without rendering a visualization you need to do the following:

  • The element() function needs to be invoked since it is required. The element passed as parameter may exist or not in the DOM, preferably it does not exist to avoid any conflicts. It is 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.