chart.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package gui
  2. import (
  3. "fmt"
  4. "github.com/g3n/engine/core"
  5. "github.com/g3n/engine/geometry"
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/graphic"
  8. "github.com/g3n/engine/material"
  9. "github.com/g3n/engine/math32"
  10. "github.com/g3n/engine/renderer/shader"
  11. "math"
  12. )
  13. func init() {
  14. shader.AddShader("shaderChartVertex", shaderChartVertex)
  15. shader.AddShader("shaderChartFrag", shaderChartFrag)
  16. shader.AddProgram("shaderChart", "shaderChartVertex", "shaderChartFrag")
  17. }
  18. //
  19. //
  20. // ChartLine implements a panel which can contain several line charts
  21. //
  22. //
  23. type ChartLine struct {
  24. Panel // Embedded panel
  25. title *Label // Optional title label
  26. left float32 // Left margin in pixels
  27. bottom float32 // Bottom margin in pixels
  28. scaleX *ChartScaleX // X scale panel
  29. scaleY *ChartScaleY // Y scale panel
  30. offsetX int // Initial offset in data buffers
  31. countX int // Count of data buffer points starting from offsetX
  32. firstX float32 // Value of first data point to show
  33. stepX float32 // Step to add to firstX for next data point
  34. minY float32 // Minimum Y value
  35. maxY float32 //
  36. autoY bool // Auto range flag for Y values
  37. formatX string // String format for scale X labels
  38. formatY string // String format for scale Y labels
  39. labelsX []*Label // Array of scale X labels
  40. labelsY []*Label // Array of scale Y labels
  41. graphs []*LineGraph // Array of line graphs
  42. }
  43. // NewChartLine creates and returns a new line chart panel with
  44. // the specified dimensions in pixels.
  45. func NewChartLine(width, height float32) *ChartLine {
  46. cl := new(ChartLine)
  47. cl.Panel.Initialize(width, height)
  48. cl.left = 30
  49. cl.bottom = 20
  50. cl.offsetX = 0
  51. cl.countX = 10
  52. cl.firstX = 0.0
  53. cl.stepX = 1.0
  54. cl.autoY = false
  55. cl.formatX = "%v"
  56. cl.formatY = "%v"
  57. return cl
  58. }
  59. //func (cl *ChartLine) SetMargins(left, bottom float32) {
  60. //
  61. // cl.baseX, cl.baseY = cl.Pix2NDC(left, bottom)
  62. // cl.recalc()
  63. //}
  64. // SetTitle sets the chart title
  65. func (cl *ChartLine) SetTitle(title *Label) {
  66. if cl.title != nil {
  67. cl.Remove(cl.title)
  68. cl.title = nil
  69. }
  70. if title != nil {
  71. cl.Add(title)
  72. cl.title = title
  73. }
  74. cl.recalc()
  75. }
  76. // SetScaleX sets the line chart x scale number of lines and color
  77. func (cl *ChartLine) SetScaleX(lines int, color *math32.Color) {
  78. if cl.scaleX != nil {
  79. cl.ClearScaleX()
  80. }
  81. // Add scale lines
  82. cl.scaleX = newChartScaleX(cl, lines, color)
  83. cl.Add(cl.scaleX)
  84. // Add scale labels
  85. // The positions of the labels will be set by 'recalc()'
  86. for i := 0; i < lines; i++ {
  87. l := NewLabel(fmt.Sprintf(cl.formatX, float32(i)))
  88. cl.Add(l)
  89. cl.labelsX = append(cl.labelsX, l)
  90. }
  91. cl.recalc()
  92. }
  93. // ClearScaleX removes the chart x scale if it was previously set
  94. func (cl *ChartLine) ClearScaleX() {
  95. if cl.scaleX == nil {
  96. return
  97. }
  98. // Remove and dispose scale lines
  99. cl.Remove(cl.scaleX)
  100. cl.scaleX.Dispose()
  101. // Remove and dispose scale labels
  102. for i := 0; i < len(cl.labelsX); i++ {
  103. label := cl.labelsX[i]
  104. cl.Remove(label)
  105. label.Dispose()
  106. }
  107. cl.labelsX = cl.labelsX[0:0]
  108. cl.scaleX = nil
  109. }
  110. // SetScaleY sets the line chart y scale number of lines and color
  111. func (cl *ChartLine) SetScaleY(lines int, color *math32.Color) {
  112. if cl.scaleY != nil {
  113. cl.ClearScaleY()
  114. }
  115. // Add scale lines
  116. cl.scaleY = newChartScaleY(cl, lines, color)
  117. cl.Add(cl.scaleY)
  118. // Add scale labels
  119. // The position of the labels will be set by 'recalc()'
  120. for i := 0; i < lines; i++ {
  121. l := NewLabel(fmt.Sprintf(cl.formatY, float32(i)))
  122. cl.Add(l)
  123. cl.labelsY = append(cl.labelsY, l)
  124. }
  125. cl.recalc()
  126. }
  127. // ClearScaleY removes the chart x scale if it was previously set
  128. func (cl *ChartLine) ClearScaleY() {
  129. if cl.scaleY == nil {
  130. return
  131. }
  132. // Remove and dispose scale lines
  133. cl.Remove(cl.scaleY)
  134. cl.scaleY.Dispose()
  135. // Remove and dispose scale labels
  136. for i := 0; i < len(cl.labelsY); i++ {
  137. label := cl.labelsY[i]
  138. cl.Remove(label)
  139. label.Dispose()
  140. }
  141. cl.labelsY = cl.labelsY[0:0]
  142. cl.scaleY = nil
  143. }
  144. // SetRangeX sets the interval of the data to be shown:
  145. // offset is the start position in the Y data array.
  146. // count is the number of data points to show, starting from the specified offset.
  147. // first is the label for the first X point
  148. // step is the value added to first for the next data point
  149. func (cl *ChartLine) SetRangeX(offset int, count int, first float32, step float32) {
  150. cl.offsetX = offset
  151. cl.countX = count
  152. cl.firstX = first
  153. cl.stepX = step
  154. }
  155. func (cl *ChartLine) SetRangeY(min float32, max float32) {
  156. cl.minY = min
  157. cl.maxY = max
  158. }
  159. // AddLine adds a line graph to the chart
  160. func (cl *ChartLine) AddGraph(color *math32.Color, data []float32) *LineGraph {
  161. graph := newLineGraph(cl, color, data)
  162. cl.graphs = append(cl.graphs, graph)
  163. cl.Add(graph)
  164. return graph
  165. }
  166. func (cl *ChartLine) calcRangeY() {
  167. if !cl.autoY {
  168. return
  169. }
  170. minY := float32(math.MaxFloat32)
  171. maxY := -float32(math.MaxFloat32)
  172. for g := 0; g < len(cl.graphs); g++ {
  173. graph := cl.graphs[g]
  174. for x := 0; x < cl.countX; x++ {
  175. if x+cl.offsetX >= len(graph.y) {
  176. break
  177. }
  178. vy := graph.y[x+cl.offsetX]
  179. if vy < minY {
  180. minY = vy
  181. }
  182. if vy > maxY {
  183. maxY = vy
  184. }
  185. }
  186. }
  187. cl.minY = minY
  188. cl.maxY = maxY
  189. }
  190. // recalc recalculates the positions of the inner panels
  191. func (cl *ChartLine) recalc() {
  192. // Center title position
  193. th := float32(0)
  194. if cl.title != nil {
  195. xpos := (cl.ContentWidth() - cl.title.width) / 2
  196. cl.title.SetPositionX(xpos)
  197. th = cl.title.height
  198. }
  199. // Recalc scale X and its labels
  200. if cl.scaleX != nil {
  201. cl.scaleX.recalc()
  202. step := (cl.ContentWidth() - cl.left) / float32(len(cl.labelsX))
  203. for i := 0; i < len(cl.labelsX); i++ {
  204. label := cl.labelsX[i]
  205. px := cl.left + float32(i)*step
  206. label.SetPosition(px, cl.ContentHeight()-cl.bottom)
  207. }
  208. }
  209. // Recalc scale Y and its labels
  210. if cl.scaleY != nil {
  211. cl.scaleY.recalc()
  212. step := (cl.ContentHeight() - cl.bottom - th) / float32(len(cl.labelsY))
  213. for i := 0; i < len(cl.labelsY); i++ {
  214. label := cl.labelsY[i]
  215. py := cl.ContentHeight() - cl.bottom - float32(i)*step
  216. label.SetPosition(0, py-label.Height()/2)
  217. }
  218. }
  219. // Recalc graphs
  220. for i := 0; i < len(cl.graphs); i++ {
  221. cl.graphs[i].recalc()
  222. }
  223. }
  224. //
  225. //
  226. // ChartScaleX is a panel with GL_LINES geometry which draws the chart X horizontal scale axis,
  227. // vertical lines and line labels.
  228. //
  229. //
  230. type ChartScaleX struct {
  231. Panel // Embedded panel
  232. chart *ChartLine // Container chart
  233. lines int // Number of vertical lines
  234. mat chartMaterial // Chart material
  235. }
  236. // newChartScaleX creates and returns a pointer to a new ChartScaleX for the specified
  237. // chart, number of lines and color
  238. func newChartScaleX(chart *ChartLine, lines int, color *math32.Color) *ChartScaleX {
  239. sx := new(ChartScaleX)
  240. sx.chart = chart
  241. sx.lines = lines
  242. // Appends bottom horizontal line
  243. positions := math32.NewArrayF32(0, 0)
  244. positions.Append(0, -1, 0, 1, -1, 0)
  245. // Appends vertical lines
  246. step := 1 / float32(lines)
  247. for i := 0; i < lines; i++ {
  248. nx := float32(i) * step
  249. positions.Append(nx, 0, 0, nx, -1, 0)
  250. }
  251. // Creates geometry and adds VBO
  252. geom := geometry.NewGeometry()
  253. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  254. // Initializes the panel graphic
  255. gr := graphic.NewGraphic(geom, gls.LINES)
  256. sx.mat.Init(color)
  257. gr.AddMaterial(sx, &sx.mat, 0, 0)
  258. sx.Panel.InitializeGraphic(chart.ContentWidth(), chart.ContentHeight(), gr)
  259. sx.recalc()
  260. return sx
  261. }
  262. func (sx *ChartScaleX) setLabelsText(x []float32) {
  263. }
  264. // recalc recalculates the position and size of this scale inside its parent
  265. func (sx *ChartScaleX) recalc() {
  266. py := float32(0)
  267. if sx.chart.title != nil {
  268. py = sx.chart.title.Height()
  269. }
  270. sx.SetPosition(sx.chart.left, py)
  271. sx.SetSize(sx.chart.ContentWidth()-sx.chart.left, sx.chart.ContentHeight()-py-sx.chart.bottom)
  272. }
  273. // RenderSetup is called by the renderer before drawing this graphic
  274. // Calculates the model matrix and transfer to OpenGL.
  275. func (sx *ChartScaleX) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
  276. //log.Error("ChartScaleX RenderSetup:%v", sx.pospix)
  277. // Sets model matrix and transfer to shader
  278. var mm math32.Matrix4
  279. sx.SetModelMatrix(gs, &mm)
  280. sx.modelMatrixUni.SetMatrix4(&mm)
  281. sx.modelMatrixUni.Transfer(gs)
  282. }
  283. //
  284. // ChartScaleY is a panel with LINE geometry which draws the chart Y vertical scale axis,
  285. // horizontal and labels.
  286. //
  287. type ChartScaleY struct {
  288. Panel // Embedded panel
  289. chart *ChartLine // Container chart
  290. lines int // Number of horizontal lines
  291. mat chartMaterial // Chart material
  292. }
  293. // newChartScaleY creates and returns a pointer to a new ChartScaleY for the specified
  294. // chart, number of lines and color
  295. func newChartScaleY(chart *ChartLine, lines int, color *math32.Color) *ChartScaleY {
  296. sy := new(ChartScaleY)
  297. sy.chart = chart
  298. sy.lines = lines
  299. // Appends left vertical line
  300. positions := math32.NewArrayF32(0, 0)
  301. positions.Append(0, 0, 0, 0, -1, 0)
  302. // Appends horizontal lines
  303. step := 1 / float32(lines)
  304. for i := 0; i < lines; i++ {
  305. ny := -1 + float32(i)*step
  306. positions.Append(0, ny, 0, 1, ny, 0)
  307. }
  308. // Creates geometry and adds VBO
  309. geom := geometry.NewGeometry()
  310. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  311. // Initializes the panel with this graphic
  312. gr := graphic.NewGraphic(geom, gls.LINES)
  313. sy.mat.Init(color)
  314. gr.AddMaterial(sy, &sy.mat, 0, 0)
  315. sy.Panel.InitializeGraphic(chart.ContentWidth(), chart.ContentHeight(), gr)
  316. sy.recalc()
  317. return sy
  318. }
  319. // recalc recalculates the position and size of this scale inside its parent
  320. func (sy *ChartScaleY) recalc() {
  321. py := float32(0)
  322. if sy.chart.title != nil {
  323. py = sy.chart.title.Height()
  324. }
  325. sy.SetPosition(sy.chart.left, py)
  326. sy.SetSize(sy.chart.ContentWidth()-sy.chart.left, sy.chart.ContentHeight()-py-sy.chart.bottom)
  327. }
  328. //
  329. //
  330. // LineGraph
  331. //
  332. //
  333. type LineGraph struct {
  334. Panel // Embedded panel
  335. chart *ChartLine // Container chart
  336. color math32.Color // Line color
  337. y []float32 // Data y
  338. mat chartMaterial // Chart material
  339. }
  340. func newLineGraph(chart *ChartLine, color *math32.Color, y []float32) *LineGraph {
  341. log.Error("newLineGraph")
  342. lg := new(LineGraph)
  343. lg.chart = chart
  344. lg.color = *color
  345. lg.y = y
  346. lg.setGeometry()
  347. return lg
  348. }
  349. func (lg *LineGraph) SetColor(color *math32.Color) {
  350. }
  351. func (lg *LineGraph) SetData(x, y []float32) {
  352. lg.y = y
  353. }
  354. func (lg *LineGraph) setGeometry() {
  355. lg.chart.calcRangeY()
  356. log.Error("minY:%v maxY:%v", lg.chart.minY, lg.chart.maxY)
  357. // Creates array for vertices and colors
  358. positions := math32.NewArrayF32(0, 0)
  359. origin := false
  360. step := 1.0 / float32(lg.chart.countX-1)
  361. rangeY := lg.chart.maxY - lg.chart.minY
  362. for i := 0; i < lg.chart.countX; i++ {
  363. x := i + lg.chart.offsetX
  364. if x >= len(lg.y) {
  365. break
  366. }
  367. px := float32(i) * step
  368. if !origin {
  369. positions.Append(px, -1, 0)
  370. origin = true
  371. }
  372. vy := lg.y[x]
  373. py := -1 + (vy / rangeY)
  374. positions.Append(px, py, 0)
  375. }
  376. // Creates geometry using one interlaced VBO
  377. geom := geometry.NewGeometry()
  378. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  379. // Initializes the panel with this graphic
  380. gr := graphic.NewGraphic(geom, gls.LINE_STRIP)
  381. lg.mat.Init(&lg.color)
  382. gr.AddMaterial(lg, &lg.mat, 0, 0)
  383. lg.Panel.InitializeGraphic(lg.chart.ContentWidth(), lg.chart.ContentHeight(), gr)
  384. }
  385. func (lg *LineGraph) recalc() {
  386. px := lg.chart.left
  387. py := float32(0)
  388. w := lg.chart.ContentWidth() - lg.chart.left
  389. h := lg.chart.ContentHeight() - lg.chart.bottom
  390. if lg.chart.title != nil {
  391. py += lg.chart.title.Height()
  392. h -= lg.chart.title.Height()
  393. }
  394. lg.SetPosition(px, py)
  395. lg.SetSize(w, h)
  396. }
  397. //
  398. //
  399. // Chart material (for lines)
  400. //
  401. //
  402. type chartMaterial struct {
  403. material.Material // Embedded material
  404. color *gls.Uniform3f // Emissive color uniform
  405. }
  406. func (cm *chartMaterial) Init(color *math32.Color) {
  407. cm.Material.Init()
  408. cm.SetShader("shaderChart")
  409. // Creates uniforms and adds to material
  410. cm.color = gls.NewUniform3f("MatColor")
  411. // Set initial values
  412. cm.color.SetColor(color)
  413. }
  414. func (cm *chartMaterial) RenderSetup(gs *gls.GLS) {
  415. cm.Material.RenderSetup(gs)
  416. cm.color.Transfer(gs)
  417. }
  418. //
  419. // Vertex Shader template
  420. //
  421. const shaderChartVertex = `
  422. #version {{.Version}}
  423. // Vertex attributes
  424. {{template "attributes" .}}
  425. // Input uniforms
  426. uniform mat4 ModelMatrix;
  427. uniform vec3 MatColor;
  428. // Outputs for fragment shader
  429. out vec3 Color;
  430. void main() {
  431. Color = MatColor;
  432. // Set position
  433. vec4 pos = vec4(VertexPosition.xyz, 1);
  434. gl_Position = ModelMatrix * pos;
  435. }
  436. `
  437. //
  438. // Fragment Shader template
  439. //
  440. const shaderChartFrag = `
  441. #version {{.Version}}
  442. in vec3 Color;
  443. out vec4 FragColor;
  444. void main() {
  445. FragColor = vec4(Color, 1.0);
  446. }
  447. `