Step 1. Modify Query Variables
Self-Service Analytics’s front-end client communicates with the back-end data repositories using a WebSocket API designed to translate query messages into the appropriate native query language. To generate query messages, a custom chart must define the data variables that specify the query type and required fields. Follow these steps:- Using the chart you created in Part 1: Custom Chart Basics, find the list of query variables created with the default Single-Group chart template.
-
Enter the following command in the terminal window from the chart’s root directory window:
-
Use arrow keys to follow the prompts and press Enter to select the following options, in this sequence:
VariablesList variables
Notice the group and metric variable list in the table. A group variable indicates that the chart generates a request for grouped, or aggregated, data in the query. In this case, the chart defines a single group by field. A metric variable indicates that the chart will perform an aggregation function on a numeric field as part of the grouped data query. In this chart, a single metric is specified. The chart’s default configuration defines the actual group-by and metric fields at the source level.
- Enter Y for the “Would you like to make additional edits?” prompt.
-
Select the following options, in this sequence:
VariablesEdit a variableMetric |metric|
-
For the remaining prompts, enter the following information:
- What would you like to edit: Name
- Please enter a new name for the variable: Size
-
Enter N when prompted to make additional edits.
If you are still running in watch mode in the terminal window, (see Part 1: Custom Chart Basics), the variable change will be automatically pushed to the server. If it is not, run the following command to ensure the change is pushed to the server.
Step 2. Preview the Data
Preview the data retrieved for your custom chart-
In the terminal window, change directories to the local directory of “My First Custom Chart” and enter the following command to set the custom chart CLI in watch mode, if it is not in watch mode already.
-
In another terminal window, run the following command to compile a development version of the chart’s code using the webpack bundler:
For a refresher on this topic, see Step 4. Edit the Chart Code.
-
Edit the
src/index.jsfile in your preferred text editor or integrated development environment. -
Modify the code of the
controller.updatefunction as follows to output the data received into the console: -
Remove the following code that creates the chart container and axis labels and pickers on the custom chart:
-
Save the
src/index.jsfile. The CLI updates the server’s copy with latest changes. -
Preview the chart in the dashboard again (see Step 5. Preview the Chart) and the chart’s data in the browser’s console. Since the data is output to the browser’s console, open the browser’s developer tools to get to console. See instructions for Google Chrome and Mozilla Firefox browsers. The following image shows what the results might look like:
Notice the JSON array with multiple objects in the result. Each object contains a group property with an array of strings (one per group by field) and current object with a nested count property (# of records) and a metrics property (an object with the metrics requested). Each metric objects contains an aggregation function property, like sum and its resulting value.
Step 3. Use Data Accessors
Now that we have captured the results of the chart’s query, we need to find a way to reshape the result set in a format that can be consumed by a charting library. We also want to ensure our charts remain flexible and applicable to multiple data sources. We do not want to hard-code any object properties in the code. Self-Service Analytics data accessors, exposed via the chart API, can be used to avoid hard-coding property names. Use data accessors-
Create some variables to hold the data accessors. We use one per query variable.
The API is simple:
controller.dataAccessors[<variable-name>]. Each data accessor holds the information about the field defined for the variable and provides a raw method that can be used to extract data values from a data object inside the returned data array. -
Assume we want to reshape our data array so that the contained JSON object looks like:
With this information, let’s create a
reshapeDatafunction that takes the original Self-Service Analytics data array and returns a new array with the desired object structure, as follows: -
Use the new
reshapeDatafunction to output the reshaped results into the console. The chart’s code should look like this: - Refresh the dashboard with a copy of the chart and check the contents of the browser console.