# -*- coding: utf-8 -*-
from django.utils import timezone
from django.utils.translation import gettext as _

import plotly.offline as plotly
import plotly.graph_objs as go

from reports import utils


def pumping_amounts(objects):
    """
    Create a graph showing pumping amounts over time.
    :param instances: a QuerySet of Pumping instances.
    :returns: a tuple of the the graph's html and javascript.
    """
    objects = objects.order_by("-time")

    trace = go.Bar(
        name=_("Pumping"),
        x=list(objects.values_list("time", flat=True)),
        y=list(objects.values_list("amount", flat=True)),
        fill="tozeroy",
    )

    layout_args = utils.default_graph_layout_options()
    layout_args["barmode"] = "stack"
    layout_args["title"] = _("<b>Pumping</b>")
    layout_args["xaxis"]["title"] = _("Date")
    layout_args["xaxis"]["rangeselector"] = utils.rangeselector_date()
    layout_args["yaxis"]["title"] = _("Pumping")

    fig = go.Figure({"data": [trace], "layout": go.Layout(**layout_args)})
    output = plotly.plot(fig, output_type="div", include_plotlyjs=False)
    return utils.split_graph_output(output)

@@ -443,17 +443,16 @@
 der_by(%22
--
 time%22)%0A%0A
@@ -459,106 +459,794 @@
     
-trace = go.Bar(%0A        name=_(%22Pumping%22),%0A        x=list(objects.values_list(%22time%22, flat=True
+# We need to find date totals for annotations at the end%0A    curr_date = ''%0A    date_totals = %5B0.0%5D%0A    index = -1%0A    for object in objects:%0A        date_s = str(object.time.date())%0A        if curr_date != date_s:%0A            index += 1%0A            date_totals.append(0.0)%0A            curr_date = date_s%0A        date_totals%5Bindex%5D += object.amount%0A    date_totals.pop(-1)%0A%0A    dates = %5B%5D # Single array for each bar%0A    amounts = %5B%5D # Array of arrays containing amounts%0A    index_x, index_y = 0,-1%0A    for object in objects:%0A        date_s = str(object.time.date())%0A        if date_s not in dates:%0A            dates.append(date_s)%0A            index_y += 1%0A            index_x = 0%0A        if len(amounts) == 0 or len(amounts) %3C= index_x:%0A            amounts.append(%5B0%5D*len(date_totals
 ))
-,
 %0A   
@@ -1254,82 +1254,395 @@
     
-y=list(objects.values_list(%22amount%22, flat=True)),%0A        fill=%22tozeroy%22,%0A
+amounts%5Bindex_x%5D%5Bindex_y%5D = object.amount%0A        index_x += 1%0A        %0A    traces = %5B%5D%0A    for i in range(0, len(date_totals)-1):%0A        traces.append(%0A            go.Bar(%0A                name=%22Amount%22,%0A                x=dates,%0A                y=amounts%5Bi%5D,%0A                text=amounts%5Bi%5D,%0A                hovertemplate=amounts%5Bi%5D,%0A                showlegend=False,%0A            )%0A    
     
@@ -1703,45 +1703,8 @@
 s()%0A
-    layout_args%5B%22barmode%22%5D = %22stack%22%0A
     
@@ -1732,23 +1732,36 @@
 = _(%22%3Cb%3E
+Total 
 Pumping
+ Amount
 %3C/b%3E%22)%0A 
@@ -1922,20 +1922,159 @@
 %22Pumping
+ Amount
 %22)%0A%0A
+    total_labels = %5B%7B%22x%22: x, %22y%22: total*1.1, %22text%22: str(total), %22showarrow%22: False%7D for x, total in zip(list(dates), date_totals)%5D%0A
     fig 
@@ -2098,15 +2098,14 @@
 a%22: 
-%5B
 trace
-%5D
+s
 , %22l
@@ -2139,16 +2139,81 @@
 args)%7D)%0A
+    fig.update_layout(barmode=%22stack%22, annotations=total_labels)%0A
     outp

Fix graphs for pumping amounts