feat: support between ticks
This commit is contained in:
parent
402141c484
commit
f3009b965f
7 changed files with 175 additions and 29 deletions
17
axis.go
17
axis.go
|
|
@ -42,10 +42,18 @@ type (
|
||||||
|
|
||||||
const axisStrokeWidth = 1
|
const axisStrokeWidth = 1
|
||||||
|
|
||||||
func GetXAxisAndValues(xAxis XAxis, theme string) (chart.XAxis, []float64) {
|
func GetXAxisAndValues(xAxis XAxis, tickPosition chart.TickPosition, theme string) (chart.XAxis, []float64) {
|
||||||
xValues := make([]float64, len(xAxis.Data))
|
data := xAxis.Data
|
||||||
ticks := make([]chart.Tick, len(xAxis.Data))
|
// 如果居中,则需要多添加一个值
|
||||||
for index, key := range xAxis.Data {
|
if tickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
data = append([]string{
|
||||||
|
"",
|
||||||
|
}, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
xValues := make([]float64, len(data))
|
||||||
|
ticks := make([]chart.Tick, len(data))
|
||||||
|
for index, key := range data {
|
||||||
f := float64(index)
|
f := float64(index)
|
||||||
xValues[index] = f
|
xValues[index] = f
|
||||||
ticks[index] = chart.Tick{
|
ticks[index] = chart.Tick{
|
||||||
|
|
@ -61,6 +69,7 @@ func GetXAxisAndValues(xAxis XAxis, theme string) (chart.XAxis, []float64) {
|
||||||
}
|
}
|
||||||
return chart.XAxis{
|
return chart.XAxis{
|
||||||
Ticks: ticks,
|
Ticks: ticks,
|
||||||
|
TickPosition: tickPosition,
|
||||||
Style: chart.Style{
|
Style: chart.Style{
|
||||||
FontColor: AxisColorLight,
|
FontColor: AxisColorLight,
|
||||||
StrokeColor: AxisColorLight,
|
StrokeColor: AxisColorLight,
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,26 @@
|
||||||
|
|
||||||
package charts
|
package charts
|
||||||
|
|
||||||
import "github.com/wcharczuk/go-chart/v2"
|
import (
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultBarMargin = 10
|
||||||
|
|
||||||
type BarSeries struct {
|
type BarSeries struct {
|
||||||
BaseSeries
|
BaseSeries
|
||||||
|
Count int
|
||||||
|
Index int
|
||||||
|
// 间隔
|
||||||
|
Margin int
|
||||||
|
// 偏移量
|
||||||
|
Offset int
|
||||||
|
// 宽度
|
||||||
|
BarWidth int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
|
func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
|
||||||
if bs.Len() == 0 {
|
if bs.Len() == 0 || bs.Count <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
style := bs.Style.InheritFrom(defaults)
|
style := bs.Style.InheritFrom(defaults)
|
||||||
|
|
@ -40,18 +52,25 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
|
||||||
|
|
||||||
cb := canvasBox.Bottom
|
cb := canvasBox.Bottom
|
||||||
cl := canvasBox.Left
|
cl := canvasBox.Left
|
||||||
|
margin := bs.Margin
|
||||||
|
if margin <= 0 {
|
||||||
|
margin = defaultBarMargin
|
||||||
|
}
|
||||||
|
barWidth := bs.BarWidth
|
||||||
|
if barWidth <= 0 {
|
||||||
|
barWidth = canvasBox.Width() / (bs.Len() * bs.Count)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < bs.Len(); i++ {
|
for i := 0; i < bs.Len(); i++ {
|
||||||
vx, vy := bs.GetValues(i)
|
vx, vy := bs.GetValues(i)
|
||||||
|
|
||||||
x := cl + xrange.Translate(vx)
|
x := cl + xrange.Translate(vx) + bs.Index*(margin+barWidth) + bs.Offset
|
||||||
y := cb - yrange.Translate(vy)
|
y := cb - yrange.Translate(vy)
|
||||||
|
|
||||||
chart.Draw.Box(r, chart.Box{
|
chart.Draw.Box(r, chart.Box{
|
||||||
Left: x,
|
Left: x,
|
||||||
Top: y,
|
Top: y,
|
||||||
// TODO 计算宽度
|
Right: x + barWidth,
|
||||||
Right: x + 10,
|
|
||||||
Bottom: canvasBox.Bottom - 1,
|
Bottom: canvasBox.Bottom - 1,
|
||||||
}, style)
|
}, style)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ var (
|
||||||
type BaseSeries struct {
|
type BaseSeries struct {
|
||||||
Name string
|
Name string
|
||||||
Style chart.Style
|
Style chart.Style
|
||||||
|
TickPosition chart.TickPosition
|
||||||
|
|
||||||
YAxis chart.YAxisType
|
YAxis chart.YAxisType
|
||||||
|
|
||||||
|
|
@ -61,16 +62,26 @@ func (cs BaseSeries) GetStyle() chart.Style {
|
||||||
|
|
||||||
// Len returns the number of elements in the series.
|
// Len returns the number of elements in the series.
|
||||||
func (cs BaseSeries) Len() int {
|
func (cs BaseSeries) Len() int {
|
||||||
return len(cs.XValues)
|
offset := 0
|
||||||
|
if cs.TickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
offset = -1
|
||||||
|
}
|
||||||
|
return len(cs.XValues) + offset
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValues gets the x,y values at a given index.
|
// GetValues gets the x,y values at a given index.
|
||||||
func (cs BaseSeries) GetValues(index int) (float64, float64) {
|
func (cs BaseSeries) GetValues(index int) (float64, float64) {
|
||||||
|
if cs.TickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
index++
|
||||||
|
}
|
||||||
return cs.XValues[index], cs.YValues[index]
|
return cs.XValues[index], cs.YValues[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFirstValues gets the first x,y values.
|
// GetFirstValues gets the first x,y values.
|
||||||
func (cs BaseSeries) GetFirstValues() (float64, float64) {
|
func (cs BaseSeries) GetFirstValues() (float64, float64) {
|
||||||
|
if cs.TickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
return cs.XValues[1], cs.YValues[1]
|
||||||
|
}
|
||||||
return cs.XValues[0], cs.YValues[0]
|
return cs.XValues[0], cs.YValues[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,10 @@ func ToSVG(c *chart.Chart) ([]byte, error) {
|
||||||
return render(c, chart.SVG)
|
return render(c, chart.SVG)
|
||||||
}
|
}
|
||||||
func New(opt Option) *chart.Chart {
|
func New(opt Option) *chart.Chart {
|
||||||
xAxis, xValues := GetXAxisAndValues(opt.XAxis, opt.Theme)
|
tickPosition := chart.TickPositionBetweenTicks
|
||||||
|
// tickPosition = chart.TickPositionUnset
|
||||||
|
|
||||||
|
xAxis, xValues := GetXAxisAndValues(opt.XAxis, tickPosition, opt.Theme)
|
||||||
|
|
||||||
legendSize := len(opt.Legend.Data)
|
legendSize := len(opt.Legend.Data)
|
||||||
for index, item := range opt.Series {
|
for index, item := range opt.Series {
|
||||||
|
|
@ -87,7 +90,7 @@ func New(opt Option) *chart.Chart {
|
||||||
Height: opt.Height,
|
Height: opt.Height,
|
||||||
XAxis: xAxis,
|
XAxis: xAxis,
|
||||||
YAxis: GetYAxis(opt.Theme),
|
YAxis: GetYAxis(opt.Theme),
|
||||||
Series: GetSeries(opt.Series, opt.Theme),
|
Series: GetSeries(opt.Series, tickPosition, opt.Theme),
|
||||||
}
|
}
|
||||||
// 设置secondary的样式
|
// 设置secondary的样式
|
||||||
c.YAxisSecondary.Style = c.YAxis.Style
|
c.YAxisSecondary.Style = c.YAxis.Style
|
||||||
|
|
|
||||||
40
line_series.go
Normal file
40
line_series.go
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
// 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 (
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LineSeries struct {
|
||||||
|
BaseSeries
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bs LineSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
|
||||||
|
style := bs.Style.InheritFrom(defaults)
|
||||||
|
// 如果是居中,画线时重新调整
|
||||||
|
if bs.TickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
xrange = wrapRange(xrange, bs.TickPosition)
|
||||||
|
}
|
||||||
|
chart.Draw.LineSeries(r, canvasBox, xrange, yrange, style, bs)
|
||||||
|
}
|
||||||
52
range.go
Normal file
52
range.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
// 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 (
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Range struct {
|
||||||
|
TickPosition chart.TickPosition
|
||||||
|
chart.ContinuousRange
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapRange(r chart.Range, tickPosition chart.TickPosition) chart.Range {
|
||||||
|
xr, ok := r.(*chart.ContinuousRange)
|
||||||
|
if !ok {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return &Range{
|
||||||
|
TickPosition: tickPosition,
|
||||||
|
ContinuousRange: *xr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Translate maps a given value into the ContinuousRange space.
|
||||||
|
func (r Range) Translate(value float64) int {
|
||||||
|
v := r.ContinuousRange.Translate(value)
|
||||||
|
if r.TickPosition == chart.TickPositionBetweenTicks {
|
||||||
|
v -= int(float64(r.Domain) / (r.GetDelta() * 2))
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
36
series.go
36
series.go
|
|
@ -49,8 +49,15 @@ func getSeriesColor(theme string, index int) drawing.Color {
|
||||||
return SeriesColorsLight[index%len(SeriesColorsLight)]
|
return SeriesColorsLight[index%len(SeriesColorsLight)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSeries(series []Series, theme string) []chart.Series {
|
func GetSeries(series []Series, tickPosition chart.TickPosition, theme string) []chart.Series {
|
||||||
arr := make([]chart.Series, len(series))
|
arr := make([]chart.Series, len(series))
|
||||||
|
barCount := 0
|
||||||
|
barIndex := 0
|
||||||
|
for _, item := range series {
|
||||||
|
if item.Type == SeriesBar {
|
||||||
|
barCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
for index, item := range series {
|
for index, item := range series {
|
||||||
style := chart.Style{
|
style := chart.Style{
|
||||||
StrokeWidth: lineStrokeWidth,
|
StrokeWidth: lineStrokeWidth,
|
||||||
|
|
@ -58,23 +65,28 @@ func GetSeries(series []Series, theme string) []chart.Series {
|
||||||
DotColor: getSeriesColor(theme, index),
|
DotColor: getSeriesColor(theme, index),
|
||||||
DotWidth: dotWith,
|
DotWidth: dotWith,
|
||||||
}
|
}
|
||||||
|
item.Data = append([]float64{
|
||||||
|
0.0,
|
||||||
|
}, item.Data...)
|
||||||
|
baseSeries := BaseSeries{
|
||||||
|
Name: item.Name,
|
||||||
|
XValues: item.XValues,
|
||||||
|
Style: style,
|
||||||
|
YValues: item.Data,
|
||||||
|
TickPosition: tickPosition,
|
||||||
|
}
|
||||||
// TODO 判断类型
|
// TODO 判断类型
|
||||||
switch item.Type {
|
switch item.Type {
|
||||||
case SeriesBar:
|
case SeriesBar:
|
||||||
arr[index] = BarSeries{
|
arr[index] = BarSeries{
|
||||||
BaseSeries: BaseSeries{
|
Count: barCount,
|
||||||
Name: item.Name,
|
Index: barIndex,
|
||||||
XValues: item.XValues,
|
BaseSeries: baseSeries,
|
||||||
Style: style,
|
|
||||||
YValues: item.Data,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
barIndex++
|
||||||
default:
|
default:
|
||||||
arr[index] = chart.ContinuousSeries{
|
arr[index] = LineSeries{
|
||||||
Name: item.Name,
|
BaseSeries: baseSeries,
|
||||||
XValues: item.XValues,
|
|
||||||
Style: style,
|
|
||||||
YValues: item.Data,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue