i want show quarters in first row and years in second row

Open
#533 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python

Research direction

Start with the provided chart setup, especially chart.x_labels and chart.x_labels_major, and inspect how pygal renders major and regular x-axis labels. Reproduce the supplied quarterly data example and determine what API or rendering change would show quarters on one row and years on another; done means the generated chart visibly has both label levels.

Written by the indexing model from the issue text.

Description

image

Here is my code

import pygal
from openpyxl import load_workbook

Define a function to format the fiscal period labels

def format_fiscal_period(fiscal_period):
quarter, year = fiscal_period.split('-')
quarter = 'Q' + quarter[1]
return f"{quarter}-{year}"

Load the Excel file

wb = load_workbook(filename='C:/Users/wajiha/Test_Jupyter/Assets.xlsx')

Select the sheet you want to read data from

sheet = wb['Data']

Get the data from the sheet

data = []

for row in sheet.iter_rows(min_row=2, values_only=True):
data.append(row)

Group the data by year and quarter

data_by_year_and_quarter = {}
for row in data:
year = row[2]
quarter = row[0]
if year not in data_by_year_and_quarter:
data_by_year_and_quarter[year] = {}
data_by_year_and_quarter[year][quarter] = row[1]

Set the x-labels to be the years

#x_labels_major = ['2012', '2013', '2014', '2015']
x_labels = ['','2012','Q1-12','Q2-12','Q3-12','Q4-12','2013','Q1-13','Q2-13','Q3-13','Q4-13','2014','Q1-14','Q2-14','Q3-14',
'Q4-14','2015','Q1-15','Q2-15','Q3-15','Q4-15']

#x_labels =[format_fiscal_period(quarter)]

Create a Pygal chart

chart = pygal.Bar(x_label_rotation=90)
chart.title = 'Data from Excel'
chart.x_labels = x_labels
chart.x_labels_major = [x_labels[1], x_labels[6], x_labels[11], x_labels[16]]

Add the data to the chart

for year in sorted(data_by_year_and_quarter.keys()):
year_data = data_by_year_and_quarter[year]
data = [year_data.get(quarter, None) for quarter in x_labels]
chart.add(str(year), data)

Render the chart

chart.render_to_file('chart.svg')

Dominant language
Python
Stars
2.8k
Forks
419
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Kozea/pygal

All issues in Kozea/pygal

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.