Skip to content

Commit

Permalink
bugfix/未提供橫軸時會造成圖表不正確,此修改在沒提供橫軸時自動產生
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Sep 16, 2024
1 parent 8db0c7d commit 14db1df
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions plot/bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package plot

import (
"fmt"

"github.com/HazelnutParadise/insyra" // 確保這是正確的導入路徑
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
Expand Down Expand Up @@ -76,6 +78,37 @@ func CreateBarChart(config BarChartConfig) *charts.Bar {
)
}

if len(config.XAxis) == 0 {
// 如果 X 軸沒有提供,則根據數據長度生成默認標籤
var maxDataLength int
switch data := config.SeriesData.(type) {
case map[string][]float64:
for _, vals := range data {
if len(vals) > maxDataLength {
maxDataLength = len(vals)
}
}
case []*insyra.DataList:
for _, dataList := range data {
if dataList.Len() > maxDataLength {
maxDataLength = len(dataList.ToF64Slice())
}
}
case []insyra.IDataList:
for _, dataList := range data {
if dataList.Len() > maxDataLength {
maxDataLength = len(dataList.ToF64Slice())
}
}
}

// 生成 1, 2, 3, ... n 的 X 軸標籤
config.XAxis = make([]string, maxDataLength)
for i := 0; i < maxDataLength; i++ {
config.XAxis[i] = fmt.Sprintf("%d", i+1)
}
}

// 設置 X 軸標籤
bar.SetXAxis(config.XAxis)

Expand Down
33 changes: 33 additions & 0 deletions plot/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package plot

import (
"fmt"

"github.com/HazelnutParadise/insyra"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
Expand Down Expand Up @@ -78,6 +80,37 @@ func CreateLineChart(config LineChartConfig) *charts.Line {
)
}

if len(config.XAxis) == 0 {
// 如果 X 軸沒有提供,則根據數據長度生成默認標籤
var maxDataLength int
switch data := config.SeriesData.(type) {
case map[string][]float64:
for _, vals := range data {
if len(vals) > maxDataLength {
maxDataLength = len(vals)
}
}
case []*insyra.DataList:
for _, dataList := range data {
if dataList.Len() > maxDataLength {
maxDataLength = len(dataList.ToF64Slice())
}
}
case []insyra.IDataList:
for _, dataList := range data {
if dataList.Len() > maxDataLength {
maxDataLength = len(dataList.ToF64Slice())
}
}
}

// 生成 1, 2, 3, ... n 的 X 軸標籤
config.XAxis = make([]string, maxDataLength)
for i := 0; i < maxDataLength; i++ {
config.XAxis[i] = fmt.Sprintf("%d", i+1)
}
}

// 設置 X 軸標籤
line.SetXAxis(config.XAxis)

Expand Down

0 comments on commit 14db1df

Please sign in to comment.