Compare commits
No commits in common. "main" and "v3.0" have entirely different histories.
17
.circleci/config.yml
Normal file
|
@ -0,0 +1,17 @@
|
|||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
working_directory: /go/src/github.com/wcharczuk/go-chart
|
||||
docker:
|
||||
- image: circleci/golang:1.11
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: new-install
|
||||
command: make new-install
|
||||
- run:
|
||||
name: ci
|
||||
command: make ci
|
||||
- store_artifacts:
|
||||
path: coverage.html
|
||||
destination: coverage.html
|
33
.github/workflows/ci.yml
vendored
|
@ -1,33 +0,0 @@
|
|||
name: "Continuous Integration"
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches: [ main ]
|
||||
paths: [ "*.go" ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
paths: [ "*.go" ]
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
name: "Tests"
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
env:
|
||||
GOOS: "linux"
|
||||
GOARCH: "amd64"
|
||||
GO111MODULE: "on"
|
||||
CGO_ENABLED: "0"
|
||||
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.21
|
||||
|
||||
- name: Check out go-incr
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Run all tests
|
||||
run: go test ./...
|
3
.gitignore
vendored
|
@ -16,5 +16,4 @@
|
|||
# Other
|
||||
.vscode
|
||||
.DS_Store
|
||||
coverage.html
|
||||
.idea
|
||||
coverage.html
|
2
COVERAGE
|
@ -1 +1 @@
|
|||
29.02
|
||||
70.89
|
1
LICENSE
|
@ -1,7 +1,6 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2016 William Charczuk.
|
||||
Copyright (c) 2024 Zeni Kim.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
|
18
Makefile
|
@ -1,10 +1,18 @@
|
|||
all: new-install test
|
||||
all: test
|
||||
|
||||
ci: profanity coverage
|
||||
|
||||
new-install:
|
||||
@go get -v -u ./...
|
||||
|
||||
generate:
|
||||
@go generate ./...
|
||||
@go get -v -u github.com/blend/go-sdk/cmd/coverage
|
||||
@go get -v -u github.com/blend/go-sdk/cmd/profanity
|
||||
|
||||
test:
|
||||
@go test ./...
|
||||
@go test ./...
|
||||
|
||||
.PHONY: profanity
|
||||
profanity:
|
||||
@profanity
|
||||
|
||||
coverage:
|
||||
@coverage
|
35
README.md
|
@ -1,18 +1,20 @@
|
|||
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)
|
||||
|
||||
This project starts from a full copy from [https://git.smarteching.com/zeni/go-chart](https://git.smarteching.com/zeni/go-chart). 28 Oct 2024.
|
||||
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 v3.x codebase, which overhauls the api significantly. Per usual, see `examples` for more information.
|
||||
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.
|
||||
|
||||
# Installation
|
||||
|
||||
To install `chart` run the following:
|
||||
|
||||
```bash
|
||||
> go get git.smarteching.com/zeni/go-chart/v2@latest
|
||||
> go get -u github.com/wcharczuk/go-chart
|
||||
```
|
||||
|
||||
Most of the components are interchangeable so feel free to crib whatever you want.
|
||||
|
@ -21,35 +23,33 @@ Most of the components are interchangeable so feel free to crib whatever you wan
|
|||
|
||||
Spark Lines:
|
||||
|
||||
![](https://git.smarteching.com/zeni/go-chart/raw/branch/main/_images/tvix_ltm.png)
|
||||
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/tvix_ltm.png)
|
||||
|
||||
Single axis:
|
||||
|
||||
![](https://git.smarteching.com/zeni/go-chart/raw/branch/main/_images/goog_ltm.png)
|
||||
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/goog_ltm.png)
|
||||
|
||||
Two axis:
|
||||
|
||||
![](https://git.smarteching.com/zeni/go-chart/raw/branch/main/_images/two_axis.png)
|
||||
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/two_axis.png)
|
||||
|
||||
# Other Chart Types
|
||||
|
||||
Pie Chart:
|
||||
|
||||
![](https://git.smarteching.com/zeni/go-chart/raw/branch/main/_images/pie_chart.png)
|
||||
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/pie_chart.png)
|
||||
|
||||
The code for this chart can be found in `examples/pie_chart/main.go`.
|
||||
The code for this chart can be found in `_examples/pie_chart/main.go`.
|
||||
|
||||
Stacked Bar:
|
||||
|
||||
![](https://git.smarteching.com/zeni/go-chart/raw/branch/main/_images/stacked_bar.png)
|
||||
![](https://raw.githubusercontent.com/wcharczuk/go-chart/master/_images/stacked_bar.png)
|
||||
|
||||
The code for this chart can be found in `examples/stacked_bar/main.go`.
|
||||
The code for this chart can be found in `_examples/stacked_bar/main.go`.
|
||||
|
||||
# Code Examples
|
||||
|
||||
Actual chart configurations and examples can be found in the `./examples/` directory. They are simple CLI programs that write to `output.png` (they are also updated with `go generate`.
|
||||
|
||||
If folder ends in "web", has web servers, so start them with `go run main.go` then access `http://localhost:8080` to see the output.
|
||||
Actual chart configurations and examples can be found in the `./_examples/` directory. They are web servers, so start them with `go run main.go` then access `http://localhost:8080` to see the output.
|
||||
|
||||
# Usage
|
||||
|
||||
|
@ -61,7 +61,7 @@ import (
|
|||
...
|
||||
"bytes"
|
||||
...
|
||||
"git.smarteching.com/zeni/go-chart/v2" //exposes "chart"
|
||||
"github.com/wcharczuk/go-chart" //exposes "chart"
|
||||
)
|
||||
|
||||
graph := chart.Chart{
|
||||
|
@ -83,7 +83,8 @@ 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.
|
||||
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`.
|
||||
|
||||
The best way to see the api in action is to look at the examples in the `./_examples/` directory.
|
||||
|
||||
|
@ -95,4 +96,4 @@ The goal with the API itself is to have the "zero value be useful", and to requi
|
|||
|
||||
# Contributions
|
||||
|
||||
Contributions are welcome though this library is in a holding pattern for the forseable future.
|
||||
This library is super early but contributions are welcome.
|
||||
|
|
|
@ -1,147 +0,0 @@
|
|||
aliceblue #f0f8ff 240,248,255
|
||||
antiquewhite #faebd7 250,235,215
|
||||
aqua #00ffff 0,255,255
|
||||
aquamarine #7fffd4 127,255,212
|
||||
azure #f0ffff 240,255,255
|
||||
beige #f5f5dc 245,245,220
|
||||
bisque #ffe4c4 255,228,196
|
||||
black #000000 0,0,0
|
||||
blanchedalmond #ffebcd 255,235,205
|
||||
blue #0000ff 0,0,255
|
||||
blueviolet #8a2be2 138,43,226
|
||||
brown #a52a2a 165,42,42
|
||||
burlywood #deb887 222,184,135
|
||||
cadetblue #5f9ea0 95,158,160
|
||||
chartreuse #7fff00 127,255,0
|
||||
chocolate #d2691e 210,105,30
|
||||
coral #ff7f50 255,127,80
|
||||
cornflowerblue #6495ed 100,149,237
|
||||
cornsilk #fff8dc 255,248,220
|
||||
crimson #dc143c 220,20,60
|
||||
cyan #00ffff 0,255,255
|
||||
darkblue #00008b 0,0,139
|
||||
darkcyan #008b8b 0,139,139
|
||||
darkgoldenrod #b8860b 184,134,11
|
||||
darkgray #a9a9a9 169,169,169
|
||||
darkgreen #006400 0,100,0
|
||||
darkgrey #a9a9a9 169,169,169
|
||||
darkkhaki #bdb76b 189,183,107
|
||||
darkmagenta #8b008b 139,0,139
|
||||
darkolivegreen #556b2f 85,107,47
|
||||
darkorange #ff8c00 255,140,0
|
||||
darkorchid #9932cc 153,50,204
|
||||
darkred #8b0000 139,0,0
|
||||
darksalmon #e9967a 233,150,122
|
||||
darkseagreen #8fbc8f 143,188,143
|
||||
darkslateblue #483d8b 72,61,139
|
||||
darkslategray #2f4f4f 47,79,79
|
||||
darkslategrey #2f4f4f 47,79,79
|
||||
darkturquoise #00ced1 0,206,209
|
||||
darkviolet #9400d3 148,0,211
|
||||
deeppink #ff1493 255,20,147
|
||||
deepskyblue #00bfff 0,191,255
|
||||
dimgray #696969 105,105,105
|
||||
dimgrey #696969 105,105,105
|
||||
dodgerblue #1e90ff 30,144,255
|
||||
firebrick #b22222 178,34,34
|
||||
floralwhite #fffaf0 255,250,240
|
||||
forestgreen #228b22 34,139,34
|
||||
fuchsia #ff00ff 255,0,255
|
||||
gainsboro #dcdcdc 220,220,220
|
||||
ghostwhite #f8f8ff 248,248,255
|
||||
gold #ffd700 255,215,0
|
||||
goldenrod #daa520 218,165,32
|
||||
gray #808080 128,128,128
|
||||
green #008000 0,128,0
|
||||
greenyellow #adff2f 173,255,47
|
||||
grey #808080 128,128,128
|
||||
honeydew #f0fff0 240,255,240
|
||||
hotpink #ff69b4 255,105,180
|
||||
indianred #cd5c5c 205,92,92
|
||||
indigo #4b0082 75,0,130
|
||||
ivory #fffff0 255,255,240
|
||||
khaki #f0e68c 240,230,140
|
||||
lavender #e6e6fa 230,230,250
|
||||
lavenderblush #fff0f5 255,240,245
|
||||
lawngreen #7cfc00 124,252,0
|
||||
lemonchiffon #fffacd 255,250,205
|
||||
lightblue #add8e6 173,216,230
|
||||
lightcoral #f08080 240,128,128
|
||||
lightcyan #e0ffff 224,255,255
|
||||
lightgoldenrodyellow #fafad2 250,250,210
|
||||
lightgray #d3d3d3 211,211,211
|
||||
lightgreen #90ee90 144,238,144
|
||||
lightgrey #d3d3d3 211,211,211
|
||||
lightpink #ffb6c1 255,182,193
|
||||
lightsalmon #ffa07a 255,160,122
|
||||
lightseagreen #20b2aa 32,178,170
|
||||
lightskyblue #87cefa 135,206,250
|
||||
lightslategray #778899 119,136,153
|
||||
lightslategrey #778899 119,136,153
|
||||
lightsteelblue #b0c4de 176,196,222
|
||||
lightyellow #ffffe0 255,255,224
|
||||
lime #00ff00 0,255,0
|
||||
limegreen #32cd32 50,205,50
|
||||
linen #faf0e6 250,240,230
|
||||
magenta #ff00ff 255,0,255
|
||||
maroon #800000 128,0,0
|
||||
mediumaquamarine #66cdaa 102,205,170
|
||||
mediumblue #0000cd 0,0,205
|
||||
mediumorchid #ba55d3 186,85,211
|
||||
mediumpurple #9370db 147,112,219
|
||||
mediumseagreen #3cb371 60,179,113
|
||||
mediumslateblue #7b68ee 123,104,238
|
||||
mediumspringgreen #00fa9a 0,250,154
|
||||
mediumturquoise #48d1cc 72,209,204
|
||||
mediumvioletred #c71585 199,21,133
|
||||
midnightblue #191970 25,25,112
|
||||
mintcream #f5fffa 245,255,250
|
||||
mistyrose #ffe4e1 255,228,225
|
||||
moccasin #ffe4b5 255,228,181
|
||||
navajowhite #ffdead 255,222,173
|
||||
navy #000080 0,0,128
|
||||
oldlace #fdf5e6 253,245,230
|
||||
olive #808000 128,128,0
|
||||
olivedrab #6b8e23 107,142,35
|
||||
orange #ffa500 255,165,0
|
||||
orangered #ff4500 255,69,0
|
||||
orchid #da70d6 218,112,214
|
||||
palegoldenrod #eee8aa 238,232,170
|
||||
palegreen #98fb98 152,251,152
|
||||
paleturquoise #afeeee 175,238,238
|
||||
palevioletred #db7093 219,112,147
|
||||
papayawhip #ffefd5 255,239,213
|
||||
peachpuff #ffdab9 255,218,185
|
||||
peru #cd853f 205,133,63
|
||||
pink #ffc0cb 255,192,203
|
||||
plum #dda0dd 221,160,221
|
||||
powderblue #b0e0e6 176,224,230
|
||||
purple #800080 128,0,128
|
||||
red #ff0000 255,0,0
|
||||
rosybrown #bc8f8f 188,143,143
|
||||
royalblue #4169e1 65,105,225
|
||||
saddlebrown #8b4513 139,69,19
|
||||
salmon #fa8072 250,128,114
|
||||
sandybrown #f4a460 244,164,96
|
||||
seagreen #2e8b57 46,139,87
|
||||
seashell #fff5ee 255,245,238
|
||||
sienna #a0522d 160,82,45
|
||||
silver #c0c0c0 192,192,192
|
||||
skyblue #87ceeb 135,206,235
|
||||
slateblue #6a5acd 106,90,205
|
||||
slategray #708090 112,128,144
|
||||
slategrey #708090 112,128,144
|
||||
snow #fffafa 255,250,250
|
||||
springgreen #00ff7f 0,255,127
|
||||
steelblue #4682b4 70,130,180
|
||||
tan #d2b48c 210,180,140
|
||||
teal #008080 0,128,128
|
||||
thistle #d8bfd8 216,191,216
|
||||
tomato #ff6347 255,99,71
|
||||
turquoise #40e0d0 64,224,208
|
||||
violet #ee82ee 238,130,238
|
||||
wheat #f5deb3 245,222,179
|
||||
white #ffffff 255,255,255
|
||||
whitesmoke #f5f5f5 245,245,245
|
||||
yellow #ffff00 255,255,0
|
||||
yellowgreen #9acd32 154,205,50
|
|
@ -1,14 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
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).
|
||||
|
@ -38,7 +37,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/annotations/output.png
Normal file
After Width: | Height: | Size: 14 KiB |
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
The below will draw the same chart as the `basic` example, except with both the x and y axes turned on.
|
||||
|
@ -17,14 +15,15 @@ func main() {
|
|||
|
||||
graph := chart.Chart{
|
||||
XAxis: chart.XAxis{
|
||||
Name: "The XAxis",
|
||||
Style: chart.StyleShow(), //enables / displays the x-axis
|
||||
},
|
||||
YAxis: chart.YAxis{
|
||||
Name: "The YAxis",
|
||||
Style: chart.StyleShow(), //enables / displays the y-axis
|
||||
},
|
||||
Series: []chart.Series{
|
||||
chart.ContinuousSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64),
|
||||
FillColor: chart.GetDefaultColor(0).WithAlpha(64),
|
||||
},
|
||||
|
@ -34,7 +33,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/axes/output.png
Normal file
After Width: | Height: | Size: 22 KiB |
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
The below will draw the same chart as the `basic` example, except with both the x and y axes turned on.
|
||||
|
@ -16,9 +14,20 @@ func main() {
|
|||
*/
|
||||
|
||||
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),
|
||||
},
|
||||
|
@ -28,7 +37,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/axes_labels/output.png
Normal file
After Width: | Height: | Size: 24 KiB |
|
@ -1,19 +1,18 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
graph := chart.BarChart{
|
||||
Title: "Test Bar Chart",
|
||||
sbc := chart.BarChart{
|
||||
Title: "Test Bar Chart",
|
||||
TitleStyle: chart.StyleShow(),
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{
|
||||
Top: 40,
|
||||
|
@ -21,6 +20,10 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
|
|||
},
|
||||
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"},
|
||||
|
@ -33,11 +36,10 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
err := graph.Render(chart.PNG, res)
|
||||
err := sbc.Render(chart.PNG, res)
|
||||
if err != nil {
|
||||
fmt.Printf("Error rendering chart: %v\n", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func port() string {
|
BIN
_examples/bar_chart/output.png
Normal file
After Width: | Height: | Size: 19 KiB |
|
@ -1,29 +1,33 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
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",
|
||||
Title: "Bar Chart Using BaseValue",
|
||||
TitleStyle: chart.StyleShow(),
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{
|
||||
Top: 40,
|
||||
|
@ -31,7 +35,13 @@ func main() {
|
|||
},
|
||||
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"},
|
||||
|
@ -56,7 +66,23 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
sbc.Render(chart.PNG, f)
|
||||
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))
|
||||
}
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
43
_examples/basic/main.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
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))
|
||||
}
|
BIN
_examples/basic/output.png
Normal file
After Width: | Height: | Size: 11 KiB |
79
_examples/benchmark_line_charts/main.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
// 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)
|
||||
}
|
BIN
_examples/benchmark_line_charts/output.png
Normal file
After Width: | Height: | Size: 68 KiB |
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
// Note: Additional examples on how to add Stylesheets are in the custom_stylesheets example
|
|
@ -1,15 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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.
|
||||
|
@ -18,6 +16,7 @@ func main() {
|
|||
|
||||
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)
|
||||
|
@ -32,7 +31,12 @@ func main() {
|
|||
},
|
||||
},
|
||||
}
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/custom_formatters/output.png
Normal file
After Width: | Height: | Size: 27 KiB |
67
_examples/custom_padding/main.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
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)
|
||||
}
|
BIN
_examples/custom_padding/output.png
Normal file
After Width: | Height: | Size: 66 KiB |
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
/*
|
||||
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.
|
||||
|
@ -16,6 +14,7 @@ func main() {
|
|||
|
||||
graph := chart.Chart{
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
Range: &chart.ContinuousRange{
|
||||
Min: 0.0,
|
||||
Max: 10.0,
|
||||
|
@ -28,7 +27,12 @@ func main() {
|
|||
},
|
||||
},
|
||||
}
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/custom_ranges/output.png
Normal file
After Width: | Height: | Size: 12 KiB |
|
@ -1,15 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
/*
|
||||
In this example we set some custom colors for the series and the chart background and canvas.
|
||||
*/
|
||||
|
@ -23,6 +21,7 @@ func main() {
|
|||
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
|
||||
},
|
||||
|
@ -32,7 +31,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/custom_styles/output.png
Normal file
After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 987 B After Width: | Height: | Size: 987 B |
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
const style = "svg .background { fill: white; }" +
|
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
/*
|
||||
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.
|
||||
|
@ -16,6 +14,7 @@ func main() {
|
|||
|
||||
graph := chart.Chart{
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
Range: &chart.ContinuousRange{
|
||||
Min: 0.0,
|
||||
Max: 4.0,
|
||||
|
@ -36,7 +35,12 @@ func main() {
|
|||
},
|
||||
},
|
||||
}
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/custom_ticks/output.png
Normal file
After Width: | Height: | Size: 11 KiB |
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
The below will draw the same chart as the `basic` example, except with both the x and y axes turned on.
|
||||
|
@ -21,12 +19,14 @@ func main() {
|
|||
graph := chart.Chart{
|
||||
Height: 500,
|
||||
Width: 500,
|
||||
XAxis: chart.XAxis{
|
||||
XAxis: chart.XAxis{
|
||||
Style: chart.StyleShow(),
|
||||
/*Range: &chart.ContinuousRange{
|
||||
Descending: true,
|
||||
},*/
|
||||
},
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
Range: &chart.ContinuousRange{
|
||||
Descending: true,
|
||||
},
|
||||
|
@ -34,6 +34,7 @@ func main() {
|
|||
Series: []chart.Series{
|
||||
chart.ContinuousSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeColor: chart.GetDefaultColor(0).WithAlpha(64),
|
||||
FillColor: chart.GetDefaultColor(0).WithAlpha(64),
|
||||
},
|
||||
|
@ -43,7 +44,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/descending/output.png
Normal file
After Width: | Height: | Size: 19 KiB |
54
_examples/donut_chart/main.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
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))
|
||||
}
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
In this example we add a `Renderable` or a custom component to the `Elements` array.
|
||||
|
@ -17,6 +15,12 @@ func main() {
|
|||
*/
|
||||
|
||||
graph := chart.Chart{
|
||||
XAxis: chart.XAxis{
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{
|
||||
Top: 20,
|
||||
|
@ -37,7 +41,11 @@ func main() {
|
|||
chart.Legend(&graph),
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/legend/output.png
Normal file
After Width: | Height: | Size: 24 KiB |
|
@ -1,14 +1,12 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
In this example we add a `Renderable` or a custom component to the `Elements` array.
|
||||
|
@ -17,6 +15,12 @@ func main() {
|
|||
*/
|
||||
|
||||
graph := chart.Chart{
|
||||
XAxis: chart.XAxis{
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
Background: chart.Style{
|
||||
Padding: chart.Box{
|
||||
Top: 20,
|
||||
|
@ -97,7 +101,11 @@ func main() {
|
|||
chart.LegendLeft(&graph),
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/legend_left/output.png
Normal file
After Width: | Height: | Size: 44 KiB |
|
@ -1,14 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/seq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
In this example we add a new type of series, a `SimpleMovingAverageSeries` that takes another series as a required argument.
|
||||
|
@ -17,8 +16,8 @@ func main() {
|
|||
|
||||
mainSeries := chart.ContinuousSeries{
|
||||
Name: "A test series",
|
||||
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.
|
||||
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.
|
||||
}
|
||||
|
||||
// note we create a LinearRegressionSeries series by assignin the inner series.
|
||||
|
@ -34,7 +33,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/linear_regression/output.png
Normal file
After Width: | Height: | Size: 60 KiB |
|
@ -1,22 +1,22 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/seq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
mainSeries := chart.ContinuousSeries{
|
||||
Name: "A test series",
|
||||
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(),
|
||||
XValues: SeqRange(1.0, 100.0),
|
||||
YValues: seq.New(seq.NewRandom().WithLen(100).WithMax(150).WithMin(50)).Array(),
|
||||
}
|
||||
|
||||
minSeries := &chart.MinSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeColor: chart.ColorAlternateGray,
|
||||
StrokeDashArray: []float64{5.0, 5.0},
|
||||
},
|
||||
|
@ -25,6 +25,7 @@ func main() {
|
|||
|
||||
maxSeries := &chart.MaxSeries{
|
||||
Style: chart.Style{
|
||||
Show: true,
|
||||
StrokeColor: chart.ColorAlternateGray,
|
||||
StrokeDashArray: []float64{5.0, 5.0},
|
||||
},
|
||||
|
@ -35,27 +36,35 @@ func main() {
|
|||
Width: 1920,
|
||||
Height: 1080,
|
||||
YAxis: chart.YAxis{
|
||||
Name: "Random Values",
|
||||
Name: "Random Values",
|
||||
NameStyle: chart.StyleShow(),
|
||||
Style: chart.StyleShow(),
|
||||
Range: &chart.ContinuousRange{
|
||||
Min: 25,
|
||||
Max: 175,
|
||||
},
|
||||
},
|
||||
XAxis: chart.XAxis{
|
||||
Name: "Random Other Values",
|
||||
Name: "Random Other Values",
|
||||
NameStyle: chart.StyleShow(),
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
Series: []chart.Series{
|
||||
mainSeries,
|
||||
minSeries,
|
||||
maxSeries,
|
||||
chart.LastValueAnnotationSeries(minSeries),
|
||||
chart.LastValueAnnotationSeries(maxSeries),
|
||||
chart.LastValueAnnotation(minSeries),
|
||||
chart.LastValueAnnotation(maxSeries),
|
||||
},
|
||||
}
|
||||
|
||||
graph.Elements = []chart.Renderable{chart.Legend(&graph)}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/min_max/output.png
Normal file
After Width: | Height: | Size: 170 KiB |
|
@ -5,7 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 565 B After Width: | Height: | Size: 565 B |
|
@ -1,14 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/seq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
In this example we add a new type of series, a `PolynomialRegressionSeries` that takes another series as a required argument.
|
||||
|
@ -17,8 +16,8 @@ func main() {
|
|||
|
||||
mainSeries := chart.ContinuousSeries{
|
||||
Name: "A test series",
|
||||
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.
|
||||
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.
|
||||
}
|
||||
|
||||
polyRegSeries := &chart.PolynomialRegressionSeries{
|
||||
|
@ -33,7 +32,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/poly_regression/output.png
Normal file
After Width: | Height: | Size: 54 KiB |
|
@ -1,22 +1,14 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := chart.NewLogger()
|
||||
drawChart(log)
|
||||
}
|
||||
|
||||
func parseInt(str string) int {
|
||||
v, _ := strconv.Atoi(str)
|
||||
return v
|
||||
|
@ -118,16 +110,24 @@ func drawChart(log chart.Logger) http.HandlerFunc {
|
|||
Series: []chart.Series{
|
||||
mainSeries,
|
||||
linreg,
|
||||
chart.LastValueAnnotationSeries(linreg),
|
||||
chart.LastValueAnnotation(linreg),
|
||||
sma,
|
||||
chart.LastValueAnnotationSeries(sma),
|
||||
chart.LastValueAnnotation(sma),
|
||||
},
|
||||
}
|
||||
|
||||
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", chart.ContentTypePNG)
|
||||
if err := graph.Render(chart.PNG, res); err != nil {
|
||||
log.Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
log := chart.NewLogger()
|
||||
log.Infof("listening on :8080")
|
||||
http.HandleFunc("/", drawChart(log))
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
Before Width: | Height: | Size: 116 KiB After Width: | Height: | Size: 116 KiB |
|
@ -6,7 +6,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
var lock sync.Mutex
|
|
@ -6,8 +6,8 @@ import (
|
|||
|
||||
_ "net/http/pprof"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
@ -20,12 +20,13 @@ 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.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(127)}.Values(),
|
||||
YValues: chart.Seq{Sequence: chart.NewRandomSequence().WithLen(128).WithMin(0).WithMax(1024)}.Values(),
|
||||
XValues: chart.SeqRange(0, 127),
|
||||
YValues: chart.NewSeq(chart.NewSeqRandom().WithLen(128).WithMax(1024)).Values(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -49,8 +50,8 @@ func unit(res http.ResponseWriter, req *http.Request) {
|
|||
},
|
||||
Series: []chart.Series{
|
||||
chart.ContinuousSeries{
|
||||
XValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(),
|
||||
YValues: chart.Seq{Sequence: chart.NewLinearSequence().WithStart(0).WithEnd(4)}.Values(),
|
||||
XValues: chart.SeqRangeWithStep(0, 4, 1),
|
||||
YValues: chart.SeqRangeWithStep(0, 4, 1),
|
||||
},
|
||||
},
|
||||
}
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
|
@ -1,18 +1,17 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
mainSeries := chart.ContinuousSeries{
|
||||
Name: "A test series",
|
||||
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.
|
||||
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.
|
||||
}
|
||||
|
||||
// note we create a SimpleMovingAverage series by assignin the inner series.
|
||||
|
@ -28,7 +27,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
BIN
_examples/simple_moving_average/output.png
Normal file
After Width: | Height: | Size: 68 KiB |
|
@ -1,20 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
sbc := chart.StackedBarChart{
|
||||
Title: "Test Stacked Bar Chart",
|
||||
Title: "Test Stacked Bar Chart",
|
||||
TitleStyle: chart.StyleShow(),
|
||||
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.",
|
||||
|
@ -47,7 +52,14 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
sbc.Render(chart.PNG, f)
|
||||
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))
|
||||
}
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
|
@ -1,16 +1,14 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
xv, yv := xvalues(), yvalues()
|
||||
|
||||
priceSeries := chart.TimeSeries{
|
||||
|
@ -57,9 +55,8 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func xvalues() []time.Time {
|
||||
|
@ -76,3 +73,8 @@ 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)
|
||||
}
|
BIN
_examples/stock_analysis/output.png
Normal file
After Width: | Height: | Size: 66 KiB |
|
@ -1,15 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
f, _ := chart.GetDefaultFont()
|
||||
r, _ := chart.PNG(1024, 1024)
|
||||
|
||||
|
@ -45,7 +43,11 @@ func main() {
|
|||
StrokeWidth: 2,
|
||||
})
|
||||
|
||||
file, _ := os.Create("output.png")
|
||||
defer file.Close()
|
||||
r.Save(file)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
r.Save(res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 8.6 KiB |
|
@ -4,7 +4,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
chart "git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
@ -13,6 +13,9 @@ 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{
|
||||
|
@ -43,6 +46,7 @@ func drawCustomChart(res http.ResponseWriter, req *http.Request) {
|
|||
*/
|
||||
graph := chart.Chart{
|
||||
XAxis: chart.XAxis{
|
||||
Style: chart.StyleShow(),
|
||||
ValueFormatter: chart.TimeHourValueFormatter,
|
||||
},
|
||||
Series: []chart.Series{
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
@ -1,15 +1,13 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"net/http"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func drawChart(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
/*
|
||||
In this example we add a second series, and assign it to the secondary y axis, giving that series it's own range.
|
||||
|
@ -19,6 +17,7 @@ func main() {
|
|||
|
||||
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)
|
||||
|
@ -26,6 +25,12 @@ func main() {
|
|||
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},
|
||||
|
@ -39,7 +44,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
f, _ := os.Create("output.png")
|
||||
defer f.Close()
|
||||
graph.Render(chart.PNG, f)
|
||||
res.Header().Set("Content-Type", "image/png")
|
||||
graph.Render(chart.PNG, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", drawChart)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
@ -1,27 +1,28 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
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),
|
||||
},
|
||||
|
||||
|
@ -33,11 +34,15 @@ func main() {
|
|||
|
||||
XAxis: chart.XAxis{
|
||||
Name: "The XAxis",
|
||||
NameStyle: chart.StyleShow(),
|
||||
Style: chart.StyleShow(),
|
||||
ValueFormatter: chart.TimeMinuteValueFormatter, //TimeHourValueFormatter,
|
||||
},
|
||||
|
||||
YAxis: chart.YAxis{
|
||||
Name: "The YAxis",
|
||||
Name: "The YAxis",
|
||||
NameStyle: chart.StyleShow(),
|
||||
Style: chart.StyleShow(),
|
||||
},
|
||||
|
||||
Series: []chart.Series{
|
BIN
_examples/twopoint/output.png
Normal file
After Width: | Height: | Size: 40 KiB |
|
@ -4,12 +4,13 @@ import (
|
|||
"image/color"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func TestAnnotationSeriesMeasure(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
as := AnnotationSeries{
|
||||
Annotations: []Value2{
|
||||
|
@ -21,10 +22,10 @@ func TestAnnotationSeriesMeasure(t *testing.T) {
|
|||
}
|
||||
|
||||
r, err := PNG(110, 110)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
xrange := &ContinuousRange{
|
||||
Min: 1.0,
|
||||
|
@ -49,15 +50,15 @@ func TestAnnotationSeriesMeasure(t *testing.T) {
|
|||
}
|
||||
|
||||
box := as.Measure(r, cb, xrange, yrange, sd)
|
||||
testutil.AssertFalse(t, box.IsZero())
|
||||
testutil.AssertEqual(t, -5.0, box.Top)
|
||||
testutil.AssertEqual(t, 5.0, box.Left)
|
||||
testutil.AssertEqual(t, 146.0, box.Right) //the top,left annotation sticks up 5px and out ~44px.
|
||||
testutil.AssertEqual(t, 115.0, box.Bottom)
|
||||
assert.False(box.IsZero())
|
||||
assert.Equal(-5.0, box.Top)
|
||||
assert.Equal(5.0, box.Left)
|
||||
assert.Equal(146.0, box.Right) //the top,left annotation sticks up 5px and out ~44px.
|
||||
assert.Equal(115.0, box.Bottom)
|
||||
}
|
||||
|
||||
func TestAnnotationSeriesRender(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
as := AnnotationSeries{
|
||||
Style: Style{
|
||||
|
@ -73,10 +74,10 @@ func TestAnnotationSeriesRender(t *testing.T) {
|
|||
}
|
||||
|
||||
r, err := PNG(110, 110)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
xrange := &ContinuousRange{
|
||||
Min: 1.0,
|
||||
|
@ -103,13 +104,13 @@ func TestAnnotationSeriesRender(t *testing.T) {
|
|||
as.Render(r, cb, xrange, yrange, sd)
|
||||
|
||||
rr, isRaster := r.(*rasterRenderer)
|
||||
testutil.AssertTrue(t, isRaster)
|
||||
testutil.AssertNotNil(t, rr)
|
||||
assert.True(isRaster)
|
||||
assert.NotNil(rr)
|
||||
|
||||
c := rr.i.At(38, 70)
|
||||
converted, isRGBA := color.RGBAModel.Convert(c).(color.RGBA)
|
||||
testutil.AssertTrue(t, isRGBA)
|
||||
testutil.AssertEqual(t, 0, converted.R)
|
||||
testutil.AssertEqual(t, 0, converted.G)
|
||||
testutil.AssertEqual(t, 0, converted.B)
|
||||
assert.True(isRGBA)
|
||||
assert.Equal(0, converted.R)
|
||||
assert.Equal(0, converted.G)
|
||||
assert.Equal(0, converted.B)
|
||||
}
|
||||
|
|
27
bar_chart.go
|
@ -264,7 +264,32 @@ func (bc BarChart) drawXAxis(r Renderer, canvasBox Box) {
|
|||
|
||||
func (bc BarChart) drawYAxis(r Renderer, canvasBox Box, yr Range, ticks []Tick) {
|
||||
if !bc.YAxis.Style.Hidden {
|
||||
bc.YAxis.Render(r, canvasBox, yr, bc.styleDefaultsAxes(), ticks)
|
||||
axisStyle := bc.YAxis.Style.InheritFrom(bc.styleDefaultsAxes())
|
||||
axisStyle.WriteToRenderer(r)
|
||||
|
||||
r.MoveTo(canvasBox.Right, canvasBox.Top)
|
||||
r.LineTo(canvasBox.Right, canvasBox.Bottom)
|
||||
r.Stroke()
|
||||
|
||||
r.MoveTo(canvasBox.Right, canvasBox.Bottom)
|
||||
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, canvasBox.Bottom)
|
||||
r.Stroke()
|
||||
|
||||
var ty int
|
||||
var tb Box
|
||||
for _, t := range ticks {
|
||||
ty = canvasBox.Bottom - yr.Translate(t.Value)
|
||||
|
||||
axisStyle.GetStrokeOptions().WriteToRenderer(r)
|
||||
r.MoveTo(canvasBox.Right, ty)
|
||||
r.LineTo(canvasBox.Right+DefaultHorizontalTickWidth, ty)
|
||||
r.Stroke()
|
||||
|
||||
axisStyle.GetTextOptions().WriteToRenderer(r)
|
||||
tb = r.MeasureText(t.Label)
|
||||
Draw.Text(r, t.Label, canvasBox.Right+DefaultYAxisMargin+5, ty+(tb.Height()>>1), axisStyle)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,11 +5,11 @@ import (
|
|||
"math"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
assert "github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestBarChartRender(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Width: 1024,
|
||||
|
@ -25,12 +25,12 @@ func TestBarChartRender(t *testing.T) {
|
|||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
err := bc.Render(PNG, buf)
|
||||
testutil.AssertNil(t, err)
|
||||
testutil.AssertNotZero(t, buf.Len())
|
||||
assert.Nil(err)
|
||||
assert.NotZero(buf.Len())
|
||||
}
|
||||
|
||||
func TestBarChartRenderZero(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Width: 1024,
|
||||
|
@ -43,64 +43,64 @@ func TestBarChartRenderZero(t *testing.T) {
|
|||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
err := bc.Render(PNG, buf)
|
||||
testutil.AssertNotNil(t, err)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
func TestBarChartProps(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
|
||||
testutil.AssertEqual(t, DefaultDPI, bc.GetDPI())
|
||||
assert.Equal(DefaultDPI, bc.GetDPI())
|
||||
bc.DPI = 100
|
||||
testutil.AssertEqual(t, 100, bc.GetDPI())
|
||||
assert.Equal(100, bc.GetDPI())
|
||||
|
||||
testutil.AssertNil(t, bc.GetFont())
|
||||
assert.Nil(bc.GetFont())
|
||||
f, err := GetDefaultFont()
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
bc.Font = f
|
||||
testutil.AssertNotNil(t, bc.GetFont())
|
||||
assert.NotNil(bc.GetFont())
|
||||
|
||||
testutil.AssertEqual(t, DefaultChartWidth, bc.GetWidth())
|
||||
assert.Equal(DefaultChartWidth, bc.GetWidth())
|
||||
bc.Width = DefaultChartWidth - 1
|
||||
testutil.AssertEqual(t, DefaultChartWidth-1, bc.GetWidth())
|
||||
assert.Equal(DefaultChartWidth-1, bc.GetWidth())
|
||||
|
||||
testutil.AssertEqual(t, DefaultChartHeight, bc.GetHeight())
|
||||
assert.Equal(DefaultChartHeight, bc.GetHeight())
|
||||
bc.Height = DefaultChartHeight - 1
|
||||
testutil.AssertEqual(t, DefaultChartHeight-1, bc.GetHeight())
|
||||
assert.Equal(DefaultChartHeight-1, bc.GetHeight())
|
||||
|
||||
testutil.AssertEqual(t, DefaultBarSpacing, bc.GetBarSpacing())
|
||||
assert.Equal(DefaultBarSpacing, bc.GetBarSpacing())
|
||||
bc.BarSpacing = 150
|
||||
testutil.AssertEqual(t, 150, bc.GetBarSpacing())
|
||||
assert.Equal(150, bc.GetBarSpacing())
|
||||
|
||||
testutil.AssertEqual(t, DefaultBarWidth, bc.GetBarWidth())
|
||||
assert.Equal(DefaultBarWidth, bc.GetBarWidth())
|
||||
bc.BarWidth = 75
|
||||
testutil.AssertEqual(t, 75, bc.GetBarWidth())
|
||||
assert.Equal(75, bc.GetBarWidth())
|
||||
}
|
||||
|
||||
func TestBarChartRenderNoBars(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
err := bc.Render(PNG, bytes.NewBuffer([]byte{}))
|
||||
testutil.AssertNotNil(t, err)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
func TestBarChartGetRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
|
||||
yr := bc.getRanges()
|
||||
testutil.AssertNotNil(t, yr)
|
||||
testutil.AssertFalse(t, yr.IsZero())
|
||||
assert.NotNil(yr)
|
||||
assert.False(yr.IsZero())
|
||||
|
||||
testutil.AssertEqual(t, -math.MaxFloat64, yr.GetMax())
|
||||
testutil.AssertEqual(t, math.MaxFloat64, yr.GetMin())
|
||||
assert.Equal(-math.MaxFloat64, yr.GetMax())
|
||||
assert.Equal(math.MaxFloat64, yr.GetMin())
|
||||
}
|
||||
|
||||
func TestBarChartGetRangesBarsMinMax(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Bars: []Value{
|
||||
|
@ -110,15 +110,15 @@ func TestBarChartGetRangesBarsMinMax(t *testing.T) {
|
|||
}
|
||||
|
||||
yr := bc.getRanges()
|
||||
testutil.AssertNotNil(t, yr)
|
||||
testutil.AssertFalse(t, yr.IsZero())
|
||||
assert.NotNil(yr)
|
||||
assert.False(yr.IsZero())
|
||||
|
||||
testutil.AssertEqual(t, 10, yr.GetMax())
|
||||
testutil.AssertEqual(t, 1, yr.GetMin())
|
||||
assert.Equal(10, yr.GetMax())
|
||||
assert.Equal(1, yr.GetMin())
|
||||
}
|
||||
|
||||
func TestBarChartGetRangesMinMax(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
YAxis: YAxis{
|
||||
|
@ -138,15 +138,15 @@ func TestBarChartGetRangesMinMax(t *testing.T) {
|
|||
}
|
||||
|
||||
yr := bc.getRanges()
|
||||
testutil.AssertNotNil(t, yr)
|
||||
testutil.AssertFalse(t, yr.IsZero())
|
||||
assert.NotNil(yr)
|
||||
assert.False(yr.IsZero())
|
||||
|
||||
testutil.AssertEqual(t, 15, yr.GetMax())
|
||||
testutil.AssertEqual(t, 5, yr.GetMin())
|
||||
assert.Equal(15, yr.GetMax())
|
||||
assert.Equal(5, yr.GetMin())
|
||||
}
|
||||
|
||||
func TestBarChartGetRangesTicksMinMax(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
YAxis: YAxis{
|
||||
|
@ -162,56 +162,56 @@ func TestBarChartGetRangesTicksMinMax(t *testing.T) {
|
|||
}
|
||||
|
||||
yr := bc.getRanges()
|
||||
testutil.AssertNotNil(t, yr)
|
||||
testutil.AssertFalse(t, yr.IsZero())
|
||||
assert.NotNil(yr)
|
||||
assert.False(yr.IsZero())
|
||||
|
||||
testutil.AssertEqual(t, 11, yr.GetMax())
|
||||
testutil.AssertEqual(t, 7, yr.GetMin())
|
||||
assert.Equal(11, yr.GetMax())
|
||||
assert.Equal(7, yr.GetMin())
|
||||
}
|
||||
|
||||
func TestBarChartHasAxes(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
testutil.AssertTrue(t, bc.hasAxes())
|
||||
assert.True(bc.hasAxes())
|
||||
bc.YAxis = YAxis{
|
||||
Style: Hidden(),
|
||||
}
|
||||
testutil.AssertFalse(t, bc.hasAxes())
|
||||
assert.False(bc.hasAxes())
|
||||
}
|
||||
|
||||
func TestBarChartGetDefaultCanvasBox(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
b := bc.getDefaultCanvasBox()
|
||||
testutil.AssertFalse(t, b.IsZero())
|
||||
assert.False(b.IsZero())
|
||||
}
|
||||
|
||||
func TestBarChartSetRangeDomains(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
cb := bc.box()
|
||||
yr := bc.getRanges()
|
||||
yr2 := bc.setRangeDomains(cb, yr)
|
||||
testutil.AssertNotZero(t, yr2.GetDomain())
|
||||
assert.NotZero(yr2.GetDomain())
|
||||
}
|
||||
|
||||
func TestBarChartGetValueFormatters(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{}
|
||||
vf := bc.getValueFormatters()
|
||||
testutil.AssertNotNil(t, vf)
|
||||
testutil.AssertEqual(t, "1234.00", vf(1234.0))
|
||||
assert.NotNil(vf)
|
||||
assert.Equal("1234.00", vf(1234.0))
|
||||
|
||||
bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" }
|
||||
testutil.AssertEqual(t, "test", bc.getValueFormatters()(1234))
|
||||
assert.Equal("test", bc.getValueFormatters()(1234))
|
||||
}
|
||||
|
||||
func TestBarChartGetAxesTicks(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Bars: []Value{
|
||||
|
@ -222,21 +222,21 @@ func TestBarChartGetAxesTicks(t *testing.T) {
|
|||
}
|
||||
|
||||
r, err := PNG(128, 128)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
yr := bc.getRanges()
|
||||
yf := bc.getValueFormatters()
|
||||
|
||||
bc.YAxis.Style.Hidden = true
|
||||
ticks := bc.getAxesTicks(r, yr, yf)
|
||||
testutil.AssertEmpty(t, ticks)
|
||||
assert.Empty(ticks)
|
||||
|
||||
bc.YAxis.Style.Hidden = false
|
||||
ticks = bc.getAxesTicks(r, yr, yf)
|
||||
testutil.AssertLen(t, ticks, 2)
|
||||
assert.Len(ticks, 2)
|
||||
}
|
||||
|
||||
func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Width: 1024,
|
||||
|
@ -251,15 +251,15 @@ func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
|
|||
}
|
||||
|
||||
spacing := bc.calculateEffectiveBarSpacing(bc.box())
|
||||
testutil.AssertNotZero(t, spacing)
|
||||
assert.NotZero(spacing)
|
||||
|
||||
bc.BarWidth = 250
|
||||
spacing = bc.calculateEffectiveBarSpacing(bc.box())
|
||||
testutil.AssertZero(t, spacing)
|
||||
assert.Zero(spacing)
|
||||
}
|
||||
|
||||
func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BarChart{
|
||||
Width: 1024,
|
||||
|
@ -276,35 +276,35 @@ func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
|
|||
cb := bc.box()
|
||||
|
||||
spacing := bc.calculateEffectiveBarSpacing(bc.box())
|
||||
testutil.AssertNotZero(t, spacing)
|
||||
assert.NotZero(spacing)
|
||||
|
||||
barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing)
|
||||
testutil.AssertEqual(t, 10, barWidth)
|
||||
assert.Equal(10, barWidth)
|
||||
|
||||
bc.BarWidth = 250
|
||||
spacing = bc.calculateEffectiveBarSpacing(bc.box())
|
||||
testutil.AssertZero(t, spacing)
|
||||
assert.Zero(spacing)
|
||||
barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing)
|
||||
testutil.AssertEqual(t, 199, barWidth)
|
||||
assert.Equal(199, barWidth)
|
||||
|
||||
testutil.AssertEqual(t, cb.Width()+1, bc.calculateTotalBarWidth(barWidth, spacing))
|
||||
assert.Equal(cb.Width()+1, bc.calculateTotalBarWidth(barWidth, spacing))
|
||||
|
||||
bw, bs, total := bc.calculateScaledTotalWidth(cb)
|
||||
testutil.AssertEqual(t, spacing, bs)
|
||||
testutil.AssertEqual(t, barWidth, bw)
|
||||
testutil.AssertEqual(t, cb.Width()+1, total)
|
||||
assert.Equal(spacing, bs)
|
||||
assert.Equal(barWidth, bw)
|
||||
assert.Equal(cb.Width()+1, total)
|
||||
}
|
||||
|
||||
func TestBarChatGetTitleFontSize(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
size := BarChart{Width: 2049, Height: 2049}.getTitleFontSize()
|
||||
testutil.AssertEqual(t, 48, size)
|
||||
assert.Equal(48, size)
|
||||
size = BarChart{Width: 1025, Height: 1025}.getTitleFontSize()
|
||||
testutil.AssertEqual(t, 24, size)
|
||||
assert.Equal(24, size)
|
||||
size = BarChart{Width: 513, Height: 513}.getTitleFontSize()
|
||||
testutil.AssertEqual(t, 18, size)
|
||||
assert.Equal(18, size)
|
||||
size = BarChart{Width: 257, Height: 257}.getTitleFontSize()
|
||||
testutil.AssertEqual(t, 12, size)
|
||||
assert.Equal(12, size)
|
||||
size = BarChart{Width: 128, Height: 128}.getTitleFontSize()
|
||||
testutil.AssertEqual(t, 10, size)
|
||||
assert.Equal(10, size)
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ import (
|
|||
"math"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestBollingerBandSeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
s1 := mockValuesProvider{
|
||||
X: LinearRange(1.0, 100.0),
|
||||
|
@ -29,12 +29,12 @@ func TestBollingerBandSeries(t *testing.T) {
|
|||
}
|
||||
|
||||
for x := bbs.GetPeriod(); x < 100; x++ {
|
||||
testutil.AssertTrue(t, y1values[x] > y2values[x], fmt.Sprintf("%v vs. %v", y1values[x], y2values[x]))
|
||||
assert.True(y1values[x] > y2values[x], fmt.Sprintf("%v vs. %v", y1values[x], y2values[x]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBollingerBandLastValue(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
s1 := mockValuesProvider{
|
||||
X: LinearRange(1.0, 100.0),
|
||||
|
@ -46,7 +46,7 @@ func TestBollingerBandLastValue(t *testing.T) {
|
|||
}
|
||||
|
||||
x, y1, y2 := bbs.GetBoundedLastValues()
|
||||
testutil.AssertEqual(t, 100.0, x)
|
||||
testutil.AssertEqual(t, 101, math.Floor(y1))
|
||||
testutil.AssertEqual(t, 83, math.Floor(y2))
|
||||
assert.Equal(100.0, x)
|
||||
assert.Equal(101, math.Floor(y1))
|
||||
assert.Equal(83, math.Floor(y2))
|
||||
}
|
||||
|
|
16
box.go
|
@ -254,22 +254,6 @@ func (b Box) OuterConstrain(bounds, other Box) Box {
|
|||
return newBox
|
||||
}
|
||||
|
||||
func (b Box) Validate() error {
|
||||
if b.Left < 0 {
|
||||
return fmt.Errorf("invalid left; must be >= 0")
|
||||
}
|
||||
if b.Right < 0 {
|
||||
return fmt.Errorf("invalid right; must be > 0")
|
||||
}
|
||||
if b.Top < 0 {
|
||||
return fmt.Errorf("invalid top; must be > 0")
|
||||
}
|
||||
if b.Bottom < 0 {
|
||||
return fmt.Errorf("invalid bottom; must be > 0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BoxCorners is a box with independent corners.
|
||||
type BoxCorners struct {
|
||||
TopLeft, TopRight, BottomRight, BottomLeft Point
|
||||
|
|
134
box_test.go
|
@ -4,131 +4,131 @@ import (
|
|||
"math"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestBoxClone(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
a := Box{Top: 5, Left: 5, Right: 15, Bottom: 15}
|
||||
b := a.Clone()
|
||||
testutil.AssertTrue(t, a.Equals(b))
|
||||
testutil.AssertTrue(t, b.Equals(a))
|
||||
assert.True(a.Equals(b))
|
||||
assert.True(b.Equals(a))
|
||||
}
|
||||
|
||||
func TestBoxEquals(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 5, Left: 5, Right: 15, Bottom: 15}
|
||||
b := Box{Top: 10, Left: 10, Right: 30, Bottom: 30}
|
||||
c := Box{Top: 5, Left: 5, Right: 15, Bottom: 15}
|
||||
testutil.AssertTrue(t, a.Equals(a))
|
||||
testutil.AssertTrue(t, a.Equals(c))
|
||||
testutil.AssertTrue(t, c.Equals(a))
|
||||
testutil.AssertFalse(t, a.Equals(b))
|
||||
testutil.AssertFalse(t, c.Equals(b))
|
||||
testutil.AssertFalse(t, b.Equals(a))
|
||||
testutil.AssertFalse(t, b.Equals(c))
|
||||
assert.True(a.Equals(a))
|
||||
assert.True(a.Equals(c))
|
||||
assert.True(c.Equals(a))
|
||||
assert.False(a.Equals(b))
|
||||
assert.False(c.Equals(b))
|
||||
assert.False(b.Equals(a))
|
||||
assert.False(b.Equals(c))
|
||||
}
|
||||
|
||||
func TestBoxIsBiggerThan(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 5, Left: 5, Right: 25, Bottom: 25}
|
||||
b := Box{Top: 10, Left: 10, Right: 20, Bottom: 20} // only half bigger
|
||||
c := Box{Top: 1, Left: 1, Right: 30, Bottom: 30} //bigger
|
||||
testutil.AssertTrue(t, a.IsBiggerThan(b))
|
||||
testutil.AssertFalse(t, a.IsBiggerThan(c))
|
||||
testutil.AssertTrue(t, c.IsBiggerThan(a))
|
||||
assert.True(a.IsBiggerThan(b))
|
||||
assert.False(a.IsBiggerThan(c))
|
||||
assert.True(c.IsBiggerThan(a))
|
||||
}
|
||||
|
||||
func TestBoxIsSmallerThan(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 5, Left: 5, Right: 25, Bottom: 25}
|
||||
b := Box{Top: 10, Left: 10, Right: 20, Bottom: 20} // only half bigger
|
||||
c := Box{Top: 1, Left: 1, Right: 30, Bottom: 30} //bigger
|
||||
testutil.AssertFalse(t, a.IsSmallerThan(b))
|
||||
testutil.AssertTrue(t, a.IsSmallerThan(c))
|
||||
testutil.AssertFalse(t, c.IsSmallerThan(a))
|
||||
assert.False(a.IsSmallerThan(b))
|
||||
assert.True(a.IsSmallerThan(c))
|
||||
assert.False(c.IsSmallerThan(a))
|
||||
}
|
||||
|
||||
func TestBoxGrow(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 1, Left: 2, Right: 15, Bottom: 15}
|
||||
b := Box{Top: 4, Left: 5, Right: 30, Bottom: 35}
|
||||
c := a.Grow(b)
|
||||
testutil.AssertFalse(t, c.Equals(b))
|
||||
testutil.AssertFalse(t, c.Equals(a))
|
||||
testutil.AssertEqual(t, 1, c.Top)
|
||||
testutil.AssertEqual(t, 2, c.Left)
|
||||
testutil.AssertEqual(t, 30, c.Right)
|
||||
testutil.AssertEqual(t, 35, c.Bottom)
|
||||
assert.False(c.Equals(b))
|
||||
assert.False(c.Equals(a))
|
||||
assert.Equal(1, c.Top)
|
||||
assert.Equal(2, c.Left)
|
||||
assert.Equal(30, c.Right)
|
||||
assert.Equal(35, c.Bottom)
|
||||
}
|
||||
|
||||
func TestBoxFit(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 64, Left: 64, Right: 192, Bottom: 192}
|
||||
b := Box{Top: 16, Left: 16, Right: 256, Bottom: 170}
|
||||
c := Box{Top: 16, Left: 16, Right: 170, Bottom: 256}
|
||||
|
||||
fab := a.Fit(b)
|
||||
testutil.AssertEqual(t, a.Left, fab.Left)
|
||||
testutil.AssertEqual(t, a.Right, fab.Right)
|
||||
testutil.AssertTrue(t, fab.Top < fab.Bottom)
|
||||
testutil.AssertTrue(t, fab.Left < fab.Right)
|
||||
testutil.AssertTrue(t, math.Abs(b.Aspect()-fab.Aspect()) < 0.02)
|
||||
assert.Equal(a.Left, fab.Left)
|
||||
assert.Equal(a.Right, fab.Right)
|
||||
assert.True(fab.Top < fab.Bottom)
|
||||
assert.True(fab.Left < fab.Right)
|
||||
assert.True(math.Abs(b.Aspect()-fab.Aspect()) < 0.02)
|
||||
|
||||
fac := a.Fit(c)
|
||||
testutil.AssertEqual(t, a.Top, fac.Top)
|
||||
testutil.AssertEqual(t, a.Bottom, fac.Bottom)
|
||||
testutil.AssertTrue(t, math.Abs(c.Aspect()-fac.Aspect()) < 0.02)
|
||||
assert.Equal(a.Top, fac.Top)
|
||||
assert.Equal(a.Bottom, fac.Bottom)
|
||||
assert.True(math.Abs(c.Aspect()-fac.Aspect()) < 0.02)
|
||||
}
|
||||
|
||||
func TestBoxConstrain(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
a := Box{Top: 64, Left: 64, Right: 192, Bottom: 192}
|
||||
b := Box{Top: 16, Left: 16, Right: 256, Bottom: 170}
|
||||
c := Box{Top: 16, Left: 16, Right: 170, Bottom: 256}
|
||||
|
||||
cab := a.Constrain(b)
|
||||
testutil.AssertEqual(t, 64, cab.Top)
|
||||
testutil.AssertEqual(t, 64, cab.Left)
|
||||
testutil.AssertEqual(t, 192, cab.Right)
|
||||
testutil.AssertEqual(t, 170, cab.Bottom)
|
||||
assert.Equal(64, cab.Top)
|
||||
assert.Equal(64, cab.Left)
|
||||
assert.Equal(192, cab.Right)
|
||||
assert.Equal(170, cab.Bottom)
|
||||
|
||||
cac := a.Constrain(c)
|
||||
testutil.AssertEqual(t, 64, cac.Top)
|
||||
testutil.AssertEqual(t, 64, cac.Left)
|
||||
testutil.AssertEqual(t, 170, cac.Right)
|
||||
testutil.AssertEqual(t, 192, cac.Bottom)
|
||||
assert.Equal(64, cac.Top)
|
||||
assert.Equal(64, cac.Left)
|
||||
assert.Equal(170, cac.Right)
|
||||
assert.Equal(192, cac.Bottom)
|
||||
}
|
||||
|
||||
func TestBoxOuterConstrain(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
box := NewBox(0, 0, 100, 100)
|
||||
canvas := NewBox(5, 5, 95, 95)
|
||||
taller := NewBox(-10, 5, 50, 50)
|
||||
|
||||
c := canvas.OuterConstrain(box, taller)
|
||||
testutil.AssertEqual(t, 15, c.Top, c.String())
|
||||
testutil.AssertEqual(t, 5, c.Left, c.String())
|
||||
testutil.AssertEqual(t, 95, c.Right, c.String())
|
||||
testutil.AssertEqual(t, 95, c.Bottom, c.String())
|
||||
assert.Equal(15, c.Top, c.String())
|
||||
assert.Equal(5, c.Left, c.String())
|
||||
assert.Equal(95, c.Right, c.String())
|
||||
assert.Equal(95, c.Bottom, c.String())
|
||||
|
||||
wider := NewBox(5, 5, 110, 50)
|
||||
d := canvas.OuterConstrain(box, wider)
|
||||
testutil.AssertEqual(t, 5, d.Top, d.String())
|
||||
testutil.AssertEqual(t, 5, d.Left, d.String())
|
||||
testutil.AssertEqual(t, 85, d.Right, d.String())
|
||||
testutil.AssertEqual(t, 95, d.Bottom, d.String())
|
||||
assert.Equal(5, d.Top, d.String())
|
||||
assert.Equal(5, d.Left, d.String())
|
||||
assert.Equal(85, d.Right, d.String())
|
||||
assert.Equal(95, d.Bottom, d.String())
|
||||
}
|
||||
|
||||
func TestBoxShift(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
b := Box{
|
||||
Top: 5,
|
||||
|
@ -138,14 +138,14 @@ func TestBoxShift(t *testing.T) {
|
|||
}
|
||||
|
||||
shifted := b.Shift(1, 2)
|
||||
testutil.AssertEqual(t, 7, shifted.Top)
|
||||
testutil.AssertEqual(t, 6, shifted.Left)
|
||||
testutil.AssertEqual(t, 11, shifted.Right)
|
||||
testutil.AssertEqual(t, 12, shifted.Bottom)
|
||||
assert.Equal(7, shifted.Top)
|
||||
assert.Equal(6, shifted.Left)
|
||||
assert.Equal(11, shifted.Right)
|
||||
assert.Equal(12, shifted.Bottom)
|
||||
}
|
||||
|
||||
func TestBoxCenter(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
b := Box{
|
||||
Top: 10,
|
||||
|
@ -154,12 +154,12 @@ func TestBoxCenter(t *testing.T) {
|
|||
Bottom: 30,
|
||||
}
|
||||
cx, cy := b.Center()
|
||||
testutil.AssertEqual(t, 15, cx)
|
||||
testutil.AssertEqual(t, 20, cy)
|
||||
assert.Equal(15, cx)
|
||||
assert.Equal(20, cy)
|
||||
}
|
||||
|
||||
func TestBoxCornersCenter(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BoxCorners{
|
||||
TopLeft: Point{5, 5},
|
||||
|
@ -169,12 +169,12 @@ func TestBoxCornersCenter(t *testing.T) {
|
|||
}
|
||||
|
||||
cx, cy := bc.Center()
|
||||
testutil.AssertEqual(t, 10, cx)
|
||||
testutil.AssertEqual(t, 10, cy)
|
||||
assert.Equal(10, cx)
|
||||
assert.Equal(10, cy)
|
||||
}
|
||||
|
||||
func TestBoxCornersRotate(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
bc := BoxCorners{
|
||||
TopLeft: Point{5, 5},
|
||||
|
@ -184,5 +184,5 @@ func TestBoxCornersRotate(t *testing.T) {
|
|||
}
|
||||
|
||||
rotated := bc.Rotate(45)
|
||||
testutil.AssertTrue(t, rotated.TopLeft.Equals(Point{10, 3}), rotated.String())
|
||||
assert.True(rotated.TopLeft.Equals(Point{10, 3}), rotated.String())
|
||||
}
|
||||
|
|
224
chart_test.go
|
@ -8,57 +8,58 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
|
||||
"github.com/wcharczuk/go-chart/drawing"
|
||||
)
|
||||
|
||||
func TestChartGetDPI(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
unset := Chart{}
|
||||
testutil.AssertEqual(t, DefaultDPI, unset.GetDPI())
|
||||
testutil.AssertEqual(t, 192, unset.GetDPI(192))
|
||||
assert.Equal(DefaultDPI, unset.GetDPI())
|
||||
assert.Equal(192, unset.GetDPI(192))
|
||||
|
||||
set := Chart{DPI: 128}
|
||||
testutil.AssertEqual(t, 128, set.GetDPI())
|
||||
testutil.AssertEqual(t, 128, set.GetDPI(192))
|
||||
assert.Equal(128, set.GetDPI())
|
||||
assert.Equal(128, set.GetDPI(192))
|
||||
}
|
||||
|
||||
func TestChartGetFont(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
f, err := GetDefaultFont()
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
unset := Chart{}
|
||||
testutil.AssertNil(t, unset.GetFont())
|
||||
assert.Nil(unset.GetFont())
|
||||
|
||||
set := Chart{Font: f}
|
||||
testutil.AssertNotNil(t, set.GetFont())
|
||||
assert.NotNil(set.GetFont())
|
||||
}
|
||||
|
||||
func TestChartGetWidth(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
unset := Chart{}
|
||||
testutil.AssertEqual(t, DefaultChartWidth, unset.GetWidth())
|
||||
assert.Equal(DefaultChartWidth, unset.GetWidth())
|
||||
|
||||
set := Chart{Width: DefaultChartWidth + 10}
|
||||
testutil.AssertEqual(t, DefaultChartWidth+10, set.GetWidth())
|
||||
assert.Equal(DefaultChartWidth+10, set.GetWidth())
|
||||
}
|
||||
|
||||
func TestChartGetHeight(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
unset := Chart{}
|
||||
testutil.AssertEqual(t, DefaultChartHeight, unset.GetHeight())
|
||||
assert.Equal(DefaultChartHeight, unset.GetHeight())
|
||||
|
||||
set := Chart{Height: DefaultChartHeight + 10}
|
||||
testutil.AssertEqual(t, DefaultChartHeight+10, set.GetHeight())
|
||||
assert.Equal(DefaultChartHeight+10, set.GetHeight())
|
||||
}
|
||||
|
||||
func TestChartGetRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Series: []Series{
|
||||
|
@ -79,14 +80,14 @@ func TestChartGetRanges(t *testing.T) {
|
|||
}
|
||||
|
||||
xrange, yrange, yrangeAlt := c.getRanges()
|
||||
testutil.AssertEqual(t, -2.0, xrange.GetMin())
|
||||
testutil.AssertEqual(t, 5.0, xrange.GetMax())
|
||||
assert.Equal(-2.0, xrange.GetMin())
|
||||
assert.Equal(5.0, xrange.GetMax())
|
||||
|
||||
testutil.AssertEqual(t, -2.1, yrange.GetMin())
|
||||
testutil.AssertEqual(t, 4.5, yrange.GetMax())
|
||||
assert.Equal(-2.1, yrange.GetMin())
|
||||
assert.Equal(4.5, yrange.GetMax())
|
||||
|
||||
testutil.AssertEqual(t, 10.0, yrangeAlt.GetMin())
|
||||
testutil.AssertEqual(t, 14.0, yrangeAlt.GetMax())
|
||||
assert.Equal(10.0, yrangeAlt.GetMin())
|
||||
assert.Equal(14.0, yrangeAlt.GetMax())
|
||||
|
||||
cSet := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -116,18 +117,18 @@ func TestChartGetRanges(t *testing.T) {
|
|||
}
|
||||
|
||||
xr2, yr2, yra2 := cSet.getRanges()
|
||||
testutil.AssertEqual(t, 9.8, xr2.GetMin())
|
||||
testutil.AssertEqual(t, 19.8, xr2.GetMax())
|
||||
assert.Equal(9.8, xr2.GetMin())
|
||||
assert.Equal(19.8, xr2.GetMax())
|
||||
|
||||
testutil.AssertEqual(t, 9.9, yr2.GetMin())
|
||||
testutil.AssertEqual(t, 19.9, yr2.GetMax())
|
||||
assert.Equal(9.9, yr2.GetMin())
|
||||
assert.Equal(19.9, yr2.GetMax())
|
||||
|
||||
testutil.AssertEqual(t, 9.7, yra2.GetMin())
|
||||
testutil.AssertEqual(t, 19.7, yra2.GetMax())
|
||||
assert.Equal(9.7, yra2.GetMin())
|
||||
assert.Equal(19.7, yra2.GetMax())
|
||||
}
|
||||
|
||||
func TestChartGetRangesUseTicks(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
// this test asserts that ticks should supercede manual ranges when generating the overall ranges.
|
||||
|
||||
|
@ -155,15 +156,15 @@ func TestChartGetRangesUseTicks(t *testing.T) {
|
|||
}
|
||||
|
||||
xr, yr, yar := c.getRanges()
|
||||
testutil.AssertEqual(t, -2.0, xr.GetMin())
|
||||
testutil.AssertEqual(t, 2.0, xr.GetMax())
|
||||
testutil.AssertEqual(t, 0.0, yr.GetMin())
|
||||
testutil.AssertEqual(t, 5.0, yr.GetMax())
|
||||
testutil.AssertTrue(t, yar.IsZero(), yar.String())
|
||||
assert.Equal(-2.0, xr.GetMin())
|
||||
assert.Equal(2.0, xr.GetMax())
|
||||
assert.Equal(0.0, yr.GetMin())
|
||||
assert.Equal(5.0, yr.GetMax())
|
||||
assert.True(yar.IsZero(), yar.String())
|
||||
}
|
||||
|
||||
func TestChartGetRangesUseUserRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
YAxis: YAxis{
|
||||
|
@ -181,15 +182,15 @@ func TestChartGetRangesUseUserRanges(t *testing.T) {
|
|||
}
|
||||
|
||||
xr, yr, yar := c.getRanges()
|
||||
testutil.AssertEqual(t, -2.0, xr.GetMin())
|
||||
testutil.AssertEqual(t, 2.0, xr.GetMax())
|
||||
testutil.AssertEqual(t, -5.0, yr.GetMin())
|
||||
testutil.AssertEqual(t, 5.0, yr.GetMax())
|
||||
testutil.AssertTrue(t, yar.IsZero(), yar.String())
|
||||
assert.Equal(-2.0, xr.GetMin())
|
||||
assert.Equal(2.0, xr.GetMax())
|
||||
assert.Equal(-5.0, yr.GetMin())
|
||||
assert.Equal(5.0, yr.GetMax())
|
||||
assert.True(yar.IsZero(), yar.String())
|
||||
}
|
||||
|
||||
func TestChartGetBackgroundStyle(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Background: Style{
|
||||
|
@ -198,11 +199,11 @@ func TestChartGetBackgroundStyle(t *testing.T) {
|
|||
}
|
||||
|
||||
bs := c.getBackgroundStyle()
|
||||
testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
|
||||
assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String())
|
||||
}
|
||||
|
||||
func TestChartGetCanvasStyle(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Canvas: Style{
|
||||
|
@ -211,19 +212,19 @@ func TestChartGetCanvasStyle(t *testing.T) {
|
|||
}
|
||||
|
||||
bs := c.getCanvasStyle()
|
||||
testutil.AssertEqual(t, bs.FillColor.String(), drawing.ColorBlack.String())
|
||||
assert.Equal(bs.FillColor.String(), drawing.ColorBlack.String())
|
||||
}
|
||||
|
||||
func TestChartGetDefaultCanvasBox(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{}
|
||||
canvasBoxDefault := c.getDefaultCanvasBox()
|
||||
testutil.AssertFalse(t, canvasBoxDefault.IsZero())
|
||||
testutil.AssertEqual(t, DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
|
||||
testutil.AssertEqual(t, DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
|
||||
testutil.AssertEqual(t, c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
|
||||
testutil.AssertEqual(t, c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)
|
||||
assert.False(canvasBoxDefault.IsZero())
|
||||
assert.Equal(DefaultBackgroundPadding.Top, canvasBoxDefault.Top)
|
||||
assert.Equal(DefaultBackgroundPadding.Left, canvasBoxDefault.Left)
|
||||
assert.Equal(c.GetWidth()-DefaultBackgroundPadding.Right, canvasBoxDefault.Right)
|
||||
assert.Equal(c.GetHeight()-DefaultBackgroundPadding.Bottom, canvasBoxDefault.Bottom)
|
||||
|
||||
custom := Chart{
|
||||
Background: Style{
|
||||
|
@ -236,15 +237,15 @@ func TestChartGetDefaultCanvasBox(t *testing.T) {
|
|||
},
|
||||
}
|
||||
canvasBoxCustom := custom.getDefaultCanvasBox()
|
||||
testutil.AssertFalse(t, canvasBoxCustom.IsZero())
|
||||
testutil.AssertEqual(t, DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
|
||||
testutil.AssertEqual(t, DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
|
||||
testutil.AssertEqual(t, c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
|
||||
testutil.AssertEqual(t, c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
|
||||
assert.False(canvasBoxCustom.IsZero())
|
||||
assert.Equal(DefaultBackgroundPadding.Top+1, canvasBoxCustom.Top)
|
||||
assert.Equal(DefaultBackgroundPadding.Left+1, canvasBoxCustom.Left)
|
||||
assert.Equal(c.GetWidth()-(DefaultBackgroundPadding.Right+1), canvasBoxCustom.Right)
|
||||
assert.Equal(c.GetHeight()-(DefaultBackgroundPadding.Bottom+1), canvasBoxCustom.Bottom)
|
||||
}
|
||||
|
||||
func TestChartGetValueFormatters(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Series: []Series{
|
||||
|
@ -265,16 +266,16 @@ func TestChartGetValueFormatters(t *testing.T) {
|
|||
}
|
||||
|
||||
dxf, dyf, dyaf := c.getValueFormatters()
|
||||
testutil.AssertNotNil(t, dxf)
|
||||
testutil.AssertNotNil(t, dyf)
|
||||
testutil.AssertNotNil(t, dyaf)
|
||||
assert.NotNil(dxf)
|
||||
assert.NotNil(dyf)
|
||||
assert.NotNil(dyaf)
|
||||
}
|
||||
|
||||
func TestChartHasAxes(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
testutil.AssertTrue(t, Chart{}.hasAxes())
|
||||
testutil.AssertFalse(t, Chart{XAxis: XAxis{Style: Hidden()}, YAxis: YAxis{Style: Hidden()}, YAxisSecondary: YAxis{Style: Hidden()}}.hasAxes())
|
||||
assert.True(Chart{}.hasAxes())
|
||||
assert.False(Chart{XAxis: XAxis{Style: Hidden()}, YAxis: YAxis{Style: Hidden()}, YAxisSecondary: YAxis{Style: Hidden()}}.hasAxes())
|
||||
|
||||
x := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -287,7 +288,7 @@ func TestChartHasAxes(t *testing.T) {
|
|||
Style: Hidden(),
|
||||
},
|
||||
}
|
||||
testutil.AssertTrue(t, x.hasAxes())
|
||||
assert.True(x.hasAxes())
|
||||
|
||||
y := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -300,7 +301,7 @@ func TestChartHasAxes(t *testing.T) {
|
|||
Style: Hidden(),
|
||||
},
|
||||
}
|
||||
testutil.AssertTrue(t, y.hasAxes())
|
||||
assert.True(y.hasAxes())
|
||||
|
||||
ya := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -313,14 +314,14 @@ func TestChartHasAxes(t *testing.T) {
|
|||
Style: Shown(),
|
||||
},
|
||||
}
|
||||
testutil.AssertTrue(t, ya.hasAxes())
|
||||
assert.True(ya.hasAxes())
|
||||
}
|
||||
|
||||
func TestChartGetAxesTicks(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
r, err := PNG(1024, 1024)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
c := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -336,13 +337,13 @@ func TestChartGetAxesTicks(t *testing.T) {
|
|||
xr, yr, yar := c.getRanges()
|
||||
|
||||
xt, yt, yat := c.getAxesTicks(r, xr, yr, yar, FloatValueFormatter, FloatValueFormatter, FloatValueFormatter)
|
||||
testutil.AssertNotEmpty(t, xt)
|
||||
testutil.AssertNotEmpty(t, yt)
|
||||
testutil.AssertNotEmpty(t, yat)
|
||||
assert.NotEmpty(xt)
|
||||
assert.NotEmpty(yt)
|
||||
assert.NotEmpty(yat)
|
||||
}
|
||||
|
||||
func TestChartSingleSeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
now := time.Now()
|
||||
c := Chart{
|
||||
Title: "Hello!",
|
||||
|
@ -365,11 +366,11 @@ func TestChartSingleSeries(t *testing.T) {
|
|||
|
||||
buffer := bytes.NewBuffer([]byte{})
|
||||
c.Render(PNG, buffer)
|
||||
testutil.AssertNotEmpty(t, buffer.Bytes())
|
||||
assert.NotEmpty(buffer.Bytes())
|
||||
}
|
||||
|
||||
func TestChartRegressionBadRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Series: []Series{
|
||||
|
@ -381,11 +382,11 @@ func TestChartRegressionBadRanges(t *testing.T) {
|
|||
}
|
||||
buffer := bytes.NewBuffer([]byte{})
|
||||
c.Render(PNG, buffer)
|
||||
testutil.AssertTrue(t, true, "Render needs to finish.")
|
||||
assert.True(true, "Render needs to finish.")
|
||||
}
|
||||
|
||||
func TestChartRegressionBadRangesByUser(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
YAxis: YAxis{
|
||||
|
@ -403,11 +404,11 @@ func TestChartRegressionBadRangesByUser(t *testing.T) {
|
|||
}
|
||||
buffer := bytes.NewBuffer([]byte{})
|
||||
c.Render(PNG, buffer)
|
||||
testutil.AssertTrue(t, true, "Render needs to finish.")
|
||||
assert.True(true, "Render needs to finish.")
|
||||
}
|
||||
|
||||
func TestChartValidatesSeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Series: []Series{
|
||||
|
@ -418,7 +419,7 @@ func TestChartValidatesSeries(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
testutil.AssertNil(t, c.validateSeries())
|
||||
assert.Nil(c.validateSeries())
|
||||
|
||||
c = Chart{
|
||||
Series: []Series{
|
||||
|
@ -428,11 +429,11 @@ func TestChartValidatesSeries(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
testutil.AssertNotNil(t, c.validateSeries())
|
||||
assert.NotNil(c.validateSeries())
|
||||
}
|
||||
|
||||
func TestChartCheckRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Series: []Series{
|
||||
|
@ -444,11 +445,11 @@ func TestChartCheckRanges(t *testing.T) {
|
|||
}
|
||||
|
||||
xr, yr, yra := c.getRanges()
|
||||
testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
|
||||
assert.Nil(c.checkRanges(xr, yr, yra))
|
||||
}
|
||||
|
||||
func TestChartCheckRangesWithRanges(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
XAxis: XAxis{
|
||||
|
@ -472,7 +473,7 @@ func TestChartCheckRangesWithRanges(t *testing.T) {
|
|||
}
|
||||
|
||||
xr, yr, yra := c.getRanges()
|
||||
testutil.AssertNil(t, c.checkRanges(xr, yr, yra))
|
||||
assert.Nil(c.checkRanges(xr, yr, yra))
|
||||
}
|
||||
|
||||
func at(i image.Image, x, y int) drawing.Color {
|
||||
|
@ -480,7 +481,7 @@ func at(i image.Image, x, y int) drawing.Color {
|
|||
}
|
||||
|
||||
func TestChartE2ELine(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
c := Chart{
|
||||
Height: 50,
|
||||
|
@ -505,26 +506,26 @@ func TestChartE2ELine(t *testing.T) {
|
|||
|
||||
var buffer = &bytes.Buffer{}
|
||||
err := c.Render(PNG, buffer)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
// do color tests ...
|
||||
|
||||
i, err := png.Decode(buffer)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
// test the bottom and top of the line
|
||||
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
|
||||
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 49, 49))
|
||||
assert.Equal(drawing.ColorWhite, at(i, 0, 0))
|
||||
assert.Equal(drawing.ColorWhite, at(i, 49, 49))
|
||||
|
||||
// test a line mid point
|
||||
defaultSeriesColor := GetDefaultColor(0)
|
||||
testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
|
||||
testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
|
||||
testutil.AssertEqual(t, drawing.ColorFromHex("bddbf6"), at(i, 24, 24))
|
||||
assert.Equal(defaultSeriesColor, at(i, 0, 49))
|
||||
assert.Equal(defaultSeriesColor, at(i, 49, 0))
|
||||
assert.Equal(drawing.ColorFromHex("bddbf6"), at(i, 24, 24))
|
||||
}
|
||||
|
||||
func TestChartE2ELineWithFill(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
logBuffer := new(bytes.Buffer)
|
||||
|
||||
|
@ -554,41 +555,22 @@ func TestChartE2ELineWithFill(t *testing.T) {
|
|||
Log: NewLogger(OptLoggerStdout(logBuffer), OptLoggerStderr(logBuffer)),
|
||||
}
|
||||
|
||||
testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).XValues))
|
||||
testutil.AssertEqual(t, 5, len(c.Series[0].(ContinuousSeries).YValues))
|
||||
assert.Equal(5, len(c.Series[0].(ContinuousSeries).XValues))
|
||||
assert.Equal(5, len(c.Series[0].(ContinuousSeries).YValues))
|
||||
|
||||
var buffer = &bytes.Buffer{}
|
||||
err := c.Render(PNG, buffer)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
i, err := png.Decode(buffer)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
|
||||
// test the bottom and top of the line
|
||||
testutil.AssertEqual(t, drawing.ColorWhite, at(i, 0, 0))
|
||||
testutil.AssertEqual(t, drawing.ColorRed, at(i, 49, 49))
|
||||
assert.Equal(drawing.ColorWhite, at(i, 0, 0))
|
||||
assert.Equal(drawing.ColorRed, at(i, 49, 49))
|
||||
|
||||
// test a line mid point
|
||||
defaultSeriesColor := drawing.ColorBlue
|
||||
testutil.AssertEqual(t, defaultSeriesColor, at(i, 0, 49))
|
||||
testutil.AssertEqual(t, defaultSeriesColor, at(i, 49, 0))
|
||||
}
|
||||
|
||||
func Test_Chart_cve(t *testing.T) {
|
||||
poc := StackedBarChart{
|
||||
Title: "poc",
|
||||
Bars: []StackedBar{
|
||||
{
|
||||
Name: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
|
||||
Values: []Value{
|
||||
{Value: 1, Label: "infinite"},
|
||||
{Value: 1, Label: "loop"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var imgContent bytes.Buffer
|
||||
err := poc.Render(PNG, &imgContent)
|
||||
testutil.AssertNotNil(t, err)
|
||||
assert.Equal(defaultSeriesColor, at(i, 0, 49))
|
||||
assert.Equal(defaultSeriesColor, at(i, 49, 0))
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
chart "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package chart
|
||||
|
||||
import "git.smarteching.com/zeni/go-chart/v2/drawing"
|
||||
import "github.com/wcharczuk/go-chart/drawing"
|
||||
|
||||
var (
|
||||
// ColorWhite is white.
|
||||
|
|
|
@ -3,11 +3,11 @@ package chart
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
assert "github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestConcatSeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
s1 := ContinuousSeries{
|
||||
XValues: LinearRange(1.0, 10.0),
|
||||
|
@ -25,17 +25,17 @@ func TestConcatSeries(t *testing.T) {
|
|||
}
|
||||
|
||||
cs := ConcatSeries([]Series{s1, s2, s3})
|
||||
testutil.AssertEqual(t, 30, cs.Len())
|
||||
assert.Equal(30, cs.Len())
|
||||
|
||||
x0, y0 := cs.GetValue(0)
|
||||
testutil.AssertEqual(t, 1.0, x0)
|
||||
testutil.AssertEqual(t, 1.0, y0)
|
||||
assert.Equal(1.0, x0)
|
||||
assert.Equal(1.0, y0)
|
||||
|
||||
xm, ym := cs.GetValue(19)
|
||||
testutil.AssertEqual(t, 20.0, xm)
|
||||
testutil.AssertEqual(t, 1.0, ym)
|
||||
assert.Equal(20.0, xm)
|
||||
assert.Equal(1.0, ym)
|
||||
|
||||
xn, yn := cs.GetValue(29)
|
||||
testutil.AssertEqual(t, 30.0, xn)
|
||||
testutil.AssertEqual(t, 10.0, yn)
|
||||
assert.Equal(30.0, xn)
|
||||
assert.Equal(10.0, yn)
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ package chart
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestRangeTranslate(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
values := []float64{1.0, 2.0, 2.5, 2.7, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}
|
||||
r := ContinuousRange{Domain: 1000}
|
||||
r.Min, r.Max = MinMax(values...)
|
||||
|
@ -16,7 +16,7 @@ func TestRangeTranslate(t *testing.T) {
|
|||
// value = ~5.0
|
||||
// domain = ~1000
|
||||
// 5/8 * 1000 ~=
|
||||
testutil.AssertEqual(t, 0, r.Translate(1.0))
|
||||
testutil.AssertEqual(t, 1000, r.Translate(8.0))
|
||||
testutil.AssertEqual(t, 572, r.Translate(5.0))
|
||||
assert.Equal(0, r.Translate(1.0))
|
||||
assert.Equal(1000, r.Translate(8.0))
|
||||
assert.Equal(572, r.Translate(5.0))
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
assert "github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestContinuousSeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
cs := ContinuousSeries{
|
||||
Name: "Test Series",
|
||||
|
@ -16,23 +16,23 @@ func TestContinuousSeries(t *testing.T) {
|
|||
YValues: LinearRange(1.0, 10.0),
|
||||
}
|
||||
|
||||
testutil.AssertEqual(t, "Test Series", cs.GetName())
|
||||
testutil.AssertEqual(t, 10, cs.Len())
|
||||
assert.Equal("Test Series", cs.GetName())
|
||||
assert.Equal(10, cs.Len())
|
||||
x0, y0 := cs.GetValues(0)
|
||||
testutil.AssertEqual(t, 1.0, x0)
|
||||
testutil.AssertEqual(t, 1.0, y0)
|
||||
assert.Equal(1.0, x0)
|
||||
assert.Equal(1.0, y0)
|
||||
|
||||
xn, yn := cs.GetValues(9)
|
||||
testutil.AssertEqual(t, 10.0, xn)
|
||||
testutil.AssertEqual(t, 10.0, yn)
|
||||
assert.Equal(10.0, xn)
|
||||
assert.Equal(10.0, yn)
|
||||
|
||||
xn, yn = cs.GetLastValues()
|
||||
testutil.AssertEqual(t, 10.0, xn)
|
||||
testutil.AssertEqual(t, 10.0, yn)
|
||||
assert.Equal(10.0, xn)
|
||||
assert.Equal(10.0, yn)
|
||||
}
|
||||
|
||||
func TestContinuousSeriesValueFormatter(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
cs := ContinuousSeries{
|
||||
XValueFormatter: func(v interface{}) string {
|
||||
|
@ -44,29 +44,29 @@ func TestContinuousSeriesValueFormatter(t *testing.T) {
|
|||
}
|
||||
|
||||
xf, yf := cs.GetValueFormatters()
|
||||
testutil.AssertEqual(t, "0.100000 foo", xf(0.1))
|
||||
testutil.AssertEqual(t, "0.100000 bar", yf(0.1))
|
||||
assert.Equal("0.100000 foo", xf(0.1))
|
||||
assert.Equal("0.100000 bar", yf(0.1))
|
||||
}
|
||||
|
||||
func TestContinuousSeriesValidate(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
cs := ContinuousSeries{
|
||||
Name: "Test Series",
|
||||
XValues: LinearRange(1.0, 10.0),
|
||||
YValues: LinearRange(1.0, 10.0),
|
||||
}
|
||||
testutil.AssertNil(t, cs.Validate())
|
||||
assert.Nil(cs.Validate())
|
||||
|
||||
cs = ContinuousSeries{
|
||||
Name: "Test Series",
|
||||
XValues: LinearRange(1.0, 10.0),
|
||||
}
|
||||
testutil.AssertNotNil(t, cs.Validate())
|
||||
assert.NotNil(cs.Validate())
|
||||
|
||||
cs = ContinuousSeries{
|
||||
Name: "Test Series",
|
||||
YValues: LinearRange(1.0, 10.0),
|
||||
}
|
||||
testutil.AssertNotNil(t, cs.Validate())
|
||||
assert.NotNil(cs.Validate())
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"bytes"
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestDonutChart(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
pie := DonutChart{
|
||||
Canvas: Style{
|
||||
|
@ -27,11 +27,11 @@ func TestDonutChart(t *testing.T) {
|
|||
|
||||
b := bytes.NewBuffer([]byte{})
|
||||
pie.Render(PNG, b)
|
||||
testutil.AssertNotZero(t, b.Len())
|
||||
assert.NotZero(b.Len())
|
||||
}
|
||||
|
||||
func TestDonutChartDropsZeroValues(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
pie := DonutChart{
|
||||
Canvas: Style{
|
||||
|
@ -46,11 +46,11 @@ func TestDonutChartDropsZeroValues(t *testing.T) {
|
|||
|
||||
b := bytes.NewBuffer([]byte{})
|
||||
err := pie.Render(PNG, b)
|
||||
testutil.AssertNil(t, err)
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
func TestDonutChartAllZeroValues(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
pie := DonutChart{
|
||||
Canvas: Style{
|
||||
|
@ -65,5 +65,5 @@ func TestDonutChartAllZeroValues(t *testing.T) {
|
|||
|
||||
b := bytes.NewBuffer([]byte{})
|
||||
err := pie.Render(PNG, b)
|
||||
testutil.AssertNotNil(t, err)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
|
6
draw.go
|
@ -296,10 +296,8 @@ func (d draw) TextWithin(r Renderer, text string, box Box, style Style) {
|
|||
switch style.GetTextVerticalAlign() {
|
||||
case TextVerticalAlignBottom, TextVerticalAlignBaseline: // i have to build better baseline handling into measure text
|
||||
y = y - linesBox.Height()
|
||||
case TextVerticalAlignMiddle:
|
||||
y = y + (box.Height() >> 1) - (linesBox.Height() >> 1)
|
||||
case TextVerticalAlignMiddleBaseline:
|
||||
y = y + (box.Height() >> 1) - linesBox.Height()
|
||||
case TextVerticalAlignMiddle, TextVerticalAlignMiddleBaseline:
|
||||
y = (y - linesBox.Height()) >> 1
|
||||
}
|
||||
|
||||
var tx, ty int
|
||||
|
|
162
drawing/color.go
|
@ -2,46 +2,27 @@ package drawing
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Basic Colors from:
|
||||
// https://www.w3.org/wiki/CSS/Properties/color/keywords
|
||||
var (
|
||||
// ColorTransparent is a fully transparent color.
|
||||
ColorTransparent = Color{R: 255, G: 255, B: 255, A: 0}
|
||||
ColorTransparent = Color{}
|
||||
|
||||
// ColorWhite is white.
|
||||
ColorWhite = Color{R: 255, G: 255, B: 255, A: 255}
|
||||
|
||||
// ColorBlack is black.
|
||||
ColorBlack = Color{R: 0, G: 0, B: 0, A: 255}
|
||||
|
||||
// ColorRed is red.
|
||||
ColorRed = Color{R: 255, G: 0, B: 0, A: 255}
|
||||
|
||||
// ColorGreen is green.
|
||||
ColorGreen = Color{R: 0, G: 128, B: 0, A: 255}
|
||||
ColorGreen = Color{R: 0, G: 255, B: 0, A: 255}
|
||||
|
||||
// ColorBlue is blue.
|
||||
ColorBlue = Color{R: 0, G: 0, B: 255, A: 255}
|
||||
// ColorSilver is a known color.
|
||||
ColorSilver = Color{R: 192, G: 192, B: 192, A: 255}
|
||||
// ColorMaroon is a known color.
|
||||
ColorMaroon = Color{R: 128, G: 0, B: 0, A: 255}
|
||||
// ColorPurple is a known color.
|
||||
ColorPurple = Color{R: 128, G: 0, B: 128, A: 255}
|
||||
// ColorFuchsia is a known color.
|
||||
ColorFuchsia = Color{R: 255, G: 0, B: 255, A: 255}
|
||||
// ColorLime is a known color.
|
||||
ColorLime = Color{R: 0, G: 255, B: 0, A: 255}
|
||||
// ColorOlive is a known color.
|
||||
ColorOlive = Color{R: 128, G: 128, B: 0, A: 255}
|
||||
// ColorYellow is a known color.
|
||||
ColorYellow = Color{R: 255, G: 255, B: 0, A: 255}
|
||||
// ColorNavy is a known color.
|
||||
ColorNavy = Color{R: 0, G: 0, B: 128, A: 255}
|
||||
// ColorTeal is a known color.
|
||||
ColorTeal = Color{R: 0, G: 128, B: 128, A: 255}
|
||||
// ColorAqua is a known color.
|
||||
ColorAqua = Color{R: 0, G: 255, B: 255, A: 255}
|
||||
)
|
||||
|
||||
func parseHex(hex string) uint8 {
|
||||
|
@ -49,97 +30,8 @@ func parseHex(hex string) uint8 {
|
|||
return uint8(v)
|
||||
}
|
||||
|
||||
// ParseColor parses a color from a string.
|
||||
func ParseColor(rawColor string) Color {
|
||||
if strings.HasPrefix(rawColor, "rgba") {
|
||||
return ColorFromRGBA(rawColor)
|
||||
}
|
||||
if strings.HasPrefix(rawColor, "rgb") {
|
||||
return ColorFromRGB(rawColor)
|
||||
}
|
||||
if strings.HasPrefix(rawColor, "#") {
|
||||
return ColorFromHex(rawColor)
|
||||
}
|
||||
return ColorFromKnown(rawColor)
|
||||
}
|
||||
|
||||
var rgbaexpr = regexp.MustCompile(`rgba\((?P<R>.+),(?P<G>.+),(?P<B>.+),(?P<A>.+)\)`)
|
||||
|
||||
// ColorFromRGBA returns a color from an `rgba()` css function.
|
||||
func ColorFromRGBA(rgba string) (output Color) {
|
||||
values := rgbaexpr.FindStringSubmatch(rgba)
|
||||
for i, name := range rgbaexpr.SubexpNames() {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if i >= len(values) {
|
||||
break
|
||||
}
|
||||
switch name {
|
||||
case "R":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.R = uint8(parsed)
|
||||
case "G":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.G = uint8(parsed)
|
||||
case "B":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.B = uint8(parsed)
|
||||
case "A":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseFloat(value, 32)
|
||||
if parsed > 1 {
|
||||
parsed = 1
|
||||
} else if parsed < 0 {
|
||||
parsed = 0
|
||||
}
|
||||
output.A = uint8(parsed * 255)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var rgbexpr = regexp.MustCompile(`rgb\((?P<R>.+),(?P<G>.+),(?P<B>.+)\)`)
|
||||
|
||||
// ColorFromRGB returns a color from an `rgb()` css function.
|
||||
func ColorFromRGB(rgb string) (output Color) {
|
||||
output.A = 255
|
||||
values := rgbexpr.FindStringSubmatch(rgb)
|
||||
for i, name := range rgbaexpr.SubexpNames() {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if i >= len(values) {
|
||||
break
|
||||
}
|
||||
switch name {
|
||||
case "R":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.R = uint8(parsed)
|
||||
case "G":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.G = uint8(parsed)
|
||||
case "B":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.B = uint8(parsed)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ColorFromHex returns a color from a css hex code.
|
||||
//
|
||||
// NOTE: it will trim a leading '#' character if present.
|
||||
func ColorFromHex(hex string) Color {
|
||||
if strings.HasPrefix(hex, "#") {
|
||||
hex = strings.TrimPrefix(hex, "#")
|
||||
}
|
||||
var c Color
|
||||
if len(hex) == 3 {
|
||||
c.R = parseHex(string(hex[0])) * 0x11
|
||||
|
@ -154,46 +46,6 @@ func ColorFromHex(hex string) Color {
|
|||
return c
|
||||
}
|
||||
|
||||
// ColorFromKnown returns an internal color from a known (basic) color name.
|
||||
func ColorFromKnown(known string) Color {
|
||||
switch strings.ToLower(known) {
|
||||
case "transparent":
|
||||
return ColorTransparent
|
||||
case "white":
|
||||
return ColorWhite
|
||||
case "black":
|
||||
return ColorBlack
|
||||
case "red":
|
||||
return ColorRed
|
||||
case "blue":
|
||||
return ColorBlue
|
||||
case "green":
|
||||
return ColorGreen
|
||||
case "silver":
|
||||
return ColorSilver
|
||||
case "maroon":
|
||||
return ColorMaroon
|
||||
case "purple":
|
||||
return ColorPurple
|
||||
case "fuchsia":
|
||||
return ColorFuchsia
|
||||
case "lime":
|
||||
return ColorLime
|
||||
case "olive":
|
||||
return ColorOlive
|
||||
case "yellow":
|
||||
return ColorYellow
|
||||
case "navy":
|
||||
return ColorNavy
|
||||
case "teal":
|
||||
return ColorTeal
|
||||
case "aqua":
|
||||
return ColorAqua
|
||||
default:
|
||||
return Color{}
|
||||
}
|
||||
}
|
||||
|
||||
// ColorFromAlphaMixedRGBA returns the system alpha mixed rgba values.
|
||||
func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color {
|
||||
fa := float64(a) / 255.0
|
||||
|
|
|
@ -1,114 +1,53 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"image/color"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
func TestColorFromHex(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
white := ColorFromHex("FFFFFF")
|
||||
testutil.AssertEqual(t, ColorWhite, white)
|
||||
assert.Equal(ColorWhite, white)
|
||||
|
||||
shortWhite := ColorFromHex("FFF")
|
||||
testutil.AssertEqual(t, ColorWhite, shortWhite)
|
||||
assert.Equal(ColorWhite, shortWhite)
|
||||
|
||||
black := ColorFromHex("000000")
|
||||
testutil.AssertEqual(t, ColorBlack, black)
|
||||
assert.Equal(ColorBlack, black)
|
||||
|
||||
shortBlack := ColorFromHex("000")
|
||||
testutil.AssertEqual(t, ColorBlack, shortBlack)
|
||||
assert.Equal(ColorBlack, shortBlack)
|
||||
|
||||
red := ColorFromHex("FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, red)
|
||||
assert.Equal(ColorRed, red)
|
||||
|
||||
shortRed := ColorFromHex("F00")
|
||||
testutil.AssertEqual(t, ColorRed, shortRed)
|
||||
assert.Equal(ColorRed, shortRed)
|
||||
|
||||
green := ColorFromHex("008000")
|
||||
testutil.AssertEqual(t, ColorGreen, green)
|
||||
green := ColorFromHex("00FF00")
|
||||
assert.Equal(ColorGreen, green)
|
||||
|
||||
// shortGreen := ColorFromHex("0F0")
|
||||
// testutil.AssertEqual(t, ColorGreen, shortGreen)
|
||||
shortGreen := ColorFromHex("0F0")
|
||||
assert.Equal(ColorGreen, shortGreen)
|
||||
|
||||
blue := ColorFromHex("0000FF")
|
||||
testutil.AssertEqual(t, ColorBlue, blue)
|
||||
assert.Equal(ColorBlue, blue)
|
||||
|
||||
shortBlue := ColorFromHex("00F")
|
||||
testutil.AssertEqual(t, ColorBlue, shortBlue)
|
||||
}
|
||||
|
||||
func TestColorFromHex_handlesHash(t *testing.T) {
|
||||
withHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withHash)
|
||||
|
||||
withoutHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withoutHash)
|
||||
assert.Equal(ColorBlue, shortBlue)
|
||||
}
|
||||
|
||||
func TestColorFromAlphaMixedRGBA(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
black := ColorFromAlphaMixedRGBA(color.Black.RGBA())
|
||||
testutil.AssertTrue(t, black.Equals(ColorBlack), black.String())
|
||||
assert.True(black.Equals(ColorBlack), black.String())
|
||||
|
||||
white := ColorFromAlphaMixedRGBA(color.White.RGBA())
|
||||
testutil.AssertTrue(t, white.Equals(ColorWhite), white.String())
|
||||
}
|
||||
|
||||
func Test_ColorFromRGBA(t *testing.T) {
|
||||
value := "rgba(192, 192, 192, 1.0)"
|
||||
parsed := ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
|
||||
value = "rgba(192,192,192,1.0)"
|
||||
parsed = ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
|
||||
value = "rgba(192,192,192,1.5)"
|
||||
parsed = ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
}
|
||||
|
||||
func TestParseColor(t *testing.T) {
|
||||
testCases := [...]struct {
|
||||
Input string
|
||||
Expected Color
|
||||
}{
|
||||
{"", Color{}},
|
||||
{"white", ColorWhite},
|
||||
{"WHITE", ColorWhite}, // caps!
|
||||
{"black", ColorBlack},
|
||||
{"red", ColorRed},
|
||||
{"green", ColorGreen},
|
||||
{"blue", ColorBlue},
|
||||
{"silver", ColorSilver},
|
||||
{"maroon", ColorMaroon},
|
||||
{"purple", ColorPurple},
|
||||
{"fuchsia", ColorFuchsia},
|
||||
{"lime", ColorLime},
|
||||
{"olive", ColorOlive},
|
||||
{"yellow", ColorYellow},
|
||||
{"navy", ColorNavy},
|
||||
{"teal", ColorTeal},
|
||||
{"aqua", ColorAqua},
|
||||
|
||||
{"rgba(192, 192, 192, 1.0)", ColorSilver},
|
||||
{"rgba(192,192,192,1.0)", ColorSilver},
|
||||
{"rgb(192, 192, 192)", ColorSilver},
|
||||
{"rgb(192,192,192)", ColorSilver},
|
||||
|
||||
{"#FF0000", ColorRed},
|
||||
{"#008000", ColorGreen},
|
||||
{"#0000FF", ColorBlue},
|
||||
{"#F00", ColorRed},
|
||||
{"#080", Color{0, 136, 0, 255}},
|
||||
{"#00F", ColorBlue},
|
||||
}
|
||||
|
||||
for index, tc := range testCases {
|
||||
actual := ParseColor(tc.Input)
|
||||
testutil.AssertEqual(t, tc.Expected, actual, fmt.Sprintf("test case: %d -> %s", index, tc.Input))
|
||||
}
|
||||
assert.True(white.Equals(ColorWhite), white.String())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package drawing
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
assert "github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
type point struct {
|
||||
|
@ -23,7 +23,7 @@ func (ml mockLine) Len() int {
|
|||
}
|
||||
|
||||
func TestTraceQuad(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
// Quad
|
||||
// x1, y1, cpx1, cpy2, x2, y2 float64
|
||||
|
@ -31,5 +31,5 @@ func TestTraceQuad(t *testing.T) {
|
|||
quad := []float64{10, 20, 20, 20, 20, 10}
|
||||
liner := &mockLine{}
|
||||
TraceQuad(liner, quad, 0.5)
|
||||
testutil.AssertNotZero(t, liner.Len())
|
||||
assert.NotZero(liner.Len())
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package chart
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2/testutil"
|
||||
"github.com/blend/go-sdk/assert"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -73,13 +73,13 @@ var (
|
|||
)
|
||||
|
||||
func TestEMASeries(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
assert := assert.New(t)
|
||||
|
||||
mockSeries := mockValuesProvider{
|
||||
emaXValues,
|
||||
emaYValues,
|
||||
}
|
||||
testutil.AssertEqual(t, 50, mockSeries.Len())
|
||||
assert.Equal(50, mockSeries.Len())
|
||||
|
||||
ema := &EMASeries{
|
||||
InnerSeries: mockSeries,
|
||||
|
@ -87,7 +87,7 @@ func TestEMASeries(t *testing.T) {
|
|||
}
|
||||
|
||||
sig := ema.GetSigma()
|
||||
testutil.AssertEqual(t, 2.0/(26.0+1), sig)
|
||||
assert.Equal(2.0/(26.0+1), sig)
|
||||
|
||||
var yvalues []float64
|
||||
for x := 0; x < ema.Len(); x++ {
|
||||
|
@ -96,10 +96,10 @@ func TestEMASeries(t *testing.T) {
|
|||
}
|
||||
|
||||
for index, yv := range yvalues {
|
||||
testutil.AssertInDelta(t, yv, emaExpected[index], emaDelta)
|
||||
assert.InDelta(yv, emaExpected[index], emaDelta)
|
||||
}
|
||||
|
||||
lvx, lvy := ema.GetLastValues()
|
||||
testutil.AssertEqual(t, 50.0, lvx)
|
||||
testutil.AssertInDelta(t, lvy, emaExpected[49], emaDelta)
|
||||
assert.Equal(50.0, lvx)
|
||||
assert.InDelta(lvy, emaExpected[49], emaDelta)
|
||||
}
|
||||
|
|
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 24 KiB |
|
@ -1,35 +0,0 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
Before Width: | Height: | Size: 19 KiB |
|
@ -1,23 +0,0 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
Before Width: | Height: | Size: 24 KiB |
|
@ -1,52 +0,0 @@
|
|||
package main
|
||||
|
||||
//go:generate go run main.go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/zeni/go-chart/v2"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|