def _plot_crop_mix(self, crop_mix):
     crop_mix, data, years, commodities = read_crop_mix(crop_mix.id)
     groups = [g.as_cropgroup() for g in crop_mix.cropmixgroup_set.all()]
     if groups:
         revenue_table = data.get_derived_table("Revenue", groups)
         revenue_table_cpi = econ_analysis.adjust_cpi(
             revenue_table, get_bls_key(), crop_mix.cpi_adjustment_year,
             ConsumerPriceIndexData.as_dataframe())
         niwr_table = data.get_derived_table("NIWR", groups)
         labor_table = data.get_derived_table("Labor", groups)
         revenue_af_plot = econ.plots.plot_revenue_af_table(
             revenue_table_cpi, niwr_table)
         labor_plot = econ.plots.plot_labor_table(labor_table)
         return self.EconPlots(crop_mix.name, revenue_af_plot, labor_plot)
     else:
         # Just do the best we can with what we have and return only the
         # acreages.
         acre_plot = econ.plots.plot_acres(data, groups)
         acre_fraction_plot = econ.plots.plot_acre_fractions(data, groups)
         return self.EconPlots(crop_mix.name, acre_plot, acre_fraction_plot)
 def _plot_crop_mix(self, crop_mix):
     crop_mix, data, years, commodities = read_crop_mix(crop_mix.id)
     groups = [g.as_cropgroup() for g in crop_mix.cropmixgroup_set.all()]
     if groups:
         revenue_table = data.get_derived_table("Revenue", groups)
         revenue_table_cpi = econ_analysis.adjust_cpi(
             revenue_table,
             get_bls_key(),
             crop_mix.cpi_adjustment_year,
             ConsumerPriceIndexData.as_dataframe()
         )
         niwr_table = data.get_derived_table("NIWR", groups)
         labor_table = data.get_derived_table("Labor", groups)
         revenue_af_plot = econ.plots.plot_revenue_af_table(revenue_table_cpi, niwr_table)
         labor_plot = econ.plots.plot_labor_table(labor_table)
         return self.EconPlots(crop_mix.name, revenue_af_plot, labor_plot)
     else:
         # Just do the best we can with what we have and return only the
         # acreages.
         acre_plot = econ.plots.plot_acres(data, groups)
         acre_fraction_plot = econ.plots.plot_acre_fractions(data, groups)
         return self.EconPlots(crop_mix.name, acre_plot, acre_fraction_plot)
def crop_mix_detail(request, crop_mix_id):
    crop_mix, data, years, commodities = read_crop_mix(crop_mix_id)
    bls_key = get_bls_key()

    groups = map(lambda g: g.as_cropgroup(), crop_mix.cropmixgroup_set.all())

    context = {
        'id': crop_mix_id,
        'crop_mix': crop_mix,
        'title': crop_mix.name,
        'description': crop_mix.description,
        'state': crop_mix.state,
        'county': crop_mix.county,
        'years': ', '.join(map(str, years)),
        'commodities': ', '.join(commodities),
        'year': datetime.now().year,
        'source': crop_mix.source,
    }


    # We take the top 8 columns of all charts below because we're going to use
    # a 9 color palette to display the data.

    acre_plot = plots.plot_acres(data, groups)
    acre_script, acre_div = components(acre_plot, CDN)
    context.update({
        'acre_script': acre_script,
        'acre_div': acre_div,
    })

    acre_pct_plot = plots.plot_acre_fractions(data, groups)
    acre_pct_script, acre_pct_div = components(acre_pct_plot, CDN)
    context.update({
        'acre_pct_script': acre_pct_script,
        'acre_pct_div': acre_pct_div,
    })

    if groups:
        group_map, uncategorized = data.get_group_map(groups)
        group_values = {g.title: g for g in groups}
        context.update({
            'group_map': {
                key: (group_values[key], ', '.join(value))
                for key, value in group_map.items()
            },
            'uncategorized': ", ".join(uncategorized),
        })
        revenue_table = data.get_derived_table("Revenue", groups)

        niwr_table = data.get_derived_table("NIWR", groups)
        niwr_plot = plotting.bar_plot_table(
            niwr_table,
            title='Water Use (acre-feet)',
            palette=Spectral9,
            legend='bottom_right',
            xlabel='Year',
            ylabel='Acre-Feet',
            tools=DEFAULT_TOOLS,
            logo=None,
            responsive=True,
            number_of_categories=8,
            yaxis_formatter=NumeralTickFormatter(format='0,0')
        )
        niwr_script, niwr_div = components(niwr_plot, CDN)
        context.update({
            'niwr_script': niwr_script,
            'niwr_div': niwr_div,
        })

        labor_table = data.get_derived_table("Labor", groups)
        labor_plot = plots.plot_labor_table(labor_table)
        labor_script, labor_div = components(labor_plot, CDN)
        context.update({
            'labor_script': labor_script,
            'labor_div': labor_div,
        })

        revenue_af_plot = plots.plot_revenue_af_table(revenue_table, niwr_table)
        revenue_af_script, revenue_af_div = components(revenue_af_plot, CDN)
        context.update({
            'revenue_af_script': revenue_af_script,
            'revenue_af_div': revenue_af_div,
        })
    else:
        revenue_table = data.get_table("$")

    revenue_plot = plotting.bar_plot_table(
        revenue_table,
        title='Gross Revenue ($)',
        palette=Spectral9,
        legend='bottom_right',
        xlabel='Year',
        ylabel='',
        tools=DEFAULT_TOOLS,
        logo=None,
        responsive=True,
        number_of_categories=8,
        yaxis_formatter=NumeralTickFormatter(format='$0,0')
    )
    revenue_script, revenue_div = components(revenue_plot, CDN)
    context.update({
        'revenue_script': revenue_script,
        'revenue_div': revenue_div,
    })

    cpi_data = ConsumerPriceIndexData.as_dataframe()
    revenue_table_cpi = analysis.adjust_cpi(
        revenue_table,
        bls_key,
        crop_mix.cpi_adjustment_year,
        cpi_data
    )
    revenue_cpi_plot = plotting.bar_plot_table(
        revenue_table_cpi,
        title='Gross Revenue (%s $)' % crop_mix.cpi_adjustment_year,
        palette=Spectral9,
        legend='bottom_right',
        xlabel='Year',
        ylabel='',
        tools=DEFAULT_TOOLS,
        logo=None,
        responsive=True,
        number_of_categories=8,
        yaxis_formatter=NumeralTickFormatter(format='$0,0')
    )
    revenue_cpi_script, revenue_cpi_div = components(revenue_cpi_plot, CDN)
    context.update({
        'revenue_cpi_script': revenue_cpi_script,
        'revenue_cpi_div': revenue_cpi_div,
    })

    return render(request, 'econ/crop_mix_detail.django.html', context)
