Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Created August 3, 2015 18:06
Show Gist options
  • Save ajstarks/b0922afeffaf2fb52adf to your computer and use it in GitHub Desktop.
Save ajstarks/b0922afeffaf2fb52adf to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/ajstarks/svgo"
)
type shotchart struct {
Resource string `json:"resource"`
Parameters struct {
Leagueid string `json:"LeagueID"`
Season string `json:"Season"`
Seasontype string `json:"SeasonType"`
Teamid int `json:"TeamID"`
Playerid int `json:"PlayerID"`
Contextfilter string `json:"ContextFilter"`
Contextmeasure string `json:"ContextMeasure"`
} `json:"parameters"`
Resultsets []struct {
Name string `json:"name"`
Headers []string `json:"headers"`
Rowset [][]interface{} `json:"rowSet"`
} `json:"resultSets"`
}
func vmap(value, low1, high1, low2, high2 float64) float64 {
return low2 + (high2-low2)*(value-low1)/(high1-low1)
}
func shots(filename string) {
var shots shotchart
var r io.ReadCloser
r, err := os.Open(filename + ".json")
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
defer r.Close()
err = json.NewDecoder(r).Decode(&shots)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return
}
canvas := svg.New(os.Stdout)
width, height := 500, 470
fw, fh := float64(width), float64(height)
imw, imh := 230, 185
top := fw / 10
canvas.Start(width, height)
canvas.Rect(0, 0, width, height, "fill:none;stroke:black;stroke-width:3")
canvas.Image(width-imw, height-imh, imw, imh, filename+".png")
canvas.Gstyle("font-family:Calibri;sans-serif;font-size:16px")
nfg := 0
for _, r := range shots.Resultsets {
if r.Name == "Shot_Chart_Detail" {
var playername string
attempts := len(r.Rowset)
for _, rs := range r.Rowset {
var x, y float64
var fill string
for i, v := range rs {
if i == 4 {
playername = v.(string)
}
if i == 17 {
x = v.(float64)
}
if i == 18 {
y = v.(float64)
}
if i == 20 {
xp := int(vmap(x, -300, 300, 0, fw))
yp := int(vmap(y, -300, 300, top, fh))
if v.(float64) == 0 {
fill = "red"
} else {
nfg++
fill = "black"
}
canvas.Circle(xp, (yp-(height/2))+10, 4, "fill-opacity:0.3;fill:"+fill)
}
}
}
fgpct := (float64(nfg) / float64(attempts)) * 100
canvas.Text(10, height-40, playername, "fill:gray")
canvas.Text(10, height-20, fmt.Sprintf("%d out of %d", attempts, nfg), "fill:gray")
canvas.Text(2*width/5, height-20, fmt.Sprintf("%.1f%%", fgpct), "font-size:120%;fill:gray")
canvas.Gend()
}
}
canvas.End()
}
func main() {
for _, file := range os.Args[1:] {
shots(file)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment