def get_chart(self): if self.cached_chart is not None: return self.cached_chart chart_options = { "scales": { "yAxes": [{ "ticks": { "beginAtZero": True } }] } } today = date.today() chart_start_date = today - timedelta(days=365) orders = get_orders_for_shop(self.request) sum_sales_data = group_by_period(orders.valid().since( (today - chart_start_date).days), "order_date", "month", sum=Sum("taxful_total_price_value")) for (month, year) in month_iter(chart_start_date, today): sales_date = date(year, month, 1) if sales_date not in sum_sales_data: sum_sales_data[sales_date] = {"sum": Decimal(0)} # sort and recreated the ordered dict since we've put new items into sum_sales_data = OrderedDict( sorted(six.iteritems(sum_sales_data), key=lambda x: x[0])) locale = get_current_babel_locale() labels = [ format_date(k, format=get_year_and_month_format(locale), locale=locale) for k in sum_sales_data ] mixed_chart = MixedChart(title=_("Sales per Month (past 12 months)"), labels=labels, data_type=ChartDataType.CURRENCY, options=chart_options, currency=self.currency, locale=locale) cumulative_sales = [] average_sales = [] # only calculate cumulative and average if there are at least 3 months if len(sum_sales_data) >= 3: count = 0 total = Decimal() for month_sale in sum_sales_data.values(): total = total + month_sale["sum"] cumulative_sales.append(total) average_sales.append(total / (count + 1)) count = count + 1 # this will be on top of all bars if average_sales: mixed_chart.add_data(_("Average Sales"), [v for v in average_sales], ChartType.LINE) # this will be under the cummulative bars mixed_chart.add_data(_("Sales"), [v["sum"] for v in sum_sales_data.values()], ChartType.BAR) # this will be under all others charts if cumulative_sales: mixed_chart.add_data(_("Cumulative Total Sales"), [v for v in cumulative_sales], ChartType.BAR) self.cached_chart = mixed_chart return mixed_chart
def get_chart(self): if self.cached_chart is not None: return self.cached_chart chart_options = { "scales": { "yAxes": [{ "ticks": { "beginAtZero": True } }] } } today = date.today() chart_start_date = today - timedelta(days=365) orders = get_orders_by_currency(self.currency) sum_sales_data = group_by_period( orders.valid().since((today - chart_start_date).days), "order_date", "month", sum=Sum("taxful_total_price_value") ) for (month, year) in month_iter(chart_start_date, today): sales_date = date(year, month, 1) if sales_date not in sum_sales_data: sum_sales_data[sales_date] = {"sum": Decimal(0)} # sort and recreated the ordered dict since we've put new items into sum_sales_data = OrderedDict(sorted(six.iteritems(sum_sales_data), key=lambda x: x[0])) locale = get_current_babel_locale() labels = [ format_date(k, format=get_year_and_month_format(locale), locale=locale) for k in sum_sales_data ] mixed_chart = MixedChart(title=_("Sales per Month (past 12 months)"), labels=labels, data_type=ChartDataType.CURRENCY, options=chart_options, currency=self.currency, locale=locale) cumulative_sales = [] average_sales = [] # only calculate cumulative and average if there are at least 3 months if len(sum_sales_data) >= 3: count = 0 total = Decimal() for month_sale in sum_sales_data.values(): total = total + month_sale["sum"] cumulative_sales.append(total) average_sales.append(total / (count+1)) count = count + 1 # this will be on top of all bars if average_sales: mixed_chart.add_data(_("Average Sales"), [v for v in average_sales], ChartType.LINE) # this will be under the cummulative bars mixed_chart.add_data(_("Sales"), [v["sum"] for v in sum_sales_data.values()], ChartType.BAR) # this will be under all others charts if cumulative_sales: mixed_chart.add_data(_("Cumulative Total Sales"), [v for v in cumulative_sales], ChartType.BAR) self.cached_chart = mixed_chart return mixed_chart