Skip to content

Commit

Permalink
Create charts
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-5546 committed Jan 5, 2024
1 parent db3ebc6 commit c860718
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/xlbudget/rwxlb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pandas as pd
from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.styles import Alignment, Font
from openpyxl.utils import get_column_letter
from openpyxl.utils.cell import column_index_from_string, coordinate_from_string
Expand All @@ -16,9 +17,10 @@

FORMAT_ACCOUNTING = '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)'
FORMAT_DATE = "MM/DD/YYYY"
FORMAT_NUMBER = '_($* #,##0_);_($* (#,##0);_($* "-"??_);_(@_)'

MONTH_NAME_0_IND = calendar.month_name[1:]
MONTH_TABLES_ROW = 16
MONTH_TABLES_ROW = 17
MONTH_TABLES_COL = 6


Expand Down Expand Up @@ -56,6 +58,10 @@ def __init__(self, ref: str) -> None:

self.__last_col, self.__initial_last_row = coordinate_from_string(end)

@property
def header_row(self) -> int:
return self.__header_row

@property
def first_col(self) -> int:
return self.__first_col_ind
Expand All @@ -75,10 +81,10 @@ def get_ref(self) -> str:
# implemented as follows so that `next_row` can be incremented consistently.
last_row = (
self.next_row - 1
if self.next_row - 1 >= self.__header_row + 1
else self.__header_row + 1
if self.next_row - 1 >= self.header_row + 1
else self.header_row + 1
)
return f"{self.__first_col}{self.__header_row}:{self.__last_col}{last_row}"
return f"{self.__first_col}{self.header_row}:{self.__last_col}{last_row}"


def create_year_sheet(wb: Workbook, year: int) -> None:
Expand Down Expand Up @@ -154,12 +160,35 @@ def create_year_sheet(wb: Workbook, year: int) -> None:
# set month cell
ws.cell(row=summ_tab_pos.next_row, column=summ_tab_pos.first_col).value = "Total"

# set other cells
# set other cells and create charts
for i in range(1, len(SUMMARY_COLUMNS)):
cell = ws.cell(row=summ_tab_pos.next_row, column=summ_tab_pos.first_col + i)
cell.value = f"=SUM({summ_table_name}[{SUMMARY_COLUMNS[i].name}])"
cell.number_format = SUMMARY_COLUMNS[i].format

chart = BarChart()
data = Reference(
ws,
min_col=i + 1,
min_row=summ_tab_pos.header_row,
max_row=summ_tab_pos.next_row - 1,
)
cats = Reference(
ws,
min_col=summ_tab_pos.first_col,
min_row=summ_tab_pos.header_row + 1,
max_row=summ_tab_pos.next_row - 1,
)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
chart.legend = None
chart.y_axis.numFmt = FORMAT_NUMBER
chart.height = 7.5
chart.width = 8.5
start_col = MONTH_TABLES_COL + (i - 1) * (len(MONTH_COLUMNS) + 1)
anchor = f"{get_column_letter(start_col)}1"
ws.add_chart(chart, anchor)


def _add_table(
ws: Worksheet,
Expand Down

0 comments on commit c860718

Please sign in to comment.