From 09982ece64636d801a37c9d152a8211055da17db Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Thu, 16 Jun 2022 06:16:39 -0600 Subject: [PATCH] graphing with random numbers works --- graph/graph.go | 16 +++++++++------- main.go | 9 ++++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/graph/graph.go b/graph/graph.go index 75ce734..47e3a2e 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -40,7 +40,7 @@ func SetColorScheme(c []color.Color) { } } -func ColorForFrequency(freq, min, max int) color.Color { +func ColorForFrequency(freq, min, max int) sc.SimpleColor { spread := max - min interval := float64(spread) / float64(len(colorScheme)) colorIndex := 0 @@ -53,7 +53,7 @@ func ColorForFrequency(freq, min, max int) color.Color { return colorScheme[colorIndex] } func GetImage(frequencies []int) bytes.Buffer { - squareColors := []color.Color{} + squareColors := []sc.SimpleColor{} min, max := minmax(frequencies) for _, f := range frequencies { squareColors = append(squareColors, ColorForFrequency(f, min, max)) @@ -91,13 +91,15 @@ func drawImage(c []sc.SimpleColor) bytes.Buffer { //TODO here, draw suqares in appropriate colors, hopefully as an svg var sb bytes.Buffer sbw := bufio.NewWriter(&sb) - width := 500 - height := 500 + width := 717 + height := 112 + squareLength := 10 canvas := svg.New(sbw) canvas.Start(width, height) - canvas.Square(width/20, height/300, width/10, fmt.Sprintf("fill:%s", colorScheme[0].HexString())) - canvas.Circle(width/2, height/2, 100) - canvas.Text(width/2, height/2, "Hello, SVG", "text-anchor:middle;font-size:30px;fill:white") + for i, c := range c { + canvas.Square(10+squareLength+width/52*(i/7), squareLength/2+height/7*(i%7), squareLength, fmt.Sprintf("fill:%s", c.HexString())) + } + canvas.Text(width/100, squareLength*2+10, "Mon", "text-anchor:middle;font-size:10px;fill:black") canvas.End() sbw.Flush() return sb diff --git a/main.go b/main.go index 6af0efa..de0ad49 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "math/rand" "os" + "time" "github.com/taigrr/gitgraph/graph" ) @@ -10,7 +12,12 @@ import ( type DayCount [366]int func main() { - svg := graph.GetImage([]int{1, 2, 5, 6, 5, 4, 5, 8, 7, 43, 2, 3}) + freq := []int{} + rand.Seed(time.Now().UnixMilli()) + for i := 0; i < 366; i++ { + freq = append(freq, rand.Int()) + } + svg := graph.GetImage(freq) f, err := os.Create("out.svg") if err != nil { fmt.Printf("Error: %v\n", err)