http chart to src/view
This commit is contained in:
109
src/view/chart.go
Normal file
109
src/view/chart.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/go-echarts/go-echarts/v2/charts"
|
||||
"github.com/go-echarts/go-echarts/v2/opts"
|
||||
)
|
||||
|
||||
type Chart interface {
|
||||
AddX(interface{})
|
||||
AddY(string, []int)
|
||||
Render(io.Writer) error
|
||||
Overlap(Chart)
|
||||
}
|
||||
|
||||
func NewChart(name string) Chart {
|
||||
switch name {
|
||||
case "line":
|
||||
return NewLine()
|
||||
case "bar":
|
||||
return NewBar()
|
||||
case "stack":
|
||||
return NewStack()
|
||||
default:
|
||||
panic("bad chart name " + name)
|
||||
}
|
||||
}
|
||||
|
||||
type Line struct {
|
||||
*charts.Line
|
||||
}
|
||||
|
||||
func NewLine() Line {
|
||||
return Line{Line: charts.NewLine()}
|
||||
}
|
||||
|
||||
func (line Line) AddX(v interface{}) {
|
||||
line.SetXAxis(v)
|
||||
}
|
||||
|
||||
func (line Line) AddY(name string, v []int) {
|
||||
y := make([]opts.LineData, len(v))
|
||||
for i := range y {
|
||||
y[i].Value = v[i]
|
||||
}
|
||||
line.AddSeries(name, y).
|
||||
SetSeriesOptions(charts.WithBarChartOpts(opts.BarChart{
|
||||
Stack: "stackB",
|
||||
}))
|
||||
}
|
||||
|
||||
func (line Line) Overlap(other Chart) {
|
||||
overlapper, ok := other.(charts.Overlaper)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("cannot overlap %T", other))
|
||||
}
|
||||
line.Line.Overlap(overlapper)
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
*charts.Bar
|
||||
}
|
||||
|
||||
func NewBar() Bar {
|
||||
return Bar{Bar: charts.NewBar()}
|
||||
}
|
||||
|
||||
func (bar Bar) AddX(v interface{}) {
|
||||
bar.SetXAxis(v)
|
||||
}
|
||||
|
||||
func (bar Bar) AddY(name string, v []int) {
|
||||
y := make([]opts.BarData, len(v))
|
||||
for i := range v {
|
||||
y[i].Value = v[i]
|
||||
}
|
||||
bar.AddSeries(name, y)
|
||||
}
|
||||
|
||||
func (bar Bar) Overlap(other Chart) {
|
||||
overlapper, ok := other.(charts.Overlaper)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("cannot overlap %T", other))
|
||||
}
|
||||
bar.Bar.Overlap(overlapper)
|
||||
}
|
||||
|
||||
type Stack struct {
|
||||
Bar
|
||||
}
|
||||
|
||||
func NewStack() Stack {
|
||||
bar := NewBar()
|
||||
bar.SetSeriesOptions(charts.WithBarChartOpts(opts.BarChart{Stack: "x"}))
|
||||
return Stack{Bar: bar}
|
||||
}
|
||||
|
||||
func (stack Stack) AddY(name string, v []int) {
|
||||
y := make([]opts.BarData, len(v))
|
||||
for i := range v {
|
||||
y[i].Value = v[i]
|
||||
}
|
||||
stack.AddSeries(name, y).
|
||||
SetSeriesOptions(charts.WithBarChartOpts(opts.BarChart{
|
||||
Stack: "stackA",
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user