feat: support multi y axis

This commit is contained in:
vicanso 2021-12-18 09:49:44 +08:00
parent ead48fef8e
commit 23e2eca0c6
6 changed files with 62 additions and 61 deletions

72
axis.go
View file

@ -42,6 +42,7 @@ type (
type YAxisOption struct { type YAxisOption struct {
Formater chart.ValueFormatter Formater chart.ValueFormatter
Disabled bool
} }
const axisStrokeWidth = 1 const axisStrokeWidth = 1
@ -111,46 +112,6 @@ func defaultFloatFormater(v interface{}) string {
} }
func GetSecondaryYAxis(theme string, option *YAxisOption) chart.YAxis { func GetSecondaryYAxis(theme string, option *YAxisOption) chart.YAxis {
// TODO
if theme == ThemeDark {
return chart.YAxis{}
}
// strokeColor := drawing.Color{
// R: 224,
// G: 230,
// B: 241,
// A: 255,
// }
formater := defaultFloatFormater
if option != nil {
if option.Formater != nil {
formater = option.Formater
}
}
hidden := chart.Hidden()
return chart.YAxis{
ValueFormatter: formater,
AxisType: chart.YAxisPrimary,
GridMajorStyle: hidden,
GridMinorStyle: hidden,
Style: chart.Style{
FontColor: getAxisColor(theme),
// alpha 0隐藏
StrokeColor: hiddenColor,
StrokeWidth: axisStrokeWidth,
},
}
// c := chart.Hidden()
// return chart.YAxis{
// ValueFormatter: defaultFloatFormater,
// AxisType: chart.YAxisPrimary,
// GridMajorStyle: c,
// GridMinorStyle: c,
// Style: c,
// }
}
func GetYAxis(theme string, option *YAxisOption) chart.YAxis {
strokeColor := getGridColor(theme) strokeColor := getGridColor(theme)
formater := defaultFloatFormater formater := defaultFloatFormater
if option != nil { if option != nil {
@ -177,3 +138,34 @@ func GetYAxis(theme string, option *YAxisOption) chart.YAxis {
}, },
} }
} }
func GetYAxis(theme string, option *YAxisOption) chart.YAxis {
// strokeColor := getGridColor(theme)
formater := defaultFloatFormater
disabled := false
if option != nil {
if option.Formater != nil {
formater = option.Formater
}
disabled = option.Disabled
}
hidden := chart.Hidden()
yAxis := chart.YAxis{
ValueFormatter: formater,
AxisType: chart.YAxisPrimary,
GridMajorStyle: hidden,
GridMinorStyle: hidden,
Style: chart.Style{
FontColor: getAxisColor(theme),
// alpha 0隐藏
StrokeColor: hiddenColor,
StrokeWidth: axisStrokeWidth,
},
}
if disabled {
yAxis.Range = &HiddenRange{}
yAxis.Style.Hidden = true
}
return yAxis
}

View file

@ -58,6 +58,7 @@ type (
Title Title Title Title
Legend Legend Legend Legend
TickPosition chart.TickPosition TickPosition chart.TickPosition
Log chart.Logger
} }
) )
@ -142,16 +143,20 @@ func New(opt Options) (Graph, error) {
} }
} }
var yAxisOption *YAxisOption
if len(opt.YAxisOptions) != 0 {
yAxisOption = opt.YAxisOptions[0]
}
var secondaryYAxisOption *YAxisOption var secondaryYAxisOption *YAxisOption
if len(opt.YAxisOptions) != 0 {
secondaryYAxisOption = opt.YAxisOptions[0]
}
yAxisOption := &YAxisOption{
Disabled: true,
}
if len(opt.YAxisOptions) > 1 { if len(opt.YAxisOptions) > 1 {
secondaryYAxisOption = opt.YAxisOptions[1] yAxisOption = opt.YAxisOptions[1]
} }
g := &chart.Chart{ g := &chart.Chart{
Log: opt.Log,
ColorPalette: &ThemeColorPalette{ ColorPalette: &ThemeColorPalette{
Theme: opt.Theme, Theme: opt.Theme,
}, },

View file

@ -154,14 +154,10 @@ func convertEChartsSeries(e *ECharsOptions) ([]Series, chart.TickPosition) {
} }
data[j] = sd data[j] = sd
} }
yAxisType := chart.YAxisPrimary
if item.YAxisIndex != 0 {
yAxisType = chart.YAxisSecondary
}
series[index] = Series{ series[index] = Series{
YAxis: yAxisType, YAxisIndex: item.YAxisIndex,
Data: data, Data: data,
Type: item.Type, Type: item.Type,
} }
} }
return series, tickPosition return series, tickPosition

View file

@ -288,9 +288,6 @@ var chartOptions = []map[string]string{
func render(theme string) ([]byte, error) { func render(theme string) ([]byte, error) {
data := bytes.Buffer{} data := bytes.Buffer{}
for _, m := range chartOptions { for _, m := range chartOptions {
if m["title"] != "折柱混合" {
continue
}
chartHTML := []byte(`<div> chartHTML := []byte(`<div>
<h1>{{title}}</h1> <h1>{{title}}</h1>
<pre>{{option}}</pre> <pre>{{option}}</pre>

View file

@ -50,3 +50,11 @@ func (r Range) Translate(value float64) int {
} }
return v return v
} }
type HiddenRange struct {
chart.ContinuousRange
}
func (r HiddenRange) GetDelta() float64 {
return 0
}

View file

@ -32,11 +32,11 @@ type SeriesData struct {
} }
type Series struct { type Series struct {
Type string Type string
Name string Name string
Data []SeriesData Data []SeriesData
XValues []float64 XValues []float64
YAxis chart.YAxisType YAxisIndex int
} }
const lineStrokeWidth = 2 const lineStrokeWidth = 2
@ -103,7 +103,10 @@ func GetSeries(series []Series, tickPosition chart.TickPosition, theme string) [
Style: style, Style: style,
YValues: yValues, YValues: yValues,
TickPosition: tickPosition, TickPosition: tickPosition,
YAxis: item.YAxis, YAxis: chart.YAxisSecondary,
}
if item.YAxisIndex != 0 {
baseSeries.YAxis = chart.YAxisPrimary
} }
// TODO 判断类型 // TODO 判断类型
switch item.Type { switch item.Type {