Getting Started
The custom chart API provides developers with an interface to create interactive charts that integrate with the native Self-Service Analytics chart controls. To get started with the custom chart API, you need to install the Custom Chart CLI. The tool is designed to help you manage all aspects of the custom chart creation process in Self-Service Analytics.You must be an administrator to configure the chart CLI.
Install Self-Service Analytics’s Custom Chart CLI
npm install composer-chart-cli@latest -g
Use the Self-Service Analytics Custom Chart CLI
- Configure the CLI.
cmp-chart config - Create a new custom chart.
cmp-chart init <some_path>/<your_chart_name> - From the newly created custom chart directory, run
npm install— This will install your custom chart’s development dependencies. - Compile the custom chart code that will be pushed to the server
—npm run build - Push the custom chart to the server —
cmp-chart push - Enable the custom chart for a source. Navigate to the Sources page, locate a data source configuration to edit, and select the more menu (
) button. Select Available Visual Types, then locate and enable your chart in the Custom Visual Types list. - Create a new dashboard with your custom chart. You should see a chart widget with a Group and Metric picker.
- You are now ready to continue building out your custom chart.
Add Custom Chart Packages
Runcmp-chart import <name> <filepath.zip> to add the specified custom chart to the Self-Service Analytics server.
For more information about working with custom charts, see these topics:
- Chart Variables
- Controller
- Create Your Own Chart Container
- Receive Chart Data
- Transform Data Using Data Accessors
- Get Values From Constant Variables
- React to Resize Events
- Update Queries with Axis Labels or Pickers
- Add Tooltips
- Interacting with the Context Menu
- Listening to Other Query Events
Controller
The global object controller exposes the Self-Service Analytics custom chart API. The controller object contains several properties holding information about the chart. These include, but are not limited to:- HTML Element to be used as a chart container (
controller.element) - Object with properties containing the configuration of each query variable (
controller.dataAccessors) - Object with the information about the current Self-Service Analytics data source (
controller.source) - Object with properties to access the value of each constant variable (
controller.variables)
controller object to react to chart events like data updates, chart resizing, and query errors. Triggering these events execute the specified handler function.
For more information about the controller properties and methods, visit the individual guides for tips on how to use custom chart API.
Chart Variables
Chart variables serve as configuration parameters that can be read from the chart’s code and are used to promote reusability of charts across Self-Service Analytics data sources. There are two types of chart variables in Self-Service Analytics:Query Variables
These variables drive the type of query that executes against the backend database. For example, a chart with a single query variable of type Group aggregates the data based on the configured field and uses count as the aggregation function. If the developer additionally adds a query variable of type Multi-Metric, the data includes the aggregated values of the configured metrics based on the aggregation functions specified.Constant Variables
These variables are useful for chart settings specified as numeric, text, or list values. For example, a custom chart might use a service that requires an API key. A chart developer can create a string constant variable for the API key to allow users to specify a different key per data source.Supported Variable Types
A list of the various variable types supported by Self-Service Analytics custom charts and their configurations can be found here.Add Tooltips
Charting libraries often include a tooltip implementation that may not look similar to the tooltips included with Self-Service Analytics native charts. As a developer, you can re-use the tooltip design in your custom charts by leveraging theshow and hide methods in controller.tooltip.
To show a tooltip, use controller.tooltip.show. The show method takes an object as its only argument with the following properties:
Optional properties:
Example:
myChart variable. It hooks into the mouseoverevent of the chart and provides a handler function. The chart provides the handler function with an object argument param with the x and y screen coordinates that describe where the tooltip should display.
In this example, the chart has also provided the original Self-Service Analytics data element for the hovered point. controller.tooltip.show can be called with an object that specifies the tooltip location and data for the tooltip.
To hide the tooltip, we can call the controller.tooltip.hide method.
Example:
mouseout event and provide a handler function that calls the controller.toolitp.hide method.
Create Your Own Chart Container
Self-Service Analytics provides a reference to an HTML DIV element that developers can use as a container for their charts. This element is accessed via thecontroller.element property. Sometimes, it is useful to create an inner chart container inside the controller.element div to gain full control over its styling via the CSS chart components.
Example:
chart-container to render your chart.
Get Values From Constant Variables
The values of constant variables are accessed by reading the properties ofcontroller.variables. This object contains a property for each constant variable defined.
Example:
controller.variables.
Interacting with the Context Menu
The Self-Service Analytics context menu is a native control that you can add to custom charts to provide users with a set of standard chart interactions. Your chart must be running an aggregated data query to leverage and provide access to the context menu. Default context menu actions:
Optional context menu options:
Create a Context Menu with Custom Actions
Add custom actions to your context menu, and bind methods to left and right mouse click actions in place of default actions.customActions items array. Your customActions can be bound as a method for the left or right mouse click in place of one of the default actions.
Defining customActions:
| Option | Description |
|---|---|
name | A name for each of the customActions.Required. |
action | The action you define for the customActions.Required. |
icon | An icon to associate with the customActions.Optional. |
Listening to Other Query Events
Chart developers may need to apply particular logic to their charts whenever a query event is triggered. Handler functions can be defined to override the following list of query methods:controller.onStart: Called as soon as the query execution begins.controller.onNoDataFound: Called when no data is available for the given query.controller.onNotDirtyData: Called as soon as all of the data is received.controller.onStreamError: Called when the query execution returns an error from the server.
React to Resize Events
When a user resizes a chart widget, you must account for the new widget dimensions. You can specify your own resize handler function by overriding thecontroller.resize method.
Example:
- A user changes the dimensions of the browser window.
- A user changes the dimensions of an individual widget.
Receive Chart Data
To receive the results of queries executed against a Self-Service Analytics data source, chart developers can override thecontroller.update method with a function that will receive the array of data elements.
Example:
Structure of a Data Element in Aggregated Queries
When working with aggregated queries that haveGroup or Multi-Group variables defined, you can expect to receive data elements with the following structure:
Multi-Group variable and Metric variable. The Multi-Group variable contains two levels of grouping (“Product Group” & “User Income” fields), and the Metric variable has a configuration using the “Price” field and the “SUM” function. The count property represents the total count of records for the grouping combination. This property is always available in all aggregated queries.
Structure of a Data Element in Non-Aggregated Queries
When working with non-aggregated queries, the data elements in the array received from the server represent a row of data in the data source. Each row is made up of an array of values; one for each of the fields requested:Ungrouped variable with three fields requested: “Gender”, “Payment Type”, “Price”.
Transform Data Using Data Accessors
When working with charting libraries, it is often necessary to structure your data elements in a way the charting libraries understand. For instance, given the following structure of data element received from the server:group array and the metric value defined in price.sum. You may be inclined to create a new data array by mapping each data element to the necessary structure like:
A Better Approach Using Data Accessors
AData Accessor is an object with methods that can be used to extract information about the current configuration of query variables. A data accessor can also extract data values out of the data elements received from query execution results.
Example:
controller.dataAccessors object. The query variable’s name exists as property in this object. The most commonly used method in data accessors is raw which accepts a Self-Service Analytics data element and returns a value corresponding to the query variable. In the above example, we used the groupAccessor and metricAccessor to retrieve the group and metric values without hard-coding the name of the fields.
Update Queries with Axis Labels or Pickers
Axis Labels or Axis Pickers are Self-Service Analytics native controls that can be added to charts to provide a way for users to change query parameters dynamically. Chart developers can these controls by calling the methodcontroller.createaAxisLabel. The createAxislabel method takes an object as its only argument with the following properties:
| Option | Description |
|---|---|
picks | Name of the query variable. Use the data accessor’s getName method to avoid hardcoding variable names. |
position | Location of the axis label in the chart widget. Valid options: bottom, left, right, top |
orientation | Orientation of the axis label text. Valid options: vertical, horizontal |
Group By and Size query variables.