2016-08-06 01:55:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/wcharczuk/go-chart"
|
|
|
|
)
|
|
|
|
|
|
|
|
func drawChart(res http.ResponseWriter, req *http.Request) {
|
|
|
|
sbc := chart.BarChart{
|
2018-01-24 18:43:24 +01:00
|
|
|
Title: "Test Bar Chart",
|
|
|
|
TitleStyle: chart.StyleShow(),
|
|
|
|
Background: chart.Style{
|
|
|
|
Padding: chart.Box{
|
|
|
|
Top: 40,
|
|
|
|
},
|
|
|
|
},
|
2016-08-06 01:55:55 +02:00
|
|
|
Height: 512,
|
|
|
|
BarWidth: 60,
|
|
|
|
XAxis: chart.Style{
|
2016-08-06 09:28:12 +02:00
|
|
|
Show: true,
|
2016-08-06 01:55:55 +02:00
|
|
|
},
|
|
|
|
YAxis: chart.YAxis{
|
|
|
|
Style: chart.Style{
|
2016-08-06 09:28:12 +02:00
|
|
|
Show: true,
|
2016-08-06 01:55:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Bars: []chart.Value{
|
2016-08-06 05:08:24 +02:00
|
|
|
{Value: 5.25, Label: "Blue"},
|
|
|
|
{Value: 4.88, Label: "Green"},
|
|
|
|
{Value: 4.74, Label: "Gray"},
|
|
|
|
{Value: 3.22, Label: "Orange"},
|
2016-08-06 01:55:55 +02:00
|
|
|
{Value: 3, Label: "Test"},
|
2016-08-06 05:08:24 +02:00
|
|
|
{Value: 2.27, Label: "??"},
|
2016-08-06 01:55:55 +02:00
|
|
|
{Value: 1, Label: "!!"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Header().Set("Content-Type", "image/png")
|
|
|
|
err := sbc.Render(chart.PNG, res)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering chart: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func port() string {
|
|
|
|
if len(os.Getenv("PORT")) > 0 {
|
|
|
|
return os.Getenv("PORT")
|
|
|
|
}
|
|
|
|
return "8080"
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
listenPort := fmt.Sprintf(":%s", port())
|
|
|
|
fmt.Printf("Listening on %s\n", listenPort)
|
|
|
|
http.HandleFunc("/", drawChart)
|
|
|
|
log.Fatal(http.ListenAndServe(listenPort, nil))
|
|
|
|
}
|