2018-12-21 19:44:08 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
import random
|
|
|
|
|
2022-06-11 13:01:27 +00:00
|
|
|
"""
|
|
|
|
Demo - Using a Graph Element to make Bar Charts
|
|
|
|
|
|
|
|
The Graph Element is very versatile. Because you can define your own
|
|
|
|
coordinate system, it makes producing graphs of many lines (bar, line, etc) very
|
|
|
|
straightforward.
|
|
|
|
|
|
|
|
In this Demo a "bar" is nothing more than a rectangle drawn in a Graph Element (draw_rectangle).
|
|
|
|
|
|
|
|
To make things a little more interesting, this is a barchart with that data values
|
|
|
|
placed as labels atop each bar, another Graph element method (draw_text)
|
|
|
|
|
|
|
|
Copyright 2022 PySimpleGUI
|
|
|
|
"""
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
|
|
|
|
2022-01-31 13:48:50 +00:00
|
|
|
BAR_WIDTH = 50 # width of each bar
|
|
|
|
BAR_SPACING = 75 # space between each bar
|
|
|
|
EDGE_OFFSET = 3 # offset from the left edge for first bar
|
|
|
|
GRAPH_SIZE= DATA_SIZE = (500,500) # size in pixels
|
2018-12-21 19:44:08 +00:00
|
|
|
|
2022-01-31 13:48:50 +00:00
|
|
|
sg.theme('Light brown 1')
|
2018-12-21 19:44:08 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('Labelled Bar graphs using PySimpleGUI')],
|
2022-01-31 13:48:50 +00:00
|
|
|
[sg.Graph(GRAPH_SIZE, (0,0), DATA_SIZE, k='-GRAPH-')],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Button('OK'), sg.T('Click to display more data'), sg.Exit()]]
|
2018-12-21 19:44:08 +00:00
|
|
|
|
2022-01-31 13:48:50 +00:00
|
|
|
window = sg.Window('Bar Graph', layout, finalize=True)
|
|
|
|
|
|
|
|
graph = window['-GRAPH-'] # type: sg.Graph
|
2018-12-21 19:44:08 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.erase()
|
2018-12-21 19:44:08 +00:00
|
|
|
for i in range(7):
|
2022-06-11 13:01:27 +00:00
|
|
|
graph_value = random.randint(0, GRAPH_SIZE[1]-25) # choose an int just short of the max value to give room for the label
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_rectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, graph_value),
|
2022-01-31 13:48:50 +00:00
|
|
|
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0),
|
2022-06-11 13:01:27 +00:00
|
|
|
fill_color='green')
|
|
|
|
# fill_color=sg.theme_button_color()[1])
|
2022-01-31 13:48:50 +00:00
|
|
|
|
2022-06-11 13:01:27 +00:00
|
|
|
graph.draw_text(text=graph_value, location=(i*BAR_SPACING+EDGE_OFFSET+25, graph_value+10), font='_ 14')
|
2022-01-31 13:48:50 +00:00
|
|
|
|
|
|
|
# Normally at the top of the loop, but because we're drawing the graph first, making it at the bottom
|
|
|
|
event, values = window.read()
|
|
|
|
|
|
|
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2021-03-01 11:59:36 +00:00
|
|
|
window.close()
|