Adds the ability to draw an XY scatter plot. (#27)
* works more or less * updating comment * removing debugging printf * adding output * tweaks * missed a couple series validations * testing auto coloring * updated output.png * color tests etc. * sanity check tests. * should not use unkeyed fields anyway.
This commit is contained in:
parent
17b28beae8
commit
b713ff85cc
22 changed files with 511 additions and 72 deletions
|
|
@ -23,7 +23,7 @@ func parseFloat64(str string) float64 {
|
|||
func readData() ([]time.Time, []float64) {
|
||||
var xvalues []time.Time
|
||||
var yvalues []float64
|
||||
chart.File.ReadByLines("requests.csv", func(line string) {
|
||||
err := chart.File.ReadByLines("requests.csv", func(line string) {
|
||||
parts := strings.Split(line, ",")
|
||||
year := parseInt(parts[0])
|
||||
month := parseInt(parts[1])
|
||||
|
|
@ -33,6 +33,9 @@ func readData() ([]time.Time, []float64) {
|
|||
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
|
||||
yvalues = append(yvalues, elapsedMillis)
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
return xvalues, yvalues
|
||||
}
|
||||
|
||||
|
|
|
|||
80
_examples/scatter/main.go
Normal file
80
_examples/scatter/main.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
graph := chart.Chart{
|
||||
Series: []chart.Series{
|
||||
chart.ContinuousSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeWidth: chart.Disabled,
|
||||
DotWidth: 3,
|
||||
},
|
||||
XValues: chart.Sequence.Random(32, 1024),
|
||||
YValues: chart.Sequence.Random(32, 1024),
|
||||
},
|
||||
chart.ContinuousSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeWidth: chart.Disabled,
|
||||
DotWidth: 5,
|
||||
},
|
||||
XValues: chart.Sequence.Random(16, 1024),
|
||||
YValues: chart.Sequence.Random(16, 1024),
|
||||
},
|
||||
chart.ContinuousSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeWidth: chart.Disabled,
|
||||
DotWidth: 7,
|
||||
},
|
||||
XValues: chart.Sequence.Random(8, 1024),
|
||||
YValues: chart.Sequence.Random(8, 1024),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
err := graph.Render(chart.PNG, res)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func unit(res http.ResponseWriter, req *http.Request) {
|
||||
graph := chart.Chart{
|
||||
Height: 50,
|
||||
Width: 50,
|
||||
Canvas: chart.Style{
|
||||
Padding: chart.Box{IsSet: true},
|
||||
},
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{IsSet: true},
|
||||
},
|
||||
Series: []chart.Series{
|
||||
chart.ContinuousSeries{
|
||||
XValues: chart.Sequence.Float64(0, 4, 1),
|
||||
YValues: chart.Sequence.Float64(0, 4, 1),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
err := graph.Render(chart.PNG, res)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.HandleFunc("/unit", unit)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
BIN
_examples/scatter/output.png
Normal file
BIN
_examples/scatter/output.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Loading…
Add table
Add a link
Reference in a new issue