diff --git a/COVERAGE b/COVERAGE index f309e21..7e492f8 100644 --- a/COVERAGE +++ b/COVERAGE @@ -1 +1 @@ -70.89 \ No newline at end of file +29.02 \ No newline at end of file diff --git a/Makefile b/Makefile index 6b8f9da..a393b6f 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ new-install: @go get -v -u github.com/blend/go-sdk/cmd/coverage @go get -v -u github.com/blend/go-sdk/cmd/profanity +generate: + @go generate ./... + test: @go test ./... @@ -15,4 +18,4 @@ profanity: @profanity coverage: - @coverage \ No newline at end of file + @coverage diff --git a/README.md b/README.md index 0ba77f8..87f97ba 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,9 @@ go-chart ======== [![CircleCI](https://circleci.com/gh/wcharczuk/go-chart.svg?style=svg)](https://circleci.com/gh/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. +Package `chart` is a very simple golang native charting library that supports timeseries and continuous line charts. -The v1.0 release has been tagged so things should be more or less stable, if something changes please log an issue. - -Master should now be on the v2.x codebase, which brings a couple new features and better handling of basics like axes labeling etc. Per usual, see `_examples` for more information. +Master should now be on the v3.x codebase, which overhauls the api significantly. Per usual, see `_examples` for more information. # Installation @@ -83,8 +80,7 @@ Here, we have a single series with x range values as float64s, rendered to a PNG # API Overview -Everything on the `chart.Chart` object has defaults that can be overriden. Whenever a developer sets a property on the chart object, it is to be assumed that value will be used instead of the default. One complication here -is any object's root `chart.Style` object (i.e named `Style`) and the `Show` property specifically, if any other property is set and the `Show` property is unset, it is assumed to be it's default value of `False`. +Everything on the `chart.Chart` object has defaults that can be overriden. Whenever a developer sets a property on the chart object, it is to be assumed that value will be used instead of the default. The best way to see the api in action is to look at the examples in the `./_examples/` directory. @@ -96,4 +92,4 @@ The goal with the API itself is to have the "zero value be useful", and to requi # Contributions -This library is super early but contributions are welcome. +Contributions are welcome though this library is in a holding pattern for the forseable future. diff --git a/_examples/annotations/output.png b/_examples/annotations/output.png deleted file mode 100644 index a2e80fa..0000000 Binary files a/_examples/annotations/output.png and /dev/null differ diff --git a/_examples/axes/output.png b/_examples/axes/output.png deleted file mode 100644 index cacd47d..0000000 Binary files a/_examples/axes/output.png and /dev/null differ diff --git a/_examples/axes_labels/output.png b/_examples/axes_labels/output.png deleted file mode 100644 index 61c376c..0000000 Binary files a/_examples/axes_labels/output.png and /dev/null differ diff --git a/_examples/bar_chart/main.go b/_examples/bar_chart/main.go deleted file mode 100644 index 1694a29..0000000 --- a/_examples/bar_chart/main.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "os" - - chart "github.com/wcharczuk/go-chart" -) - -func drawChart(res http.ResponseWriter, req *http.Request) { - sbc := chart.BarChart{ - Title: "Test Bar Chart", - TitleStyle: chart.StyleShow(), - Background: chart.Style{ - Padding: chart.Box{ - Top: 40, - }, - }, - Height: 512, - BarWidth: 60, - XAxis: chart.StyleShow(), - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, - Bars: []chart.Value{ - {Value: 5.25, Label: "Blue"}, - {Value: 4.88, Label: "Green"}, - {Value: 4.74, Label: "Gray"}, - {Value: 3.22, Label: "Orange"}, - {Value: 3, Label: "Test"}, - {Value: 2.27, Label: "??"}, - {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)) -} diff --git a/_examples/basic/main.go b/_examples/basic/main.go deleted file mode 100644 index 797dcb9..0000000 --- a/_examples/basic/main.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "log" - "net/http" - - chart "github.com/wcharczuk/go-chart" -) - -func drawChart(res http.ResponseWriter, req *http.Request) { - graph := chart.Chart{ - 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 drawChartWide(res http.ResponseWriter, req *http.Request) { - graph := chart.Chart{ - Width: 1920, //this overrides the default. - Series: []chart.Series{ - chart.ContinuousSeries{ - XValues: []float64{1.0, 2.0, 3.0, 4.0}, - YValues: []float64{1.0, 2.0, 3.0, 4.0}, - }, - }, - } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.HandleFunc("/wide", drawChartWide) - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/_examples/basic/output.png b/_examples/basic/output.png deleted file mode 100644 index 2c781a7..0000000 Binary files a/_examples/basic/output.png and /dev/null differ diff --git a/_examples/benchmark_line_charts/main.go b/_examples/benchmark_line_charts/main.go deleted file mode 100644 index c3790b3..0000000 --- a/_examples/benchmark_line_charts/main.go +++ /dev/null @@ -1,79 +0,0 @@ -// Usage: http://localhost:8080?series=100&values=1000 -package main - -import ( - "fmt" - "math/rand" - "net/http" - "strconv" - "time" - - chart "github.com/wcharczuk/go-chart" -) - -func random(min, max float64) float64 { - return rand.Float64()*(max-min) + min -} - -func drawLargeChart(res http.ResponseWriter, r *http.Request) { - numSeriesInt64, err := strconv.ParseInt(r.FormValue("series"), 10, 64) - if err != nil { - numSeriesInt64 = int64(1) - } - if numSeriesInt64 == 0 { - numSeriesInt64 = 1 - } - numSeries := int(numSeriesInt64) - - numValuesInt64, err := strconv.ParseInt(r.FormValue("values"), 10, 64) - if err != nil { - numValuesInt64 = int64(100) - } - if numValuesInt64 == 0 { - numValuesInt64 = int64(100) - } - numValues := int(numValuesInt64) - - series := make([]chart.Series, numSeries) - - for i := 0; i < numSeries; i++ { - xValues := make([]time.Time, numValues) - yValues := make([]float64, numValues) - - for j := 0; j < numValues; j++ { - xValues[j] = time.Now().AddDate(0, 0, (numValues-j)*-1) - yValues[j] = random(float64(-500), float64(500)) - } - - series[i] = chart.TimeSeries{ - Name: fmt.Sprintf("aaa.bbb.hostname-%v.ccc.ddd.eee.fff.ggg.hhh.iii.jjj.kkk.lll.mmm.nnn.value", i), - XValues: xValues, - YValues: yValues, - } - } - - graph := chart.Chart{ - XAxis: chart.XAxis{ - Name: "Time", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Name: "Value", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), - }, - Series: series, - } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawLargeChart) - http.HandleFunc("/favico.ico", func(res http.ResponseWriter, req *http.Request) { - res.Write([]byte{}) - }) - http.ListenAndServe(":8080", nil) -} diff --git a/_examples/benchmark_line_charts/output.png b/_examples/benchmark_line_charts/output.png deleted file mode 100644 index 444c790..0000000 Binary files a/_examples/benchmark_line_charts/output.png and /dev/null differ diff --git a/_examples/custom_formatters/output.png b/_examples/custom_formatters/output.png deleted file mode 100644 index ced62f1..0000000 Binary files a/_examples/custom_formatters/output.png and /dev/null differ diff --git a/_examples/custom_padding/main.go b/_examples/custom_padding/main.go deleted file mode 100644 index a705054..0000000 --- a/_examples/custom_padding/main.go +++ /dev/null @@ -1,67 +0,0 @@ -package main - -import ( - "net/http" - - chart "github.com/wcharczuk/go-chart" - "github.com/wcharczuk/go-chart/drawing" - "github.com/wcharczuk/go-chart/seq" -) - -func drawChart(res http.ResponseWriter, req *http.Request) { - graph := chart.Chart{ - Background: chart.Style{ - Padding: chart.Box{ - Top: 50, - Left: 25, - Right: 25, - Bottom: 10, - }, - FillColor: drawing.ColorFromHex("efefef"), - }, - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, - Series: []chart.Series{ - chart.ContinuousSeries{ - XValues: SeqRange(1.0, 100.0), - YValues: seq.RandomValuesWithMax(100, 512), - }, - }, - } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func drawChartDefault(res http.ResponseWriter, req *http.Request) { - graph := chart.Chart{ - Background: chart.Style{ - FillColor: drawing.ColorFromHex("efefef"), - }, - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, - Series: []chart.Series{ - chart.ContinuousSeries{ - XValues: SeqRange(1.0, 100.0), - YValues: seq.RandomValuesWithMax(100, 512), - }, - }, - } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.HandleFunc("/default", drawChartDefault) - http.ListenAndServe(":8080", nil) -} diff --git a/_examples/custom_padding/output.png b/_examples/custom_padding/output.png deleted file mode 100644 index 368e539..0000000 Binary files a/_examples/custom_padding/output.png and /dev/null differ diff --git a/_examples/custom_ranges/output.png b/_examples/custom_ranges/output.png deleted file mode 100644 index 51b2321..0000000 Binary files a/_examples/custom_ranges/output.png and /dev/null differ diff --git a/_examples/custom_styles/output.png b/_examples/custom_styles/output.png deleted file mode 100644 index 84066a4..0000000 Binary files a/_examples/custom_styles/output.png and /dev/null differ diff --git a/_examples/custom_ticks/output.png b/_examples/custom_ticks/output.png deleted file mode 100644 index aefa3c3..0000000 Binary files a/_examples/custom_ticks/output.png and /dev/null differ diff --git a/_examples/descending/output.png b/_examples/descending/output.png deleted file mode 100644 index a9a00a9..0000000 Binary files a/_examples/descending/output.png and /dev/null differ diff --git a/_examples/donut_chart/main.go b/_examples/donut_chart/main.go deleted file mode 100644 index 010dbec..0000000 --- a/_examples/donut_chart/main.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - - "github.com/wcharczuk/go-chart" -) - -func drawChart(res http.ResponseWriter, req *http.Request) { - pie := chart.DonutChart{ - Width: 512, - Height: 512, - Values: []chart.Value{ - {Value: 5, Label: "Blue"}, - {Value: 5, Label: "Green"}, - {Value: 4, Label: "Gray"}, - {Value: 4, Label: "Orange"}, - {Value: 3, Label: "Deep Blue"}, - {Value: 3, Label: "test"}, - }, - } - - res.Header().Set("Content-Type", "image/png") - err := pie.Render(chart.PNG, res) - if err != nil { - fmt.Printf("Error rendering pie chart: %v\n", err) - } -} - -func drawChartRegression(res http.ResponseWriter, req *http.Request) { - pie := chart.DonutChart{ - Width: 512, - Height: 512, - Values: []chart.Value{ - {Value: 5, Label: "Blue"}, - {Value: 2, Label: "Two"}, - {Value: 1, Label: "One"}, - }, - } - - res.Header().Set("Content-Type", chart.ContentTypeSVG) - err := pie.Render(chart.SVG, res) - if err != nil { - fmt.Printf("Error rendering pie chart: %v\n", err) - } -} - -func main() { - http.HandleFunc("/", drawChart) - http.HandleFunc("/reg", drawChartRegression) - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/_examples/legend/output.png b/_examples/legend/output.png deleted file mode 100644 index d5fd6d8..0000000 Binary files a/_examples/legend/output.png and /dev/null differ diff --git a/_examples/legend_left/output.png b/_examples/legend_left/output.png deleted file mode 100644 index 7708b48..0000000 Binary files a/_examples/legend_left/output.png and /dev/null differ diff --git a/_examples/linear_regression/output.png b/_examples/linear_regression/output.png deleted file mode 100644 index f4c09a1..0000000 Binary files a/_examples/linear_regression/output.png and /dev/null differ diff --git a/_examples/min_max/output.png b/_examples/min_max/output.png deleted file mode 100644 index e2e1b38..0000000 Binary files a/_examples/min_max/output.png and /dev/null differ diff --git a/_examples/pie_chart/main.go b/_examples/pie_chart/main.go deleted file mode 100644 index 268239d..0000000 --- a/_examples/pie_chart/main.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - - chart "github.com/wcharczuk/go-chart" -) - -func drawChart(res http.ResponseWriter, req *http.Request) { - pie := chart.PieChart{ - Width: 512, - Height: 512, - Values: []chart.Value{ - {Value: 5, Label: "Blue"}, - {Value: 5, Label: "Green"}, - {Value: 4, Label: "Gray"}, - {Value: 4, Label: "Orange"}, - {Value: 3, Label: "Deep Blue"}, - {Value: 3, Label: "??"}, - {Value: 1, Label: "!!"}, - }, - } - - res.Header().Set("Content-Type", "image/png") - err := pie.Render(chart.PNG, res) - if err != nil { - fmt.Printf("Error rendering pie chart: %v\n", err) - } -} - -func drawChartRegression(res http.ResponseWriter, req *http.Request) { - pie := chart.PieChart{ - Width: 512, - Height: 512, - Values: []chart.Value{ - {Value: 5, Label: "Blue"}, - {Value: 2, Label: "Two"}, - {Value: 1, Label: "One"}, - }, - } - - res.Header().Set("Content-Type", chart.ContentTypeSVG) - err := pie.Render(chart.SVG, res) - if err != nil { - fmt.Printf("Error rendering pie chart: %v\n", err) - } -} - -func main() { - http.HandleFunc("/", drawChart) - http.HandleFunc("/reg", drawChartRegression) - log.Fatal(http.ListenAndServe(":8080", nil)) -} diff --git a/_examples/poly_regression/output.png b/_examples/poly_regression/output.png deleted file mode 100644 index ae9b8f6..0000000 Binary files a/_examples/poly_regression/output.png and /dev/null differ diff --git a/_examples/simple_moving_average/output.png b/_examples/simple_moving_average/output.png deleted file mode 100644 index 8671d97..0000000 Binary files a/_examples/simple_moving_average/output.png and /dev/null differ diff --git a/_examples/stock_analysis/output.png b/_examples/stock_analysis/output.png deleted file mode 100644 index bf67ba3..0000000 Binary files a/_examples/stock_analysis/output.png and /dev/null differ diff --git a/_examples/twopoint/output.png b/_examples/twopoint/output.png deleted file mode 100644 index 8d35b97..0000000 Binary files a/_examples/twopoint/output.png and /dev/null differ diff --git a/_examples/annotations/main.go b/examples/annotations/main.go similarity index 83% rename from _examples/annotations/main.go rename to examples/annotations/main.go index 84220b1..87be119 100644 --- a/_examples/annotations/main.go +++ b/examples/annotations/main.go @@ -1,13 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { - +func main() { /* In this example we add an `Annotation` series, which is a special type of series that draws annotation labels at given X and Y values (as translated by their respective ranges). @@ -37,11 +38,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/annotations/output.png b/examples/annotations/output.png new file mode 100644 index 0000000..3127025 Binary files /dev/null and b/examples/annotations/output.png differ diff --git a/_examples/axes_labels/main.go b/examples/axes/main.go similarity index 60% rename from _examples/axes_labels/main.go rename to examples/axes/main.go index 935aa3f..491ff8e 100644 --- a/_examples/axes_labels/main.go +++ b/examples/axes/main.go @@ -1,12 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" chart "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* The below will draw the same chart as the `basic` example, except with both the x and y axes turned on. @@ -14,20 +16,9 @@ func drawChart(res http.ResponseWriter, req *http.Request) { */ graph := chart.Chart{ - XAxis: chart.XAxis{ - Name: "The XAxis", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Name: "The YAxis", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), - }, Series: []chart.Series{ chart.ContinuousSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.GetDefaultColor(0).WithAlpha(64), FillColor: chart.GetDefaultColor(0).WithAlpha(64), }, @@ -37,11 +28,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/axes/output.png b/examples/axes/output.png new file mode 100644 index 0000000..9a16db0 Binary files /dev/null and b/examples/axes/output.png differ diff --git a/_examples/axes/main.go b/examples/axes_labels/main.go similarity index 68% rename from _examples/axes/main.go rename to examples/axes_labels/main.go index daf7267..e714884 100644 --- a/_examples/axes/main.go +++ b/examples/axes_labels/main.go @@ -1,12 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" chart "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* The below will draw the same chart as the `basic` example, except with both the x and y axes turned on. @@ -15,15 +17,14 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph := chart.Chart{ XAxis: chart.XAxis{ - Style: chart.StyleShow(), //enables / displays the x-axis + Name: "The XAxis", }, YAxis: chart.YAxis{ - Style: chart.StyleShow(), //enables / displays the y-axis + Name: "The YAxis", }, Series: []chart.Series{ chart.ContinuousSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.GetDefaultColor(0).WithAlpha(64), FillColor: chart.GetDefaultColor(0).WithAlpha(64), }, @@ -33,11 +34,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/axes_labels/output.png b/examples/axes_labels/output.png new file mode 100644 index 0000000..5ca998a Binary files /dev/null and b/examples/axes_labels/output.png differ diff --git a/examples/bar_chart/main.go b/examples/bar_chart/main.go new file mode 100644 index 0000000..e89c463 --- /dev/null +++ b/examples/bar_chart/main.go @@ -0,0 +1,35 @@ +package main + +//go:generate go run main.go + +import ( + "os" + + "github.com/wcharczuk/go-chart" +) + +func main() { + graph := chart.BarChart{ + Title: "Test Bar Chart", + Background: chart.Style{ + Padding: chart.Box{ + Top: 40, + }, + }, + Height: 512, + BarWidth: 60, + Bars: []chart.Value{ + {Value: 5.25, Label: "Blue"}, + {Value: 4.88, Label: "Green"}, + {Value: 4.74, Label: "Gray"}, + {Value: 3.22, Label: "Orange"}, + {Value: 3, Label: "Test"}, + {Value: 2.27, Label: "??"}, + {Value: 1, Label: "!!"}, + }, + } + + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) +} diff --git a/_examples/bar_chart/output.png b/examples/bar_chart/output.png similarity index 100% rename from _examples/bar_chart/output.png rename to examples/bar_chart/output.png diff --git a/_examples/bar_chart_base_value/main.go b/examples/bar_chart_base_value/main.go similarity index 62% rename from _examples/bar_chart_base_value/main.go rename to examples/bar_chart_base_value/main.go index a0ce4c3..84ad9c4 100644 --- a/_examples/bar_chart_base_value/main.go +++ b/examples/bar_chart_base_value/main.go @@ -1,33 +1,29 @@ package main +//go:generate go run main.go + import ( - "fmt" - "log" - "net/http" "os" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart/drawing" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { profitStyle := chart.Style{ - Show: true, FillColor: drawing.ColorFromHex("13c158"), StrokeColor: drawing.ColorFromHex("13c158"), StrokeWidth: 0, } lossStyle := chart.Style{ - Show: true, FillColor: drawing.ColorFromHex("c11313"), StrokeColor: drawing.ColorFromHex("c11313"), StrokeWidth: 0, } sbc := chart.BarChart{ - Title: "Bar Chart Using BaseValue", - TitleStyle: chart.StyleShow(), + Title: "Bar Chart Using BaseValue", Background: chart.Style{ Padding: chart.Box{ Top: 40, @@ -35,13 +31,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, Height: 512, BarWidth: 60, - XAxis: chart.Style{ - Show: true, - }, YAxis: chart.YAxis{ - Style: chart.Style{ - Show: true, - }, Ticks: []chart.Tick{ {Value: -4.0, Label: "-4"}, {Value: -2.0, Label: "-2"}, @@ -66,23 +56,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - 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)) + f, _ := os.Create("output.png") + defer f.Close() + sbc.Render(chart.PNG, f) } diff --git a/_examples/bar_chart_base_value/output.png b/examples/bar_chart_base_value/output.png similarity index 100% rename from _examples/bar_chart_base_value/output.png rename to examples/bar_chart_base_value/output.png diff --git a/examples/basic/main.go b/examples/basic/main.go new file mode 100644 index 0000000..20839cf --- /dev/null +++ b/examples/basic/main.go @@ -0,0 +1,23 @@ +package main + +//go:generate go run main.go + +import ( + "os" + + "github.com/wcharczuk/go-chart" +) + +func main() { + graph := chart.Chart{ + 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}, + }, + }, + } + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) +} diff --git a/examples/basic/output.png b/examples/basic/output.png new file mode 100644 index 0000000..fa0573a Binary files /dev/null and b/examples/basic/output.png differ diff --git a/examples/benchmark_line_charts/main.go b/examples/benchmark_line_charts/main.go new file mode 100644 index 0000000..975670c --- /dev/null +++ b/examples/benchmark_line_charts/main.go @@ -0,0 +1,52 @@ +package main + +//go:generate go run main.go + +import ( + "fmt" + "math/rand" + "os" + "time" + + "github.com/wcharczuk/go-chart" +) + +func random(min, max float64) float64 { + return rand.Float64()*(max-min) + min +} + +func main() { + numValues := 1024 + numSeries := 100 + series := make([]chart.Series, numSeries) + + for i := 0; i < numSeries; i++ { + xValues := make([]time.Time, numValues) + yValues := make([]float64, numValues) + + for j := 0; j < numValues; j++ { + xValues[j] = time.Now().AddDate(0, 0, (numValues-j)*-1) + yValues[j] = random(float64(-500), float64(500)) + } + + series[i] = chart.TimeSeries{ + Name: fmt.Sprintf("aaa.bbb.hostname-%v.ccc.ddd.eee.fff.ggg.hhh.iii.jjj.kkk.lll.mmm.nnn.value", i), + XValues: xValues, + YValues: yValues, + } + } + + graph := chart.Chart{ + XAxis: chart.XAxis{ + Name: "Time", + }, + YAxis: chart.YAxis{ + Name: "Value", + }, + Series: series, + } + + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) +} diff --git a/examples/benchmark_line_charts/output.png b/examples/benchmark_line_charts/output.png new file mode 100644 index 0000000..588d6a2 Binary files /dev/null and b/examples/benchmark_line_charts/output.png differ diff --git a/_examples/css_classes/main.go b/examples/css_classes/main.go similarity index 97% rename from _examples/css_classes/main.go rename to examples/css_classes/main.go index 6cff1e4..2ec5def 100644 --- a/_examples/css_classes/main.go +++ b/examples/css_classes/main.go @@ -5,7 +5,7 @@ import ( "log" "net/http" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) // Note: Additional examples on how to add Stylesheets are in the custom_stylesheets example diff --git a/_examples/custom_formatters/main.go b/examples/custom_formatters/main.go similarity index 71% rename from _examples/custom_formatters/main.go rename to examples/custom_formatters/main.go index b569581..b215389 100644 --- a/_examples/custom_formatters/main.go +++ b/examples/custom_formatters/main.go @@ -1,13 +1,15 @@ package main +//go:generate go run main.go + import ( "fmt" - "net/http" + "os" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* 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. @@ -16,7 +18,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { 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) @@ -31,12 +32,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, }, } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/custom_formatters/output.png b/examples/custom_formatters/output.png new file mode 100644 index 0000000..8d8b4e9 Binary files /dev/null and b/examples/custom_formatters/output.png differ diff --git a/examples/custom_padding/main.go b/examples/custom_padding/main.go new file mode 100644 index 0000000..1561497 --- /dev/null +++ b/examples/custom_padding/main.go @@ -0,0 +1,34 @@ +package main + +//go:generate go run main.go + +import ( + "os" + + "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart/drawing" +) + +func main() { + graph := chart.Chart{ + Background: chart.Style{ + Padding: chart.Box{ + Top: 50, + Left: 25, + Right: 25, + Bottom: 10, + }, + FillColor: drawing.ColorFromHex("efefef"), + }, + Series: []chart.Series{ + chart.ContinuousSeries{ + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(100).WithMax(512)}.Values(), + }, + }, + } + + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) +} diff --git a/examples/custom_padding/output.png b/examples/custom_padding/output.png new file mode 100644 index 0000000..63c17cc Binary files /dev/null and b/examples/custom_padding/output.png differ diff --git a/_examples/custom_ranges/main.go b/examples/custom_ranges/main.go similarity index 66% rename from _examples/custom_ranges/main.go rename to examples/custom_ranges/main.go index d8ee5eb..75a5142 100644 --- a/_examples/custom_ranges/main.go +++ b/examples/custom_ranges/main.go @@ -1,12 +1,14 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we set a custom range for the y-axis, overriding the automatic range generation. Note: the chart will still generate the ticks automatically based on the custom range, so the intervals may be a bit weird. @@ -14,7 +16,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph := chart.Chart{ YAxis: chart.YAxis{ - Style: chart.StyleShow(), Range: &chart.ContinuousRange{ Min: 0.0, Max: 10.0, @@ -27,12 +28,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, }, } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/custom_ranges/output.png b/examples/custom_ranges/output.png new file mode 100644 index 0000000..8ea7da4 Binary files /dev/null and b/examples/custom_ranges/output.png differ diff --git a/_examples/custom_styles/main.go b/examples/custom_styles/main.go similarity index 65% rename from _examples/custom_styles/main.go rename to examples/custom_styles/main.go index 612e175..c8c3da7 100644 --- a/_examples/custom_styles/main.go +++ b/examples/custom_styles/main.go @@ -1,13 +1,15 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart/drawing" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we set some custom colors for the series and the chart background and canvas. */ @@ -21,7 +23,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Series: []chart.Series{ chart.ContinuousSeries{ Style: chart.Style{ - Show: true, //note; if we set ANY other properties, we must set this to true. StrokeColor: drawing.ColorRed, // will supercede defaults FillColor: drawing.ColorRed.WithAlpha(64), // will supercede defaults }, @@ -31,11 +32,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/custom_styles/output.png b/examples/custom_styles/output.png new file mode 100644 index 0000000..981745d Binary files /dev/null and b/examples/custom_styles/output.png differ diff --git a/_examples/custom_stylesheets/inlineOutput.svg b/examples/custom_stylesheets/inlineOutput.svg similarity index 100% rename from _examples/custom_stylesheets/inlineOutput.svg rename to examples/custom_stylesheets/inlineOutput.svg diff --git a/_examples/custom_stylesheets/main.go b/examples/custom_stylesheets/main.go similarity index 100% rename from _examples/custom_stylesheets/main.go rename to examples/custom_stylesheets/main.go diff --git a/_examples/custom_ticks/main.go b/examples/custom_ticks/main.go similarity index 74% rename from _examples/custom_ticks/main.go rename to examples/custom_ticks/main.go index 4afdd18..fa93c00 100644 --- a/_examples/custom_ticks/main.go +++ b/examples/custom_ticks/main.go @@ -1,12 +1,14 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we set a custom set of ticks to use for the y-axis. It can be (almost) whatever you want, including some custom labels for ticks. Custom ticks will supercede a custom range, which will supercede automatic generation based on series values. @@ -14,7 +16,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph := chart.Chart{ YAxis: chart.YAxis{ - Style: chart.StyleShow(), Range: &chart.ContinuousRange{ Min: 0.0, Max: 4.0, @@ -35,12 +36,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, }, } - - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/custom_ticks/output.png b/examples/custom_ticks/output.png new file mode 100644 index 0000000..fc9d7b6 Binary files /dev/null and b/examples/custom_ticks/output.png differ diff --git a/_examples/descending/main.go b/examples/descending/main.go similarity index 77% rename from _examples/descending/main.go rename to examples/descending/main.go index 683c3dd..c066808 100644 --- a/_examples/descending/main.go +++ b/examples/descending/main.go @@ -1,12 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* The below will draw the same chart as the `basic` example, except with both the x and y axes turned on. @@ -19,14 +21,12 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph := chart.Chart{ Height: 500, Width: 500, - XAxis: chart.XAxis{ - Style: chart.StyleShow(), + XAxis: chart.XAxis{ /*Range: &chart.ContinuousRange{ Descending: true, },*/ }, YAxis: chart.YAxis{ - Style: chart.StyleShow(), Range: &chart.ContinuousRange{ Descending: true, }, @@ -34,7 +34,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Series: []chart.Series{ chart.ContinuousSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.GetDefaultColor(0).WithAlpha(64), FillColor: chart.GetDefaultColor(0).WithAlpha(64), }, @@ -44,11 +43,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/descending/output.png b/examples/descending/output.png new file mode 100644 index 0000000..2f30474 Binary files /dev/null and b/examples/descending/output.png differ diff --git a/examples/donut_chart/main.go b/examples/donut_chart/main.go new file mode 100644 index 0000000..98f3de5 --- /dev/null +++ b/examples/donut_chart/main.go @@ -0,0 +1,28 @@ +package main + +//go:generate go run main.go + +import ( + "os" + + "github.com/wcharczuk/go-chart" +) + +func main() { + pie := chart.DonutChart{ + Width: 512, + Height: 512, + Values: []chart.Value{ + {Value: 5, Label: "Blue"}, + {Value: 5, Label: "Green"}, + {Value: 4, Label: "Gray"}, + {Value: 4, Label: "Orange"}, + {Value: 3, Label: "Deep Blue"}, + {Value: 3, Label: "test"}, + }, + } + + f, _ := os.Create("output.png") + defer f.Close() + pie.Render(chart.PNG, f) +} diff --git a/_examples/donut_chart/output.png b/examples/donut_chart/output.png similarity index 100% rename from _examples/donut_chart/output.png rename to examples/donut_chart/output.png diff --git a/_examples/donut_chart/reg.svg b/examples/donut_chart/reg.svg similarity index 100% rename from _examples/donut_chart/reg.svg rename to examples/donut_chart/reg.svg diff --git a/_examples/image_writer/main.go b/examples/image_writer/main.go similarity index 92% rename from _examples/image_writer/main.go rename to examples/image_writer/main.go index 9bf92ce..13bf013 100644 --- a/_examples/image_writer/main.go +++ b/examples/image_writer/main.go @@ -4,7 +4,7 @@ import ( "fmt" "log" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) func main() { diff --git a/_examples/legend/main.go b/examples/legend/main.go similarity index 72% rename from _examples/legend/main.go rename to examples/legend/main.go index ecb243b..fcdaaa5 100644 --- a/_examples/legend/main.go +++ b/examples/legend/main.go @@ -1,12 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" chart "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we add a `Renderable` or a custom component to the `Elements` array. @@ -15,12 +17,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { */ graph := chart.Chart{ - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, Background: chart.Style{ Padding: chart.Box{ Top: 20, @@ -41,11 +37,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { chart.Legend(&graph), } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/legend/output.png b/examples/legend/output.png new file mode 100644 index 0000000..82ebfa8 Binary files /dev/null and b/examples/legend/output.png differ diff --git a/_examples/legend_left/main.go b/examples/legend_left/main.go similarity index 87% rename from _examples/legend_left/main.go rename to examples/legend_left/main.go index e7f0caf..33c71db 100644 --- a/_examples/legend_left/main.go +++ b/examples/legend_left/main.go @@ -1,12 +1,14 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we add a `Renderable` or a custom component to the `Elements` array. @@ -15,12 +17,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { */ graph := chart.Chart{ - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), - }, Background: chart.Style{ Padding: chart.Box{ Top: 20, @@ -101,11 +97,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { chart.LegendLeft(&graph), } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/legend_left/output.png b/examples/legend_left/output.png new file mode 100644 index 0000000..94ff2b5 Binary files /dev/null and b/examples/legend_left/output.png differ diff --git a/_examples/linear_regression/main.go b/examples/linear_regression/main.go similarity index 63% rename from _examples/linear_regression/main.go rename to examples/linear_regression/main.go index 9daf3a6..fbea690 100644 --- a/_examples/linear_regression/main.go +++ b/examples/linear_regression/main.go @@ -1,13 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" chart "github.com/wcharczuk/go-chart" - "github.com/wcharczuk/go-chart/seq" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument. @@ -16,8 +17,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) { mainSeries := chart.ContinuousSeries{ Name: "A test series", - XValues: SeqRange(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. - YValues: seq.RandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(0).WithMax(100)}.Values(), //generates a []float64 randomly from 0 to 100 with 100 elements. } // note we create a LinearRegressionSeries series by assignin the inner series. @@ -33,11 +34,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/linear_regression/output.png b/examples/linear_regression/output.png new file mode 100644 index 0000000..a0ff7b6 Binary files /dev/null and b/examples/linear_regression/output.png differ diff --git a/_examples/min_max/main.go b/examples/min_max/main.go similarity index 51% rename from _examples/min_max/main.go rename to examples/min_max/main.go index cd112a0..359be16 100644 --- a/_examples/min_max/main.go +++ b/examples/min_max/main.go @@ -1,22 +1,22 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" - "github.com/wcharczuk/go-chart/seq" +import ( + "os" + + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { mainSeries := chart.ContinuousSeries{ Name: "A test series", - XValues: SeqRange(1.0, 100.0), - YValues: seq.New(seq.NewRandom().WithLen(100).WithMax(150).WithMin(50)).Array(), + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(50).WithMax(150)}.Values(), } minSeries := &chart.MinSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.ColorAlternateGray, StrokeDashArray: []float64{5.0, 5.0}, }, @@ -25,7 +25,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { maxSeries := &chart.MaxSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.ColorAlternateGray, StrokeDashArray: []float64{5.0, 5.0}, }, @@ -36,35 +35,27 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Width: 1920, Height: 1080, YAxis: chart.YAxis{ - Name: "Random Values", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), + Name: "Random Values", Range: &chart.ContinuousRange{ Min: 25, Max: 175, }, }, XAxis: chart.XAxis{ - Name: "Random Other Values", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), + Name: "Random Other Values", }, Series: []chart.Series{ mainSeries, minSeries, maxSeries, - chart.LastValueAnnotation(minSeries), - chart.LastValueAnnotation(maxSeries), + chart.LastValueAnnotationSeries(minSeries), + chart.LastValueAnnotationSeries(maxSeries), }, } 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) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/min_max/output.png b/examples/min_max/output.png new file mode 100644 index 0000000..738b2fd Binary files /dev/null and b/examples/min_max/output.png differ diff --git a/examples/pie_chart/main.go b/examples/pie_chart/main.go new file mode 100644 index 0000000..2268776 --- /dev/null +++ b/examples/pie_chart/main.go @@ -0,0 +1,29 @@ +package main + +//go:generate go run main.go + +import ( + "os" + + "github.com/wcharczuk/go-chart" +) + +func main() { + pie := chart.PieChart{ + Width: 512, + Height: 512, + Values: []chart.Value{ + {Value: 5, Label: "Blue"}, + {Value: 5, Label: "Green"}, + {Value: 4, Label: "Gray"}, + {Value: 4, Label: "Orange"}, + {Value: 3, Label: "Deep Blue"}, + {Value: 3, Label: "??"}, + {Value: 1, Label: "!!"}, + }, + } + + f, _ := os.Create("output.png") + defer f.Close() + pie.Render(chart.PNG, f) +} diff --git a/_examples/pie_chart/output.png b/examples/pie_chart/output.png similarity index 100% rename from _examples/pie_chart/output.png rename to examples/pie_chart/output.png diff --git a/_examples/pie_chart/reg.svg b/examples/pie_chart/reg.svg similarity index 100% rename from _examples/pie_chart/reg.svg rename to examples/pie_chart/reg.svg diff --git a/_examples/poly_regression/main.go b/examples/poly_regression/main.go similarity index 55% rename from _examples/poly_regression/main.go rename to examples/poly_regression/main.go index 81a7a27..e41b0f2 100644 --- a/_examples/poly_regression/main.go +++ b/examples/poly_regression/main.go @@ -1,13 +1,14 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" chart "github.com/wcharczuk/go-chart" - "github.com/wcharczuk/go-chart/seq" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we add a new type of series, a `PolynomialRegressionSeries` that takes another series as a required argument. @@ -16,8 +17,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) { mainSeries := chart.ContinuousSeries{ Name: "A test series", - XValues: SeqRange(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. - YValues: seq.RandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(0).WithMax(100)}.Values(), //generates a []float64 randomly from 0 to 100 with 100 elements. } polyRegSeries := &chart.PolynomialRegressionSeries{ @@ -32,11 +33,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/poly_regression/output.png b/examples/poly_regression/output.png new file mode 100644 index 0000000..84c56f9 Binary files /dev/null and b/examples/poly_regression/output.png differ diff --git a/_examples/request_timings/main.go b/examples/request_timings/main.go similarity index 89% rename from _examples/request_timings/main.go rename to examples/request_timings/main.go index b1a8929..6c6df03 100644 --- a/_examples/request_timings/main.go +++ b/examples/request_timings/main.go @@ -1,14 +1,22 @@ package main +//go:generate go run main.go + import ( "fmt" "net/http" + "os" "strconv" "time" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) +func main() { + log := chart.NewLogger() + drawChart(log) +} + func parseInt(str string) int { v, _ := strconv.Atoi(str) return v @@ -110,24 +118,16 @@ func drawChart(log chart.Logger) http.HandlerFunc { Series: []chart.Series{ mainSeries, linreg, - chart.LastValueAnnotation(linreg), + chart.LastValueAnnotationSeries(linreg), sma, - chart.LastValueAnnotation(sma), + chart.LastValueAnnotationSeries(sma), }, } graph.Elements = []chart.Renderable{chart.LegendThin(&graph)} - res.Header().Set("Content-Type", chart.ContentTypePNG) - if err := graph.Render(chart.PNG, res); err != nil { - log.Err(err) - } + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } } - -func main() { - log := chart.NewLogger() - log.Infof("listening on :8080") - http.HandleFunc("/", drawChart(log)) - http.ListenAndServe(":8080", nil) -} diff --git a/_examples/request_timings/output.png b/examples/request_timings/output.png similarity index 100% rename from _examples/request_timings/output.png rename to examples/request_timings/output.png diff --git a/_examples/request_timings/requests.csv b/examples/request_timings/requests.csv similarity index 100% rename from _examples/request_timings/requests.csv rename to examples/request_timings/requests.csv diff --git a/_examples/rerender/main.go b/examples/rerender/main.go similarity index 95% rename from _examples/rerender/main.go rename to examples/rerender/main.go index e014e8f..ca4da0c 100644 --- a/_examples/rerender/main.go +++ b/examples/rerender/main.go @@ -6,7 +6,7 @@ import ( "sync" "time" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) var lock sync.Mutex diff --git a/_examples/scatter/main.go b/examples/scatter/main.go similarity index 74% rename from _examples/scatter/main.go rename to examples/scatter/main.go index 99ba2e0..5ef7675 100644 --- a/_examples/scatter/main.go +++ b/examples/scatter/main.go @@ -6,7 +6,7 @@ import ( _ "net/http/pprof" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart/drawing" ) @@ -20,13 +20,12 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Series: []chart.Series{ chart.ContinuousSeries{ Style: chart.Style{ - Show: true, StrokeWidth: chart.Disabled, DotWidth: 5, DotColorProvider: viridisByY, }, - XValues: chart.SeqRange(0, 127), - YValues: chart.NewSeq(chart.NewSeqRandom().WithLen(128).WithMax(1024)).Values(), + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(127)}.Values(), + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(128).WithMin(0).WithMax(1024)}.Values(), }, }, } @@ -50,8 +49,8 @@ func unit(res http.ResponseWriter, req *http.Request) { }, Series: []chart.Series{ chart.ContinuousSeries{ - XValues: chart.SeqRangeWithStep(0, 4, 1), - YValues: chart.SeqRangeWithStep(0, 4, 1), + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(), + YValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(), }, }, } diff --git a/_examples/scatter/output.png b/examples/scatter/output.png similarity index 100% rename from _examples/scatter/output.png rename to examples/scatter/output.png diff --git a/_examples/simple_moving_average/main.go b/examples/simple_moving_average/main.go similarity index 51% rename from _examples/simple_moving_average/main.go rename to examples/simple_moving_average/main.go index 485dacb..35c90c7 100644 --- a/_examples/simple_moving_average/main.go +++ b/examples/simple_moving_average/main.go @@ -1,17 +1,18 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { - +func main() { mainSeries := chart.ContinuousSeries{ Name: "A test series", - XValues: chart.SeqRange(1.0, 100.0), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. - YValues: chart.SeqRandomValuesWithMax(100, 100), //generates a []float64 randomly from 0 to 100 with 100 elements. + XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(1.0).WithEnd(100.0)}.Values(), //generates a []float64 from 1.0 to 100.0 in 1.0 step increments, or 100 elements. + YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(100).WithMin(0).WithMax(100)}.Values(), //generates a []float64 randomly from 0 to 100 with 100 elements. } // note we create a SimpleMovingAverage series by assignin the inner series. @@ -27,11 +28,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/examples/simple_moving_average/output.png b/examples/simple_moving_average/output.png new file mode 100644 index 0000000..1c00f38 Binary files /dev/null and b/examples/simple_moving_average/output.png differ diff --git a/_examples/stacked_bar/main.go b/examples/stacked_bar/main.go similarity index 63% rename from _examples/stacked_bar/main.go rename to examples/stacked_bar/main.go index 3f5015d..4fc1465 100644 --- a/_examples/stacked_bar/main.go +++ b/examples/stacked_bar/main.go @@ -1,25 +1,20 @@ package main import ( - "fmt" - "log" - "net/http" + "os" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { sbc := chart.StackedBarChart{ - Title: "Test Stacked Bar Chart", - TitleStyle: chart.StyleShow(), + Title: "Test Stacked Bar Chart", Background: chart.Style{ Padding: chart.Box{ Top: 40, }, }, Height: 512, - XAxis: chart.StyleShow(), - YAxis: chart.StyleShow(), Bars: []chart.StackedBar{ { Name: "This is a very long string to test word break wrapping.", @@ -52,14 +47,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - 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 main() { - http.HandleFunc("/", drawChart) - log.Fatal(http.ListenAndServe(":8080", nil)) + f, _ := os.Create("output.png") + defer f.Close() + sbc.Render(chart.PNG, f) } diff --git a/_examples/stacked_bar/output.png b/examples/stacked_bar/output.png similarity index 100% rename from _examples/stacked_bar/output.png rename to examples/stacked_bar/output.png diff --git a/_examples/stock_analysis/main.go b/examples/stock_analysis/main.go similarity index 96% rename from _examples/stock_analysis/main.go rename to examples/stock_analysis/main.go index a8bd5a9..a99804d 100644 --- a/_examples/stock_analysis/main.go +++ b/examples/stock_analysis/main.go @@ -1,14 +1,16 @@ package main +//go:generate go run main.go + import ( - "net/http" + "os" "time" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart/drawing" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { xv, yv := xvalues(), yvalues() priceSeries := chart.TimeSeries{ @@ -55,8 +57,9 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } func xvalues() []time.Time { @@ -73,8 +76,3 @@ func xvalues() []time.Time { func yvalues() []float64 { return []float64{212.47, 212.59, 211.76, 211.37, 210.18, 208.00, 206.79, 209.33, 210.77, 210.82, 210.50, 209.79, 209.38, 210.07, 208.35, 207.95, 210.57, 208.66, 208.92, 208.66, 209.42, 210.59, 209.98, 208.32, 203.97, 197.83, 189.50, 187.27, 194.46, 199.27, 199.28, 197.67, 191.77, 195.41, 195.55, 192.59, 197.43, 194.79, 195.85, 196.74, 196.01, 198.45, 200.18, 199.73, 195.45, 196.46, 193.90, 193.60, 192.90, 192.87, 188.01, 188.12, 191.63, 192.13, 195.00, 198.47, 197.79, 199.41, 201.21, 201.33, 201.52, 200.25, 199.29, 202.35, 203.27, 203.37, 203.11, 201.85, 205.26, 207.51, 207.00, 206.60, 208.95, 208.83, 207.93, 210.39, 211.00, 210.36, 210.15, 210.04, 208.08, 208.56, 207.74, 204.84, 202.54, 205.62, 205.47, 208.73, 208.55, 209.31, 209.07, 209.35, 209.32, 209.56, 208.69, 210.68, 208.53, 205.61, 209.62, 208.35, 206.95, 205.34, 205.87, 201.88, 202.90, 205.03, 208.03, 204.86, 200.02, 201.67, 203.50, 206.02, 205.68, 205.21, 207.40, 205.93, 203.87, 201.02, 201.36, 198.82, 194.05, 191.92, 192.11, 193.66, 188.83, 191.93, 187.81, 188.06, 185.65, 186.69, 190.52, 187.64, 190.20, 188.13, 189.11, 193.72, 193.65, 190.16, 191.30, 191.60, 187.95, 185.42, 185.43, 185.27, 182.86, 186.63, 189.78, 192.88, 192.09, 192.00, 194.78, 192.32, 193.20, 195.54, 195.09, 193.56, 198.11, 199.00, 199.78, 200.43, 200.59, 198.40, 199.38, 199.54, 202.76, 202.50, 202.17, 203.34, 204.63, 204.38, 204.67, 204.56, 203.21, 203.12, 203.24, 205.12, 206.02, 205.52, 206.92, 206.25, 204.19, 206.42, 203.95, 204.50, 204.02, 205.92, 208.00, 208.01, 207.78, 209.24, 209.90, 210.10, 208.97, 208.97, 208.61, 208.92, 209.35, 207.45, 206.33, 207.97, 206.16, 205.01, 204.97, 205.72, 205.89, 208.45, 206.50, 206.56, 204.76, 206.78, 204.85, 204.91, 204.20, 205.49, 205.21, 207.87, 209.28, 209.34, 210.24, 209.84, 210.27, 210.91, 210.28, 211.35, 211.68, 212.37, 212.08, 210.07, 208.45, 208.04, 207.75, 208.37, 206.52, 207.85, 208.44, 208.10, 210.81, 203.24, 199.60, 203.20, 206.66, 209.48, 209.92, 208.41, 209.66, 209.53, 212.65, 213.40, 214.95, 214.92, 216.12, 215.83} } - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) -} diff --git a/examples/stock_analysis/output.png b/examples/stock_analysis/output.png new file mode 100644 index 0000000..dc804c8 Binary files /dev/null and b/examples/stock_analysis/output.png differ diff --git a/_examples/text_rotation/main.go b/examples/text_rotation/main.go similarity index 77% rename from _examples/text_rotation/main.go rename to examples/text_rotation/main.go index 4fa60ef..f90ce49 100644 --- a/_examples/text_rotation/main.go +++ b/examples/text_rotation/main.go @@ -1,13 +1,15 @@ package main -import ( - "net/http" +//go:generate go run main.go - chart "github.com/wcharczuk/go-chart" +import ( + "os" + + "github.com/wcharczuk/go-chart" "github.com/wcharczuk/go-chart/drawing" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { f, _ := chart.GetDefaultFont() r, _ := chart.PNG(1024, 1024) @@ -43,11 +45,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { StrokeWidth: 2, }) - res.Header().Set("Content-Type", "image/png") - r.Save(res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + file, _ := os.Create("output.png") + defer file.Close() + r.Save(file) } diff --git a/_examples/text_rotation/output.png b/examples/text_rotation/output.png similarity index 100% rename from _examples/text_rotation/output.png rename to examples/text_rotation/output.png diff --git a/_examples/timeseries/main.go b/examples/timeseries/main.go similarity index 96% rename from _examples/timeseries/main.go rename to examples/timeseries/main.go index c2e05ff..3cffd22 100644 --- a/_examples/timeseries/main.go +++ b/examples/timeseries/main.go @@ -13,9 +13,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { Note: chart.TimeSeries implements `ValueFormatterProvider` and as a result gives the XAxis the appropriate formatter to use for the ticks. */ graph := chart.Chart{ - XAxis: chart.XAxis{ - Style: chart.StyleShow(), - }, Series: []chart.Series{ chart.TimeSeries{ XValues: []time.Time{ @@ -46,7 +43,6 @@ func drawCustomChart(res http.ResponseWriter, req *http.Request) { */ graph := chart.Chart{ XAxis: chart.XAxis{ - Style: chart.StyleShow(), ValueFormatter: chart.TimeHourValueFormatter, }, Series: []chart.Series{ diff --git a/_examples/timeseries/output.png b/examples/timeseries/output.png similarity index 100% rename from _examples/timeseries/output.png rename to examples/timeseries/output.png diff --git a/_examples/twoaxis/main.go b/examples/twoaxis/main.go similarity index 64% rename from _examples/twoaxis/main.go rename to examples/twoaxis/main.go index a3ba063..87eeea6 100644 --- a/_examples/twoaxis/main.go +++ b/examples/twoaxis/main.go @@ -1,13 +1,15 @@ package main +//go:generate go run main.go + import ( "fmt" - "net/http" + "os" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) -func drawChart(res http.ResponseWriter, req *http.Request) { +func main() { /* In this example we add a second series, and assign it to the secondary y axis, giving that series it's own range. @@ -17,7 +19,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph := chart.Chart{ XAxis: chart.XAxis{ - Style: chart.StyleShow(), //enables / displays the x-axis TickPosition: chart.TickPositionBetweenTicks, ValueFormatter: func(v interface{}) string { typed := v.(float64) @@ -25,12 +26,6 @@ func drawChart(res http.ResponseWriter, req *http.Request) { return fmt.Sprintf("%d-%d\n%d", typedDate.Month(), typedDate.Day(), typedDate.Year()) }, }, - YAxis: chart.YAxis{ - Style: chart.StyleShow(), //enables / displays the y-axis - }, - YAxisSecondary: chart.YAxis{ - Style: chart.StyleShow(), //enables / displays the secondary y-axis - }, Series: []chart.Series{ chart.ContinuousSeries{ XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0}, @@ -44,11 +39,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, } - res.Header().Set("Content-Type", "image/png") - graph.Render(chart.PNG, res) -} - -func main() { - http.HandleFunc("/", drawChart) - http.ListenAndServe(":8080", nil) + f, _ := os.Create("output.png") + defer f.Close() + graph.Render(chart.PNG, f) } diff --git a/_examples/twoaxis/output.png b/examples/twoaxis/output.png similarity index 100% rename from _examples/twoaxis/output.png rename to examples/twoaxis/output.png diff --git a/_examples/twopoint/main.go b/examples/twopoint/main.go similarity index 80% rename from _examples/twopoint/main.go rename to examples/twopoint/main.go index 9030f76..e66a709 100644 --- a/_examples/twopoint/main.go +++ b/examples/twopoint/main.go @@ -1,28 +1,27 @@ package main +//go:generate go run main.go + import ( "bytes" "log" "os" - chart "github.com/wcharczuk/go-chart" + "github.com/wcharczuk/go-chart" ) func main() { - var b float64 b = 1000 ts1 := chart.ContinuousSeries{ //TimeSeries{ Name: "Time Series", - Style: chart.StyleShow(), XValues: []float64{10 * b, 20 * b, 30 * b, 40 * b, 50 * b, 60 * b, 70 * b, 80 * b}, YValues: []float64{1.0, 2.0, 30.0, 4.0, 50.0, 6.0, 7.0, 88.0}, } ts2 := chart.ContinuousSeries{ //TimeSeries{ Style: chart.Style{ - Show: true, StrokeColor: chart.GetDefaultColor(1), }, @@ -34,15 +33,11 @@ func main() { XAxis: chart.XAxis{ Name: "The XAxis", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), ValueFormatter: chart.TimeMinuteValueFormatter, //TimeHourValueFormatter, }, YAxis: chart.YAxis{ - Name: "The YAxis", - NameStyle: chart.StyleShow(), - Style: chart.StyleShow(), + Name: "The YAxis", }, Series: []chart.Series{ diff --git a/examples/twopoint/output.png b/examples/twopoint/output.png new file mode 100644 index 0000000..10059e7 Binary files /dev/null and b/examples/twopoint/output.png differ