Ver Fonte

add `RADAR_CHART` :eyes::heart:

Ethosa há 3 anos atrás
pai
commit
e766f31675
3 ficheiros alterados com 52 adições e 1 exclusões
  1. 2 1
      src/nodesnim/core/enums.nim
  2. 37 0
      src/nodesnim/nodescontrol/chart.nim
  3. 13 0
      tests/test3.nim

+ 2 - 1
src/nodesnim/core/enums.nim

@@ -104,7 +104,8 @@ type
   ChartType* = enum
     LINE_CHART,
     BAR_CHART,
-    PIE_CHART
+    PIE_CHART,
+    RADAR_CHART
 
   ChartDataValueType* = enum
     INTEGER_VALUE,

+ 37 - 0
src/nodesnim/nodescontrol/chart.nim

@@ -104,6 +104,43 @@ method draw*(self: ChartRef, w, h: GLfloat) =
         glEnd()
         glColor4f(angle/6, angle/5, angle/4, 1f)
 
+    of RADAR_CHART:
+      let
+        radius_max = min(self.rect_size.x, self.rect_size.y)/2
+        radius_step = radius_max/5
+        center = Vector2(x + self.rect_size.x/2, y - self.rect_size.y/2)
+        angle_step = TAU / data.len.float
+      var
+        radius = radius_step
+        angle = 0f
+      glColor(current_theme~foreground)
+      glLineWidth(1)
+      for step in 0..5:
+        glBegin(GL_LINE_LOOP)
+        for i in data.low..data.high:
+          glVertex2f(center.x + cos(angle)*radius, center.y + sin(angle)*radius)
+          angle += angle_step
+        radius += radius_step
+        angle = 0f
+        glEnd()
+
+      glBegin(GL_LINES)
+      for i in data.low..data.high:
+        glVertex2f(center.x + cos(angle)*radius_max, center.y + sin(angle)*radius_max)
+        glVertex2f(center.x, center.y)
+        angle += angle_step
+      angle = 0f
+      glEnd()
+
+      glColor4f(chart_data.data_color.r, chart_data.data_color.g, chart_data.data_color.b, 0.8)
+      glBegin(GL_POLYGON)
+      for i in data.low..data.high:
+        let r = radius_max * (data[i][1].getNum() / max_val)
+        glVertex2f(center.x + cos(angle)*r, center.y + sin(angle)*r)
+        angle += angle_step
+      glEnd()
+
+
 method addChartData*(self: ChartRef, chart_data: ChartData) {.base.} =
   self.data.add(chart_data)
 

+ 13 - 0
tests/test3.nim

@@ -367,6 +367,19 @@ suite "Work with Control nodes.":
 
     getSceneByName("main").addChildren(circle_chart)
 
+  test "Radar chart test":
+    build:
+      - Chart spiderweb_chart:
+        call addChartData(
+          newChartData(
+            @["one", "two", "three", "four", "five", "six"],
+            @[10, 24, 18, 32, 4, 16], "myData", current_theme~accent_dark, RADAR_CHART))
+
+        call move(700, 450)
+        call resize(128, 128)
+
+    getSceneByName("main").addChildren(spiderweb_chart)
+
 
   test "Launch window":
     windowLaunch()