From f58bd419ba9ae88ea374d75a2bb80333e970f93f Mon Sep 17 00:00:00 2001 From: pseusys Date: Mon, 13 Feb 2023 01:27:28 +0100 Subject: [PATCH] new graph drawing backend --- loc.py | 5 +- make_bar_graph.py | 138 +++++++++++++++++----------------------------- requirements.txt | 7 +-- 3 files changed, 53 insertions(+), 97 deletions(-) diff --git a/loc.py b/loc.py index f4c31f0..5e98026 100644 --- a/loc.py +++ b/loc.py @@ -10,7 +10,7 @@ from io import StringIO, BytesIO from dotenv import load_dotenv import time -from make_bar_graph import BarGraph +from make_bar_graph import build_graph class LinesOfCode: @@ -34,8 +34,7 @@ class LinesOfCode: return yearly_data def plotLoc(self, yearly_data): - graph = BarGraph(yearly_data) - graph.build_graph() + build_graph(yearly_data) self.pushChart() def run_query_v3(self, endPoint): diff --git a/make_bar_graph.py b/make_bar_graph.py index 94f06cd..207f402 100644 --- a/make_bar_graph.py +++ b/make_bar_graph.py @@ -1,106 +1,68 @@ -import os -import pandas as pd +from typing import Dict +from os.path import join, dirname +from json import load + import numpy as np -import altair as alt -import json -import os -# npm install vega-lite vega-cli canvas +import matplotlib.patches as mpatches +import matplotlib.pyplot as plt -class BarGraph: +MAX_LANGUAGES = 5 - def __init__(self, yearly_data): - self.yearly_data = yearly_data - def build_graph(self): - - with open(os.path.join(os.path.dirname(__file__), 'colors.json')) as f: - colors = json.load(f) - allColorsValues = [] +def build_graph(yearly_data: Dict) -> str: + """ + Draws graph of lines of code written by user by quarters of years. + Picks top `MAX_LANGUAGES` languages from each quarter only. - # filter data - max_languages = 5 - top_languages = {} - for year in self.yearly_data.keys(): - for quarter in self.yearly_data[year].keys(): - for language in sorted(list(self.yearly_data[year][quarter].keys()), - key=lambda lang: self.yearly_data[year][quarter][lang], reverse=True)[ - 0:max_languages]: - if 'top' not in self.yearly_data[year][quarter]: - self.yearly_data[year][quarter]['top'] = {} - if self.yearly_data[year][quarter][language] != 0: - self.yearly_data[year][quarter]['top'][language] = self.yearly_data[year][quarter][language] + :param yearly_data: GitHub user yearly data. + :return: String, path to graph file. + """ + with open(join(dirname(__file__), "colors.json")) as f: + colors = load(f) - if language not in top_languages: - top_languages[language] = 1 - top_languages[language] += 1 + languages_all_loc = dict() + years = len(yearly_data.keys()) + year_indexes = np.arange(years) - # print(self.yearly_data) + for i, y in enumerate(sorted(yearly_data.keys())): + for q in yearly_data[y].keys(): + langs = sorted(yearly_data[y][q].keys(), key=lambda l: yearly_data[y][q][l], reverse=True)[0:MAX_LANGUAGES] - all_languages = list(top_languages.keys()) + for lang in langs: + if lang not in languages_all_loc: + languages_all_loc[lang] = np.array([[0] * years] * 4) + languages_all_loc[lang][q - 1][i] = yearly_data[y][q][lang] - for language in all_languages: - if colors[language]['color'] is not None: - allColorsValues.append(colors[language]['color']) + fig = plt.figure() + ax = fig.add_axes([0, 0, 1.5, 1]) - languages_all_loc = {} + language_handles = [] + cumulative = np.array([[0] * years] * 4) - for language in all_languages: - language_year = [] - for year in self.yearly_data.keys(): - language_quarter = [0, 0, 0, 0] - for quarter in self.yearly_data[year].keys(): - if language in self.yearly_data[year][quarter]['top']: - language_quarter[quarter - 1] = self.yearly_data[year][quarter]['top'][language] - else: - language_quarter[quarter - 1] = 0 - language_year.append(language_quarter) - languages_all_loc[language] = language_year + for key, value in languages_all_loc.items(): + color = colors[key]["color"] if colors[key]["color"] is not None else "w" + language_handles += [mpatches.Patch(color=color, label=key)] - # print(languages_all_loc) + for quarter in range(4): + ax.bar(year_indexes + quarter * 0.21, value[quarter], 0.2, bottom=cumulative[quarter], color=color) + cumulative[quarter] = np.add(cumulative[quarter], value[quarter]) - language_df = {} + ax.set_ylabel("LOC added", fontdict=dict(weight="bold")) + ax.set_xticks(np.array([np.arange(i, i + 0.84, step=0.21) for i in year_indexes]).flatten(), labels=["Q1", "Q2", "Q3", "Q4"] * years) - def prep_df(df, name): - df = df.stack().reset_index() - df.columns = ['c1', 'c2', 'values'] - df['Language'] = name - return df + sax = ax.secondary_xaxis("top") + sax.set_xticks(year_indexes + 0.42, labels=sorted(yearly_data.keys())) + sax.spines["top"].set_visible(False) - for language in languages_all_loc.keys(): - language_df[language] = pd.DataFrame(languages_all_loc[language], index=list(self.yearly_data.keys()), - columns=["Q1", "Q2", "Q3", "Q4"]) + ax.legend(title="Language", handles=language_handles, loc="upper left", bbox_to_anchor=(1, 1), framealpha=0, title_fontproperties=dict(weight="bold")) - for language in language_df.keys(): - language_df[language] = prep_df(language_df[language], language) + sax.tick_params(axis="both", length=0) + sax.spines["top"].set_visible(False) + ax.spines["top"].set_visible(False) + ax.spines["right"].set_visible(False) - df = pd.concat(language_df.values()) - - chart = alt.Chart(df).mark_bar().encode( - - # tell Altair which field to group columns on - x=alt.X('c2:N', title=None), - - # tell Altair which field to use as Y values and how to calculate - y=alt.Y('sum(values):Q', - axis=alt.Axis( - grid=False, - title='LOC added')), - - # tell Altair which field to use to use as the set of columns to be represented in each group - column=alt.Column('c1:N', title=None), - - # tell Altair which field to use for color segmentation - color=alt.Color('Language:N', - scale=alt.Scale( - domain=all_languages, - # make it look pretty with an enjoyable color pallet - range=allColorsValues, - ), - )) \ - .configure_view( - # remove grid lines around column clusters - strokeOpacity=0 - ) - chart.save('bar_graph.png') - return 'bar_graph.png' + plt.ylim(0, 1.05 * np.amax(cumulative)) + plt.savefig("bar_graph.png", bbox_inches="tight") + plt.close(fig) + return "bar_graph.png" diff --git a/requirements.txt b/requirements.txt index cdc1bf0..e4795bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,3 @@ -altair==4.1.0 -altair-data-server==0.4.1 -altair-saver==0.5.0 -altair-viewer==0.3.0 attrs==20.3.0 certifi==2020.12.5 chardet==4.0.0 @@ -14,9 +10,8 @@ Jinja2==2.11.3 jsonschema==3.2.0 kiwisolver==1.3.1 MarkupSafe==1.1.1 -matplotlib==3.4.1 +matplotlib==3.6.3 numpy==1.20.2 -pandas==1.2.3 Pillow==8.2.0 portpicker==1.3.1 PyGithub==1.54.1