feat: init commit
This commit is contained in:
parent
2e4fa9b674
commit
deb77f5761
5 changed files with 344 additions and 0 deletions
112
charts.go
Normal file
112
charts.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
// MIT License
|
||||
|
||||
// Copyright (c) 2021 Tree Xie
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package charts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
)
|
||||
|
||||
type (
|
||||
Title struct {
|
||||
Text string
|
||||
}
|
||||
Legend struct {
|
||||
Data []string
|
||||
}
|
||||
Option struct {
|
||||
Width int
|
||||
Height int
|
||||
Theme string
|
||||
XAxis XAxis
|
||||
// YAxis Axis
|
||||
Series []Series
|
||||
Title Title
|
||||
Legend Legend
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
ThemeLight = "light"
|
||||
ThemeDark = "dark"
|
||||
)
|
||||
|
||||
func render(c *chart.Chart, rp chart.RendererProvider) ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
err := c.Render(rp, &buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func ToPNG(c *chart.Chart) ([]byte, error) {
|
||||
return render(c, chart.PNG)
|
||||
}
|
||||
|
||||
func ToSVG(c *chart.Chart) ([]byte, error) {
|
||||
return render(c, chart.SVG)
|
||||
}
|
||||
|
||||
func New(opt Option) *chart.Chart {
|
||||
xAxis, xValues := GetXAxisAndValues(opt.XAxis, opt.Theme)
|
||||
|
||||
legendSize := len(opt.Legend.Data)
|
||||
for index, item := range opt.Series {
|
||||
if len(item.XValues) == 0 {
|
||||
opt.Series[index].XValues = xValues
|
||||
}
|
||||
if index < legendSize && opt.Series[index].Name == "" {
|
||||
opt.Series[index].Name = opt.Legend.Data[index]
|
||||
}
|
||||
}
|
||||
|
||||
c := &chart.Chart{
|
||||
Title: opt.Title.Text,
|
||||
Width: opt.Width,
|
||||
Height: opt.Height,
|
||||
XAxis: xAxis,
|
||||
YAxis: GetYAxis(opt.Theme),
|
||||
Series: GetSeries(opt.Series, opt.Theme),
|
||||
}
|
||||
if legendSize != 0 {
|
||||
c.Elements = []chart.Renderable{
|
||||
chart.LegendThin(c, chart.Style{
|
||||
FillColor: hiddenColor,
|
||||
// FillColor: drawing.ColorWhite,
|
||||
// FontColor: DefaultTextColor,
|
||||
FontSize: 8.0,
|
||||
StrokeColor: hiddenColor,
|
||||
// StrokeWidth: DefaultAxisLineWidth,
|
||||
// Padding: Box{
|
||||
// Top: 2,
|
||||
// Left: 7,
|
||||
// Right: 7,
|
||||
// Bottom: 5,
|
||||
// },
|
||||
}),
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue