Skip to content

Commit

Permalink
Fix date display
Browse files Browse the repository at this point in the history
  • Loading branch information
donkomura committed Jul 31, 2019
1 parent 07c9b1f commit beb87bd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Binary file added cmd/ginvoice/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ package graph

import (
"bytes"
"github.com/future-architect/gbilling-plot/invoice"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
"image/color"
"log"
"math"
"strconv"
"time"
"unicode/utf8"

"github.com/future-architect/gbilling-plot/invoice"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)

func Draw(costList invoice.CostList) ([]byte, error) {
Expand Down Expand Up @@ -89,12 +91,16 @@ func addBarChart(p *plot.Plot, w vg.Length, costList invoice.CostList) error {

plotCostList := make(invoice.CostList, 0, len(costList))

log.Printf("costList length: %d\n", len(costList))

for _, c := range costList {
if currentProject == "" {
currentProject = c.Project
}

log.Printf("cost: %v\n", c)
if currentProject != c.Project {
plotCostList = plotCostList.Padding().Sort()
barChart, err := newBarChart(colorCount, plotCostList.Values(), w)
if err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions invoice/daily_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package invoice

import (
"sort"
"time"
)

type Cost struct {
Date string `bigquery:"date"`
Project string `bigquery:"project"`
Expand All @@ -40,3 +45,29 @@ func (list CostList) Values() []float64 {
}
return result
}

// return zero padded list to adopt month length
func (list CostList) Padding() CostList {
begin := time.Now().AddDate(0, 0, -29)
end := time.Now()
for d := begin; d.Day() < end.Day(); d = d.AddDate(0, 0, 1) {
isFind := false
for i := 0; i < len(list); i++ {
if d.Format("2006-01-02") == list[i].Date {
isFind = true
break
}
}
if !isFind {
list = append(list, Cost{d.Format("2006-01-02"), "", 0})
}
}
return list
}

func (list CostList) Sort() CostList {
sort.Slice(list, func(i, j int) bool {
return list[i].Date < list[j].Date
})
return list
}

0 comments on commit beb87bd

Please sign in to comment.