Улучшить две шкалы, навигацию и формулы графиков
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
"""Local polynomial representation of the curves actually drawn on a plot."""
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
import math
|
||||
|
||||
|
||||
@@ -38,7 +39,7 @@ class FormulaPiece:
|
||||
+ f'{number(left)} ≤ x ≤ {number(self.right - origin)} {unit}').rstrip()
|
||||
|
||||
|
||||
def linear_piece(timestamps, values, x):
|
||||
def linear_piece(timestamps, values, x, window=None):
|
||||
"""A source polyline has a linear equation, not an inferred analytic model."""
|
||||
previous = None
|
||||
for stamp, value in zip(timestamps, values):
|
||||
@@ -48,11 +49,39 @@ def linear_piece(timestamps, values, x):
|
||||
if previous is not None:
|
||||
t0, y0 = previous
|
||||
if t0 <= x <= stamp and stamp > t0:
|
||||
if window is not None:
|
||||
left, right = max(t0, window[0]), min(stamp, window[1])
|
||||
if not left <= x <= right or left >= right:
|
||||
return None
|
||||
slope = (value - y0) / (stamp - t0)
|
||||
return FormulaPiece(left, right, (y0 + slope * (left - t0), slope * (right - left)))
|
||||
return FormulaPiece(t0, stamp, (y0, value - y0))
|
||||
previous = stamp, value
|
||||
return None
|
||||
|
||||
|
||||
@lru_cache(maxsize=32)
|
||||
def window_curve(curve, left, right):
|
||||
"""Reconstruct a formula using only source samples in the focused window."""
|
||||
from dataclasses import replace
|
||||
from .plot_processing import process
|
||||
request = curve.request
|
||||
points = tuple((x, y) for x, y in request.series.points if left <= x <= right)
|
||||
if points == request.series.points:
|
||||
return curve
|
||||
if len({x for x, _ in points}) < 2:
|
||||
return None
|
||||
try:
|
||||
return process(replace(request, series=replace(request.series, points=points), x_range=(left, right)))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def window_piece(curve, x, left, right):
|
||||
local = window_curve(curve, left, right)
|
||||
return local.piece_at(x) if local is not None and left <= x <= right else None
|
||||
|
||||
|
||||
def polynomial_coefficients(nodes, values):
|
||||
"""Convert evaluations of an existing polynomial to power coefficients."""
|
||||
divided = list(values)
|
||||
|
||||
Reference in New Issue
Block a user