| import io, base64 | |
| import matplotlib.pyplot as plt | |
| def _b64(fig): | |
| buf = io.BytesIO() | |
| fig.savefig(buf, format="png", bbox_inches="tight") | |
| plt.close(fig) | |
| return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode("utf-8") | |
| def line_chart_base64(xs, ys, xlabel="", ylabel="", title=""): | |
| fig, ax = plt.subplots() | |
| ax.plot(xs, ys) | |
| ax.set(xlabel=xlabel, ylabel=ylabel, title=title) | |
| return _b64(fig) | |
| def materiality_base64(labels, x_vals, y_vals, title="Materiality Matrix"): | |
| fig, ax = plt.subplots() | |
| ax.scatter(x_vals, y_vals) | |
| for lbl, x, y in zip(labels, x_vals, y_vals): | |
| ax.annotate(lbl, (x, y), xytext=(5,5), textcoords="offset points") | |
| ax.set(xlabel="Stakeholder Importance", ylabel="Business Impact", title=title) | |
| ax.grid(True, alpha=0.3) | |
| return _b64(fig) | |