diff --git a/pybadges/__init__.py b/pybadges/__init__.py index 2180724..0f6cd7a 100644 --- a/pybadges/__init__.py +++ b/pybadges/__init__.py @@ -199,11 +199,8 @@ def badge( raise ValueError('right-image and trend cannot be used together.') if show_trend: - samples = show_trend if len(show_trend) <= 10 else show_trend[:10] - if len(samples) < 10: - samples = [0] * (10 - len(samples)) + samples right_image = trend( - samples=samples, + samples=show_trend, stroke_color=(trend_color or right_color), stroke_width=trend_width, ) diff --git a/pybadges/badge-template-full.svg b/pybadges/badge-template-full.svg index 4ae9b39..469a4d6 100644 --- a/pybadges/badge-template-full.svg +++ b/pybadges/badge-template-full.svg @@ -1,6 +1,6 @@ {% set logo_width = 14 if logo else 0 %} {% set logo_padding = 3 if (logo and left_text) else 0 %} -{% set image_width = 110 if right_image else 0 %} +{% set image_width = 107 if right_image else 0 %} {% set left_width = image_width + left_text_width + 10 + logo_width + logo_padding %} {% set right_width = right_text_width + 10 %} diff --git a/pybadges/trend.py b/pybadges/trend.py index ec86bcd..abb2a83 100644 --- a/pybadges/trend.py +++ b/pybadges/trend.py @@ -1,13 +1,12 @@ from typing import Optional, List, Tuple import drawSvg as draw -import itertools import numpy as np import pybadges HEIGHT = 13 -WIDTH = 110 +WIDTH = 107 X_OFFSET = 7 Y_OFFSET = 1 @@ -19,23 +18,11 @@ def normalize(arr: np.ndarray) -> np.ndarray: return arr -def repeat(samples: List[int], n: int) -> List[int]: - """Repeats a value n times in an array. - - Args: - samples: The list of all elements to be repeated. - n: Number of times to repeat each element in samples. - """ - return list( - itertools.chain.from_iterable( - itertools.repeat(sample, n) for sample in samples)) - - def fit_data(samples: List[int]) -> Tuple[List[int], List[int]]: - y = list( - itertools.chain.from_iterable( - itertools.repeat(sample, 10) for sample in samples)) - xp = np.arange(len(y)) + width = WIDTH - X_OFFSET + N = int(width / len(samples)) + y = np.repeat(samples, N) + xp = np.linspace(start=X_OFFSET, stop=width, num=len(y)) yp = normalize(np.poly1d(np.polyfit(xp, y, 15))(xp)) yp[yp > 0] *= (HEIGHT - 2) return xp, yp @@ -51,9 +38,9 @@ def trend(samples: List[int], stroke_color: str, stroke_width: int) -> str: ) xp, yp = fit_data(samples) - path.M(X_OFFSET + xp[0], yp[0]) + path.M(xp[0], yp[0]) for x, y in zip(xp[1:], yp[1:]): - path.L(X_OFFSET + x, y) + path.L(x, y) canvas.append(path) return canvas.asDataUri()