Commit ce3cee77 authored by Bruno Burke's avatar Bruno Burke 😁

fix updating of question type reports, ensure order in binary questions

parent 440f9b10
Pipeline #48475 passed with stages
in 3 minutes and 24 seconds
......@@ -7,7 +7,8 @@
[lernmeister.components.ui :as ui]
[lernmeister.components.i18n.core :refer [i18n-get]]
[reagent.core :as reagent]
[reagent.dom :as rdom]))
[reagent.dom :as rdom]
[oops.core :refer [oget+ oget oset!]]))
(defn chart [{:keys [answers answers-count]}]
......@@ -16,13 +17,12 @@
(reagent/create-class
{:component-did-mount
(fn [comp]
(let [answers (sort-by :count > answers)]
(reset! chart-instance
(new js/Chart
(rdom/dom-node comp)
(clj->js
{:type :horizontalBar
:data {:labels (map :text answers)
:data {:labels (map :label answers)
:datasets [{:label ""
:data
(map :count answers)}]}
......@@ -38,7 +38,18 @@
}]}}
;;:plugins [{:afterDatasetsDraw #(afterDraw % {:mean mean :stdev stdev})}]
})))))
}))))
:component-did-update
(fn [comp]
(let [answers (:answers (reagent/props comp))
answers-count (:answers-count (reagent/props comp))]
(oset! @chart-instance "data.labels"
(clj->js (map :label answers)))
(oset! @chart-instance "data.datasets.0.data"
(clj->js (map :count answers)))
(oset! @chart-instance "options.scales.xAxes.0.ticks.max"
(clj->js answers-count))
(.update @chart-instance)))
:component-will-unmount
(fn []
(when @chart-instance
......@@ -52,8 +63,12 @@
(fn [question {:keys [answers]}]
(let [freqs (frequencies (map :value answers))
answers-count (count answers)
answers (map-indexed #(assoc %2 :count (get freqs %1))
(get question :answers))]
answers (sort-by :index < (mapv (fn [[idx answer]]
(assoc answer
:label (i18n-get (:text answer))
:count (get freqs idx)
:index idx))
(get question :answers)))]
[:div
[:b (:title question)][:br]
[default-task-description (:question question)]
......
......@@ -30,11 +30,11 @@
(let [qid (:id question)
answer-options (:answers question)]
[:div
[sc-input qid (nth answer-options 0)
[sc-input qid (get answer-options 0)
:index 0
:answer answer
:on-change on-change]
[sc-input qid (nth answer-options 1)
[sc-input qid (get answer-options 1)
:index 1
:answer answer
:on-change on-change]]))})))
......
......@@ -8,7 +8,8 @@
[lernmeister.components.ui :as ui]
[com.stuartsierra.frequencies :as freq]
[reagent.core :as reagent]
[reagent.dom :as rdom]))
[reagent.dom :as rdom]
[oops.core :refer [oget+ oget oset!]]))
(defn chart [{:keys [answers answers-count]}]
......@@ -23,7 +24,8 @@
(rdom/dom-node comp)
(clj->js
{:type :horizontalBar
:data {:labels (map :text answers)
:data {:labels (map (fn [a]
(i18n-get (:text a))) answers)
:datasets [{:label ""
:data
(map :count answers)}]}
......@@ -40,6 +42,18 @@
;;:plugins [{:afterDatasetsDraw #(afterDraw % {:mean mean :stdev stdev})}]
})))))
:component-did-update
(fn [comp]
(let [answers (:answers (reagent/props comp))
answers-count (:answers-count (reagent/props comp))]
(oset! @chart-instance "data.labels"
(clj->js (map (fn [a]
(i18n-get (:text a))) answers)))
(oset! @chart-instance "data.datasets.0.data"
(clj->js (map :count answers)))
(oset! @chart-instance "options.scales.xAxes.0.ticks.max"
(clj->js answers-count))
(.update @chart-instance)))
:component-will-unmount
(fn []
(when @chart-instance
......
......@@ -75,7 +75,18 @@
)
)
(defn chart [{:keys [freqs stats bars mean stdev]}]
(defn get-labels [bars left right]
(let [min-idx 0
max-idx (dec (count bars))]
(map-indexed
(fn [idx bar]
(condp = idx
min-idx (str bar "\n(" (i18n-get left) ")")
max-idx (str bar "\n(" (i18n-get right) ")")
bar))
bars)))
(defn chart [{:keys [freqs stats bars mean stdev text-left text-right]}]
(let [chart-id (gensym "scaled-report-chart")
chart-instance (reagent/atom nil)]
(reagent/create-class
......@@ -86,7 +97,7 @@
(rdom/dom-node comp)
(clj->js
{:type :bar
:data {:labels bars
:data {:labels (get-labels bars text-left text-right)
:datasets [{:label ""
:data
(mapv
......@@ -106,8 +117,10 @@
:component-did-update
(fn [comp]
(let [bars (:bars (reagent/props comp))
freqs (:freqs (reagent/props comp))]
(oset! @chart-instance "data.labels" (clj->js bars))
freqs (:freqs (reagent/props comp))
text-left (:text-left (reagent/props comp))
text-right (:text-right (reagent/props comp))]
(oset! @chart-instance "data.labels" (clj->js (get-labels bars text-left text-right)))
(oset! @chart-instance "data.datasets.0.data"
(clj->js (mapv
#(get freqs % 0)
......@@ -143,7 +156,9 @@
:freqs freqs
:bars bars
:mean mean
:stdev stdev}]
:stdev stdev
:text-left (:text-left question)
:text-right (:text-right question)}]
]
[:div.column.is-one-fifth
[:div
......
......@@ -451,7 +451,10 @@
:type :survey-question,
:core
{:answers
[{:text "Nein", :id "first", :explanation false} {:text "Ja"}],
{0
{:text "Nein", :id "first", :explanation false}
1
{:text "Ja"}},
:type :binary-choice,
:question
{:i18n true,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment