go-chart/_examples/custom_formatters/main.go

43 lines
1005 B
Go

package main
import (
"fmt"
"net/http"
"git.fireandbrimst.one/aw/go-chart"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
/*
In this example we use a custom `ValueFormatter` for the y axis, letting us specify how to format text of the y-axis ticks.
You can also do this for the x-axis, or the secondary y-axis.
This example also shows what the chart looks like with the x-axis left off or not shown.
*/
graph := chart.Chart{
YAxis: chart.YAxis{
Style: chart.StyleShow(),
ValueFormatter: func(v interface{}) string {
if vf, isFloat := v.(float64); isFloat {
return fmt.Sprintf("%0.6f", vf)
}
return ""
},
},
Series: []chart.Series{
chart.ContinuousSeries{
XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
},
},
}
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}