mirror of
https://github.com/google/pybadges.git
synced 2026-01-07 20:00:07 +01:00
Stretch plot rather than zero padding
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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 %}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{{ left_width + right_width }}" height="20">
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user