Exemple #4
0
def crop_mix_detail(request, crop_mix_id):
    crop_mix, data, years, commodities = read_crop_mix(crop_mix_id)
    bls_key = get_bls_key()

    groups = map(lambda g: g.as_cropgroup(), crop_mix.cropmixgroup_set.all())

    context = {
        'id': crop_mix_id,
        'crop_mix': crop_mix,
        'title': crop_mix.name,
        'description': crop_mix.description,
        'state': crop_mix.state,
        'county': crop_mix.county,
        'years': ', '.join(map(str, years)),
        'commodities': ', '.join(commodities),
        'year': datetime.now().year,
        'source': crop_mix.source,
    }

    # We take the top 8 columns of all charts below because we're going to use
    # a 9 color palette to display the data.

    acre_plot = plots.plot_acres(data, groups)
    acre_script, acre_div = components(acre_plot, CDN)
    context.update({
        'acre_script': acre_script,
        'acre_div': acre_div,
    })

    acre_pct_plot = plots.plot_acre_fractions(data, groups)
    acre_pct_script, acre_pct_div = components(acre_pct_plot, CDN)
    context.update({
        'acre_pct_script': acre_pct_script,
        'acre_pct_div': acre_pct_div,
    })

    if groups:
        group_map, uncategorized = data.get_group_map(groups)
        group_values = {g.title: g for g in groups}
        context.update({
            'group_map': {
                key: (group_values[key], ', '.join(value))
                for key, value in group_map.items()
            },
            'uncategorized': ", ".join(uncategorized),
        })
        revenue_table = data.get_derived_table("Revenue", groups)

        niwr_table = data.get_derived_table("NIWR", groups)
        niwr_plot = plotting.bar_plot_table(
            niwr_table,
            title='Water Use (acre-feet)',
            palette=Spectral9,
            legend='bottom_right',
            xlabel='Year',
            ylabel='Acre-Feet',
            tools=DEFAULT_TOOLS,
            logo=None,
            responsive=True,
            number_of_categories=8,
            yaxis_formatter=NumeralTickFormatter(format='0,0'))
        niwr_script, niwr_div = components(niwr_plot, CDN)
        context.update({
            'niwr_script': niwr_script,
            'niwr_div': niwr_div,
        })

        labor_table = data.get_derived_table("Labor", groups)
        labor_plot = plots.plot_labor_table(labor_table)
        labor_script, labor_div = components(labor_plot, CDN)
        context.update({
            'labor_script': labor_script,
            'labor_div': labor_div,
        })

        revenue_af_plot = plots.plot_revenue_af_table(revenue_table,
                                                      niwr_table)
        revenue_af_script, revenue_af_div = components(revenue_af_plot, CDN)
        context.update({
            'revenue_af_script': revenue_af_script,
            'revenue_af_div': revenue_af_div,
        })
    else:
        revenue_table = data.get_table("$")

    revenue_plot = plotting.bar_plot_table(
        revenue_table,
        title='Gross Revenue ($)',
        palette=Spectral9,
        legend='bottom_right',
        xlabel='Year',
        ylabel='',
        tools=DEFAULT_TOOLS,
        logo=None,
        responsive=True,
        number_of_categories=8,
        yaxis_formatter=NumeralTickFormatter(format='$0,0'))
    revenue_script, revenue_div = components(revenue_plot, CDN)
    context.update({
        'revenue_script': revenue_script,
        'revenue_div': revenue_div,
    })

    cpi_data = ConsumerPriceIndexData.as_dataframe()
    revenue_table_cpi = analysis.adjust_cpi(revenue_table, bls_key,
                                            crop_mix.cpi_adjustment_year,
                                            cpi_data)
    revenue_cpi_plot = plotting.bar_plot_table(
        revenue_table_cpi,
        title='Gross Revenue (%s $)' % crop_mix.cpi_adjustment_year,
        palette=Spectral9,
        legend='bottom_right',
        xlabel='Year',
        ylabel='',
        tools=DEFAULT_TOOLS,
        logo=None,
        responsive=True,
        number_of_categories=8,
        yaxis_formatter=NumeralTickFormatter(format='$0,0'))
    revenue_cpi_script, revenue_cpi_div = components(revenue_cpi_plot, CDN)
    context.update({
        'revenue_cpi_script': revenue_cpi_script,
        'revenue_cpi_div': revenue_cpi_div,
    })

    return render(request, 'econ/crop_mix_detail.django.html', context)