Playing around with SVG

This commit is contained in:
2017-07-07 02:07:34 +02:00
parent 137a2270df
commit 567e8e63d1
7 changed files with 153 additions and 0 deletions

7
utils/AnalyticsItem.go Normal file
View File

@ -0,0 +1,7 @@
package utils
// AnalyticsItem ...
type AnalyticsItem struct {
Key string
Value int
}

27
utils/SVGPieChartPath.go Normal file
View File

@ -0,0 +1,27 @@
package utils
import (
"fmt"
"math"
)
// coords returns the coordinates for the given percentage.
func coords(percent float64) (float64, float64) {
x := math.Cos(2 * math.Pi * percent)
y := math.Sin(2 * math.Pi * percent)
return x, y
}
// SVGSlicePath creates a path string for a slice in a pie chart.
func SVGSlicePath(from float64, to float64) string {
x1, y1 := coords(from)
x2, y2 := coords(to)
largeArc := "0"
if to-from > 0.5 {
largeArc = "1"
}
return fmt.Sprintf("M %.2f %.2f A 1 1 0 %s 1 %.2f %.2f L 0 0", x1, y1, largeArc, x2, y2)
}

9
utils/ToJSON.go Normal file
View File

@ -0,0 +1,9 @@
package utils
import "encoding/json"
// ToJSON converts an object to a JSON string, ignoring errors.
func ToJSON(v interface{}) string {
str, _ := json.Marshal(v)
return string(str)
}