'Allows the user to select one string question and multiple numeric
questions. The string question is used to group the numeric data,
with the result displayed as a bar chart.
For example:
The string question can be Geographic area, and the numeric
questions could be people injured and families displaced.
Then the results will be grouped by each geographical area.'

@staticmethod
def seriesGraph(r, **attr):

    T = current.T
    response = current.response
    s3 = current.response.s3
    output = {}
    rvars = r.vars
    if ('viewing' in rvars):
        (dummy, series_id) = rvars.viewing.split('.')
    elif ('series' in rvars):
        series_id = rvars.series
    else:
        series_id = r.id
    chart_file = S3SurveySeriesModel.getChartName()
    cache_path = S3Chart.getCachedPath(chart_file)
    if (cache_path and r.ajax):
        return IMG(_src=cache_path)
    else:
        numeric_question_list = None
        label_question = None
        post_vars = r.post_vars
        if (post_vars is not None):
            if ('labelQuestion' in post_vars):
                label_question = post_vars.labelQuestion
            if ('numericQuestion' in post_vars):
                numeric_question_list = post_vars.numericQuestion
                if (not isinstance(numeric_question_list, (list, tuple))):
                    numeric_question_list = [numeric_question_list]
            if ((numeric_question_list != None) and (label_question != None)):
                S3SurveySeriesModel.drawChart(output, series_id, numeric_question_list, label_question)
    if ((r.ajax == True) and ('chart' in output)):
        return output['chart']
    rheader = attr.get('rheader', None)
    if rheader:
        rheader = rheader(r)
        output['rheader'] = rheader
    def addQstnChkboxToTR(numeric_question_list, question):
        '\n                                                                Build    the    form\n                                                '
        tr = TR()
        if ((numeric_question_list != None) and (question['code'] in numeric_question_list)):
            tr.append(INPUT(_type='checkbox', _name='numericQuestion', _value=question['code'], value=True))
        else:
            tr.append(INPUT(_type='checkbox', _name='numericQuestion', _value=question['code']))
        tr.append(LABEL(question['name']))
        return tr
    if (series_id is None):
        return output
    all_questions = survey_getAllQuestionsForSeries(series_id)
    label_type_list = ('String', 'Option', 'YesNo', 'YesNoDontKnow', 'Location')
    label_questions = survey_get_series_questions_of_type(all_questions, label_type_list)
    question_labels = []
    for question in label_questions:
        question_labels.append(question['name'])
    numeric_type_list = 'Numeric'
    form = FORM(_id='mapGraphForm')
    table = TABLE()
    label_question = SELECT(question_labels, _name='labelQuestion', value=label_question)
    table.append(TR(TH(('%s:' % T('Select    Label    Question'))), _class='survey_question'))
    table.append(label_question)
    table.append(TR(TH(T('Select    Numeric    Questions    (one    or    more):')), _class='survey_question'))
    special_questions = [{'code': 'Count', 'name': T('Number    of    Completed    Assessment    Forms')}]
    inner_table = TABLE()
    for question in special_questions:
        tr = addQstnChkboxToTR(numeric_question_list, question)
        inner_table.append(tr)
    table.append(inner_table)
    numeric_questions = survey_get_series_questions_of_type(all_questions, numeric_type_list)
    inner_table = TABLE()
    for question in numeric_questions:
        tr = addQstnChkboxToTR(numeric_question_list, question)
        inner_table.append(tr)
    table.append(inner_table)
    form.append(table)
    series = INPUT(_type='hidden', _id='selectSeriesID', _name='series', _value=('%s' % series_id))
    button = INPUT(_type='button', _id='chart_btn', _name='Chart', _value=T('Display    Chart'), _class='small    primary    button')
    form.append(series)
    form.append(button)
    jurl = URL(c=r.prefix, f=r.function, args=r.args)
    s3.jquery_ready.append(('\n$(\'#chart_btn\').click(function(){\n    var    data=$(\'#mapGraphForm\').serialize()\n    var    url=\'<a    class="action-btn"    href=series_chart_download?\'    +    data    +    \'>Download    Chart</a>\'\n    $.post(\'%s\',data,function(data){\n        $(\'#survey_chart\').empty();\n        $(\'#survey_chart\').append(data);\n        $(\'#survey_chart_download\').empty();\n        $(\'#survey_chart_download\').append(url);\n    });\n});\n' % jurl))
    output['showForm'] = P(T('Click    on    the    chart    to    show/hide    the    form.'))
    output['form'] = form
    output['title'] = s3.crud_strings['survey_series'].title_analysis_chart
    response.view = 'survey/series_analysis.html'
    return output
