app/controllers/surveyor_gui/reports_controller.rb
def generate_stacked_bar_chart(q, answer_info)

    # StackOverflow answer: how to set up the data for chartkick stacked bar chart?
    # @see https://stackoverflow.com/a/24388877

    q_group = q.question_group

    questions = q_group.questions.reject(&:is_comment)

    categories = q_group.answers.map { |a| a.text }

    data_array = []
    questions.each do |quest|

      q_data = chart_data_for_answer_info_in_q(answer_info, quest)

      data_array << { name: quest.text, data: q_data }

    end

    @chart[q.id.to_s] = chart_info :column_chart, data_array, { label: q.text, stacked: true }

=begin

    @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
      f.options[:chart][:defaultSeriesType] = 'column'

      f.options[:title][:text] = q.question_group.text

      f.options[:xAxis][:categories] = q.answers.order('answers.id').map { |a| a.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'
                  }
              }
          }
      )

      q.question_group.questions.where(is_comment: false).each_with_index do |question, question_index|

        bararray = []
        responses.where(:question_id => question.id).each_with_index do |a, answer_index|
          bararray[answer_index]= { :y => a.answer_count.to_i }
        end


        f.series(
            name: question.text,
            data: bararray,
            color: colors[question_index].to_s
        )
      end

    end
=end

  end
# each bar has all the questions in a question group
each bar has all the questions in a question group
each bar has all the questions in a question group
def generate_stacked_bar_chart(q, answer_info)
    q_group = q.question_group
    questions = q_group.questions.reject(&:is_comment)
    categories = q_group.answers.map { |a| a.text }
    data_array = []
    questions.each do |quest|
      q_data = chart_data_for_answer_info_in_q(answer_info, quest)
      data_array << { name: quest.text, data: q_data }
    end
    @chart[q.id.to_s] = chart_info :column_chart, data_array, { label: q.text, stacked: true }
  end
def generate_stacked_bar_chart ( q , answer_info ) q_group = q . question_group questions = q_group . questions . reject ( & :is_comment ) categories = q_group . answers . map { | a | a . text } data_array = [ ] questions . each do | quest | q_data = chart_data_for_answer_info_in_q ( answer_info , quest ) data_array << { name : quest . text , data : q_data } end @chart [ q . id . to_s ] = chart_info :column_chart , data_array , { label : q . text , stacked : true } end
each bar has all the questions in a question group
each bar has all the questions in a question group
# StackOverflow answer: how to set up the data for chartkick stacked bar chart? # @see https://stackoverflow.com/a/24388877 =begin

    @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
      f.options[:chart][:defaultSeriesType] = 'column'

      f.options[:title][:text] = q.question_group.text

      f.options[:xAxis][:categories] = q.answers.order('answers.id').map { |a| a.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'
                  }
              }
          }
      )

      q.question_group.questions.where(is_comment: false).each_with_index do |question, question_index|

        bararray = []
        responses.where(:question_id => question.id).each_with_index do |a, answer_index|
          bararray[answer_index]= { :y => a.answer_count.to_i }
        end


        f.series(
            name: question.text,
            data: bararray,
            color: colors[question_index].to_s
        )
      end

    end
=end
q
answer_info