app/controllers/surveyor_gui/reports_controller.rb
# bar chart using the question group columns as categories
bar chart using the question group columns as categories
bar chart using the question group columns as categories
def generate_grid_dropdown_bar_chart(q, responses)
    data_array = []
    categories = q.question_group.columns.map { |c| c.text }
    resp_count_series = []
    q_group = q.question_group
    group_responses = q_group.responses
    q_group.columns.each_with_index do |column, column_index|
      q.answers.select { |a| a.column_id == column.id }.each_with_index do |answer, answer_index|
        responses_ans_col = group_responses.select { |g_resp| g_resp.question.id == q.id && g_resp.answer.id == answer.id && g_resp.try(:column).try(:id) == column.id }
        response_count = responses_ans_col.count
        resp_count_series << { column_id: column.id, answer_id: answer.id, name: answer.text, count: response_count.to_i }
      end
    end
    series_names = resp_count_series.map { |a| a[:name] }.uniq
    series_names.each do |answer_name|
      bar_data = []
      q_group.columns.each do |column|
        match_ans_and_col = resp_count_series.select { |s| s[:name] == answer_name && s[:column_id] == column.id }.first
        bar_data << match_ans_and_col[:count] unless match_ans_and_col.empty?
      end
      data_array << { name: answer_name, data: bar_data }
    end
    @chart[q.id.to_s] = chart_info :bar_chart, data_array, { label: q.text, stacked: true }
  end
def generate_grid_dropdown_bar_chart ( q , responses ) data_array = [ ] categories = q . question_group . columns . map { | c | c . text } resp_count_series = [ ] q_group = q . question_group group_responses = q_group . responses q_group . columns . each_with_index do | column , column_index | q . answers . select { | a | a . column_id == column . id } . each_with_index do | answer , answer_index | responses_ans_col = group_responses . select { | g_resp | g_resp . question . id == q . id && g_resp . answer . id == answer . id && g_resp . try ( :column ) . try ( :id ) == column . id } response_count = responses_ans_col . count resp_count_series << { column_id : column . id , answer_id : answer . id , name : answer . text , count : response_count . to_i } end end series_names = resp_count_series . map { | a | a [ :name ] } . uniq series_names . each do | answer_name | bar_data = [ ] q_group . columns . each do | column | match_ans_and_col = resp_count_series . select { | s | s [ :name ] == answer_name && s [ :column_id ] == column . id } . first bar_data << match_ans_and_col [ :count ] unless match_ans_and_col . empty? end data_array << { name : answer_name , data : bar_data } end @chart [ q . id . to_s ] = chart_info :bar_chart , data_array , { label : q . text , stacked : true } end
bar chart using the question group columns as categories
bar chart using the question group columns as categories
# Response.where(question_id: q.id, answer_id: answer.id, column_id: column.id).count # count of the # of responses for this column and this answer and this question # why not take care of this in the above loop? # get all of the possible answers for this column # select only the responses that are for this particular question #  get the array of [name, count for the # of responses for this answer ] #q_data = chart_data_for_responses_in_q( responses, quest ) =begin
    @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|

      f.options[:chart][:defaultSeriesType] = 'column'
      f.options[:title][:text] = q.text

      f.options[:xAxis][:categories] = q.question_group.columns.map { |c| c.text }
      f.options[:xAxis][:labels] = { :rotation => -45, :align => 'right' }

      f.options[:yAxis][:min] = 0
      f.options[:yAxis][:title] = { :text => 'Count' }

      f.plot_options(
          :column => {
              :stacking => 'normal',
              dataLabels: {
                  enabled: true,
                  color: 'black',
                  style: {
                      fontWeight: 'bold',
                      fontSize: '12px'
                  }
              }
          },
          :pointPadding => true,
          :borderWidth => 0,
          :enableMouseTracking => false,
          :shadow => false,
          :animation => false,
          :stickyTracking => false
      )

      series = []

      q.question_group.columns.each_with_index do |column, column_index|
        q.answers.where(column_id: column.id).each_with_index do |answer, answer_index|
          response_count = Response.where(question_id: q.id, answer_id: answer.id, column_id: column.id).count
          series<<{ column_id: column.id, answer_id: answer.id, name: answer.text, y: response_count.to_i }
        end
      end

      series.map { |a| a[:name] }.uniq.each_with_index do |answer_name, answer_index|

        bararray=[]
        q.question_group.columns.each do |column|
          match = series.select { |s| s[:name]==answer_name && s[:column_id]==column.id }.first
          bararray << (match ? match[:y] : 0)
        end

        f.series(
            name: answer_name,
            data: bararray,
            color: colors[answer_index].to_s
        )
      end
    end
=end
q
responses