debug_view.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package debug_view
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/vad"
  7. "github.com/go-echarts/go-echarts/v2/charts"
  8. "github.com/go-echarts/go-echarts/v2/opts"
  9. "os"
  10. "path/filepath"
  11. )
  12. func SaveDebugChart(subUnit sub_helper.SubUnit, title, subTitle string) error {
  13. return SaveDebugChartBase(subUnit.VADList, title, subTitle)
  14. }
  15. func SaveDebugChartBase(vadList []vad.VADInfo, title, subTitle string) error {
  16. line := charts.NewBar()
  17. line.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
  18. Title: title,
  19. Subtitle: subTitle,
  20. }))
  21. // 构建 X 轴
  22. xAxis := make([]string, len(vadList))
  23. for i := 0; i < len(vadList); i++ {
  24. xAxis[i] = fmt.Sprintf("%d", i)
  25. }
  26. lineData := make([]opts.BarData, len(vadList))
  27. for i := 0; i < len(vadList); i++ {
  28. value := -1
  29. if vadList[i].Active == true {
  30. value = 1
  31. }
  32. lineData[i] = opts.BarData{Value: value}
  33. }
  34. // Put data into instance
  35. line.SetXAxis(xAxis).
  36. AddSeries("VAD", lineData)
  37. rootDebugFolder, err := my_util.GetRootDebugFolder()
  38. if err != nil {
  39. return err
  40. }
  41. // Where the magic happens
  42. f, err := os.Create(filepath.Join(rootDebugFolder, title+".html"))
  43. if err != nil {
  44. return err
  45. }
  46. defer f.Close()
  47. return line.Render(f)
  48. }