Plot.py 3.96 KiB
# Plot.py -- Pamhyr
# Copyright (C) 2023  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-

from datetime import datetime

from tools import timer, trace
from View.Tools.PamhyrPlot import PamhyrPlot

from PyQt5.QtCore import (
    QCoreApplication
)

from View.LateralContribution.Edit.translate import *

_translate = QCoreApplication.translate


class Plot(PamhyrPlot):
    def __init__(self, mode="time", data=None,
                 canvas=None, trad=None, toolbar=None,
                 parent=None):
        super(Plot, self).__init__(
            canvas=canvas,
            trad=trad,
            data=data,
            toolbar=toolbar,
            parent=parent
        )

        self._table_headers = self._trad.get_dict("table_headers")
        self._mode = mode

    def custom_ticks(self):
        if self.data.header[0] != "time":
            return

        t0 = datetime.fromtimestamp(0)
        nb = len(self.data.data)
        mod = int(nb / 5)
        mod = mod if mod > 0 else nb

        fx = list(
            map(
                lambda x: x[1],
                filter(
                    lambda x: x[0] % mod == 0,
                    enumerate(self.data.data)
                )
            )
        )
        xx = list(map(lambda v: v[0], fx))
        if self._mode == "time":
            xt = list(
                map(
                    lambda v: str(
                        datetime.fromtimestamp(v[0]) - t0
                    ).split(",")[0]
                    .replace("days", _translate("LateralContribution", "days"))
                    .replace("day", _translate("LateralContribution", "day")),
                    fx
                )
            )
        else:
            xt = list(
                map(
                    lambda v: str(datetime.fromtimestamp(v[0]).date()),
                    fx
                )
            )

        self.canvas.axes.set_xticks(ticks=xx, labels=xt, rotation=45)

    @timer
    def draw(self):
        self.canvas.axes.cla()
        self.canvas.axes.grid(color='grey', linestyle='--', linewidth=0.5)

        if len(self.data) == 0:
            self._init = False
            return

        # Plot data
        x = list(map(lambda v: v[0], self.data.data))
        y = list(map(lambda v: v[1], self.data.data))
        self._line, = self.canvas.axes.plot(
            x, y,
            color='r', lw=1.,
            markersize=5, marker='+',
            picker=30,
        )

        self.custom_ticks()

        # Plot label
        header = self.data.header
        self.canvas.axes.set_xlabel(
            self._table_headers[header[0]], color='black', fontsize=10
        )
        self.canvas.axes.set_ylabel(
            self._table_headers[header[1]], color='black', fontsize=10
        )

        self.canvas.axes.autoscale_view(True, True, True)
        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()
        # self.toolbar.update()

        self._init = True

    @timer
    def update(self, ind=None):
        if not self._init:
            self.draw()
            return

        x = list(map(lambda v: v[0], self.data.data))
        y = list(map(lambda v: v[1], self.data.data))

        self._line.set_data(x, y)

        self.custom_ticks()

        self.canvas.axes.relim()
        self.canvas.axes.autoscale()
        self.canvas.figure.tight_layout()
        self.canvas.figure.canvas.draw_idle()