text rotation works, ish.

This commit is contained in:
Will Charczuk 2016-10-21 12:44:37 -07:00
parent 53280b9258
commit f800bc387b
15 changed files with 281 additions and 131 deletions

View file

@ -2,23 +2,33 @@ package main
import (
"net/http"
"strconv"
"strings"
"time"
util "github.com/blendlabs/go-util"
"github.com/wcharczuk/go-chart"
)
func parseInt(str string) int {
v, _ := strconv.Atoi(str)
return v
}
func parseFloat64(str string) float64 {
v, _ := strconv.ParseFloat(str, 64)
return v
}
func readData() ([]time.Time, []float64) {
var xvalues []time.Time
var yvalues []float64
util.ReadFileByLines("requests.csv", func(line string) {
chart.File.ReadByLines("requests.csv", func(line string) {
parts := strings.Split(line, ",")
year := util.String.ParseInt(parts[0])
month := util.String.ParseInt(parts[1])
day := util.String.ParseInt(parts[2])
hour := util.String.ParseInt(parts[3])
elapsedMillis := util.String.ParseFloat64(parts[4])
year := parseInt(parts[0])
month := parseInt(parts[1])
day := parseInt(parts[2])
hour := parseInt(parts[3])
elapsedMillis := parseFloat64(parts[4])
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis)
})
@ -27,12 +37,12 @@ func readData() ([]time.Time, []float64) {
func releases() []chart.GridLine {
return []chart.GridLine{
{Value: chart.TimeToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 3, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 3, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: chart.Time.ToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
}
}
@ -78,13 +88,19 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
Style: chart.StyleShow(),
},
XAxis: chart.XAxis{
Style: chart.StyleShow(),
Style: chart.Style{
Show: true,
},
ValueFormatter: chart.TimeHourValueFormatter,
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorAlternateGray,
StrokeWidth: 1.0,
},
TickPosition: chart.TickPositionBetweenTicks,
TickStyle: chart.Style{
TextRotationDegrees: 45,
},
GridLines: releases(),
},
Series: []chart.Series{

View file

@ -60,8 +60,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
},
}
res.Header().Set("Content-Type", "image/svg+xml")
graph.Render(chart.SVG, res)
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func xvalues() []time.Time {

View file

@ -4,60 +4,47 @@ import (
"net/http"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
/*
In this example we set a rotation on the style for the custom ticks from the `custom_ticks` example.
*/
f, _ := chart.GetDefaultFont()
r, _ := chart.PNG(1024, 1024)
graph := chart.Chart{
YAxis: chart.YAxis{
Style: chart.Style{
Show: true,
},
Range: &chart.ContinuousRange{
Min: 0.0,
Max: 4.0,
},
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
Ticks: []chart.Tick{
{Value: 0.0, Label: "0.00"},
{Value: 2.0, Label: "2.00"},
{Value: 4.0, Label: "4.00"},
{Value: 6.0, Label: "6.00"},
{Value: 8.0, Label: "Eight"},
{Value: 10.0, Label: "Ten"},
},
},
XAxis: chart.XAxis{
Style: chart.Style{
Show: true,
},
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
Ticks: []chart.Tick{
{Value: 0.0, Label: "0.00"},
{Value: 2.0, Label: "2.00"},
{Value: 4.0, Label: "4.00"},
{Value: 6.0, Label: "6.00"},
{Value: 8.0, Label: "Eight"},
{Value: 10.0, Label: "Ten"},
},
},
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},
},
},
}
chart.Draw.Text(r, "Test", 64, 64, chart.Style{
FontColor: drawing.ColorBlack,
FontSize: 18,
Font: f,
})
chart.Draw.Text(r, "Test", 64, 64, chart.Style{
FontColor: drawing.ColorBlack,
FontSize: 18,
Font: f,
TextRotationDegrees: 45.0,
})
tb := chart.Draw.MeasureText(r, "Test", chart.Style{
FontColor: drawing.ColorBlack,
FontSize: 18,
Font: f,
}).Shift(64, 64)
tbc := tb.Corners().Rotate(45)
chart.Draw.BoxCorners(r, tbc, chart.Style{
StrokeColor: drawing.ColorRed,
StrokeWidth: 2,
})
tbcb := tbc.Box()
chart.Draw.Box(r, tbcb, chart.Style{
StrokeColor: drawing.ColorBlue,
StrokeWidth: 2,
})
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
r.Save(res)
}
func main() {