-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for displaying a trend line and embedding a right image #24
base: master
Are you sure you want to change the base?
Changes from all commits
443ca2f
2bcecc0
8b5650d
22f0e14
83a015a
f1b3f76
72a7ccf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Optional, List, Tuple | ||
|
||
import drawSvg as draw | ||
import numpy as np | ||
|
||
import pybadges | ||
|
||
HEIGHT = 13 | ||
WIDTH = 107 | ||
X_OFFSET = 7 | ||
Y_OFFSET = 1 | ||
|
||
|
||
def normalize(arr: np.ndarray) -> np.ndarray: | ||
max_arr = np.max(arr) | ||
if max_arr != 0: | ||
arr /= max_arr | ||
return arr | ||
|
||
|
||
def fit_data(samples: List[int]) -> Tuple[List[int], List[int]]: | ||
width = WIDTH - X_OFFSET | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you write a doc string to explain what this does? |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the bottom is always zero? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahm, I don't quite get the question. If all the values in Polyfit might return negative numbers sometime ( |
||
yp[yp > 0] *= (HEIGHT - 2) | ||
return xp, yp | ||
|
||
|
||
def trend(samples: List[int], stroke_color: str, stroke_width: int) -> str: | ||
canvas = draw.Drawing(WIDTH, HEIGHT, origin=(0, -Y_OFFSET)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you write a doc string explaining what this does? |
||
path = draw.Path( | ||
fill="transparent", | ||
stroke=pybadges._NAME_TO_COLOR.get(stroke_color, stroke_color), | ||
stroke_width=stroke_width, | ||
stroke_linejoin="round", | ||
) | ||
|
||
xp, yp = fit_data(samples) | ||
path.M(xp[0], yp[0]) | ||
for x, y in zip(xp[1:], yp[1:]): | ||
path.L(x, y) | ||
canvas.append(path) | ||
|
||
return canvas.asDataUri() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you write a doc string explaining what this does?