- Step 1. About Third-Party Libraries
- Step 2. Add Libraries
- Step 3. Test Libraries
- Step 4. Create a Chart Container
- Step 5. Render the Chart
- Step 6. Handle Resizing
Step 1. About Third-Party Libraries
Self-Service Analytics makes use of third-party libraries in the out-of-the-box charts provided with the product. Typically, these libraries fall into two categories:- Charting libraries, which assist with the rendering of standard chart types like bar, line, pie, and scatter plot charts.
- Utility libraries, which assist with shaping data sets, formatting values, or providing useful JavaScript methods.
Step 2. Add Libraries
Begin using ECharts with your custom chart-
Add ECharts as a dependency of the custom chart. You can do this by installing ECharts using
npm, which provides an easy-to-use software registry that can leveraged to install packages for your custom charts. -
Review the contents of the file
package.jsonto ensure that Echarts is in the list of dependencies.
Step 3. Test Libraries
Test the libraries-
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. -
Add a
console.logline towards the top of the file, as shown: -
Check the contents of the browser console after adding the updated chart to a dashboard. The following image shows what the console should look like:

-
Once you confirm that the global variable is defined, you can remove the
console.logstatement.
Step 4. Create a Chart Container
As part of its charting API, Self-Service Analytics provides a<div> element that can hold the rendered contents of your custom chart. While it is perfectly fine to use that element as the chart container, we recommend that you create a separate container inside of that element to gain full control over its styles.
Create a chart container
-
Add the following
create chart containerconstlines of code to the top of thesrc/index.jsfile:Notice the line:controller.element.appendChild(chartContainer);.controller.elementis the<div>Self-Service Analytics provides with its API. -
- Refresh the dashboard with the custom chart, and you should see a dark gray background for your custom chart.
-
Change the background color to background-color:
#ffffff;to set it back to white.
Step 5. Render the Chart
In Part 2: Query Variables, Chart Defaults, Data Preview, and Data Accessors, we defined a group-by variable and a metric variable to generate a grouped data set with a single metric value. This data structure works well with bar and pie charts. This step uses the ECharts API and its chart configuration option to create a pie chart with our data set.-
In the
src/index.jsfile, create a variable to hold an instance of the pie chart and a second variable to hold the chart configuration option. The chart’s instance is created using theecharts.initmethod and passing in a chart container. Add it right below the data accessors you created in Part 2:Notice how we used the metric accessor to dynamically capture the name of the series. -
Specify the pie series data and call the chart’s
setOptionmethod inside of thecontroller.updatefunction:A pie chart renders similar to this: Unfortunately, the pie chart is slightly cut off at the bottom, so the window needs to be resized. Read on.
Step 6. Handle Resizing
Self-Service Analytics provides an API to notify the charts of a resize operation. Resize the window-
Add the
controller.resizefunction at the end of thesrc/index.jsfile.Theresizemethod of the EChart’s chart instance tells the chart to fit the new dimensions of the chart container.