Skip to content

Chart Task Values with a CDN Library

A credit reviewer approves a customer's new credit limit in a task, and a column of monthly numbers is hard to read at a glance. This gizmo charts the customer's recent purchases, passed in from the work item, and publishes the trend. It is also the pattern for any third-party library inside the sandbox: where to load it from, what to whitelist, and which libraries cannot run.

At a glance

  • Extension point Custom Gizmo with a third-party script
  • You need A work item field with monthly values (filled by an earlier step); permissions Create Custom Gizmos and Manage Layouts
  • Teaches Loading a library from a CDN, script-src through whitelist.txt, script load order, and the no-eval rule
Key Kind and meaning
monthly-values Input, string. Label:Number pairs separated by commas, for example Jan:1200, Feb:950, Mar:1430.
chart-title Input, string. The title above the chart.
trend Result, string. up, down or flat, comparing the last value with the first.

Set it up

  1. Go to Configuration › Services › Custom Gizmos › New Custom Gizmo, title "Purchase Trend". Paste the files below and save.
  2. In the Tasks app, go to Layouts › Edit Layout › Add Gizmo › Custom Gizmo › "Purchase Trend".
  3. In Configure Gizmo, on "Monthly values", tick Use Value from Input, Input Name monthly-values.
  4. In the Start A Task step, Input Mappings: map monthly-values to the work item field that an earlier step filled, for example bucket Index, key Monthly Purchases.

The files

index.html
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<script src="{project}/main.js"></script>

<style>
    body { font-family: sans-serif; padding: 12px; }
    #error { color: #c62828; }
</style>

<h3 id="title"></h3>
<canvas id="chart" height="180"></canvas>
<div id="error"></div>
main.js
// Purchase Trend gizmo.
// Inputs:  monthly-values (string, "Label:Number" pairs), chart-title (string).
// Results: trend (up, down or flat).
// API permissions: none. Outside origins: https://cdn.jsdelivr.net (Chart.js).
gizmo.SetInputSchema(function (schema) {
    schema.AddString("monthly-values")
        .SetDisplayName("Monthly values")
        .SetDescription("Label:Number pairs separated by commas")
        .SetExampleValue("Jan:1200, Feb:950, Mar:1430")
        .ConfigureUI(function (ui) { ui.AsTextbox(); });
    schema.AddString("chart-title")
        .SetDisplayName("Chart title")
        .SetDefaultValue("Purchases by month")
        .ConfigureUI(function (ui) { ui.AsTextbox(); });
});

gizmo.OnReceiveConfig(function (config) {
});

gizmo.OnShow(function () {
    document.getElementById("title").textContent = gizmo.Config.GetString("chart-title", "Purchases by month");

    if (typeof Chart === "undefined") {
        document.getElementById("error").textContent = "The chart library did not load. Check whitelist.txt.";
        return;
    }

    var points = parse(gizmo.Config.GetString("monthly-values", ""));
    if (points.labels.length === 0) {
        document.getElementById("error").textContent = "No values to chart.";
        return;
    }

    new Chart(document.getElementById("chart"), {
        type: "line",
        data: {
            labels: points.labels,
            datasets: [{ label: "Purchases", data: points.values, tension: 0.2 }]
        },
        options: { responsive: true, plugins: { legend: { display: false } } }
    });

    var first = points.values[0];
    var last = points.values[points.values.length - 1];
    var trend = last > first * 1.05 ? "up" : (last < first * 0.95 ? "down" : "flat");
    gizmo.Result.SetString("trend", trend);
    gizmo.PublishResult();
});

function parse(text) {
    var labels = [];
    var values = [];
    text.split(",").forEach(function (pair) {
        var parts = pair.split(":");
        if (parts.length !== 2) { return; }
        var value = parseFloat(parts[1].replace(/[^0-9.\-]/g, ""));
        if (isNaN(value)) { return; }
        labels.push(parts[0].trim());
        values.push(value);
    });
    return { labels: labels, values: values };
}
whitelist.txt
// Chart.js from the jsDelivr CDN
https://cdn.jsdelivr.net

How it works

  • The library URL is absolute, so the runtime does not rewrite it. Its origin is in whitelist.txt, so the CSP lets the script load.
  • Scripts can finish loading in any order. main.js uses Chart only inside OnShow, and checks that it exists first.
  • The URL pins an exact version (chart.js@4.4.1), so a new release cannot change the gizmo under you.
  • Chart.js draws on a <canvas> and does not compile code at run time, so it works under the no-eval policy.
  • The trend is a plain result that the workflow can route on or store.

Test it

  1. Create a test task and enter Jan:1200, Feb:950, Mar:1430 for monthly-values.
  2. Open the task with the browser console open.
  3. Remove the line from whitelist.txt, reload the task, and check the console. Then put the line back.

You should see

a line chart with three points and the result trend = up. Without the whitelist line, the console shows a script-src Content Security Policy error and the gizmo says "The chart library did not load."

What can go wrong

Watch out

  • "The chart library did not load." The CDN origin is missing from whitelist.txt, or the URL is wrong. The console shows the CSP or network error.
  • A library fails with an eval or "unsafe-eval" error. The sandbox blocks eval and new Function. Pick a build of the library that does not compile code at run time.
  • The CDN changes or goes down. For a task your business depends on, upload the library file to the project and load it with {project}/chart.umd.min.js instead. Then no whitelist line is needed.
  • The chart is cut off. The gizmo's size comes from the layout. Make the gizmo larger in the layout designer.