Mermaid diagrams are a javascript diagram tool that builds simple and attractive diagrams from markdown-like syntax.
Qualtrics is an expensive closed source survey tool, widely used in industry and research contexts.
While Qualtrics is very powerful, and has lots of ‘out of the box’ features/question types, I was curious whether mermaid diagrams could be embedded into the questions, because this might allow the use of a survey to - for example - build customised causal models, or theoretical frameworks, etc., without needing to engage people with the mermaid syntax directly.
Mermaid
This is an example, simple, Mermaid diagram:
flowchart LR
A ---> B
In code that looks like this, a diagram type (flowchart), direction (LR or left-right), the nodes (A, B) and their relationship (--->).
flowchart LR
A ---> B
Qualtrics
Qualtrics allows you to embed javascript in questions. So (with some chatGPT assistance), we can use a script something like this to embed mermaid inside a qualtrics survey, or question.
Qualtrics.SurveyEngine.addOnload(function() {
// Add the Mermaid.js script to the head of the document
var script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js';
script.onload = function() {
// Initialize Mermaid
mermaid.initialize({ startOnLoad: true });
// Poll to check if the diagram has been rendered
var checkMermaidRender = function() {
var diagrams = document.querySelectorAll('.mermaid');
diagrams.forEach(function(diagram) {
if (diagram.innerHTML.trim().startsWith('graph')) {
mermaid.init(undefined, diagram);
}
});
};
// Set an interval to repeatedly check the rendering
var intervalId = setInterval(function() {
checkMermaidRender();
// Clear the interval after a sufficient number of checks
if (document.querySelector('.mermaid .node')) {
clearInterval(intervalId);
}
}, 500); // Check every 500ms
};
document.head.appendChild(script);
});
That (should) have mermaid loading, and checking that the diagrams are rendered.
Getting user input
Probably the easiest way to use mermaid to elicit a user responses is by using an image hotspot question. But if you wanted to try something else, you could:
- Have inputs from users, with piped text to create diagrams
- Provide users with the text of your diagram and then allow them to edit the text (without needing to see the syntax it’s embedded in).
For the former, it’s easy enough to setup if you want to fill a node label of a pre-defined graph, but it’s more convoluted for anything involving creation of the graph from relatively unstructured user inputs. (The below is one attempt)
Qualtrics.SurveyEngine.addOnReady(function() {
let show_row = 2;
let choice_rows = this.getChoiceContainer().querySelectorAll("[class*='Choice']");
// Hide rows beyond the initially visible ones
for (let i = show_row; i < choice_rows.length; i++) {
choice_rows[i].style.display = 'none'; // Use style.display for better compatibility
}
// Create the button element
let addButton = document.createElement("button");
addButton.id = "row_adder";
addButton.innerHTML = "Add Row";
addButton.type = "button"; // Ensure it's a button to avoid form submission
// Append the button after the last choice row
choice_rows[choice_rows.length - 1].insertAdjacentElement("afterend", addButton);
// Add functionality to the button
addButton.onclick = function new_row() {
if (show_row < choice_rows.length) {
choice_rows[show_row].style.display = ''; // Use style.display for better compatibility
show_row++;
} else {
alert("You've reached the maximum number of permissible rows");
}
};
});