Building a spider-chart with turtle or tkinter

♀尐吖头ヾ 提交于 2021-02-11 15:13:46

问题


I recently did 2 beginner courses in python coding at university for my Minor, which were followed by an intro to databases and a basic course about CRUD development in PHP. The python course was decent enough and touched on most of the basics. I've been trying to teach myself by making small applications to do things that I have to do for my remaining courses in linguistics.

Now I wanted to write a small python application and that takes user input to create a Language profile for that speaker. I thought I could do this using the turtle module of python, but after drawing the grid I found out I do not know how to get the points of the circles that i have drawn.

I wrote the following piece of code:

'''
 for i in range(6):
        t.circle(20*i,None, 16)
        t.seth(90)
        t.penup()[enter image description here][1]
        t.backward(20)
        t.pendown()
        t.seth(0)
'''

I used 16 steps. because it needs to look something like this picture in the end :

Could anyone here help me along on how I can get these points on the circle mapped so that they can be drawn in after the user tells the program what his proficiency is for that point?

Please let me know if I left out anything important.

Sincerely,


回答1:


You can construct a radar, or spider chart with tkinter.

The following class takes a list of tuples('label', score), with the score expressed as a percentage (value in the interval [0, 100]).

The number of data points adjusts to the data provided.
The scale of the chart can be adjusted.

import math
import tkinter as tk


class SpiderChart(tk.Canvas):
    """a canvas that displays datapoints as a SpiderChart
    """
    width=500
    height=500
    def __init__(self, master, datapoints, concentrics=10, scale=200):
        super().__init__(master, width=self.width, height=self.height)
        self.scale = scale
        self.center = self.width // 2, self.height // 2
        self.labels = tuple(d[0] for d in datapoints)
        self.values = tuple(d[1] for d in datapoints)
        self.num_pts = len(self.labels)
        self.concentrics = [n/(concentrics) for n in range(1, concentrics + 1)]
        self.draw()
        
    def position(self, x, y):
        """use +Y pointing up, and origin at center
        """
        cx, cy = self.center
        return x + cx, cy - y
    
    def draw_circle_from_radius_center(self, radius):
        rad = radius * self.scale
        x0, y0 =  self.position(-rad, rad)
        x1, y1 =  self.position(rad, -rad)
        return self.create_oval(x0, y0, x1, y1, dash=(1, 3))
    
    def draw_label(self, idx, label):
        angle = idx * (2 * math.pi) / self.num_pts
        d = self.concentrics[-1] * self.scale
        x, y = d * math.cos(angle), d * math.sin(angle)
        self.create_line(*self.center, *self.position(x, y), dash=(1, 3))
        d *= 1.1 
        x, y = d * math.cos(angle), d * math.sin(angle)
        self.create_text(*self.position(x, y), text=label)
        
    def draw_polygon(self):
        points = []
        for idx, val in enumerate(self.values):
            d = (val / 100) * self.scale
            angle = idx * (2 * math.pi) / self.num_pts
            x, y = d * math.cos(angle), d * math.sin(angle)
            points.append(self.position(x, y))
        self.create_polygon(points, fill='cyan')
        
    def draw(self):
        self.draw_polygon()
        for concentric in self.concentrics:
            self.draw_circle_from_radius_center(concentric)
        for idx, label in enumerate(self.labels):
            self.draw_label(idx, label)
        
            
data = [('stamina', 70), ('python-skill', 100), ('strength', 80), ('break-dance', 66), ('speed', 45), ('health', 72), ('healing', 90), ('energy', 12), ('libido', 100)]

root = tk.Tk()
spider = SpiderChart(root, data)
spider.pack(expand=True, fill=tk.BOTH)

root.mainloop()
        



来源:https://stackoverflow.com/questions/64862345/building-a-spider-chart-with-turtle-or-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!