# Path: core/gui/balance_graph.py
# class BalanceGraph(Graph):
#     # BalanceGraph's data point is (float x, float y)
#     # --- Virtual
#     def _balance_for_date(self, date):
#         return 0
# 
#     # --- Override
#     def compute_data(self):
#         date_range = self.document.date_range
#         TODAY = date.today()
#         date2value = {}
#         last_balance = self._balance_for_date(date_range.start - ONE_DAY)
#         if last_balance:
#             date2value[date_range.start] = last_balance
#         for date_point in date_range:
#             balance = self._balance_for_date(date_point)
#             if (balance != last_balance) or (date_point == TODAY) or (date_point == date_range.end):
#                 if date2value and last_balance != balance:
#                     # create a "step"
#                     date2value[date_point] = last_balance
#                 date2value[date_point + ONE_DAY] = balance
#                 last_balance = balance
#         for date_point, value in list(date2value.items()):
#             if date_point <= TODAY:
#                 continue
#         self._data = []
#         # if there's only zeroes, keep the data empty
#         if any(date2value.values()):
#             for date_point, value in sorted(date2value.items()):
#                 pos = self._offset_xpos(date_point.toordinal())
#                 self._data.append((pos, float(value)))
# 
#     def yrange(self):
#         if self._data:
#             ymin = min(point[1] for point in self._data)
#             ymax = max(point[1] for point in self._data)
#             return (ymin, ymax)
#         else:
#             return (0, 1)
# 
#     def draw_graph(self, context):
#         if len(self.data) < 2:
#             return
# 
#         points = [Point(x*context.xfactor, y*context.yfactor) for x, y in self.data]
# 
#         # close the polygons and fill them.
#         # The closing point depends if we have a positive graph, a negative one or a mixed up
#         if self.ymin >= 0: # positive
#             yClose = round(self.ymin * context.yfactor)
#         elif self.ymax < 0: # negative
#             yClose = round(self.ymax * context.yfactor)
#         else: # mixed up
#             yClose = 0
#         # painter.setPen(QPen(Qt.NoPen))
#         xTodayfactored = self._offset_xpos(date.today().toordinal() + 1) * context.xfactor
#         pastPoints = [p for p in points if p.x <= xTodayfactored]
#         futurePoints = [p for p in points if p.x > xTodayfactored]
#         if pastPoints and futurePoints:
#             meetingPoint = Point(xTodayfactored, pastPoints[-1].y)
#             pastPoints.append(meetingPoint)
#             futurePoints.insert(0, meetingPoint)
#         else:
#             meetingPoint = None
#         # start with past
#         if pastPoints:
#             firstPoint = pastPoints[0]
#             lastPoint = pastPoints[-1]
#             pastPoints.append(Point(lastPoint.x, yClose))
#             pastPoints.append(Point(firstPoint.x, yClose))
#             self.view.draw_polygon(context.trpoints(pastPoints), None, BrushID.GraphNormal)
#         if futurePoints:
#             firstPoint = futurePoints[0]
#             lastPoint = futurePoints[-1]
#             futurePoints.append(Point(lastPoint.x, yClose))
#             futurePoints.append(Point(firstPoint.x, yClose))
#             self.view.draw_polygon(context.trpoints(futurePoints), None, BrushID.GraphFuture)
#         if meetingPoint is not None:
#             p1 = context.trpoint(Point(xTodayfactored, yClose))
#             p2 = context.trpoint(meetingPoint)
#             self.view.draw_line(p1, p2, PenID.TodayLine)
# 
#         self.draw_axis_overlay_y(context)
#         self.draw_axis_overlay_x(context)
# 
#         # draw the main graph line. It looks better when that line is drawn after the overlay.
#         self.view.draw_polygon(context.trpoints(points), PenID.Graph, None)
# 
#     @property
#     def currency(self):
#         return None


from core.trans import tr
from .balance_graph import BalanceGraph
# Copyright 2019 Virgil Dupras
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html


class NetWorthGraph(BalanceGraph):
    def __init__(self, networth_view):
        BalanceGraph.__init__(self, networth_view)

    def _balance_for_date(self, date):
        def bal(a):
            entries = self.document.accounts.entries_for_account(a)
            return entries.balance(date, self._currency)

        return sum(map(bal, self._accounts))

    def compute_data(self):
        accounts = set(a for a in self.document.accounts if a.is_balance_sheet_account())
        self._accounts = accounts - self.document.excluded_accounts
        self._currency = self.document.default_currency

