Skip to content

Commit 4c792a3

Browse files
committed
feat: dynamic data loading from parent variant
1 parent b950186 commit 4c792a3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## How to test
2+
3+
1. create a calculated variable `my_data` in variant
4+
5+
2. have labels and data in there e.g.
6+
7+
```js
8+
{
9+
labels: ['January', 'February', 'March', 'April', 'May'],
10+
data: {
11+
datasets: [
12+
{
13+
data: [300, 300, 200]
14+
}
15+
]
16+
}
17+
}
18+
```
19+
20+
3. add iframe question with `answerLabel` parameter
21+
e.g. `https://iframe_address.com?answerLabel=my_data`

data.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import {
44
setAutoHeight
55
} from "@formsort/custom-question-api";
66

7+
async function loadClientConfig() {
8+
const urlParams = new URLSearchParams(window.location.search);
9+
const answerLabel = urlParams.get('answerLabel');
10+
11+
const answers = await getAllAnswerValues();
12+
13+
return answers[answerLabel] || {};
14+
}
15+
16+
/**
17+
* we expect people to pass an answer that includes labels field and data field
18+
* {
19+
* labels: ['January', 'February', 'March', 'April', 'May'],
20+
* data: {
21+
datasets: [
22+
{
23+
data: [300, 300, 200]
24+
}
25+
]
26+
}
27+
* }
28+
*/
29+
730
// Need a function that can transform: current weight, goal weight, and weight loss time horizon into a fields for data.datasets.data
831
async function getAllAnswers() {
932
const answers = await getAllAnswerValues();
@@ -51,7 +74,7 @@ const options = {
5174
}
5275
};
5376

54-
const data = {
77+
const defaultData = {
5578
datasets: [
5679
{
5780
data: [300, 300, 200]
@@ -60,6 +83,12 @@ const data = {
6083
};
6184

6285
(async function () {
86+
const { data = defaultData, labels } = await loadClientConfig();
87+
88+
if (labels) {
89+
options.scales.x.labels = labels;
90+
}
91+
6392
const ctx = document.getElementById("data");
6493

6594
new Chart(ctx, {

0 commit comments

Comments
 (0)