简体   繁体   中英

Python: How does one create a pie chart from a database?

I want to build a pie chart from my database.

The python GUI app I'm building is done with tKinter Currently I'm using sqlite3 for the database, and I've been told to use matplotlib as the table generator.

This app I'm making is for a Savings System

This is the code I'm using to input data into the database

def Submit():
    #Creating or connecting to a Database
    conn = sqlite3.connect("Balance.db")

    #create cursor
    c = conn.cursor()

    c.execute("INSERT INTO Log VALUES (:value, :type, :tag)",
            {
                'value': value.get(),
                'type': var.get(),
                'tag': tag.get()
            })

    #commit changes
    conn.commit()

    #close connection
    conn.close()


    #clear the text boxes to allow for new input
    value.delete(0, END)
    #cbv is not included in this since cbv is checkbox data
    tag.delete(0,END)

Submit_Button = Button(root, text = "Add to Logs", command = Submit)
Submit_Button.grid(row=9, column = 0)

However, what I want to be able to do is from this log, pull out those three variables, the value, type, and tag to create a pie chart representing type (expenses), value (from the total of expenses combined) and tag (how I want to split the cuts in the pies)

Unfortunately, I don't know how to pull out singular Variables from the database.

Can someone help me please? Thanks

Please check the snippet if you are looking for something like this.

You can store value,type and tag values in variable and then use it to insert into database and also for calling in matplotlib

matplotlib

import tkinter as tk
from matplotlib import pyplot as plt
root=tk.Tk()
root.geometry("200x100")

def Submit():
    value=value_entry.get()
    types=type_entry.get()
    tags=tags_entry.get()
    conn = sqlite3.connect("Balance.db")
    c = conn.cursor()
    c.execute("INSERT INTO Log VALUES (:value, :type, :tag)",
            {
                'value': value,
                'type': types,
                'tag': tags
            })
    conn.commit()
    conn.close()
    
    label=["value","type","tag"]
    data=[value,types,tags]
    fig = plt.figure(figsize =(5, 5)) 
    plt.pie(data, labels = label,autopct='%1.2f%%')
    plt.show()
    #clear the text boxes to allow for new input
    values.set("")
    typess.set("")
    tagss.set("")

values=tk.IntVar() 
typess=tk.IntVar()
tagss=tk.IntVar()

value_label = tk.Label(root, text = 'Value')
value_entry = tk.Entry(root, textvariable = values)
type_label = tk.Label(root, text = 'Type')
type_entry = tk.Entry(root, textvariable = typess)
tags_label = tk.Label(root, text = 'Tags')
tags_entry = tk.Entry(root, textvariable = tagss) 
Submit_Button = tk.Button(root, text = "Add to Logs", command = Submit)

value_label.grid(row=0,column=0) 
value_entry.grid(row=0,column=1) 
type_label.grid(row=1,column=0) 
type_entry.grid(row=1,column=1)
tags_label.grid(row=2,column=0) 
tags_entry.grid(row=2,column=1) 
Submit_Button.grid(row=3, column = 1)
   
root.mainloop() 

Note - If you dont want data in percentage , you can remove autopct='%1.2f%%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM