Sure.
Let’s use the sandbox chart that I linked to earlier as an example.
The JSON for that one looks like so:
{
type: 'pie',
data: {
labels: ['January', 'February', 'March', 'April', 'May'],
datasets: [{ data: [50, 60, 70, 180, 190] }],
},
}
The two parts of the chart that we want to be dynamic are the labels
and the data
. Each of those expects a list of values, enclosed in square brackets. eg: the labels are 'January', 'February', 'March', 'April', 'May'
So when we use that JSON to create a template, we want to use placeholders for those two sections so that we can replace them with our own data. So the Glide template we want to create should look like so:
{
type: 'pie',
data: {
labels: [{labels}],
datasets: [{ data: [{data}] }],
},
}
In the above, we will replace {labels}
with a list of our labels, and {data}
with a list of corresponding values. One thing to note is that any text values (eg. the labels) must be quoted with either single or double quotes (either is okay).
Imagine we have a table that looks like the below, and we want to put that in a pie chart:
We need to create two lists, one for the labels and one for the data. The way to do that is with a Joined List column. However before doing that, we need to add single quotes to the labels using a template column. To do that, just use something like '{label}'
in a template column and replace {label}
with the value from the column to be used as the label. Once that is done, we can create two joined list columns using the default comma separator:
The next thing is to plug those two lists into a template column with the JSON:
If you’ve done everything correctly to this point, you should be able to copy the output of the above template column and paste it into the quickchart sandbox editor, and it should render your chart.
The next step is to generate a URL that can be used with an Image component. It’s best to use a Construct URL column for this, as it will ensure that everything is properly URL encoded. Configure it as follows:
- Protocol: https
- Host: quickchart.io
- Path: chart
- Query Parameters: c = your chart template column
You can then use that column as the source of an image component in your layout: