diff --git a/src/cljs/lernmeister/components/question_types/binary_choice/views/report.cljs b/src/cljs/lernmeister/components/question_types/binary_choice/views/report.cljs index 5d97f7c214ed9b54b8bdd10f7688dee9b97fc551..f1adc7f7f7cc05a448650d0472249b3899a381bc 100644 --- a/src/cljs/lernmeister/components/question_types/binary_choice/views/report.cljs +++ b/src/cljs/lernmeister/components/question_types/binary_choice/views/report.cljs @@ -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)] diff --git a/src/cljs/lernmeister/components/question_types/binary_choice/views/show.cljs b/src/cljs/lernmeister/components/question_types/binary_choice/views/show.cljs index 5c6db7e1d67cbfdc5688b039234c9647f2708dce..973838d14cd9eee99d49a9679e6f44763a29c3c0 100644 --- a/src/cljs/lernmeister/components/question_types/binary_choice/views/show.cljs +++ b/src/cljs/lernmeister/components/question_types/binary_choice/views/show.cljs @@ -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]]))}))) diff --git a/src/cljs/lernmeister/components/question_types/multiple_choice/views/report.cljs b/src/cljs/lernmeister/components/question_types/multiple_choice/views/report.cljs index a0e1db5a3382da0ce8348e34375c6602f7003cb1..1b7dbe224ce04620e97bb33f1c6c417af7752d77 100644 --- a/src/cljs/lernmeister/components/question_types/multiple_choice/views/report.cljs +++ b/src/cljs/lernmeister/components/question_types/multiple_choice/views/report.cljs @@ -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 diff --git a/src/cljs/lernmeister/components/question_types/scaled/views/report.cljs b/src/cljs/lernmeister/components/question_types/scaled/views/report.cljs index 6d2b2c73fa8cfc1df8b1a77b74e254871918637f..08c6a0d1d78e4917937cfdce860bcdb0d55218e6 100644 --- a/src/cljs/lernmeister/components/question_types/scaled/views/report.cljs +++ b/src/cljs/lernmeister/components/question_types/scaled/views/report.cljs @@ -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 diff --git a/src/devcards/lernmeister/components/sample_data.cljs b/src/devcards/lernmeister/components/sample_data.cljs index c011355801aae3b8d0d1d013653e2029b4be0594..e4c3e0b65de966a20d8fcb5b71713734be865470 100644 --- a/src/devcards/lernmeister/components/sample_data.cljs +++ b/src/devcards/lernmeister/components/sample_data.cljs @@ -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,