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.


    • Related Articles

    • What Types of Charts Can I Make in the LRS?

      In general, you can make charts in a custom dashboard using two methods: using our Chart Builder, or by programming our Veracity Query Language (VQL). Chart Builder Charts The Chart Builder and similar form-based wizards in the Veracity Learning LRS ...
    • How Do I Build a Sankey Chart?

      How to Build a Sankey Chart If you want to analyze navigation patterns in your course, then you can show branching — and even abandonment — with a Sankey chart. How to create — In an Advanced Graph Query widget, insert code like this… { "title": ...
    • Why Doesn’t My Duration Chart Work?

      Use meta.duration, Not result.duration If you’re trying to build a chart with duration as the metric and it’s not working, then the reason is probably that you’re trying to get values from result.duration. Use meta.duration instead. Statements encode ...
    • How Do I Edit Canonical Activities?

      What are canonical activities? As xAPI statements flow into the Veracity LRS, it builds a “canon” of names for activities. For example, the LRS can extract the names and hierarchy of courses and their nested lessons and interactions from the IDs, ...