diff --git a/README.md b/README.md index baa059a..e926205 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ go-chart ======== -[![Build Status](https://travis-ci.org/wcharczuk/go-chart.svg?branch=master)](https://travis-ci.org/wcharczuk/go-chart) +[![Build Status](https://travis-ci.org/wcharczuk/go-chart.svg?branch=master)](https://travis-ci.org/wcharczuk/go-chart)[![Go Report Card](https://goreportcard.com/badge/github.com/wcharczuk/go-chart)](https://goreportcard.com/report/github.com/wcharczuk/go-chart) Package `chart` is a very simple golang native charting library that supports timeseries and continuous line charts. diff --git a/_examples/request_timings/main.go b/_examples/request_timings/main.go new file mode 100644 index 0000000..dba4190 --- /dev/null +++ b/_examples/request_timings/main.go @@ -0,0 +1,113 @@ +package main + +import ( + "net/http" + "strings" + "time" + + util "github.com/blendlabs/go-util" + "github.com/wcharczuk/go-chart" +) + +func readData() ([]time.Time, []float64) { + var xvalues []time.Time + var yvalues []float64 + util.ReadFileByLines("requests.csv", func(line string) { + parts := strings.Split(line, ",") + year := util.ParseInt(parts[0]) + month := util.ParseInt(parts[1]) + day := util.ParseInt(parts[2]) + hour := util.ParseInt(parts[3]) + elapsedMillis := util.ParseFloat64(parts[4]) + xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC)) + yvalues = append(yvalues, elapsedMillis) + }) + return xvalues, yvalues +} + +func drawChart(res http.ResponseWriter, req *http.Request) { + xvalues, yvalues := readData() + mainSeries := chart.TimeSeries{ + Name: "Prod Request Timings", + Style: chart.Style{ + Show: true, + StrokeColor: chart.ColorBlue, + FillColor: chart.ColorBlue.WithAlpha(100), + }, + XValues: xvalues, + YValues: yvalues, + } + + minSeries := &chart.MinSeries{ + Style: chart.Style{ + Show: true, + StrokeColor: chart.ColorAlternateGray, + StrokeDashArray: []float64{5.0, 5.0}, + }, + InnerSeries: mainSeries, + } + + maxSeries := &chart.MaxSeries{ + Style: chart.Style{ + Show: true, + StrokeColor: chart.ColorAlternateGray, + StrokeDashArray: []float64{5.0, 5.0}, + }, + InnerSeries: mainSeries, + } + + linreg := &chart.LinearRegressionSeries{ + Name: "Linear Regression", + Style: chart.Style{ + Show: true, + StrokeColor: chart.ColorAlternateBlue, + StrokeDashArray: []float64{5.0, 5.0}, + }, + InnerSeries: mainSeries, + } + + sma := &chart.SMASeries{ + Name: "SMA", + Style: chart.Style{ + Show: true, + StrokeColor: chart.ColorRed, + StrokeDashArray: []float64{5.0, 5.0}, + }, + InnerSeries: mainSeries, + } + + graph := chart.Chart{ + Width: 1280, + Height: 720, + YAxis: chart.YAxis{ + Name: "Elapsed Millis", + NameStyle: chart.StyleShow(), + Style: chart.StyleShow(), + }, + XAxis: chart.XAxis{ + Style: chart.StyleShow(), + ValueFormatter: chart.TimeHourValueFormatter, + }, + Series: []chart.Series{ + mainSeries, + minSeries, + maxSeries, + chart.LastValueAnnotation(minSeries), + chart.LastValueAnnotation(maxSeries), + linreg, + chart.LastValueAnnotation(linreg), + sma, + chart.LastValueAnnotation(sma), + }, + } + + graph.Elements = []chart.Renderable{chart.Legend(&graph)} + + res.Header().Set("Content-Type", "image/png") + graph.Render(chart.PNG, res) +} + +func main() { + http.HandleFunc("/", drawChart) + http.ListenAndServe(":8080", nil) +} diff --git a/_examples/request_timings/requests.csv b/_examples/request_timings/requests.csv new file mode 100644 index 0000000..6d0ebd1 --- /dev/null +++ b/_examples/request_timings/requests.csv @@ -0,0 +1,170 @@ +2016,7,31,16,51.1948264984227130 +2016,7,31,17,1.7940833333333333 +2016,7,31,18,10.0383889931207000 +2016,7,31,19,6.1413707865168539 +2016,7,31,20,8.3973734513274336 +2016,7,31,21,73.5405406633348480 +2016,7,31,22,159.8895444957152270 +2016,7,31,23,34.0708326693227090 +2016,8,1,0,69.7564325518178730 +2016,8,1,1,48.1574708470847080 +2016,8,1,2,17.4290324074074070 +2016,8,1,3,5.5943366336633663 +2016,8,1,4,9.1419949109414758 +2016,8,1,5,14.4151891117478510 +2016,8,1,6,1.5541666666666667 +2016,8,1,7,15.7282261904761900 +2016,8,1,8,21.1564157175398630 +2016,8,1,9,1.5934583333333333 +2016,8,1,10,4.3529166666666667 +2016,8,1,11,87.5080657894736840 +2016,8,1,12,1.5835000000000000 +2016,8,1,13,29.9959344262295080 +2016,8,1,14,46.8553964079538170 +2016,8,1,15,20.0111057531630460 +2016,8,1,16,94.6157284061696660 +2016,8,1,17,58.6487434362045140 +2016,8,1,18,20.8815984522785900 +2016,8,1,19,72.4154909266876360 +2016,8,1,20,58.9246686590436590 +2016,8,1,21,84.3067604562737640 +2016,8,1,22,122.5261538745387450 +2016,8,1,23,85.1076058098020120 +2016,8,2,0,101.7361568656277570 +2016,8,2,1,24.3167470952749810 +2016,8,2,2,122.0310546659304250 +2016,8,2,3,74.2989314442413160 +2016,8,2,4,129.6967691387559810 +2016,8,2,5,71.1511239448219070 +2016,8,2,6,1.9676388888888889 +2016,8,2,7,45.3233373493975900 +2016,8,2,8,0.52597727272727272730 +2016,8,2,9,0.47493750000000000000 +2016,8,2,10,0.36689583333333333330 +2016,8,2,11,12.2201858407079650 +2016,8,2,12,58.0710813743218810 +2016,8,2,13,75.9720821672048800 +2016,8,2,14,86.0020336538461540 +2016,8,2,15,22.4082420588235290 +2016,8,2,16,93.1733733049989880 +2016,8,2,17,55.2889921111945910 +2016,8,2,18,56.7059533689400160 +2016,8,2,19,84.2515716615033430 +2016,8,2,20,101.4842500290866780 +2016,8,2,21,136.8598881478658540 +2016,8,2,22,62.5393413387838530 +2016,8,2,23,47.6955947622329430 +2016,8,3,0,28.6536128110975110 +2016,8,3,1,68.2935053282915090 +2016,8,3,2,65.4284130561492130 +2016,8,3,3,102.4286141260973660 +2016,8,3,4,56.5472182066809920 +2016,8,3,5,18.6264831460674160 +2016,8,3,6,67.8456661764705880 +2016,8,3,7,92.5702828579829460 +2016,8,3,8,0.41591666666666666670 +2016,8,3,9,0.38620833333333333330 +2016,8,3,10,2.4085945945945946 +2016,8,3,11,10.7638414634146340 +2016,8,3,12,31.4489252873563220 +2016,8,3,13,153.4695363601385150 +2016,8,3,14,271.2642420287705770 +2016,8,3,15,36.5611996434937610 +2016,8,3,16,29.2185496806617110 +2016,8,3,17,126.9859396924555500 +2016,8,3,18,163.7181875191189970 +2016,8,3,19,21.4754753134796240 +2016,8,3,20,34.1040598911070780 +2016,8,3,21,75.1127804170444240 +2016,8,3,22,39.4469905441016900 +2016,8,3,23,5.1409929328621908 +2016,8,4,0,255.8132918813695180 +2016,8,4,1,214.4145150846210450 +2016,8,4,2,36.1957284522706210 +2016,8,4,3,78.4117736768802230 +2016,8,4,4,9.9754722222222222 +2016,8,4,5,34.1333243123336290 +2016,8,4,6,0.39741666666666666670 +2016,8,4,7,3.5579206349206349 +2016,8,4,8,0.33243750000000000000 +2016,8,4,9,0.27264583333333333330 +2016,8,4,10,15.6974805194805190 +2016,8,4,11,33.3286867469879520 +2016,8,4,12,69.0402700057570520 +2016,8,4,13,63.8368957232099950 +2016,8,4,14,46.5617178867403310 +2016,8,4,15,9.4975930656934307 +2016,8,4,16,70.3660450209843570 +2016,8,4,17,40.3501840688912810 +2016,8,4,18,111.0351848094324100 +2016,8,4,19,63.0629592505854800 +2016,8,4,20,94.2470022670596240 +2016,8,4,21,10.0634637203166230 +2016,8,4,22,49.0879200762388820 +2016,8,4,23,23.7079982971477220 +2016,8,5,0,28.9202116946399570 +2016,8,5,1,60.5840482187837350 +2016,8,5,2,34.2454046076313890 +2016,8,5,3,26.3180027100271000 +2016,8,5,4,27.4047251497005990 +2016,8,5,5,22.3780671462829740 +2016,8,5,6,41.4588378921962990 +2016,8,5,7,62.6991675955414010 +2016,8,5,8,88.7716758230236950 +2016,8,5,9,40.4319411764705880 +2016,8,5,10,72.9793152507676560 +2016,8,5,11,10.6647535971223020 +2016,8,5,12,159.3556583236321300 +2016,8,5,13,64.6945072587532020 +2016,8,5,14,74.9644142216085610 +2016,8,5,15,62.0095938270477580 +2016,8,5,16,36.5669031007751940 +2016,8,5,17,128.6374414414414410 +2016,8,5,18,112.1804886168910650 +2016,8,5,19,161.8682954545454550 +2016,8,5,20,95.8026952301719360 +2016,8,5,21,240.8415338457264560 +2016,8,5,22,155.3652810287871640 +2016,8,5,23,388.9544503679476700 +2016,8,6,0,15.9398834146341460 +2016,8,6,1,190.7137858942065490 +2016,8,6,2,11.4065349143610010 +2016,8,6,3,236.7059518272425250 +2016,8,6,4,248.0312605042016810 +2016,8,6,5,0.24320833333333333330 +2016,8,6,6,0.26987500000000000000 +2016,8,6,7,0.31525000000000000000 +2016,8,6,8,1.2495535714285714 +2016,8,6,9,5.4469130434782609 +2016,8,6,10,0.33695833333333333330 +2016,8,6,11,91.8210377113133940 +2016,8,6,12,0.31939583333333333330 +2016,8,6,13,7.5832362869198312 +2016,8,6,14,7.8081323155216285 +2016,8,6,15,54.4206174812030080 +2016,8,6,16,1.6644098360655738 +2016,8,6,17,120.0108195020746890 +2016,8,6,18,128.2539524348810870 +2016,8,6,19,93.8680571705426360 +2016,8,6,20,1.6764107142857143 +2016,8,6,21,24.4085034965034970 +2016,8,6,22,96.0146023560209420 +2016,8,6,23,7.2911171450737005 +2016,8,7,0,22.7146944444444440 +2016,8,7,1,0.41214583333333333330 +2016,8,7,2,0.30702083333333333330 +2016,8,7,3,2.8580312500000000 +2016,8,7,4,0.26483333333333333330 +2016,8,7,5,0.26022916666666666670 +2016,8,7,6,0.27406250000000000000 +2016,8,7,7,0.31931250000000000000 +2016,8,7,8,0.35614583333333333330 +2016,8,7,9,4.6068000000000000 +2016,8,7,10,0.36356250000000000000 +2016,8,7,11,0.30760416666666666670 +2016,8,7,12,23.8985191570881230 +2016,8,7,13,4.0653140495867769 +2016,8,7,14,61.0720150753768840 +2016,8,7,15,14.6478756410256410 +2016,8,7,16,53.9795638455827770 +2016,8,7,17,97.2232321428571430 \ No newline at end of file