How Do I Show Percent of Students?

How Do I Show Percent of Students?

In a custom dashboard, you can easily build a bar chart that shows the number of unique students per course.

Chart Builder with simulated data offered in the free Veracity Learning LRS.

Problem

But what if you want to show the number of unique students per course as a percentage of all students?

Solution

You’ll need to count unique students per course, count all unique students, and then calculate the percentage proportion per course. You can’t do all that in Chart Builder. You must use Veracity Query Language (VQL).
  1. In your custom dashboard, from the left menu, select Add a Widget.

  2. In the Add a Widget pop-up, click Custom VQL Query.

  3. In the Advance Chart Builder page, in the Monaco Editor in the left panel, paste the following code:
{   "title": "Students per Course",
"subtitle": "Percent of All Students",
"filter": null,
"process": [{
"$frequentValues": {
"path": "context.contextActivities.parent.definition.name.en-US",
"limit": 100,
"metrics": {"metric1": {"$range": "actor.id"}},
"sort": {"count": "desc"}}}, {
"$addFields": {
"metric2": {
"$inlineSubQuery": {
"select": {"path": "$[0].value", "mode": "one"},
"filter": {"$source": "statements"},
"process": [{"$range": {"path": "actor.id"}}]}}}}, {
"$addFields": {
"metric3": {"$multiply": [{"$divide": ["$metric1", "$metric2"]}, 100]}}}, {
"$barChart": {
"categoryPath": "_id",
"valuePath": "metric3",
"sortCategory": false}}, {
"$addFields": {
"chart.yAxes.0.min": 0,
"chart.yAxes.0.max": 100,
"chart.yAxes.0.strictMinMax": true}}]}
  1. Click the OK button.

Outcome

You should then see a chart like this:


Notes

This custom code works because it builds a collection of documents with the iterative $frequentValues command and counts the unique students per course, and within that process it includes a count of all students using an $inlineSubQuery. This creates one collection with two fields per document: one varying and one static. It then calculates a third metric value as the percentage of the other two and charts that third metric.

A few small aesthetic details include:
  1. Turn sortCategory off so the chart inherits the sort order from the $frequentValues command.

  2. Change chart.yAxes properties to fit the chart to the limits of the percentages, as shown in the amCharts reference.