feat: adds charts for categories
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Marvin Preuss 2023-04-12 08:31:26 +00:00
parent fabaeab8d0
commit 4e80f25d32
2 changed files with 51 additions and 7 deletions

View File

@ -45,7 +45,7 @@ func (c Categories) Insert(company, value string) error {
case "ALDI", "EDEKA": case "ALDI", "EDEKA":
c["Food"] = append(c["Food"], Value{Company: company, Value: fValue}) c["Food"] = append(c["Food"], Value{Company: company, Value: fValue})
case "AMAZON": case "AMAZON":
c["Non-Food"] = append(c["Non-Food"], Value{Company: company, Value: fValue}) c["NonFood"] = append(c["NonFood"], Value{Company: company, Value: fValue})
default: default:
c["Other"] = append(c["Other"], Value{Company: company, Value: fValue}) c["Other"] = append(c["Other"], Value{Company: company, Value: fValue})
} }
@ -134,18 +134,22 @@ func Create(out, dir string) error {
m[i.Company] += i.Value m[i.Company] += i.Value
} }
count := 1 length := 0
for k, v := range m { for k, v := range m {
if err := f.SetCellStr(cat, fmt.Sprintf("A%d", count), k); err != nil { if err := f.SetCellStr(cat, fmt.Sprintf("A%d", length+1), k); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", count), v, 2, 64); err != nil { if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", length+1), v, 2, 64); err != nil {
return fmt.Errorf("failed to set cell: %w", err) return fmt.Errorf("failed to set cell: %w", err)
} }
count += 1 length += 1
}
if err := createCategoryChart(cat, length, f); err != nil {
return fmt.Errorf("failed to create category chart: %w", err)
} }
} }
@ -158,3 +162,38 @@ func Create(out, dir string) error {
return nil return nil
} }
func createCategoryChart(category string, length int, f *excelize.File) error {
if err := f.AddChart(category, "C1", &excelize.Chart{
Type: excelize.Doughnut,
Series: []excelize.ChartSeries{
{
Name: "Amount",
Categories: fmt.Sprintf("%s!A1:A%d", category, length),
Values: fmt.Sprintf("%s!B1:B%d", category, length),
},
},
Format: excelize.GraphicOptions{
OffsetX: 15,
OffsetY: 10,
},
Legend: excelize.ChartLegend{
Position: "right",
},
Title: excelize.ChartTitle{
Name: category,
},
PlotArea: excelize.ChartPlotArea{
ShowCatName: false,
ShowLeaderLines: false,
ShowPercent: true,
ShowSerName: false,
ShowVal: false,
},
ShowBlanksAs: "zero",
}); err != nil {
return fmt.Errorf("failed to create chart: %w", err)
}
return nil
}

View File

@ -29,6 +29,7 @@ func TestCreate(t *testing.T) {
"bma-zon_12.34_bar.jpg", "bma-zon_12.34_bar.jpg",
"aldi_10.98.jpg", "aldi_10.98.jpg",
"aldi_10.00.jpg", "aldi_10.00.jpg",
"edeka_10.11.jpg",
}, },
map[string][]want{ map[string][]want{
"Food": { "Food": {
@ -36,8 +37,12 @@ func TestCreate(t *testing.T) {
"ALDI", "ALDI",
"20.98", "20.98",
}, },
{
"EDEKA",
"10.11",
},
}, },
"Non-Food": { "NonFood": {
{ {
"AMAZON", "AMAZON",
"12.34", "12.34",
@ -112,5 +117,5 @@ func TestCategoriesInsert(t *testing.T) {
err := m.Insert("amazon", "11.11") err := m.Insert("amazon", "11.11")
is.NoErr(err) is.NoErr(err)
is.Equal(dirtoexcel.Categories{"Non-Food": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m) is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m)
} }