Published
Edited
May 25, 2021
1 fork
Importers
Insert cell
Insert cell
Insert cell
suggest(question)
Insert cell
Insert cell
serveQuestion = (question) => {
// use heuristics to suggest various visualizations
if(Array.isArray(question)) {
const [q1, q2] = question; // assume just two questions
return applyQuestions(question, suggestions[`compare_${q1.type}_${q2.type}`])
}
if(question.type == "choice" && question.domain.length <= 6) {
return applyQuestion(question, suggestions.simple_pie)
// return applyQuestion(question, suggestions.simple_histogram)
}
if(question.type == "choice" && question.domain.length > 6) {
return applyQuestion(question, suggestions.limit_histogram)
}
if(question.type == "multi") {
return applyQuestion(question, suggestions.multi_histogram)
}
if(question.type == "free") {
return applyQuestion(question, suggestions.free_response)
}
return {}
}
Insert cell
Insert cell
suggestions = ({
"simple_pie": {
label: "Pie chart",
note: "",
code: `survey.pie(❓, {
domain: 🏠
})`
},
"simple_histogram": {
label: "Histogram",
note: "Show the frequency of each choice",
code: `survey.histogram(❓, {
domain: 🏠
})`
},
"limit_histogram": {
label: "Histogram",
note: "Show the frequency of each choice",
code: `survey.histogram(❓, {
limit: 8,
})`
},
"multi_histogram": {
label: "Multi-select Histogram",
note: "Show the top answers selected",
code: `survey.histogram(❓, {
limit: 8,
multiselect: true
})`
},
"free_response": {
label: "Free Response",
note: "Show a list of responses",
code: `survey.freeResponse(❓)`
},
"compare_choice_multi": {
label: "",
note: "",
code: `survey.matrixCompare(❓, ❔)`
},
"compare_multi_choice": {
label: "",
note: "",
code: `survey.matrixCompare(❓, ❔)`
},
"compare_choice_choice": {
label: "",
note: "",
code: `survey.matrixCompare(❓, ❔)`
},
"compare_multi_multi": {
label: "",
note: "",
code: `survey.matrixCompare(❓, ❔)`
},
})
Insert cell
applyQuestion = (question, s, prefix = "sheet.questions.") => {
let suggest = Object.apply({}, s)
suggest.code = s.code.replaceAll("❓", prefix + question.id)
.replaceAll("🏠", JSON.stringify(question.domain))
suggest.label = s.label
return suggest
}
Insert cell
applyQuestions = (questions, s, prefix = "sheet.questions.") => {
let suggest = Object.apply({}, s)
suggest.code = s.code.replaceAll("❓", prefix + questions[0].id)
.replaceAll("❔", prefix + questions[1].id)
.replaceAll("🏠", JSON.stringify(questions[0].domain))
return suggest
}
Insert cell
render = (suggestion) => html`
<div class="copy">${Copier("Copy code", {value: suggestion.code})}</div>
<textarea class="sample" rows=${rows(suggestion.code)}>${suggestion.code}</textarea>
<style style="visibility:hidden">
textarea.sample {
font: var(--monospace-font);
font-size: 12px;
width: 100%;
padding: 5px;
padding-bottom: 1em;
border: 2px solid #3b5fc0;
background: #F6F9FF;
border-radius: 5px;
margin-bottom: 2em;
resize: none;
}
.copy form {
position: absolute;
right: -8px;
top: 4px;
width: initial;
}
`
Insert cell
function rows(code) {
const lines = code.trim().split("\n").length;
return Math.max(Math.min(lines, 15), 2);
}
Insert cell
suggest = (question) => render(serveQuestion(question))
Insert cell
import {sheet} from "@observablehq/survey-data"
Insert cell
survey = ({ histogram, pie, freeResponse: FreeResponse })
Insert cell
import {surveyHistogram as histogram} from "@observablehq/survey-histogram"
Insert cell
import {surveyPieChart as pie} from "@observablehq/survey-pie-chart"
Insert cell
import {FreeResponse} from "@observablehq/survey-free-response-component"
Insert cell
import {questionSelector} from "@observablehq/survey-question-selector"
Insert cell
import {Copier} from "@mbostock/pbcopy"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more