plot.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package lib
  2. import (
  3. "fmt"
  4. "gonum.org/v1/plot"
  5. "gonum.org/v1/plot/plotter"
  6. "gonum.org/v1/plot/vg"
  7. "image/color"
  8. "time"
  9. )
  10. func HorizontalLine(xmin, xmax, y float64) *plotter.Line {
  11. pts := make(plotter.XYs, 2)
  12. pts[0].X = xmin
  13. pts[0].Y = y
  14. pts[1].X = xmax
  15. pts[1].Y = y
  16. line, err := plotter.NewLine(pts)
  17. if err != nil {
  18. panic(err)
  19. }
  20. line.LineStyle.Dashes = []vg.Length{vg.Points(8), vg.Points(5), vg.Points(1), vg.Points(5)}
  21. line.Color = color.RGBA{R: 255, A: 255}
  22. return line
  23. }
  24. type TimeTicks struct{}
  25. func (TimeTicks) Ticks(min, max float64) []plot.Tick {
  26. tks := plot.TimeTicks{}.Ticks(min, max)
  27. for i, t := range tks {
  28. //if t.Label == "" { // Skip minor ticks, they are fine.
  29. // continue
  30. //}
  31. tks[i].Label = time.Unix(int64(t.Value), 0).Format("2006-01-02 15:04:05")
  32. }
  33. return tks
  34. }
  35. type CommaTicks struct{}
  36. // Ticks computes the default tick marks, but inserts commas
  37. // into the labels for the major tick marks.
  38. func (CommaTicks) Ticks(min, max float64) []plot.Tick {
  39. tks := plot.DefaultTicks{}.Ticks(min, max)
  40. for i, t := range tks {
  41. //if t.Label == "" { // Skip minor ticks, they are fine.
  42. // continue
  43. //}
  44. tks[i].Label = fmt.Sprintf("%.0f", t.Value)
  45. }
  46. return tks
  47. }
  48. // 生成随机颜色的辅助函数
  49. func RandomColor(i int) color.RGBA {
  50. var colors []color.RGBA
  51. colors = append(colors,
  52. color.RGBA{R: 52, G: 152, B: 219, A: 255},
  53. color.RGBA{R: 230, G: 126, B: 34, A: 255},
  54. color.RGBA{R: 142, G: 68, B: 173, A: 255},
  55. color.RGBA{R: 211, G: 84, B: 0, A: 255},
  56. color.RGBA{R: 231, G: 76, B: 60, A: 255},
  57. color.RGBA{R: 26, G: 188, B: 156, A: 255},
  58. color.RGBA{R: 243, G: 156, B: 18, A: 255},
  59. color.RGBA{R: 22, G: 160, B: 133, A: 255},
  60. color.RGBA{R: 46, G: 204, B: 113, A: 255},
  61. color.RGBA{R: 39, G: 174, B: 96, A: 255},
  62. color.RGBA{R: 41, G: 128, B: 185, A: 255},
  63. color.RGBA{R: 155, G: 89, B: 182, A: 255},
  64. color.RGBA{R: 192, G: 57, B: 43, A: 255},
  65. color.RGBA{R: 241, G: 196, B: 15, A: 255},
  66. )
  67. return colors[i%len(colors)]
  68. }