- {!surveyRatingResultsReady[`question_${questionIndex}`] ? (
+ {!surveyRatingResultsReady[questionIndex] ? (
) : (
@@ -191,7 +202,7 @@ export function RatingQuestionBarChart({
label: 'Number of responses',
barPercentage: 0.7,
minBarLength: 2,
- data: surveyRatingResults[`question_${questionIndex}`],
+ data: surveyRatingResults[questionIndex],
backgroundColor: '#1d4aff',
hoverBackgroundColor: '#1d4aff',
},
@@ -212,3 +223,119 @@ export function RatingQuestionBarChart({
)
}
+
+export function SingleChoiceQuestionPieChart({
+ questionIndex,
+ surveySingleChoiceResults,
+ surveySingleChoiceResultsReady,
+}: {
+ questionIndex: number
+ surveySingleChoiceResults: SurveySingleChoiceResults
+ surveySingleChoiceResultsReady: QuestionResultsReady
+}): JSX.Element {
+ const { loadSurveySingleChoiceResults } = useActions(surveyLogic)
+ const { survey } = useValues(surveyLogic)
+
+ const question = survey.questions[questionIndex]
+ if (question.type !== SurveyQuestionType.SingleChoice) {
+ throw new Error(`Question type must be ${SurveyQuestionType.SingleChoice}`)
+ }
+
+ // Insights colors
+ // TODO: make available in Tailwind
+ const colors = [
+ '#1D4BFF',
+ '#CD0F74',
+ '#43827E',
+ '#621DA6',
+ '#F04F58',
+ '#539B0A',
+ '#E3A605',
+ '#0476FB',
+ '#36416B',
+ '#41CBC3',
+ '#A46FFF',
+ '#FE729E',
+ '#CE1175',
+ '#B64B01',
+ ]
+
+ useEffect(() => {
+ loadSurveySingleChoiceResults({ questionIndex })
+ }, [questionIndex])
+
+ return (
+
+ {!surveySingleChoiceResultsReady[questionIndex] ? (
+
+ ) : (
+
+
Single choice
+
{question.question}
+
+
+
+ colors[i % colors.length]
+ ),
+ },
+ ]}
+ labels={surveySingleChoiceResults[questionIndex].labels}
+ />
+
+
+
{
+ const dataLength = surveySingleChoiceResults[questionIndex].data.length
+ if (dataLength < 5) {
+ return 20
+ } else if (dataLength < 7) {
+ return 15
+ } else if (dataLength < 10) {
+ return 10
+ } else {
+ return 5
+ }
+ })()} grid-cols-${Math.ceil(surveySingleChoiceResults[questionIndex].data.length / 10)}`}
+ >
+ {surveySingleChoiceResults[questionIndex].data.map((count: number, i: number) => {
+ const { total, labels } = surveySingleChoiceResults[questionIndex]
+ const percentage = ((count / total) * 100).toFixed(1)
+
+ return (
+
+
+
{`${labels[i]}`}
+
{` ${percentage}% `}
+
{`(${count})`}
+
+ )
+ })}
+
+
+
+ )}
+
+ )
+}