How to refactor dataTable to Converter
The documentation of the PocketQuery Chart API describes an option dataTable
:
dataTable
: array-of-arrays containing the dataset to be drawn. This overrides the data taken from the query results. Example:[['Month', 'Value'],['JAN-17', 52.1],['FEB-17', 51.2],['MAR-17', 51.7]]
. If you want to create a custom dataTable using the query results, you can use thePocketQuery.query*
functions described in the general JS API documentation. This can
This option has been deprecated. Historically, this option comes from a time when Converters weren’t present in PocketQuery. Any use of dataTable
can be refactored to using a Converter.
The basic structure of a Converter looks as follows:
function convert(json) {
const parsed = JSON.parse(json);
return parsed;
}
The parsed
variable in the snippet will contain the same data as what you might have retrieved using the queryArray
JavaScript helper method (see JavaScript API available in Templates). This means that you can move any code you used for creating the dataTable
array into the converter and use the result of JSON.parse(json)
the same way as the result of what queryArray
returned before.
Finally, you will have to return the prepared result for the chart in the Converter. The format of this result is slightly different: rather than an array-of-arrays as with the dataTable
option, it needs to be an array-of-objects where the headers of the dataTable
are properties of each object. Using the dataTable
example from the documentation:
[['Month', 'Value'],['JAN-17', 52.1],['FEB-17', 51.2],['MAR-17', 51.7]]
This would become the following array-of-objects returned from the Converter:
[
{ Month: 'JAN-17', Value: 52.1 },
{ Month: 'FEB-17', Value: 51.2 },
{ Month: 'MAR-17', Value: 51.7 }
]
In this example, each of the header columns (Month
and Value
) is a property key in each row. This structure resembles a default PocketQuery result that is already translated for the PocketQuery.chart
API by default, so no dataTable
is required anymore.
If you struggle refactoring your dataTable
chart option to a Converter, please reach out to us.