diff --git a/internal/dirtoexcel/dirtoexcel.go b/internal/dirtoexcel/dirtoexcel.go index 2b5213f..3c6a89a 100644 --- a/internal/dirtoexcel/dirtoexcel.go +++ b/internal/dirtoexcel/dirtoexcel.go @@ -45,7 +45,7 @@ func (c Categories) Insert(company, value string) error { case "ALDI", "EDEKA": c["Food"] = append(c["Food"], Value{Company: company, Value: fValue}) 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: 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 } - count := 1 + length := 0 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) } - 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) } - 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 } + +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 +} diff --git a/internal/dirtoexcel/dirtoexcel_test.go b/internal/dirtoexcel/dirtoexcel_test.go index 5cc5fd4..dcd2f4e 100644 --- a/internal/dirtoexcel/dirtoexcel_test.go +++ b/internal/dirtoexcel/dirtoexcel_test.go @@ -29,6 +29,7 @@ func TestCreate(t *testing.T) { "bma-zon_12.34_bar.jpg", "aldi_10.98.jpg", "aldi_10.00.jpg", + "edeka_10.11.jpg", }, map[string][]want{ "Food": { @@ -36,8 +37,12 @@ func TestCreate(t *testing.T) { "ALDI", "20.98", }, + { + "EDEKA", + "10.11", + }, }, - "Non-Food": { + "NonFood": { { "AMAZON", "12.34", @@ -112,5 +117,5 @@ func TestCategoriesInsert(t *testing.T) { err := m.Insert("amazon", "11.11") 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) }