# Path: core/gui/balance_graph.py
# class BalanceGraph(Graph):
#     # BalanceGraph's data point is (float x, float y)
#     # --- Virtual
#     def _balance_for_date(self, date):
#         return 0
# 
#     # --- Override
#     def compute_data(self):
#         date_range = self.document.date_range
#         TODAY = date.today()
#         date2value = {}
#         last_balance = self._balance_for_date(date_range.start - ONE_DAY)
#         if last_balance:
#             date2value[date_range.start] = last_balance
#         for date_point in date_range:
#             balance = self._balance_for_date(date_point)
#             if (balance != last_balance) or (date_point == TODAY) or (date_point == date_range.end):
#                 if date2value and last_balance != balance:
#                     # create a "step"
#                     date2value[date_point] = last_balance
#                 date2value[date_point + ONE_DAY] = balance
#                 last_balance = balance
#         for date_point, value in list(date2value.items()):
#             if date_point <= TODAY:
#                 continue
#         self._data = []
#         # if there's only zeroes, keep the data empty
#         if any(date2value.values()):
#             for date_point, value in sorted(date2value.items()):
#                 pos = self._offset_xpos(date_point.toordinal())
#                 self._data.append((pos, float(value)))
# 
#     def yrange(self):
#         if self._data:
#             ymin = min(point[1] for point in self._data)
#             ymax = max(point[1] for point in self._data)
#             return (ymin, ymax)
#         else:
#             return (0, 1)
# 
#     def draw_graph(self, context):
#         if len(self.data) < 2:
#             return
# 
#         points = [Point(x*context.xfactor, y*context.yfactor) for x, y in self.data]
# 
#         # close the polygons and fill them.
#         # The closing point depends if we have a positive graph, a negative one or a mixed up
#         if self.ymin >= 0: # positive
#             yClose = round(self.ymin * context.yfactor)
#         elif self.ymax < 0: # negative
#             yClose = round(self.ymax * context.yfactor)
#         else: # mixed up
#             yClose = 0
#         # painter.setPen(QPen(Qt.NoPen))
#         xTodayfactored = self._offset_xpos(date.today().toordinal() + 1) * context.xfactor
#         pastPoints = [p for p in points if p.x <= xTodayfactored]
#         futurePoints = [p for p in points if p.x > xTodayfactored]
#         if pastPoints and futurePoints:
#             meetingPoint = Point(xTodayfactored, pastPoints[-1].y)
#             pastPoints.append(meetingPoint)
#             futurePoints.insert(0, meetingPoint)
#         else:
#             meetingPoint = None
#         # start with past
#         if pastPoints:
#             firstPoint = pastPoints[0]
#             lastPoint = pastPoints[-1]
#             pastPoints.append(Point(lastPoint.x, yClose))
#             pastPoints.append(Point(firstPoint.x, yClose))
#             self.view.draw_polygon(context.trpoints(pastPoints), None, BrushID.GraphNormal)
#         if futurePoints:
#             firstPoint = futurePoints[0]
#             lastPoint = futurePoints[-1]
#             futurePoints.append(Point(lastPoint.x, yClose))
#             futurePoints.append(Point(firstPoint.x, yClose))
#             self.view.draw_polygon(context.trpoints(futurePoints), None, BrushID.GraphFuture)
#         if meetingPoint is not None:
#             p1 = context.trpoint(Point(xTodayfactored, yClose))
#             p2 = context.trpoint(meetingPoint)
#             self.view.draw_line(p1, p2, PenID.TodayLine)
# 
#         self.draw_axis_overlay_y(context)
#         self.draw_axis_overlay_x(context)
# 
#         # draw the main graph line. It looks better when that line is drawn after the overlay.
#         self.view.draw_polygon(context.trpoints(points), PenID.Graph, None)
# 
#     @property
#     def currency(self):
#         return None


# Path: core/gui/net_worth_graph.py
from core.trans import tr
from .balance_graph import BalanceGraph

# Copyright 2019 Virgil Dupras
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html


class NetWorthGraph(BalanceGraph):
    def __init__(self, networth_view):
        BalanceGraph.__init__(self, networth_view)

    def _balance_for_date(self, date):
        def bal(a):
            entries = self.document.accounts.entries_for_account(a)
            return entries.balance(date, self._currency)

        return sum(map(bal, self._accounts))

    def compute_data(self):
        accounts = set(a for a in self.document.accounts if a.is_balance_sheet_account())
        self._accounts = accounts - self.document.excluded_accounts
        self._currency = self.document.default_currency

        BalanceGraph.compute_data(self)