graphing with random numbers works

This commit is contained in:
2022-06-16 06:16:39 -06:00
parent 37a5894e41
commit 09982ece64
2 changed files with 17 additions and 8 deletions

View File

@@ -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 spread := max - min
interval := float64(spread) / float64(len(colorScheme)) interval := float64(spread) / float64(len(colorScheme))
colorIndex := 0 colorIndex := 0
@@ -53,7 +53,7 @@ func ColorForFrequency(freq, min, max int) color.Color {
return colorScheme[colorIndex] return colorScheme[colorIndex]
} }
func GetImage(frequencies []int) bytes.Buffer { func GetImage(frequencies []int) bytes.Buffer {
squareColors := []color.Color{} squareColors := []sc.SimpleColor{}
min, max := minmax(frequencies) min, max := minmax(frequencies)
for _, f := range frequencies { for _, f := range frequencies {
squareColors = append(squareColors, ColorForFrequency(f, min, max)) 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 //TODO here, draw suqares in appropriate colors, hopefully as an svg
var sb bytes.Buffer var sb bytes.Buffer
sbw := bufio.NewWriter(&sb) sbw := bufio.NewWriter(&sb)
width := 500 width := 717
height := 500 height := 112
squareLength := 10
canvas := svg.New(sbw) canvas := svg.New(sbw)
canvas.Start(width, height) canvas.Start(width, height)
canvas.Square(width/20, height/300, width/10, fmt.Sprintf("fill:%s", colorScheme[0].HexString())) for i, c := range c {
canvas.Circle(width/2, height/2, 100) canvas.Square(10+squareLength+width/52*(i/7), squareLength/2+height/7*(i%7), squareLength, fmt.Sprintf("fill:%s", c.HexString()))
canvas.Text(width/2, height/2, "Hello, SVG", "text-anchor:middle;font-size:30px;fill:white") }
canvas.Text(width/100, squareLength*2+10, "Mon", "text-anchor:middle;font-size:10px;fill:black")
canvas.End() canvas.End()
sbw.Flush() sbw.Flush()
return sb return sb

View File

@@ -2,7 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"os" "os"
"time"
"github.com/taigrr/gitgraph/graph" "github.com/taigrr/gitgraph/graph"
) )
@@ -10,7 +12,12 @@ import (
type DayCount [366]int type DayCount [366]int
func main() { 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") f, err := os.Create("out.svg")
if err != nil { if err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)